/**
  * Creates data provider instance with search query applied
  *
  * @param array $params        	
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = CmsHierarchyItem::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'parent_id' => $this->parent_id, 'position' => $this->position, 'display_state' => $this->display_state]);
     return $dataProvider;
 }
 /**
  * Set the display state of the hierarchy item with the given id to the given displayState value.
  * The result is returned as a json encoded object.
  *
  * @menuLabel __HIDDEN__
  * @menuIcon <span class="glyphicon glyphicon-list-alt"></span>
  * @functionalRight cmsBackendWrite
  *
  * @param $hierarchyItemId integer        	
  * @param $displayState integer        	
  * @return string
  */
 public function actionSetDisplayStateJson($hierarchyItemId = -1, $displayState = -1)
 {
     $result = [];
     if ($hierarchyItemId == -1 || $displayState == -1) {
         $result['result'] = 'failed';
         $result['message'] = 'missing required parameters for action';
     } else {
         $displayState = intval($displayState);
         if ($displayState <= CmsHierarchyItem::DISPLAYSTATE_MAX_VALUE && $displayState >= CmsHierarchyItem::DISPLAYSTATE_MIN_VALUE) {
             $cmsHierarchyItem = CmsHierarchyItem::findOne($hierarchyItemId);
             if ($cmsHierarchyItem) {
                 if ($cmsHierarchyItem->id == DefaultController::$ROOT_HIERARCHY_ITEM_ID) {
                     $result['result'] = 'failed';
                     $result['message'] = 'the display state of the root node cannot be changed';
                 } else {
                     $cmsHierarchyItem->display_state = $displayState;
                     if ($cmsHierarchyItem->save()) {
                         $result['result'] = 'success';
                         $result['newDisplayState'] = $displayState;
                         $result['hierarchyItemId'] = $cmsHierarchyItem->id;
                     } else {
                         $result['result'] = 'failed';
                         $result['message'] = 'error while trying to update hierarchy node';
                     }
                 }
             } else {
                 $result['result'] = 'failed';
                 $result['message'] = 'unknown hierarchy node id given. Update failed';
             }
         } else {
             $result['result'] = 'failed';
             $result['message'] = 'invalid value for display_state given. Not within valid range';
         }
     }
     Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
     $headers = Yii::$app->response->headers;
     $headers->add('Content-Type', 'application/json; charset=utf-8');
     Yii::$app->response->charset = 'UTF-8';
     return json_encode($result, JSON_PRETTY_PRINT);
 }
 /**
  * Fix the position values of all child items for the given parent id.
  * This should be called once a new item is inserted amongst other siblings or just to perform an integrity check and avoid duplicate position values amongst siblings.
  * This function is quite expensive due to the sql calls (espcially in recusrion mode) so it must not be used in often called functions.
  *
  * @param unknown $parentItemId
  *        	the item id to get the children for an set the position values
  * @param boolean $recurseTree
  *        	recurse into the subtree structure
  * @param unknown $updateCounter        	
  * @param unknown $failedUpdateCounter        	
  * @param unknown $checkedItems        	
  */
 public static function fixItemPositionsForChildren($parentItemId, &$updateCounter, &$failedUpdateCounter, &$checkedItems, $recurseTree = false)
 {
     $childItems = CmsHierarchyItem::find()->where(['parent_id' => $parentItemId])->orderby('position ASC')->all();
     foreach ($childItems as $arrayIndex => $item) {
         $checkedItems++;
         $positionValue = $arrayIndex + 1;
         if ($item->position != $positionValue) {
             $item->position = $positionValue;
             if ($item->save()) {
                 $updateCounter++;
             } else {
                 $failedUpdateCounter++;
             }
         }
         if ($recurseTree) {
             SettingsAndMaintenanceController::fixItemPositionsForChildren($item->id, $updateCounter, $failedUpdateCounter, $checkedItems, true);
         }
     }
 }
 /**
  * Finds the CmsHierarchyItem model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  *
  * @param integer $id        	
  * @return CmsHierarchyItem the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = CmsHierarchyItem::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  *
  * @return \yii\db\ActiveQuery
  */
 public function getCmsHierarchyItem()
 {
     return $this->hasOne(CmsHierarchyItem::className(), ['id' => 'cms_hierarchy_item_id']);
 }
 /**
  *
  * @return \yii\db\ActiveQuery
  */
 public function getCmsHierarchyItems()
 {
     return $this->hasMany(CmsHierarchyItem::className(), ['parent_id' => 'id']);
 }