Example #1
0
    public function actionCreate()
    {
        $model = new Category;
        $lang_model = new LanguageModel;
        $CadDescModel = new CategoryDescription;
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);


        $allLangs = $lang_model->findAll('1', array('order' => 'id ASC'));

        $language = $lang_model->find("code='de'");
        $aCategories = Category::model()->generateTreeList();

        if (isset($_POST['Category'])) {
            $model->attributes = $_POST['Category'];
            if ($model->save()) {
                $pk = $model->getPrimaryKey();
                foreach ($_POST['CategoryDescription']['category_name'] as $langId => $catName) {
                    $cat_desc_model = new CategoryDescription;
                    $cat_desc_model->attributes = array(
                        'category_id' => $pk,
                        'category_name' => $catName,
                        'language_id' => $langId,
                    );
                    $cat_desc_model->save();
                }
                $this->redirect(array('index', 'category_id' => $model->category_id));

            }
        }

        $this->render('create', array(
            'model' => $model,
            'aCategories' => $aCategories,
            'allLangs' => $allLangs,
            'CadDescModel' => $CadDescModel,
        ));
    }
Example #2
0
 /**
  * Renders form for post editing.
  *
  * @param int $id Post ID.
  *
  * @throws \EHttpException Thrown if post doesn't exist (404).
  * @throws \EHttpException Thrown if post wasn't written by current user
  * (403).
  *
  * @return void
  * @since 0.1.0
  */
 public function actionEdit($id)
 {
     /** @type \Post $model */
     $model = \Post::model()->findByPk($id);
     if ($model === null) {
         throw new \EHttpException(404);
     } elseif ((int) $model->user_id !== \Yii::app()->user->id) {
         throw new \EHttpException(403, 'notAuthorized.postOwnership');
     }
     $data = \Yii::app()->request->getPost('Post');
     $categoryData = \Yii::app()->request->getPost('Category');
     $failed = false;
     $category = new \Category();
     if ($categoryData) {
         if (!$category->setAndSave($categoryData)) {
             $failed = true;
         } else {
             $model->category_id = $category->getPrimaryKey();
         }
     }
     if ($data && !$failed) {
         $model->setAndSave($data);
         \Yii::app()->user->sendSuccessMessage('post.update.success', array('{postTitle}' => $model->name));
     }
     $this->pageTitle = $model->name;
     $url = $this->createUrl('post/show', array('slug' => $model->slug));
     $this->page->addNavigationItem($url, 'link.thisPost');
     $this->page->resetI18n(array('{postTitle}' => $model->name));
     $templateVars = array('post' => $model, 'categories' => \Category::model()->getList(), 'category' => $category);
     $this->render('form', $templateVars);
 }