Example #1
0
 public function actionEdit($id)
 {
     //var_dump($_POST);
     if ($_POST && $id) {
         if ($_POST['nest-act'] == 'create-parent' && $_POST['name_cat'] !== '') {
             //echo '<script>alert("111")</script>';
             $node = Category::model()->findByPk($id);
             $category = new Category();
             $category->name = CHtml::encode($_POST['name_cat']);
             $category->appendTo($node);
             //$this->actionIndex();
         }
         if ($_POST['nest-act'] == 'delete' && $id !== '11') {
             $node = Category::model()->findByPk($id);
             $node->deleteNode();
         }
         if ($_POST['nest-act'] == 'rename') {
             $node = Category::model()->findByPk($id);
             $node->name = CHtml::encode($_POST['name_cat']);
             $node->saveNode();
         }
         header('Location: /category');
         die;
     }
     $param = Category::model()->findByPk($id);
     $this->render('edit', ['model' => $param]);
 }
 public function actionCreate()
 {
     $err = true;
     $this->breadcrumbs = array('Теги' => array('index'), 'Создать');
     $model = new Category();
     if (isset($_POST['Category'])) {
         $model->attributes = $_POST['Category'];
         if ($model->validate()) {
             if (!empty($model->parent_id)) {
                 $parent = $this->loadModel($model->parent_id);
                 if ($model->appendTo($parent)) {
                     $model->refresh();
                     $err = false;
                 }
             } else {
                 if ($model->saveNode()) {
                     $err = false;
                 }
             }
             if ($err) {
                 $this->addFlashMessage($model->errors, 'error');
             } else {
                 $this->redirect(Yii::app()->createAbsoluteUrl('/admin/tags'));
             }
         } else {
             $this->addFlashMessage($model->errors, 'error');
         }
     }
     $this->render('_form', array('model' => $model));
 }
