예제 #1
0
 public function run()
 {
     Yii::beginProfile("NavigationWidget for " . $this->rootId);
     $items = null;
     $cacheKey = implode(':', ['Navigation', $this->rootId, $this->depth, $this->viewFile]);
     if ($this->useCache) {
         if (false === ($items = \Yii::$app->cache->get($cacheKey))) {
             $items = null;
         }
     }
     if (null === $items) {
         $root = Navigation::find()->where(['id' => $this->rootId])->with('children')->orderBy(['sort_order' => SORT_ASC])->one();
         $items = [];
         if ($this->depth > 0) {
             foreach ($root->children as $child) {
                 $items[] = self::getTree($child, $this->depth - 1);
             }
         }
         if (count($items) > 0) {
             \Yii::$app->cache->set($cacheKey, $items, 86400, new TagDependency(['tags' => [\devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag(Navigation::className())]]));
         }
     }
     $items = ArrayHelper::merge((array) $this->prependItems, $items, (array) $this->appendItems);
     $currentUri = Yii::$app->request->url;
     array_walk($items, function (&$item) use($currentUri) {
         if ($item['url'] === $currentUri) {
             $item['active'] = true;
         }
     });
     $result = $this->render($this->viewFile, ['widget' => $this->widget, 'items' => $items, 'options' => $this->options, 'linkTemplate' => $this->linkTemplate, 'submenuTemplate' => $this->submenuTemplate]);
     Yii::endProfile("NavigationWidget for " . $this->rootId);
     return $result;
 }
예제 #2
0
 public function actionRemoveAll($parent_id)
 {
     $items = Yii::$app->request->post('items', []);
     if (!empty($items)) {
         $items = Navigation::find()->where(['in', 'id', $items])->all();
         foreach ($items as $item) {
             $item->delete();
         }
     }
     return $this->redirect(['index', 'parent_id' => $parent_id]);
 }