Ejemplo n.º 1
0
 public function actionUpdate($id)
 {
     $lang = $this->getLanguageCode();
     //dùng để lấy danh sách category theo ngon ngữ đả chọn
     $errorArray = NULL;
     if (!isset($_SESSION['filemanager'])) {
         $_SESSION['filemanager'] = true;
     }
     $_SESSION['currentFolder'] = 'post/';
     //tạo array để hiện thị trên dropDownList category
     $listCategory_Array = CategoryLanguage::model()->getListCategoryWithLanguage($lang);
     unset($listCategory_Array[1]);
     //load model post
     $model = $this->loadModel($id);
     //get danh sách ngôn ngữ trong csdl
     $modellanguage = Language::model()->findAll();
     //tạo ra danh sách 1 mảng để hiển thị có dạng
     /* các dữ liệu lấy ra từ trong csdl tương ứng với postid đả có
         $arr=array(
         'vn'=>array(
         'title'=>'gia tri',
         'intro_text'=>'gia tri',
         'full_text'=>'gia tri',
         )
         'en'=>array(
         'title'=>'gia tri',
         'intro_text'=>'gia tri',
         'full_text'=>'gia tri',
         )
         );
        */
     $PostLanguage = array();
     foreach ($modellanguage as $value) {
         $tempArray = PostLanguage::model()->getPostLanguageWithLangCode($value['code'], $model->id);
         if ($tempArray != NULL) {
             $PostLanguage[$value['code']] = $tempArray;
         }
     }
     if (Yii::app()->request->getPost('Post') && Yii::app()->request->getPost('Postlanguage')) {
         $Post = Yii::app()->request->getPost('Post');
         $Post['start_date'] = Yii::app()->extraFunctions->setFormatDate($Post['start_date']);
         $Post['end_date'] = Yii::app()->extraFunctions->setFormatDate($Post['end_date']);
         $model->attributes = $Post;
         $model->meta_key = $Post['meta_key'];
         $model->meta_desc = $Post['meta_desc'];
         $model->modified_date = date('Y-m-d H:i:s');
         if ($model->start_date == '') {
             $model->start_date = date('Y-m-d H:i:s');
         }
         $model->modified_by = Yii::app()->user->id;
         //cắt chuỗi lấy tên ảnh
         $thumbnailArray = explode('/', $Post['thumbnail']);
         //            $model->thumbnail = $thumbnailArray[count($thumbnailArray) - 1];
         $thumbnail_text = '';
         if (count($thumbnailArray) > 3) {
             for ($i = 3; $i < count($thumbnailArray) - 1; $i++) {
                 $thumbnail_text = $thumbnail_text . $thumbnailArray[$i] . '/';
             }
         }
         $thumbnail_text = $thumbnail_text . $thumbnailArray[count($thumbnailArray) - 1];
         $model->thumbnail = $thumbnail_text;
         $model->post_params = CJSON::encode($_POST['Post']['post_params']);
         $model->featured = $Post['featured'];
         $transaction = Yii::app()->db->beginTransaction();
         try {
             $PostLanguage = Yii::app()->request->getPost('Postlanguage');
             if ($model->validate()) {
                 if ($model->save()) {
                     //save thành công vào table Post
                     //kiểm tra save vào table post_language
                     $this->checkPostlanguage($PostLanguage, $errorArray);
                     if ($errorArray != null) {
                         throw new Exception('error');
                         //tạo ra 1 thông báo lỗi để $transaction phía trên rollback lại
                     } else {
                         if (count($PostLanguage) == 0) {
                             Yii::app()->user->setFlash('Failure', Yii::t('post', 'title,intro_text,full_text cannot be blank.'));
                             $transaction->rollback();
                         } else {
                             //nếu $errorArray==null tức là ko có lỗi
                             //phần này sẽ có các trường hợp như sau
                             //người dùng sửa dữ liệu tại 1 trường
                             //người dùng xóa đi hết dữ liệu tại 3 trường title,intro_text,full_text =>trường hợp này phải xóa luôn trong table post_language
                             //người dùng thêm 1 hoặc nhiều ngôn ngữ mới vào
                             //do đó ta sẽ tiến hành xóa đi hết tất cả các dữ liệu trong table post_language và sau đó cập nhật lại
                             //sau khi thực hiện hàm checkPostlanguage phía trên ta đả có dc list danh sách ngôn ngũ mà người dùng nhập
                             //ta tiến hành cập nhật lại từ list này
                             //xoa hết dữ liệu đả có với post_id=$model->id
                             $postl = new PostLanguage();
                             $postl->deleteAll("post_id=:id", array(':id' => $model->id));
                             //tiến hành cập nhật lại
                             foreach ($PostLanguage as $value) {
                                 $modelpostlanguage = new PostLanguage();
                                 $languageid = Language::model()->find('code=:lang', array(':lang' => $value['code']))->id;
                                 $modelpostlanguage->post_id = $model->id;
                                 //save post success, get post_id
                                 $modelpostlanguage->language_id = $languageid;
                                 $modelpostlanguage->title = $value['title'];
                                 $modelpostlanguage->intro_text = $value['intro_text'];
                                 $modelpostlanguage->full_text = $value['full_text'];
                                 $modelpostlanguage->save();
                             }
                             $transaction->commit();
                             Yii::app()->user->setFlash('Success', Yii::t('post', 'Update Success Post with id :') . $model->id);
                             //Xóa cache redis
                             // $this->deleteCacheRedis('post:' . $model->category_id);
                             $this->redirect(array('/' . backend . '/post/admin/id/' . $model->category_id));
                         }
                     }
                 }
             }
         } catch (Exception $e) {
             Yii::app()->user->setFlash('Failure', Yii::t('post', 'Update failure'));
             $transaction->rollback();
         }
     }
     $this->render('update', array('model' => $model, 'modellanguage' => $modellanguage, 'modelpostlanguage' => $PostLanguage, 'listCategory_Array' => $listCategory_Array, 'errorArray' => $errorArray));
 }