partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

StorageLocationCategory.php (3124B)


      1 <?php
      2 
      3 namespace PartKeepr\StorageLocationBundle\Entity;
      4 
      5 use Doctrine\Common\Collections\ArrayCollection;
      6 use Doctrine\ORM\Mapping as ORM;
      7 use Gedmo\Mapping\Annotation as Gedmo;
      8 use PartKeepr\CategoryBundle\Entity\AbstractCategory;
      9 use PartKeepr\CategoryBundle\Entity\CategoryPathInterface;
     10 use PartKeepr\DoctrineReflectionBundle\Annotation\TargetService;
     11 use Symfony\Component\Serializer\Annotation\Groups;
     12 
     13 /**
     14  * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
     15  * @Gedmo\Tree(type="nested")
     16  * @ORM\Table(indexes={@ORM\Index(columns={"lft"}),@ORM\Index(columns={"rgt"})})
     17  * The entity for our storage location categories
     18  * @TargetService(uri="/api/storage_location_categories")
     19  */
     20 class StorageLocationCategory extends AbstractCategory implements CategoryPathInterface
     21 {
     22     /**
     23      * @Gedmo\TreeParent
     24      * @ORM\ManyToOne(targetEntity="StorageLocationCategory", inversedBy="children")
     25      * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
     26      */
     27     protected $parent;
     28 
     29     /**
     30      * @ORM\OneToMany(targetEntity="StorageLocationCategory", mappedBy="parent")
     31      * @ORM\OrderBy({"lft" = "ASC"})
     32      * @Groups({"tree"})
     33      *
     34      * @var ArrayCollection
     35      */
     36     protected $children;
     37 
     38     /**
     39      * @ORM\OneToMany(targetEntity="PartKeepr\StorageLocationBundle\Entity\StorageLocation", mappedBy="category")
     40      */
     41     protected $storageLocations;
     42 
     43     /**
     44      * @ORM\Column(type="text",nullable=true)
     45      * @Groups({"default"})
     46      *
     47      * @var string
     48      */
     49     protected $categoryPath;
     50 
     51     public function __construct()
     52     {
     53         parent::__construct();
     54         $this->storageLocations = new ArrayCollection();
     55     }
     56 
     57     /**
     58      * Sets the parent category.
     59      *
     60      * @Groups({"default"})
     61      *
     62      * @param AbstractCategory|null $parent
     63      */
     64     public function setParent(AbstractCategory $parent = null)
     65     {
     66         $this->parent = $parent;
     67     }
     68 
     69     /**
     70      * Returns the parent category.
     71      *
     72      * @return mixed
     73      */
     74     public function getParent()
     75     {
     76         return $this->parent;
     77     }
     78 
     79     /**
     80      * Returns the storage locations.
     81      *
     82      * @return ArrayCollection
     83      */
     84     public function getStorageLocations()
     85     {
     86         return $this->storageLocations->getValues();
     87     }
     88 
     89     /**
     90      * Returns the children.
     91      *
     92      * @return ArrayCollection
     93      */
     94     public function getChildren()
     95     {
     96         return $this->children->getValues();
     97     }
     98 
     99     /**
    100      * Returns the category path.
    101      *
    102      * @return string
    103      */
    104     public function getCategoryPath()
    105     {
    106         return $this->categoryPath;
    107     }
    108 
    109     /**
    110      * {@inheritdoc}
    111      */
    112     public function setCategoryPath($categoryPath)
    113     {
    114         $this->categoryPath = $categoryPath;
    115     }
    116 
    117     /**
    118      * {@inheritdoc}
    119      */
    120     public function generateCategoryPath($pathSeparator)
    121     {
    122         if ($this->getParent() !== null) {
    123             return $this->getParent()->generateCategoryPath($pathSeparator).$pathSeparator.$this->getName();
    124         } else {
    125             return $this->getName();
    126         }
    127     }
    128 }