/** * Создание категории */ public function create() { if (!User::isAdmin()) { App::abort('403'); } $category = new Category(); if (Request::isMethod('post')) { $category->token = Request::input('token', true); $category->parent_id = Request::input('parent_id'); $category->name = Request::input('name'); $category->slug = Request::input('slug'); $category->description = Request::input('description'); $category->sort = Request::input('sort'); if ($category->save()) { App::setFlash('success', 'Категория успешно создана!'); App::redirect('/category'); } else { App::setFlash('danger', $category->getErrors()); App::setInput($_POST); } } $maxSort = Category::find(['select' => 'max(sort) max']); $maxSort = $maxSort->max + 1; $categories = Category::getAll(); App::view('categories.create', compact('category', 'categories', 'maxSort')); }
/** * Save/Add a category with ID. * Rowid and ParentId are RowID of the current category and parentIDs * Category is the category name * blbImage is base64encoded png * meta keywords and descriptions are for meta tags displayed for SEO improvement * Custom page is a page-key defined in Custom Pages in admin panel * Position defines the sorting position of category. Lower number comes first * * @param string $passkey * @param int $intRowId * @param int $intParentId * @param string $strCategory * @param string $strMetaKeywords * @param string $strMetaDescription * @param string $strCustomPage * @param int $intPosition * @param string $blbImage * @return string */ public function save_category_with_id($passkey, $intRowId, $intParentId, $strCategory, $strMetaKeywords, $strMetaDescription, $strCustomPage, $intPosition, $blbImage) { Yii::app()->db->createCommand('SET FOREIGN_KEY_CHECKS=0;')->execute(); if (!$this->check_passkey($passkey)) { return self::FAIL_AUTH; } // Prepare values $strCategory = trim($strCategory); $strCustomPage = trim($strCustomPage); if (!$strCategory) { Yii::log("Could not save empty category", 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return self::UNKNOWN_ERROR; } $objCategoryAddl = false; // If provided a rowid, attempt to load it if ($intRowId) { $objCategoryAddl = CategoryAddl::model()->findByPk($intRowId); } else { if (!$objCategoryAddl && $intParentId) { $objCategoryAddl = CategoryAddl::LoadByNameParent($strCategory, $intParentId); } } // Failing that, create a new Category if (!$objCategoryAddl) { $objCategoryAddl = new CategoryAddl(); $objCategoryAddl->created = new CDbExpression('NOW()'); $objCategoryAddl->id = $intRowId; } $objCategoryAddl->label = $strCategory; if ($intParentId > 0) { $objCategoryAddl->parent = $intParentId; } $objCategoryAddl->menu_position = $intPosition; $objCategoryAddl->modified = new CDbExpression('NOW()'); $objCategoryAddl->save(); //Now that we've successfully saved in our cache table, update the regular Category table $objCategory = Category::model()->findByPk($intRowId); // Failing that, create a new Category if (!$objCategory) { $objCategory = new Category(); $objCategory->created = new CDbExpression('NOW()'); $objCategory->id = $objCategoryAddl->id; } if ($objCategory) { $objCategory->label = $objCategoryAddl->label; $objCategory->parent = $objCategoryAddl->parent; $objCategory->menu_position = $objCategoryAddl->menu_position; } if (!$objCategory->save()) { _xls_log("SOAP ERROR : Error saving category {$strCategory} " . print_r($objCategory->getErrors(), true)); return self::UNKNOWN_ERROR . " Error saving category {$strCategory} " . print_r($objCategory->getErrors(), true); } //After saving, update some key fields $objCategory->UpdateChildCount(); $objCategory->request_url = $objCategory->GetSEOPath(); if (!$objCategory->save()) { _xls_log("SOAP ERROR : Error saving category (after updating){$strCategory} " . print_r($objCategory->getErrors(), true)); return self::UNKNOWN_ERROR . " Error saving category (after updating){$strCategory} " . print_r($objCategory->getErrors(), true); } Yii::app()->db->createCommand('SET FOREIGN_KEY_CHECKS=1;')->execute(); return self::OK; }
public function actionEditableCreator() { if (isset($_POST['Category'])) { $model = new Category(); $model->attributes = $_POST['Category']; if ($model->save()) { echo CJSON::encode($model->getAttributes()); } else { $errors = array_map(function ($v) { return join(', ', $v); }, $model->getErrors()); echo CJSON::encode(array('errors' => $errors)); } } else { throw new CHttpException(400, 'Invalid request'); } }
/** * Saves new/existing model via AJAX call. * * @param null|string|int $slug Category slug. * * @throws \EHttpException Thrown if accessed non-ajaxly, no data sent or * model with specified ID doesn't exist. * * @todo update render method(s) to avoid lines of code like the last ones * * @return void * @since 0.1.0 */ public function actionAjaxSave() { if (\Yii::app()->request->isAjaxRequest && (!defined('YII_DEBUG') || !YII_DEBUG)) { throw new \EHttpException(400, 'badRequest.ajaxOnly'); } if (!($data = \Yii::app()->request->getPost('Category'))) { throw new \EHttpException(400, 'badRequest.noDataReceived'); } if (isset($data['id'])) { if (!($category = \Category::model()->findByPk($data['id']))) { throw new \EHttpException(404); } } else { $category = new \Category(); } $response = array('success' => true); if (!$category->setAndSave($data)) { $response['success'] = false; $response['errors'] = $category->getErrors(); } else { $response['data'] = $category->getPublicAttributes(); } $this->page->format = 'json'; header($this->page->generateFormatHeader()); echo \CJSON::encode($response); }