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));
 }
 /**
  * This function performs the validation work for complex object models.
  *
  * In addition to checking the current object, all related objects will
  * also be validated.  If all pass then <code>true</code> is returned; otherwise
  * an aggreagated array of ValidationFailed objects will be returned.
  *
  * @param      array $columns Array of column names to validate.
  * @return     mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise.
  */
 protected function doValidate($columns = null)
 {
     if (!$this->alreadyInValidation) {
         $this->alreadyInValidation = true;
         $retval = null;
         $failureMap = array();
         // We call the validate method on the following object(s) if they
         // were passed to this object by their coresponding set
         // method.  This object relates to these object(s) by a
         // foreign key reference.
         if ($this->aCategory !== null) {
             if (!$this->aCategory->validate($columns)) {
                 $failureMap = array_merge($failureMap, $this->aCategory->getValidationFailures());
             }
         }
         if ($this->aProduct !== null) {
             if (!$this->aProduct->validate($columns)) {
                 $failureMap = array_merge($failureMap, $this->aProduct->getValidationFailures());
             }
         }
         if (($retval = CategoryHasProductPeer::doValidate($this, $columns)) !== true) {
             $failureMap = array_merge($failureMap, $retval);
         }
         $this->alreadyInValidation = false;
     }
     return !empty($failureMap) ? $failureMap : true;
 }
 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));
 }
 /**
  * Update the specified category in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id)
 {
     $category = Category::findOrFail($id);
     $validator = Category::validate($data = Input::all());
     if ($validator->fails()) {
         return Redirect::back()->withErrors($validator)->withInput();
     }
     $category->update($data);
     return Redirect::route('admin.categories.index');
 }
Example #5
0
 /**
  * This function performs the validation work for complex object models.
  *
  * In addition to checking the current object, all related objects will
  * also be validated.  If all pass then <code>true</code> is returned; otherwise
  * an aggreagated array of ValidationFailed objects will be returned.
  *
  * @param      array $columns Array of column names to validate.
  * @return     mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise.
  */
 protected function doValidate($columns = null)
 {
     if (!$this->alreadyInValidation) {
         $this->alreadyInValidation = true;
         $retval = null;
         $failureMap = array();
         // We call the validate method on the following object(s) if they
         // were passed to this object by their coresponding set
         // method.  This object relates to these object(s) by a
         // foreign key reference.
         if ($this->aCategory !== null) {
             if (!$this->aCategory->validate($columns)) {
                 $failureMap = array_merge($failureMap, $this->aCategory->getValidationFailures());
             }
         }
         if ($this->aBook !== null) {
             if (!$this->aBook->validate($columns)) {
                 $failureMap = array_merge($failureMap, $this->aBook->getValidationFailures());
             }
         }
         if (($retval = ArticlePeer::doValidate($this, $columns)) !== true) {
             $failureMap = array_merge($failureMap, $retval);
         }
         if ($this->collAuthorArticles !== null) {
             foreach ($this->collAuthorArticles as $referrerFK) {
                 if (!$referrerFK->validate($columns)) {
                     $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
                 }
             }
         }
         if ($this->collAttachments !== null) {
             foreach ($this->collAttachments as $referrerFK) {
                 if (!$referrerFK->validate($columns)) {
                     $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
                 }
             }
         }
         $this->alreadyInValidation = false;
     }
     return !empty($failureMap) ? $failureMap : true;
 }
