/**
  * Создает новую модель категории.
  * Если создание прошло успешно - перенаправляет на просмотр.
  *
  * @return void
  */
 public function actionCreate()
 {
     $model = new Category();
     if (($data = Yii::app()->getRequest()->getPost('Category')) !== null) {
         $model->setAttributes($data);
         if ($model->save()) {
             Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('CategoryModule.category', 'Record was created!'));
             $this->redirect((array) Yii::app()->getRequest()->getPost('submit-type', ['create']));
         }
     }
     $languages = $this->yupe->getLanguagesList();
     //если добавляем перевод
     $id = (int) Yii::app()->getRequest()->getQuery('id');
     $lang = Yii::app()->getRequest()->getQuery('lang');
     if (!empty($id) && !empty($lang)) {
         $category = Category::model()->findByPk($id);
         if (null === $category) {
             Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::ERROR_MESSAGE, Yii::t('CategoryModule.category', 'Targeting category was not found!'));
             $this->redirect(['create']);
         }
         if (!array_key_exists($lang, $languages)) {
             Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::ERROR_MESSAGE, Yii::t('CategoryModule.category', 'Language was not found!'));
             $this->redirect(['create']);
         }
         Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('CategoryModule.category', 'You are adding translate in to {lang}!', ['{lang}' => $languages[$lang]]));
         $model->lang = $lang;
         $model->slug = $category->slug;
         $model->parent_id = $category->parent_id;
         $model->name = $category->name;
     } else {
         $model->lang = Yii::app()->language;
     }
     $this->render('create', ['model' => $model, 'languages' => $languages]);
 }
Example #2
0
 public function actionManage()
 {
     $model = new Category('search');
     $model->unsetAttributes();
     if (isset($_GET['Category'])) {
         $model->setAttributes($_GET['Category']);
     }
     $this->render('manage', array('model' => $model));
 }
 public function actionKitten($pid = 0)
 {
     if ($pid) {
         $parent = Category::model()->findByPk(intval($pid));
         if (!$parent) {
             throw new CHttpException(404, "Раздела не существует");
         }
     } else {
         $parent = new Category();
     }
     $child = new Category();
     $child->setAttributes($_POST["Category"]);
     $child->setParent($parent);
     if (!$child->save()) {
         Yii::app()->user->setFlash("error", $child->getErrorsString());
     }
     $this->redirect("/moderator/catalog?edit={$parent->id}");
 }
 /**
  * Show and process edit category page
  *
  * @param void
  * @return null
  */
 function edit_category()
 {
     if ($this->active_category->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $this->setTemplate(array('module' => RESOURCES_MODULE, 'controller' => 'categories', 'template' => 'edit'));
     $category_data = $this->request->post('category');
     if (!is_array($category_data)) {
         $category_data = array('name' => $this->active_category->getName());
     }
     // if
     $this->smarty->assign('category_data', $category_data);
     if ($this->request->isSubmitted()) {
         $old_name = $this->active_category->getName();
         $this->active_category->setAttributes($category_data);
         $save = $this->active_category->save();
         if ($save && !is_error($save)) {
             if ($this->request->isApiCall()) {
                 $this->serveData($this->active_category, 'category');
             } elseif ($this->request->isAsyncCall()) {
                 print $this->active_category->getName();
                 die;
             } else {
                 flash_success('Category :category_name has been updated', array('category_name' => $old_name));
                 $this->redirectToUrl($this->smarty->get_template_vars('categories_url'));
             }
             // if
         } else {
             if ($this->request->isApiCall() || $this->request->isAsyncCall()) {
                 $this->serveData($save);
             } else {
                 $this->smarty->assign('errors', $save);
             }
             // if
         }
         // if
     }
     // if
 }
Example #5
0
 public function importCategories($categories)
 {
     Ak::import('category');
     $CategoryInstance = new Category(array('init' => false));
     if ($this->db) {
         $CategoryInstance->setConnection($this->db);
     }
     $CategoryInstance->init();
     foreach ($categories as $category_name => $related) {
         if (!($Category = $CategoryInstance->findFirstBy('name', $category_name))) {
             $Category = new Category(array('init' => false));
             if ($this->db) {
                 $Category->setConnection($this->db);
             }
             $Category->init();
             $Category->setAttributes(array('name' => $category_name));
             if ($Category->save()) {
                 $this->log('Created new category: ' . $category_name);
             }
         }
         if (!empty($related['relations'])) {
             foreach ($related['relations'] as $related_category) {
                 if (!($RelatedCategory = $CategoryInstance->findFirstBy('name', $related_category))) {
                     $RelatedCategory = new Category(array('init' => false));
                     if ($this->db) {
                         $Category->setConnection($this->db);
                     }
                     $Category->init();
                     $Category->setAttributes(array('name' => $related_category));
                     $RelatedCategory->save();
                 }
                 $this->log('Relating category ' . $related_category . ' with ' . $category_name);
                 $Category->related_category->add($RelatedCategory);
             }
         }
     }
 }