dataProvider === null) { throw new InvalidConfigException('The "dataProvider" property must be set.'); } if ($this->keyNameId === null) { throw new InvalidConfigException('The "keyNameId" property must be set.'); } if ($this->formatter == null) { $this->formatter = Yii::$app->getFormatter(); } elseif (is_array($this->formatter)) { $this->formatter = Yii::createObject($this->formatter); } if (!$this->formatter instanceof Formatter) { throw new InvalidConfigException('The "formatter" property must be either a Format object or a configuration array.'); } } /** * Runs the widget. */ public function run() { if (!empty($this->assetBundle) && class_exists($this->assetBundle)) { $view = $this->getView(); $assetBundle = $this->assetBundle; $assetBundle::register($view); } if ($this->dataProvider->getCount() == 0) { return $this->renderEmptyResult(); } parent::run(); } protected function renderEmptyResult() { return empty($this->emptyResult) ? Yii::t('artbox', 'TreeViewEmptyResult') : Yii::t('artbox', $this->emptyResult); } /** * Normalize tree data * @param array $data * @param string $parentId * @return array */ protected function _normalizeTreeData(array $data, $parentId = null) { $result = []; foreach ($data as $element) { if ($element[$this->keyNameParentId] == $parentId) { $result[] = $element; $children = $this->_normalizeTreeData($data, $element[$this->keyNameId]); if ($children) { $result = array_merge($result, $children); } } } return $result; } /** * Hierarchy tree data * @param array $data * @param string $parentId * @return array */ protected function _hierarchyTreeData(array $data, $parentId = null) { $result = []; foreach ($data as $element) { if ($element[$this->keyNameParentId] == $parentId) { $children = $this->_hierarchyTreeData($data, $element[$this->keyNameId]); $result[] = [ 'item' => $element, 'children' => $children ]; } } return $result; } }