Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Model\Entities;
- use YetORM;
- /**
- * @property-read int $id
- * @property int|NULL $parent_id
- * @property int|NULL $order
- * @property int|NULL $state
- */
- class Category extends BaseEntity
- {
- private $lang;
- private $defaultLang = "cs";
- /**
- * @param string|NULL $lang
- */
- function setLang($lang)
- {
- $this->lang = $lang;
- }
- /** @return string|NULL */
- function getLang()
- {
- if(!$this->lang){
- return $this->defaultLang;
- }
- return $this->lang;
- }
- /** @return YetORM\EntityCollection */
- function getLocales()
- {
- $selection = $this->record->related('category_locale');
- return new YetORM\EntityCollection($selection, 'Model\Entities\CategoryLocale');
- }
- /** @return Selection */
- function createSelectionInstance($table = NULL)
- {
- return $this->record->getRow()->getTable()->createSelectionInstance($table);
- }
- /** @return array */
- function getAncestorIds()
- {
- return $this->record->related("category_path.descendant")->select("ancestor")->order("path_length DESC")->fetchPairs(NULL,"ancestor");
- }
- /** @return array */
- function getDescendantIds()
- {
- return $this->record->related("category_path.ancestor")->select("descendant")->order("path_length DESC")->fetchPairs(NULL,"descendant");
- }
- /** @return YetORM\EntityCollection */
- function getAncestors()
- {
- $selection = $this->createSelectionInstance("category")->where([":category_path(ancestor).descendant" => $this->id])->order(":category_path(ancestor).path_length DESC");
- return $this->createCollection($selection);
- }
- /** @return YetORM\EntityCollection */
- function getDescendants()
- {
- $selection = $this->record->related("category_path.ancestor")->select("descendant")->order("path_length DESC, category.order");
- return $this->createCollection($selection);
- }
- /**
- * @param string|NULL $lang
- * @return Model\Entities\CategoryLocale|NULL
- */
- function getLocaleByLang($lang = NULL)
- {
- $row = $this->record->related('category_locale')->where("language_code = ?", !$lang ? $this->defaultLang: $lang)->fetch();
- return $row === FALSE ? NULL : new CategoryLocale($row);
- }
- /**
- * @param string|NULL $lang
- * @return Model\Entities\CategoryLocale|NULL
- */
- function getLocale($lang = NULL)
- {
- if(!$localeEntity = $this->getLocaleByLang($this->lang)){
- $localeEntity = $this->getLocaleByLang();
- }
- return $localeEntity;
- }
- /** @return YetORM\EntityCollection */
- function getPath()
- {
- $selection = $this->record->related('category_path.descendant')->order("path_length DESC");
- return new YetORM\EntityCollection($selection, 'Model\Entities\CategoryPath');
- }
- /** @return int */
- function getMaxOrder()
- {
- return $this->createSelectionInstance("category")->where(["parent_id" => $this->id])->max("order");
- }
- /** @return array */
- function getTreeByParent(){
- $parentIds = $this->getParentIds();
- $selection = $this->createSelectionInstance("category")->where(["parent_id" => $parentIds])->order("order");
- $tree = [];
- foreach($selection as $row){
- $this->tree[$row->parent_id][$row->id] = $this->createCategory($row);
- }
- return $tree;
- }
- /**
- * @param Selection $selection
- * @return YetORM\EntityCollection
- */
- function createCollection($selection)
- {
- $collection = new YetORM\EntityCollection($selection, 'Model\Entities\Category');
- foreach($collection as $entity){
- if($this->lang) $entity->setLang($this->lang);
- }
- return $collection;
- }
- /**
- * @param NActiveRow $row
- * @return Category
- */
- function createEntity(\Nette\Database\Table\ActiveRow $row = NULL)
- {
- $entity = new Category($row);
- if($this->lang) $entity->setLang($this->lang);
- return $entity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement