tree = new NestedSetsTree( $this->getTable(), $this->getKeyName(), $this->nodeGetParentKey(), $this->nodesGroups, $this->nodeGetGroupKey() ); } /** * Subscribe on model events */ public static function bootNestedSets() { static::saving(function ($model) { /** @var self $model */ return $model->nodeInsert(); }); static::deleting(function ($model) { /** @var self $model */ $model->nodeDelete(); }); } /** * Left key column name * * @return string */ public function nodeGetLeftKey() { return 'numleft'; } /** * Right key column name * * @return string */ public function nodeGetRightKey() { return 'numright'; } /** * Level column name * * @return string */ public function nodeGetLevelKey() { return 'numlevel'; } /** * Parent identifier column name * * @return string */ public function nodeGetParentKey() { return 'pid'; } /** * Get current root identifier * @param integer $groupID * @param bool $justCreated @ref * @return int|bool */ public function nodeGetRootNodeID($groupID = 0, &$justCreated = false) { return $this->tree->getRootNodeID($groupID, $justCreated); } /** * Group identifier column name * * @return string */ public function nodeGetGroupKey() { return 'group_id'; } /** * Enabled column name * * @return string */ public function nodeGetEnabledKey() { return 'enabled'; } /** * Set parent identifier * * @param int $parentId * @return void */ public function nodeSetParent($parentId) { $this->setAttribute($this->nodeGetParentKey(), $parentId); } /** * Change parent (move to another parent) * * @param int $parentId * @return bool */ public function nodeChangeParent($parentId) { if ($this->exists) { if (! $this->tree->changeParent($this->getKey(), $parentId, $this->nodeGetMaxDeep())) { return false; } } $this->nodeSetParent($parentId); return true; } /** * Get current parent identifier * * @return int */ public function nodeGetParent() { return (int)$this->getAttribute($this->nodeGetParentKey()); } /** * Get max allowed nested numlevel or false (no limits) * * @return bool|int */ public function nodeGetMaxDeep() { return $this->nodesMaxDeep; } /** * Set group identifier * * @param int $groupId * @return void */ public function nodeSetGroup($groupId) { $this->tree->setGroupID($groupId); } /** * Insert node * * @param int $parentId * @return bool */ public function nodeInsert($parentId = 0) { if (! $this->exists) { if ($this->fireModelEvent('creating') === false) { return false; } if (! $parentId) { $parentId = $this->nodeGetParent(); } $nodeID = 0; $key = $this->getKeyName(); if (! empty($this->$key)) { $nodeID = $this->$key; } $id = $this->tree->insertNode($parentId, $nodeID); if (empty($id)) { return false; } if ($nodeID) { $this->original[$this->getKeyName()] = $nodeID; } else { $this->setAttribute($this->getKeyName(), $id); } if ($this->usesTimestamps()) { $this->updateTimestamps(); } $this->exists = true; $this->wasRecentlyCreated = true; $this->fireModelEvent('created', false); } return true; } /** * Rotate nodes * Based on table drag&drop input data * * @param string $prefix * @param null $groupId * @return array|bool */ public function nodesRotate($prefix = 'dnd-', $groupId = null) { if (! empty($groupId)) { $this->tree->setGroupID($groupId); } return $this->tree->rotateTablednd($prefix); } /** * Toggle node * * @param int $id node identifier * @param bool $toggleChildren * @return bool */ public function nodeToggle($id = null, $toggleChildren = false) { if (empty($id)) { $id = $this->getKey(); } return $this->tree->toggleNodeEnabled($id, true, $toggleChildren); } /** * Delete node * * @return bool */ public function nodeDelete() { $this->nodesRecentlyDeleted = $this->tree->deleteNode($this->getKey()); return false; } /** * Get list of nodes identifiers recently deleted * * @return array|mixed */ public function nodesRecentlyDeleted() { return $this->nodesRecentlyDeleted; } /** * Проверяем дерево на валидность * @param boolean $report с отчетом или без отчета * @return boolean валидация пройдена или нет, если отчет включен - развернутый ответ */ public function treeValidate($report = true) { return $this->tree->validate($report); } }