/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = DcmdNodeGroupAttrDef::find()->orderBy('attr_name');
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['attr_id' => $this->attr_id, 'optional' => $this->optional, 'attr_type' => $this->attr_type, 'ctime' => $this->ctime, 'opr_uid' => $this->opr_uid]);
     $query->andFilterWhere(['like', 'attr_name', $this->attr_name])->andFilterWhere(['like', 'def_value', $this->def_value])->andFilterWhere(['like', 'comment', $this->comment]);
     return $dataProvider;
 }
 /**
  * Displays a single DcmdNodeGroup model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $searchModel = new DcmdNodeSearch();
     $searchModel->ngroup_id = $id;
     $params = Yii::$app->request->queryParams;
     $params["DcmdNodeSearch"]["ngroup_id"] = $id;
     $params["DcmdNodeSearch"]["rack"] = "";
     $dataProvider = $searchModel->search($params);
     $show_div = "dcmd-node-group";
     if (array_key_exists('show_div', $params)) {
         $show_div = $params['show_div'];
     }
     if (array_key_exists("DcmdNodeSearch", Yii::$app->request->queryParams)) {
         $show_div = 'dcmd-node';
     }
     ///获取属性
     $self_attr = DcmdNodeGroupAttr::find()->andWhere(['ngroup_id' => $id])->asArray()->all();
     $def_attr = DcmdNodeGroupAttrDef::find()->asArray()->all();
     $attr_str = '<div id="w1" class="grid-view">
       <table class="table table-striped table-bordered"><thead>
       <tr><th>属性名</th><th>值</th><th>操作</th></tr>
       </thead><tbody>';
     $attr = array();
     foreach ($self_attr as $item) {
         $attr_str .= '<tr><td>' . $item['attr_name'] . '</td><td>' . $item['attr_value'] . '</td><td><a href="/ducter/index.php?r=dcmd-node-group-attr/update&id=' . $item['id'] . '&ngroup_id=' . $id . '">修改</a></td></tr>';
         $attr[$item['attr_name']] = $item['attr_name'];
     }
     foreach ($def_attr as $item) {
         if (array_key_exists($item['attr_name'], $attr)) {
             continue;
         }
         $attr_str .= '<tr><td>' . $item['attr_name'] . '</td><td>' . $item['def_value'] . '</td><td><a href="/ducter/index.php?r=dcmd-node-group-attr/update&id=0&attr_id=' . $item['attr_id'] . '&ngroup_id=' . $id . '">修改</a></td></tr>';
     }
     $attr_str .= "</tbody></table></div>";
     return $this->render('view', ['model' => $this->findModel($id), 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'attr_str' => $attr_str, 'ngroup_id' => $id, 'show_div' => $show_div]);
 }
 /**
  * Finds the DcmdNodeGroupAttrDef model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return DcmdNodeGroupAttrDef the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = DcmdNodeGroupAttrDef::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * Updates an existing DcmdNodeGroupAttr model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id, $ngroup_id)
 {
     $node_group = DcmdNodeGroup::findOne($ngroup_id);
     ///仅仅用户与该应用在同一个系统组才可以操作
     $query = DcmdUserGroup::findOne(['uid' => Yii::$app->user->getId(), 'gid' => $node_group->gid]);
     if ($query == NULL) {
         Yii::$app->getSession()->setFlash('success', NULL);
         Yii::$app->getSession()->setFlash('error', "对不起, 你没有权限!");
         return $this->redirect(['/dcmd-node-group/view', 'id' => $ngroup_id, 'show_div' => 'dcmd-node-group-attr']);
     }
     if ($id == 0) {
         ///需要新建
         $attr_id = Yii::$app->request->queryParams['attr_id'];
         $def_attr = DcmdNodeGroupAttrDef::findOne($attr_id);
         $query = DcmdNodeGroupAttr::findOne(['ngroup_id' => $ngroup_id, 'attr_name' => $def_attr->attr_name]);
         if (count($query) > 0) {
             $id = $query['id'];
         } else {
             $model = new DcmdNodeGroupAttr();
             $model->ngroup_id = $ngroup_id;
             $model->attr_name = $def_attr->attr_name;
             $model->attr_value = $def_attr->def_value;
             $model->comment = $def_attr->comment;
             $model->utime = date('Y-m-d H:i:s');
             $model->ctime = $model->utime;
             $model->opr_uid = Yii::$app->user->getId();
             $model->save();
             $id = $model->id;
         }
     }
     $model = $this->findModel($id);
     $group = DcmdNodeGroup::findOne($model->ngroup_id);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         Yii::$app->getSession()->setFlash('success', '修改成功!');
         return $this->redirect(['/dcmd-node-group/view', 'id' => $group->ngroup_id, 'show_div' => 'dcmd-node-group-attr']);
     } else {
         return $this->render('update', ['model' => $model, 'group' => $group]);
     }
 }