/**
  * Create a new hierarchy item and return the result as json encoded object
  *
  * @menuLabel __HIDDEN__
  * @menuIcon <span class="glyphicon glyphicon-list-alt"></span>
  * @functionalRight cmsBackendWrite
  *
  * @param integer $parentHierarchyItemId
  *        	the parent item id to create the new element below
  * @param string $newMenuName
  *        	the language sepcific display name of the menu item
  * @param integer $position
  *        	the position (for specifiying the display order amongst all siblings) within the other siblings of the newly created item
  * @param integer $language
  *        	the language id of the menu to create
  * @see CmsHierarchyItem::DISPLAYSTATE_PUBLISHED_VISIBLE_IN_NAVIGATION
  * @see CmsHierarchyItem::DISPLAYSTATE_PUBLISHED_HIDDEN_IN_NAVIGATION
  * @see CmsHierarchyItem::DISPLAYSTATE_UNPUBLISHED
  * @return string
  */
 public function actionCreateHierarchyItemJson($parentHierarchyItemId = -1, $newMenuName = null, $position = -1, $language = -1, $jsonEncode = true)
 {
     $parentHierarchyItemId = intval($parentHierarchyItemId);
     $position = intval($position);
     $language = intval($language);
     $result = [];
     $result['message'] = '';
     $result['success'] = false;
     if ($parentHierarchyItemId < 0 || $newMenuName == '' || $position <= 0 || $language < 1) {
         $result['result'] = 'failed';
         $result['success'] = false;
         $result['message'] .= 'error, missing required parameters or invalid values given';
     } else {
         $newHierarchyItem = new CmsHierarchyItem();
         $newHierarchyItem->parent_id = $parentHierarchyItemId;
         $newHierarchyItem->display_state = CmsHierarchyItem::DISPLAYSTATE_UNPUBLISHED;
         $newHierarchyItem->position = $position;
         if ($newHierarchyItem->validate() && $newHierarchyItem->save()) {
             $result['message'] .= 'hierarchy item created, id is ' . $newHierarchyItem->id . " | ";
             $newMenuItem = new CmsMenuItem();
             $newMenuItem->created_datetime = new Expression('NOW()');
             $newMenuItem->createdby_userid = Yii::$app->user->id;
             $newMenuItem->cms_hierarchy_item_id = $newHierarchyItem->id;
             $newMenuItem->language = $language;
             $newMenuItem->name = $newMenuName;
             if ($newMenuItem->validate() && $newMenuItem->save()) {
                 $result['result'] = 'success';
                 $result['success'] = true;
                 $result['message'] .= 'new hierarchy item created';
                 $hierarchyItemDetailsArray = $newHierarchyItem->getAttributes();
                 $hierarchyItemDetailsArray['menu_item'] = $newMenuItem->getAttributes();
                 $hierarchyItemDetailsArray['children'] = [];
                 $hierarchyItemDetailsArray['available_menu_items_all_languages'] = ['language' => $newMenuItem];
                 $result['item'] = new SimpleHierarchyItem($hierarchyItemDetailsArray, true, 1);
             } else {
                 $result['result'] = 'failed';
                 $result['success'] = false;
                 $result['message'] .= 'error, hierarchy item created, but creation of language version for menu failed: ' . implode($newMenuItem->getFirstErrors());
             }
             $updateCounter = 0;
             $failedUpdateCounter = 0;
             $checkedItems = 0;
             SettingsAndMaintenanceController::fixItemPositionsForChildren($parentHierarchyItemId, $updateCounter, $failedUpdateCounter, $checkedItems, false);
         } else {
             $result['result'] = 'failed';
             $result['success'] = false;
             $result['message'] .= 'error while trying to validate and save the hierarchy item: ' . implode($newHierarchyItem->getFirstErrors());
         }
     }
     if ($jsonEncode) {
         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);
     } else {
         return $result;
     }
 }
 /**
  * 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);
         }
     }
 }