public function actionIndex()
 {
     self::checkEditable();
     $productId = $_GET['product_id'];
     $productInfo = Product::model()->findByPk($_GET['product_id']);
     $this->breadcrumbs = array(Yii::t('Product', 'Back To Product List') => array('/product/index'), $productInfo->name . Yii::t('ProductModule', 'Edit Product Modules'));
     $selectedId = 0;
     $selectedParentId = 0;
     $addedModel = new ProductModule();
     $addedModel->product_id = $productId;
     $addedModel->parent_id = 0;
     $addedModel->display_order = 0;
     $moduleOptionArr = ProductModuleService::getSelectModuleListOption($productId);
     if (isset($_GET['selected_id']) && 0 != $_GET['selected_id']) {
         $selectedId = $_GET['selected_id'];
         $editedModel = ProductModuleService::loadModel($selectedId);
         if ($editedModel->parent_id != null) {
             $selectedParentId = $editedModel->parent_id;
         }
         $addedModel->parent_id = $_GET['selected_id'];
     } else {
         $editedModel = new ProductModule();
     }
     if (isset($_POST['ProductModule'])) {
         $returnJson = array();
         $productModuleInfo = $_POST['ProductModule'];
         $productModuleInfo['product_id'] = $productId;
         //delete product module
         if (!empty($_POST['is_delete'])) {
             $editResult = ProductModuleService::deleteModule($_POST['ProductModule']['id']);
         } else {
             if (!empty($_POST['separate_as_product'])) {
                 $editResult = ProductModuleService::separateModule($_POST['ProductModule']['id']);
             } else {
                 if (isset($_POST['ProductModule']['id'])) {
                     if (!empty($_POST['is_delete'])) {
                         ProductModule::model()->findByPk($_POST['ProductModule']['id'])->delete();
                     }
                     $editedModel->attributes = $_POST['ProductModule'];
                     $productModuleInfo['add_owner_name'] = $productModuleInfo['owner_name'];
                 } else {
                     $addedModel->attributes = $_POST['ProductModule'];
                     $productModuleInfo['owner_name'] = $productModuleInfo['add_owner_name'];
                     if (!empty($_GET['selected_id'])) {
                         $productModuleInfo['parent_id'] = $_GET['selected_id'];
                     } else {
                         $productModuleInfo['parent_id'] = 0;
                     }
                 }
                 $editResult = ProductModuleService::editProductModule($productModuleInfo);
             }
         }
         $returnJson['status'] = $editResult['status'];
         if ($editResult['status'] == CommonService::$ApiResult['FAIL']) {
             if (isset($_POST['ProductModule']['id'])) {
                 $editedModel->addErrors($editResult['detail']);
                 $returnJson['formid'] = 'edit';
             } else {
                 $addedModel->addErrors($editResult['detail']);
                 $returnJson['formid'] = 'add';
             }
             $returnJson['detail'] = $editResult['detail'];
         } else {
             if (!empty($_POST['is_delete'])) {
                 $returnJson['detail'] = Yii::t('ProductModule', 'Module deleted successfully');
             } elseif (!empty($_POST['separate_as_product'])) {
                 $returnJson['detail'] = Yii::t('ProductModule', 'Module separated as product successfully');
             } elseif (isset($_POST['ProductModule']['id'])) {
                 $returnJson['detail'] = Yii::t('ProductModule', 'Module edited successfully');
             } else {
                 $returnJson['detail'] = Yii::t('ProductModule', 'Module added successfully');
             }
         }
         echo json_encode($returnJson);
         return;
     }
     $this->render('index', array('productId' => $productInfo->id, 'selectedId' => $selectedId, 'selectedParentId' => $selectedParentId, 'productName' => $productInfo->name, 'addedModel' => $addedModel, 'editedModel' => $editedModel, 'moduleOptionArr' => $moduleOptionArr));
 }
Example #2
0
 /**
  * edit product module
  *
  * @author                                    youzhao.zxw<*****@*****.**>
  * @param   array             $params         module's information
  * @return  array                             module update result
  */
 public static function editProductModule($params)
 {
     $resultInfo = array();
     $parentModule = array();
     $oldFullPathName = '';
     $actionType = BugfreeModel::ACTION_OPEN;
     $oldRecordAttributs = array();
     if (!empty($params['id'])) {
         $productModule = ProductModule::model()->findByPk((int) $params['id']);
         $oldRecordAttributs = $productModule->attributes;
         $actionType = BugfreeModel::ACTION_EDIT;
         $productModule = self::loadModel($params['id']);
         $oldFullPathName = $productModule->full_path_name;
     } else {
         $productModule = new ProductModule();
     }
     $connection = Yii::app()->db;
     $transaction = $connection->beginTransaction();
     try {
         $productModule->attributes = $params;
         if (0 != $productModule->parent_id) {
             $parentModule = ProductModuleService::loadModel((int) $productModule->parent_id);
             if (!empty($parentModule)) {
                 $productModule->grade = $parentModule->grade + 1;
                 $productModule->full_path_name = $parentModule->full_path_name . ProductModule::MODULE_SPLITTER . $params['name'];
             } else {
                 $resultInfo['status'] = CommonService::$ApiResult['FAIL'];
                 $resultInfo['detail'] = array('parent_id' => Yii::t('ProductModule', self::ERROR_PARENT_MODULE_NOTEXIST));
                 return $resultInfo;
             }
         } else {
             $productModule->parent_id = null;
             $productModule->grade = 1;
             $productModule->full_path_name = $params['name'];
         }
         $productModule->scenario = ProductModule::SCENARIO_EDIT;
         if (!$productModule->save()) {
             $resultInfo['status'] = CommonService::$ApiResult['FAIL'];
             $resultInfo['detail'] = $productModule->getErrors();
         } else {
             $newRecord = self::loadModel($productModule->id);
             $addActionResult = AdminActionService::addActionNotes('product_module', $actionType, $newRecord, $oldRecordAttributs);
             if ('' != $oldFullPathName && $oldFullPathName != $newRecord['full_path_name']) {
                 $renameResult = self::updateChildModule($oldFullPathName);
                 if (CommonService::$ApiResult['SUCCESS'] == $renameResult['status']) {
                     $transaction->commit();
                     $resultInfo['status'] = CommonService::$ApiResult['SUCCESS'];
                     $resultInfo['detail'] = array('id' => $productModule->id);
                 } else {
                     $resultInfo = $renameResult;
                 }
             } else {
                 $transaction->commit();
                 $resultInfo['status'] = CommonService::$ApiResult['SUCCESS'];
                 $resultInfo['detail'] = array('id' => $productModule->id);
             }
         }
     } catch (Exception $e) {
         $transaction->rollBack();
         $resultInfo['status'] = CommonService::$ApiResult['FAIL'];
         $resultInfo['detail']['id'] = $e->getMessage();
     }
     return $resultInfo;
 }