/**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     $page_type_list = array();
     $type = PageType::model()->findAll('status=1');
     foreach ($type as $page_type) {
         $page_type_list[$page_type->page_type_id] = $page_type->name_th;
     }
     $page_id_list = array();
     $page = Page::model()->findAll('status=1');
     foreach ($page as $page_list) {
         $page_id_list[$page_list->page_id] = $page_list->name_th;
     }
     if (isset($_POST['LeftMenu'])) {
         $_POST['LeftMenu']['user_id'] = Yii::app()->user->id;
         $model->attributes = $_POST['LeftMenu'];
         if ($model->save()) {
             $this->redirect(array('index'));
         }
     }
     $this->render('update', array('model' => $model, 'page_type_list' => $page_type_list, 'page_id_list' => $page_id_list));
 }
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return PageType the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = PageType::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     $page_type_list = array();
     $type = PageType::model()->findAll('status=1');
     foreach ($type as $page_type) {
         $page_type_list[$page_type->page_type_id] = $page_type->name_th;
     }
     if (isset($_POST['Page'])) {
         $record_file_en = $model->pdf_en;
         $record_file_th = $model->pdf_th;
         $record_image = $model->images;
         $record_thumb = $model->thumbnail;
         $_POST['Page']['user_id'] = Yii::app()->user->id;
         list($day, $month, $year) = explode('/', $_POST['Page']['create_date']);
         $create_date = $year . '-' . $month . '-' . $day;
         $_POST['Page']['create_date'] = $create_date;
         $model->attributes = $_POST['Page'];
         $file_en = CUploadedFile::getInstance($model, 'pdf_en');
         if ($file_en) {
             $genName = 'en_pdf_' . date('YmdHis');
             $saveName = $genName;
             while (file_exists($this->upload_path . $saveName . '.' . $file_en->getExtensionName())) {
                 $saveName = $genName . '-' . rand(0, 99);
             }
             $model->pdf_en = $saveName . '.' . $file_en->getExtensionName();
         }
         $file_th = CUploadedFile::getInstance($model, 'pdf_th');
         if ($file_th) {
             $genName = 'th_pdf_' . date('YmdHis');
             $saveName = $genName;
             while (file_exists($this->upload_path . $saveName . '.' . $file_th->getExtensionName())) {
                 $saveName = $genName . '-' . rand(0, 99);
             }
             $model->pdf_th = $saveName . '.' . $file_th->getExtensionName();
         }
         $images = CUploadedFile::getInstance($model, 'images');
         if ($images) {
             $genName = 'img_' . date('YmdHis');
             $saveName = $genName;
             while (file_exists($this->upload_path_images . $saveName . '.' . $images->getExtensionName())) {
                 $saveName = $genName . '-' . rand(0, 99);
             }
             $model->images = $saveName . '.' . $images->getExtensionName();
         }
         $thumbnail = CUploadedFile::getInstance($model, 'thumbnail');
         if ($thumbnail) {
             $genName = 'thumb_' . date('YmdHis');
             $saveName = $genName;
             while (file_exists($this->upload_path_thumb . $saveName . '.' . $thumbnail->getExtensionName())) {
                 $saveName = $genName . '-' . rand(0, 99);
             }
             $model->thumbnail = $saveName . '.' . $thumbnail->getExtensionName();
         }
         if ($model->save()) {
             if ($file_en) {
                 if (file_exists($this->upload_path . $record_file_en)) {
                     @unlink($this->upload_path . $record_file_en);
                 }
                 $file_en->saveAs($this->upload_path . $model->pdf_en);
             }
             if ($file_th) {
                 if (file_exists($this->upload_path . $record_file_th)) {
                     @unlink($this->upload_path . $record_file_th);
                 }
                 $file_th->saveAs($this->upload_path . $model->pdf_th);
             }
             if ($images) {
                 if (file_exists($this->upload_path_images . $record_image)) {
                     @unlink($this->upload_path_images . $record_image);
                 }
                 $images->saveAs($this->upload_path_images . $model->images);
             }
             if ($thumbnail) {
                 if (file_exists($this->upload_path_thumb . $record_thumb)) {
                     @unlink($this->upload_path_thumb . $record_thumb);
                 }
                 $thumbnail->saveAs($this->upload_path_thumb . $model->thumbnail);
             }
             $this->redirect(array('index'));
         }
     }
     $this->render('update', array('model' => $model, 'page_type_list' => $page_type_list));
 }