예제 #1
0
 public static function editProduct($params)
 {
     $resultInfo = array();
     $connection = Yii::app()->db;
     $transaction = $connection->beginTransaction();
     $actionType = BugfreeModel::ACTION_OPEN;
     $oldRecordAttributs = array();
     if (isset($params['id'])) {
         $product = self::loadModel((int) $params['id']);
         $oldRecordAttributs = $product->attributes;
         $oldRecordAttributs['product_manager'] = $product->product_manager;
         if (!empty($product->group_name)) {
             $oldRecordAttributs['group_name'] = join(',', $product->group_name);
         }
         $actionType = BugfreeModel::ACTION_EDIT;
     } else {
         $product = new Product();
     }
     if (!ProductService::isProductEditable($product['id'])) {
         $resultInfo['status'] = CommonService::$ApiResult['FAIL'];
         $resultInfo['detail']['id'] = Yii::t('Common', 'Required URL not found or permission denied.');
         return $resultInfo;
     }
     try {
         $product->attributes = $params;
         if (!$product->save()) {
             $resultInfo['status'] = CommonService::$ApiResult['FAIL'];
             $resultInfo['detail'] = $product->getErrors();
         } else {
             Yii::app()->db->createCommand()->delete('{{map_product_group}}', 'product_id=:productId', array(':productId' => $product->id));
             if (!empty($params['group_name'])) {
                 foreach ($params['group_name'] as $groupId) {
                     $mapProductGroup = new MapProductGroup();
                     $mapProductGroup->product_id = $product->id;
                     $mapProductGroup->user_group_id = $groupId;
                     $mapProductGroup->save();
                 }
             }
             Yii::app()->db->createCommand()->delete('{{map_product_user}}', 'product_id=:productId', array(':productId' => $product->id));
             if ('' != trim($params['product_manager'])) {
                 $managerNameArr = CommonService::splitStringToArray(",", $params['product_manager']);
                 foreach ($managerNameArr as $managerName) {
                     $userInfo = TestUser::model()->findByAttributes(array('realname' => $managerName, 'is_dropped' => CommonService::$TrueFalseStatus['FALSE']));
                     if ($userInfo !== null) {
                         $mapProductUser = new MapProductUser();
                         $mapProductUser->product_id = $product->id;
                         $mapProductUser->test_user_id = $userInfo->id;
                         $mapProductUser->save();
                     } else {
                         $resultInfo['status'] = CommonService::$ApiResult['FAIL'];
                         $resultInfo['detail'] = array('product_manager' => '[' . $managerName . ']' . Yii::t('TestUser', self::ERROR_USER_NOT_FOUND));
                         return $resultInfo;
                     }
                 }
             }
             $newRecord = self::loadModel($product->id);
             if (!empty($newRecord->group_name)) {
                 $newRecord->group_name = join(',', $newRecord->group_name);
             }
             $addActionResult = AdminActionService::addActionNotes('product', $actionType, $newRecord, $oldRecordAttributs);
             if (!isset($params['id'])) {
                 FieldConfigService::createAddOnTable($product->id);
             }
             $transaction->commit();
             $resultInfo['status'] = CommonService::$ApiResult['SUCCESS'];
             $resultInfo['detail'] = array('id' => $product->id);
         }
         return $resultInfo;
     } catch (Exception $e) {
         $transaction->rollBack();
         $resultInfo['status'] = CommonService::$ApiResult['FAIL'];
         $resultInfo['detail']['id'] = $e->getMessage();
     }
     return $resultInfo;
 }