Пример #1
0
 /**
  * create a new media category item and return the result in json formated way
  * @menuLabel __HIDDEN__
  * @return View
  */
 public function actionCreateCategoryItemJson($parentCategoryId, $name)
 {
     $result = [];
     $result['message'] = '';
     $result['success'] = true;
     $parentCategoryId = intval($parentCategoryId);
     /* @var $parentContentMediaCategory CmsContentCategory */
     $parentContentMediaCategory = CmsContentCategory::findOne($parentCategoryId);
     if ($parentContentMediaCategory == null) {
         $result['success'] = false;
         $result['message'] .= 'The parent category id seems to be invalid (id = ' . $parentCategoryId . ' could not be found)';
     } else {
         $newCmsContentCategoryItem = new CmsContentCategory();
         $newCmsContentCategoryItem->displayname = $name;
         $newCmsContentCategoryItem->parent_id = $parentCategoryId;
         if ($newCmsContentCategoryItem->insert()) {
             $result['success'] = true;
             $result['message'] .= 'Category has been created';
             $result['newid'] = $newCmsContentCategoryItem->id;
         } else {
             $result['success'] = false;
             $result['message'] .= 'failed to create new category item';
             $result['errors'] = $newCmsContentCategoryItem->errors;
         }
     }
     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);
 }