Exemplo n.º 1
0
 public function actionCreate()
 {
     $model = new Category();
     $description = new CategoryDescription();
     $image = new XUploadForm();
     $this->performAjaxValidation($model, 'category-form');
     $userImages = array();
     if (!isset($_POST[$this->modelName])) {
         Yii::app()->user->setState('images', NULL);
     }
     if (isset($_POST[$this->modelName])) {
         $model->setAttributes($_POST[$this->modelName]);
         $description->setAttributes($_POST[$this->modelName . 'Description']);
         $suc = Yii::t('info', 'The record was successfully created');
         $err = Yii::t('info', 'The record could not be created');
         $userImages = Yii::app()->user->getState('images');
         if (Yii::app()->user->hasState('images')) {
             $description->category_id = 0;
             $description->locale_code = Yii::app()->getLanguage();
             //$description->link = str_replace(' ','-',trim(strtolower(str_replace('_','-',preg_replace('/(?<![A-Z])[A-Z]/', '\0', $description->name)))));
             $description->link = strtolower(str_replace(' ', '-', preg_replace('!\\s+!', ' ', trim(preg_replace("/[^A-Za-z ]/", ' ', $description->name)))));
             if ($model->validate() && $description->validate()) {
                 if ($model->save()) {
                     $description->category_id = $model->id;
                     $description->save();
                     $this->addImages($model->id, 'Image', 'images', 'Category');
                     Yii::app()->user->setFlash(TbHtml::ALERT_COLOR_SUCCESS, $suc);
                     if (Yii::app()->getRequest()->getIsAjaxRequest()) {
                         $this->renderPartial('_view', array('model' => $model), false, true);
                         Yii::app()->end();
                     } else {
                         $this->redirect(array('view', 'id' => $model->id));
                     }
                 } else {
                     Yii::app()->user->setFlash(TbHtml::ALERT_COLOR_ERROR, $err);
                 }
             } else {
                 $description->validate();
             }
         } else {
             Yii::app()->user->setFlash(TbHtml::ALERT_COLOR_ERROR, Yii::t('info', 'Please upload an image'));
         }
     }
     if (Yii::app()->getRequest()->getIsAjaxRequest()) {
         $this->renderPartial('_form', array('model' => $model, 'description' => $description, 'image' => $image, 'userImages' => $userImages), false, true);
         Yii::app()->end();
     }
     $this->render('create', array('model' => $model, 'description' => $description, 'image' => $image, 'userImages' => $userImages));
 }
Exemplo n.º 2
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,
        ));
    }
Exemplo n.º 3
0
 public function save()
 {
     $category = Category::model()->findByPk($this->id);
     if (is_null($category)) {
         // insert
         // Category
         $category = new Category();
         $category->date_added = date('Y-m-d');
         $category->date_modified = date('Y-m-d');
         $category->image = $this->image;
         $category->top = $this->top;
         $category->column = $this->columns;
         $category->sort_order = $this->sortOrder;
         $category->status = $this->status;
         $category->parent_id = $this->parent;
         $category->save();
         // Description
         $description = new CategoryDescription();
         $description->category_id = $category->category_id;
         $description->language_id = 1;
         // TODO: language must be dynamic
         $description->name = $this->name;
         $description->meta_description = $this->metaTagDescription;
         $description->meta_keyword = $this->metaTagKeywords;
         $description->description = $this->description;
         $description->save();
     } else {
         // update
         // Category
         $category->date_modified = date('Y-m-d');
         $category->image = $this->image;
         $category->top = $this->top;
         $category->column = $this->columns;
         $category->sort_order = $this->sortOrder;
         $category->status = $this->status;
         $category->parent_id = $this->parent;
         $category->save();
         // Description
         $category->description->name = $this->name;
         $category->description->meta_description = $this->metaTagDescription;
         $category->description->meta_keyword = $this->metaTagKeywords;
         $category->description->description = $this->description;
         $category->description->save();
     }
     // SEO keyword
     $category->updateSEOKeyword($this->seoKeyword);
     // Stores
     $category->clearAllStoresRelations();
     if (isset($this->stores) && count($this->stores)) {
         foreach ($this->stores as $storeId) {
             $category->addToStore($storeId);
         }
     }
     // Filters
     $category->clearAllFiltersRelations();
     if (isset($this->filters) && count($this->filters) > 0) {
         foreach ($this->filters as $filterId) {
             $category->addFilter($filterId);
         }
     }
 }