Advertisement
Guest User

CategoryEntity

a guest
Aug 13th, 2014
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.75 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Model\Entities;
  4.  
  5. use YetORM;
  6.  
  7. /**
  8.  * @property-read int $id
  9.  * @property int|NULL $parent_id
  10.  * @property int|NULL $order
  11.  * @property int|NULL $state
  12.  */
  13. class Category extends BaseEntity
  14. {
  15.     private $lang;
  16.     private $defaultLang = "cs";
  17.  
  18.     /**
  19.       * @param  string|NULL $lang
  20.       */   
  21.     function setLang($lang)
  22.     {
  23.         $this->lang = $lang;
  24.     }
  25.  
  26.     /** @return string|NULL */
  27.     function getLang()
  28.     {
  29.         if(!$this->lang){
  30.             return $this->defaultLang;
  31.         }
  32.         return $this->lang;
  33.     }  
  34.  
  35.     /** @return YetORM\EntityCollection */
  36.     function getLocales()
  37.     {
  38.         $selection = $this->record->related('category_locale');
  39.         return new YetORM\EntityCollection($selection, 'Model\Entities\CategoryLocale');
  40.     }
  41.  
  42.     /** @return Selection */
  43.     function createSelectionInstance($table = NULL)
  44.     {
  45.         return $this->record->getRow()->getTable()->createSelectionInstance($table);   
  46.     }
  47.  
  48.     /** @return array */
  49.     function getAncestorIds()
  50.     {
  51.         return $this->record->related("category_path.descendant")->select("ancestor")->order("path_length DESC")->fetchPairs(NULL,"ancestor");
  52.     }
  53.  
  54.     /** @return array */
  55.     function getDescendantIds()
  56.     {
  57.         return $this->record->related("category_path.ancestor")->select("descendant")->order("path_length DESC")->fetchPairs(NULL,"descendant");   
  58.     }  
  59.  
  60.     /** @return YetORM\EntityCollection */
  61.     function getAncestors()
  62.     {
  63.         $selection = $this->createSelectionInstance("category")->where([":category_path(ancestor).descendant" => $this->id])->order(":category_path(ancestor).path_length DESC");
  64.         return $this->createCollection($selection);
  65.     }
  66.  
  67.     /** @return YetORM\EntityCollection */
  68.     function getDescendants()
  69.     {
  70.         $selection = $this->record->related("category_path.ancestor")->select("descendant")->order("path_length DESC, category.order");
  71.         return $this->createCollection($selection);
  72.     }
  73.  
  74.     /**
  75.       * @param  string|NULL $lang
  76.       * @return Model\Entities\CategoryLocale|NULL
  77.       */
  78.     function getLocaleByLang($lang = NULL)
  79.     {
  80.         $row = $this->record->related('category_locale')->where("language_code = ?", !$lang ? $this->defaultLang: $lang)->fetch(); 
  81.         return $row === FALSE ? NULL : new CategoryLocale($row);
  82.     }
  83.  
  84.     /**
  85.       * @param  string|NULL $lang
  86.       * @return Model\Entities\CategoryLocale|NULL
  87.       */   
  88.     function getLocale($lang = NULL)
  89.     {
  90.         if(!$localeEntity = $this->getLocaleByLang($this->lang)){
  91.             $localeEntity = $this->getLocaleByLang();
  92.         }
  93.  
  94.         return $localeEntity;      
  95.     }      
  96.  
  97.     /** @return YetORM\EntityCollection */
  98.     function getPath()
  99.     {
  100.         $selection = $this->record->related('category_path.descendant')->order("path_length DESC");
  101.         return new YetORM\EntityCollection($selection, 'Model\Entities\CategoryPath');
  102.     }      
  103.  
  104.     /** @return int */
  105.     function getMaxOrder()
  106.     {
  107.         return $this->createSelectionInstance("category")->where(["parent_id" => $this->id])->max("order");
  108.     }
  109.  
  110.     /** @return array */
  111.     function getTreeByParent(){    
  112.         $parentIds = $this->getParentIds();
  113.  
  114.         $selection = $this->createSelectionInstance("category")->where(["parent_id" => $parentIds])->order("order");
  115.  
  116.         $tree = [];
  117.         foreach($selection as $row){
  118.             $this->tree[$row->parent_id][$row->id] = $this->createCategory($row);
  119.         }
  120.  
  121.         return $tree;
  122.     }  
  123.  
  124.     /**
  125.       * @param  Selection $selection
  126.       * @return YetORM\EntityCollection
  127.       */
  128.     function createCollection($selection)
  129.     {
  130.         $collection = new YetORM\EntityCollection($selection, 'Model\Entities\Category');
  131.         foreach($collection as $entity){
  132.             if($this->lang) $entity->setLang($this->lang);
  133.         }
  134.         return $collection;
  135.     }
  136.  
  137.     /**
  138.      * @param  NActiveRow $row
  139.      * @return Category
  140.      */
  141.     function createEntity(\Nette\Database\Table\ActiveRow $row = NULL)
  142.     {
  143.         $entity = new Category($row);
  144.         if($this->lang) $entity->setLang($this->lang);
  145.         return $entity;
  146.     }  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement