Exemplo n.º 1
0
 public function actionDelete()
 {
     if (isset($_POST['trash'])) {
         $model = $this->loadModel($_POST['id']);
         $transaction = Yii::app()->db->beginTransaction();
         try {
             $children = Menu::model()->getChildrenInNode($model->id);
             $children = implode(',', $children);
             MenuLanguage::model()->deleteAll('menu_id IN (' . $children . ')');
             if ($model->deleteNode()) {
                 $transaction->commit();
                 Yii::app()->user->setFlash('success', Yii::t('menu', 'Menu was deleted from system.'));
             }
         } catch (Exception $e) {
             $transaction->rollback();
             Yii::app()->user->setFlash('error', Yii::t('menu', 'Menu cannot delete. Please try it later.'));
         }
         $this->redirect(array('/' . backend . '/menu/admin'));
     }
 }
 /**
  * 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)
 {
     if (!isset($_SESSION['filemanager'])) {
         $_SESSION['filemanager'] = true;
     }
     $_SESSION['currentFolder'] = 'advertisement/';
     $path = Yii::getPathOfAlias('webroot') . '/themes/backend/templateDetails.xml';
     $positions = Yii::app()->extraFunctions->getPositionFromXML($path);
     //        $tree = Yii::app()->extraFunctions->parseTree(Menu::model()->getListMenu(Yii::app()->language, 'main_menu', 1, '1'));
     $tree = MenuLanguage::model()->getListMenuLanguage(Yii::app()->language, NULL, '1,2');
     $model = $this->loadModel($id);
     $model->scenario = 'update';
     $model->params = CJSON::decode($model->params);
     if (!empty($model->end_date) && !empty($model->start_date)) {
         $model->numDays = Yii::app()->extraFunctions->getNumDays($model->end_date, $model->start_date);
     }
     if (!empty($model->start_date)) {
         $model->start_date = Yii::app()->extraFunctions->getFormatDate($model->start_date);
     }
     if (isset($_POST['Advertisement'])) {
         $validateArray = array('title', 'alt', 'image', 'is_banner', 'url', 'numDays', 'language_id', 'ordering', 'position', 'type', 'status');
         if ($_POST['Advertisement']['start_date'] !== $model->start_date) {
             array_push($validateArray, 'start_date');
         }
         $imageArray = explode('/', $_POST['Advertisement']['image']);
         $imageName = $imageArray[count($imageArray) - 1];
         $model->attributes = $_POST['Advertisement'];
         $model->imgExtension = pathinfo($imageName, PATHINFO_EXTENSION);
         $model->image = $imageName;
         if (!empty($model->imgExtension)) {
             array_push($validateArray, 'imgExtension');
         }
         if ($model->type !== 'html') {
             $path = rawurldecode(Yii::getPathOfAlias('webroot') . '/images/advertisement/' . $model->image);
             if (!is_file($path)) {
                 $model->image = NULL;
             }
         }
         if ($model->validate($validateArray)) {
             $model->start_date = !empty($_POST['Advertisement']['start_date']) ? Yii::app()->extraFunctions->setFormatDate($_POST['Advertisement']['start_date']) : NULL;
             $model->end_date = !empty($_POST['Advertisement']['numDays']) && !empty($_POST['Advertisement']['start_date']) ? Yii::app()->extraFunctions->getEndTime($_POST['Advertisement']['start_date'], $model->numDays) : NULL;
             $params = array();
             //                foreach ($model->params as $key => $value)
             //                {
             //                    if (isset($_POST[$key]))
             //                    {
             //                        $params[$key] = $_POST[$key];
             //                    }
             //                }
             if ($_POST['Advertisement']['params']['menu_checkbox'] == 1) {
                 $params['menu_id'] = $_POST['Advertisement']['params']['menu_id'];
             }
             $model->params = CJSON::encode($params);
             if ($model->update()) {
                 Yii::app()->user->setFlash('success', Yii::t('advertisement', 'Update advertisement\'s info successfully.'));
             } else {
                 Yii::app()->user->setFlash('error', Yii::t('advertisement', 'Update advertisement\'s info failed. Please try it later.'));
             }
             $this->redirect(array('/' . backend . '/advertisement/admin'));
         }
     }
     $this->render(strtolower($this->action->Id), array('model' => $model, 'positions' => $positions, 'tree' => $tree));
 }