Example #6
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Category();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Category'])) {
         $model->attributes = $_POST['Category'];
         if ($model->validate()) {
             if ($model->save()) {
                 Yii::app()->clientScript->scriptMap['jquery.js'] = false;
                 echo CJSON::encode(array('status' => 'success', 'div' => "<div class=alert alert-info fade in>Successfully added ! </div>"));
                 Yii::app()->end();
             }
         }
     }
     if (Yii::app()->request->isAjaxRequest) {
         $cs = Yii::app()->clientScript;
         $cs->scriptMap = array('jquery.js' => false, 'bootstrap.js' => false, 'jquery.min.js' => false, 'bootstrap.notify.js' => false, 'bootstrap.bootbox.min.js' => false);
         echo CJSON::encode(array('status' => 'render', 'div' => $this->renderPartial('_form', array('model' => $model), true, false)));
         Yii::app()->end();
     } else {
         $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));
    }
 public function postCategoryAddNew($id = "")
 {
     if (\Request::ajax()) {
         $file = "./uploads/images/";
         if (isset($_FILES["files"]) && $_FILES["files"]['tmp_name'] != "") {
             $sign_name = $_FILES["files"]["name"];
             $File_Ext = substr($sign_name, strrpos($sign_name, '.'));
             //get file extention
             $File_Name = substr($sign_name, 0, strrpos($sign_name, '.'));
             //get file extention
             $sign_tmp_name = $_FILES["files"]["tmp_name"];
             $NewFileNameSignature = preg_replace("/\\s/", "_", $File_Name) . "_" . time() . $File_Ext;
             //move_uploaded_file($tmp_name, "$uploads_dir/$name");
             move_uploaded_file($sign_tmp_name, $file . basename($sign_name));
             rename($file . basename($sign_name), $file . $NewFileNameSignature);
         }
         ############ Configuration ##############
         $thumb_square_size = 200;
         //Thumbnails will be cropped to 200x200 pixels
         $max_image_size = 500;
         //Maximum image size (height and width)
         $thumb_prefix = "thumb_";
         //Normal thumb Prefix
         if (public_path()) {
             $destination_folder = public_path() . "/uploads/images/";
             //upload directory ends with / (slash)
             $image_invention_folder = public_path() . "/uploads/images/";
         } else {
             $image_invention_folder = '/home/medicalng/public_html/uploads/images/';
             $destination_folder = '/home/medicalng/public_html/uploads/images/';
         }
         //$file =
         $jpeg_quality = 90;
         //jpeg quality
         ##########################################
         //continue only if $_POST is set and it is a Ajax request
         if (isset($_POST) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
             // check $_FILES['ImageFile'] not empty
             if (!isset($_FILES['image_file']) || !is_uploaded_file($_FILES['image_file']['tmp_name'])) {
                 die('Image file is Missing!');
                 // output error when above checks fail.
                 exit;
             }
             //uploaded file info we need to proceed
             $image_name = $_FILES['image_file']['name'];
             //file name
             $image_size = $_FILES['image_file']['size'];
             //file size
             $image_temp = $_FILES['image_file']['tmp_name'];
             //file temp
             $image_size_info = getimagesize($image_temp);
             //get image size
             if ($image_size_info) {
                 $image_width = $image_size_info[0];
                 //image width
                 $image_height = $image_size_info[1];
                 //image height
                 $image_type = $image_size_info['mime'];
                 //image type
             } else {
                 die("Make sure image file is valid!");
             }
             //switch statement below checks allowed image type
             //as well as creates new image from given file
             switch ($image_type) {
                 case 'image/png':
                     $image_res = imagecreatefrompng($image_temp);
                     break;
                 case 'image/gif':
                     $image_res = imagecreatefromgif($image_temp);
                     break;
                 case 'image/jpeg':
                 case 'image/pjpeg':
                     $image_res = imagecreatefromjpeg($image_temp);
                     break;
                 default:
                     $image_res = false;
             }
             if ($image_res) {
                 //Get file extension and name to construct new file name
                 $image_info = pathinfo($image_name);
                 $image_extension = strtolower($image_info["extension"]);
                 //image extension
                 $image_name_only = strtolower($image_info["filename"]);
                 //file name only, no extension
                 //create a random name for new image (Eg: fileName_293749.jpg) ;
                 $mrand = rand(0, 9999999999.0);
                 $new_file_name = $image_name_only . '_' . $mrand . '.' . $image_extension;
                 //folder path to save resized images and thumbnails
                 //$thumb_save_folder 	= $destination_folder . $thumb_prefix . $new_file_name;
                 $image_save_folder = $destination_folder . $new_file_name;
                 //call normal_resize_image() function to proportionally resize image
                 if (normal_resize_image($image_res, $image_save_folder, $image_type, $max_image_size, $image_width, $image_height, $jpeg_quality)) {
                     $img1 = \Image::make($image_invention_folder . $new_file_name);
                     $img1->resize(100, 100);
                     $imageNewNameten = $image_name_only . '_' . $mrand . "-100x100" . "." . $image_extension;
                     $img1->save($image_invention_folder . "thumbs/" . $imageNewNameten);
                     $img2 = \Image::make($image_invention_folder . $new_file_name);
                     $img2->resize(50, 50);
                     $imageNewNameten = $image_name_only . '_' . $mrand . "-50x50" . "." . $image_extension;
                     $img2->save($image_invention_folder . "thumbs/" . $imageNewNameten);
                     /* We have succesfully resized and created thumbnail image
                        We can now output image to user's browser or store information in the database.$destination_folder*/
                     echo '<div align="center">';
                     echo '<img src="' . url() . '/uploads/images/' . $new_file_name . '" alt="Resized Image">';
                     echo '</div>@@' . $new_file_name;
                 }
                 imagedestroy($image_res);
                 //freeup memory
             }
         }
         exit;
     }
     $validation = \Category::validate(\Input::all());
     $input = \Input::all();
     //print_r($input);
     if ($validation->fails()) {
         return \Redirect::back()->withErrors($validation)->withInput();
     } else {
         $parent_name = $input['parent_name'];
         array_forget($input, "parent_name");
         array_forget($input, "_token");
         try {
             if ($id == "") {
                 $officer = new \Category();
                 foreach ($input as $key => $value) {
                     $officer->{$key} = $value;
                 }
                 if ($officer->save()) {
                     \Session::put("success_message", "Record Saved");
                     return \Redirect::back();
                 } else {
                     \Session::put("error_message", "Sorry, Unexpected Error! Record Could Not Be Saved");
                     return \Redirect::back();
                 }
             } else {
                 $officer = \Category::find($id);
                 $image = $input['image'] == "" ? $input['oldimage'] : $input['image'];
                 array_forget($input, "oldimage");
                 foreach ($input as $key => $value) {
                     $officer->{$key} = $value;
                 }
                 $officer->image = $image;
                 if ($officer->update()) {
                     \Session::put("success_message", "Record Updated");
                     return \Redirect::back();
                 } else {
                     \Session::put("error_message", "Sorry, Unexpected Error! Record Could Not Be Saved");
                     return \Redirect::back();
                 }
             }
         } catch (ValidationException $e) {
             \Session::put("error_message", $e->getMessage());
             return \Redirect::back()->withInput()->withErrors($e->getErrors());
         } catch (\Illuminate\Database\QueryException $e) {
             \Session::put("error_message", $e->getMessage());
             return \Redirect::back();
             exit;
         } catch (\PDOException $e) {
             \Session::put("error_message", $e->getMessage());
             return \Redirect::back();
             //exit;
         } catch (\Exception $e) {
             \Session::put("error_message", $e->getMessage());
             return \Redirect::back();
             //exit;
         }
     }
 }
 /**
  * Store a newly created resource in storage.
  * POST categories
  *
  * @return Response
  */
 public function store()
 {
     $validator = Category::validate(Input::all());
     //		if ($validator->fails())
     //			return $this->respondInsufficientPrivileges($validator->messages()->all());
     $category = Category::create(Input::all());
     if ($category) {
         return Redirect::to('/admin/categories');
     }
     return $this->respondServerError();
 }