/** * Finds the LetCategory model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return LetCategory the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = LetCategory::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function search($params) { $query = LetCategory::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'root' => $this->root, 'lft' => $this->lft, 'rgt' => $this->rgt, 'level' => $this->level]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'module', $this->module]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getCategory() { return $this->hasMany(\app\modules\category\models\LetCategory::className(), ['id' => 'category_id'])->viaTable('{{%' . self::moduleName() . '_category}}', ['item_id' => 'id']); }
/** * Renders the widget. */ public function run() { $categories = LetCategory::getCategory($this->model->moduleName(), '-- '); echo Html::activeCheckboxList($this->model, $this->attribute, $categories, $this->options); }
/** * Get category cua tat ca cac module hoac cho tung module. Trong truong hop module do chua co danh muc root thi se tao danh muc root * @param string $module * @param string $prefix * @return array */ public static function getCategory($module = '', $prefix = '') { $level = NULL; $categorys = array(); if (empty($module) or !in_array($module, self::getModules())) { $data = self::find()->addOrderBy('lft')->all(); } else { $root = self::find()->where('module = :module', [':module' => $module])->andWhere('lft = 1')->addOrderBy('lft')->one(); if ($root === null) { // Create root for module $root = new LetCategory(); $root->title = $module; $root->module = $module; $root->saveNode(); } $data = self::find()->where('module = :module', [':module' => $module])->andWhere('lft != 1')->addOrderBy('lft')->all(); $categorys[$root->id] = 'Danh mục gốc của module: ' . $root->module; } foreach ($data as $category) { if ($level == NULL) { $level = $category->level - 1; } $categorys[$category->id] = str_repeat($prefix, $category->level - $level) . $category->title; } return $categorys; }
/** * Delete a category */ public function actionDelete() { try { $id = (int) ArrayHelper::getValue($_POST, 'id', 0); $model = LetCategory::findOne($id); if ($model->deleteNode()) { echo 1; } else { echo 0; } } catch (ErrorException $e) { echo 0; } }