Example #3
0
 public static function addCategory($appendTo, $data = array())
 {
     try {
         $node = new Category($data);
         $node->appendTo($appendTo)->save();
     } catch (Exception $e) {
         echo $e->getMessage();
     }
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Category();
     //$model->enableBehavior('tree');
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Category'])) {
         $model->attributes = $_POST['Category'];
         if (!empty($_POST['Category']['parent_id'])) {
             $root = Category::model()->findByPk($_POST['Category']['parent_id']);
             $model->appendTo($root);
         } else {
             $model->saveNode();
         }
         $this->redirect(array('view', 'id' => $model->id));
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Category('create');
     $action = 'category';
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Category'])) {
         $model->attributes = $_POST['Category'];
         $parent_node = $_POST['Category']['node'];
         if ($parent_node != 0) {
             $node = Category::model()->findByPk($parent_node);
             $model->appendTo($node);
             //            print_r($_POST['DealCategory']);
             //            exit;
         }
         // file handling
         $imageUploadFile = CUploadedFile::getInstance($model, 'pic');
         if ($imageUploadFile !== null) {
             // only do if file is really uploaded
             $imageFileExt = $imageUploadFile->extensionName;
             $save_path = dirname(Yii::app()->basePath) . '/upload/' . $action . '/';
             if (!file_exists($save_path)) {
                 mkdir($save_path, 0777, true);
             }
             $ymd = date("Ymd");
             $save_path .= $ymd . '/';
             if (!file_exists($save_path)) {
                 mkdir($save_path, 0777, true);
             }
             $img_prefix = date("YmdHis") . '_' . rand(10000, 99999);
             $imageFileName = $img_prefix . '.' . $imageFileExt;
             $model->pic = $ymd . '/' . $imageFileName;
             $save_path .= $imageFileName;
         }
         if ($model->saveNode()) {
             if ($imageUploadFile !== null) {
                 // validate to save file
                 $imageUploadFile->saveAs($save_path);
             }
             $this->redirect(array('admin'));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Category('create');
     $action = 'category';
     if (isset($_POST['Category'])) {
         //Uncomment the following line if AJAX validation is needed
         //            $this->performAjaxValidation($model);
         $model->attributes = $_POST['Category'];
         $parent_node = $_POST['Category']['node'];
         if ($parent_node != 0) {
             $node = Category::model()->findByPk($parent_node);
             $model->appendTo($node);
         }
         if ($model->saveNode()) {
             $this->redirect(array('admin'));
         }
     }
     $this->render('create', array('model' => $model));
 }
    public function actionCreate() {
    
        $err = true;

        $model = new Category();
        if (isset($_POST['Category'])) {
            $model->attributes = $_POST['Category'];

            if ($model->validate()) {

                if (!empty($model->parent_id)) {
                    
                    $parent = $this->loadModel($model->parent_id);
                    if ($model->appendTo($parent)) {
                        $model->refresh();
                        $err = false;
                    }

                } else {
                    if($model->saveNode()   && $model->storeTypeAttributes(Yii::app()->getRequest()->getPost('attributes', []))){
                        $err = false;
                    }
                }

                if ($err) {
                    $this->addFlashMessage($model->errors,'error');
                } else {
                    $this->redirect(Yii::app()->createAbsoluteUrl('catalog/admin/default'));
                }
            } else {
                $this->addFlashMessage($model->errors,'error');
            }
        }

        $this->render('_form', array('model' => $model));
    }
Example #8
0
 /**
  * @expectedException Exception
  */
 public function testFailsToSaveNodeUntilParentIsSaved()
 {
     $node = new Category(array('title' => 'Node'));
     $parent = new Category(array('title' => 'Parent'));
     $node->appendTo($parent)->save();
 }
 public function actionCreate()
 {
     $lang = $this->getLanguageCode();
     $listCategory = CategoryLanguage::model()->getListCategoryWithLanguage($lang);
     $listLanguage = Language::model()->listLanguage();
     $model = new Category();
     $modelCateLang = new CategoryLanguage();
     $attributeInTab = '';
     if (Yii::app()->request->getPost('Category') && Yii::app()->request->getPost('CategoryLanguage')) {
         $catePost = Yii::app()->request->getPost('Category');
         $model->attributes = $catePost;
         $postCategoryLanguage = $_POST['CategoryLanguage'];
         $this->validateCategoryLanguage($listLanguage, $postCategoryLanguage, $modelCateLang, $errorMessage, $attributeInTab);
         if (empty($postCategoryLanguage)) {
             Yii::app()->user->setFlash('error', Yii::t('category', 'Name, Description cannot be blank.'));
         }
         if (($errorString = $this->convertErrorMessageToString($errorMessage)) !== FALSE) {
             Yii::app()->user->setFlash('error', $errorString);
         }
         if ($model->validate() && empty($errorMessage) && !empty($postCategoryLanguage)) {
             $model->attributes = Yii::app()->request->getPost('Category');
             $model->created_by = Yii::app()->user->id;
             $parent = Category::model()->findByPk($model->parent_id);
             $firstLanguage = reset($postCategoryLanguage);
             $cateUrl = Yii::app()->extraFunctions->getBreadscumFromCategoryID($parent->id);
             $catePost['params']['cateUrl'] = $cateUrl . '/' . Yii::app()->extraFunctions->setRemoveFontVi_urlTitle($firstLanguage['name']);
             $model->params = CJSON::encode($catePost['params']);
             $transaction = Yii::app()->db->beginTransaction();
             try {
                 if ($model->appendTo($parent)) {
                     foreach ($postCategoryLanguage as $index => $attribute) {
                         $modelCateLang = new CategoryLanguage('create');
                         $modelCateLang->category_id = $model->id;
                         $modelCateLang->language_id = $attribute['language_id'];
                         $modelCateLang->name = $attribute['name'];
                         $modelCateLang->description = $attribute['description'];
                         if (!$modelCateLang->save()) {
                             Yii::app()->user->setFlash('error', Yii::t('category', 'Add category language failed. Please try it later.'));
                             $transaction->rollback();
                         }
                     }
                     $transaction->commit();
                     Yii::app()->user->setFlash('success', Yii::t('category', 'Add category successfully.'));
                     $this->redirect(array('/' . backend . '/category/admin'));
                 }
                 $cateUrl = Yii::app()->extraFunctions->getBreadscumFromCategoryID($model->id);
                 $catePost['params']['cateUrl'] = $cateUrl;
                 $model->params = CJSON::encode($catePost['params']);
                 $model->save();
             } catch (Exception $ex) {
                 $transaction->rollback();
                 Yii::app()->user->setFlash('error', Yii::t('category', 'Fails.'));
                 Yii::app()->user->setFlash('message', Yii::t('category', 'Create category fails.'));
                 $this->redirect(array('/' . backend . '/category/admin'));
             }
         }
     }
     $this->render('create', array('model' => $model, 'modelCateLang' => $modelCateLang, 'listCategory' => $listCategory, 'listLanguage' => $listLanguage, 'attributeInTab' => $attributeInTab));
 }