public function actionCommentCreate()
 {
     if (isset($_POST['productCommentJSON']) && isJSON($_POST['productCommentJSON'])) {
         $productCommentModel = new ProductComment();
         $errorMessage = null;
         $commentCreatePost = json_decode($_POST['productCommentJSON']);
         $productCommentModel->text = $commentCreatePost['text'];
         $productCommentModel->create_time = date("Y-m-d H:i:s");
         $productCommentModel->product_id = $commentCreatePost['product_id'];
         $productCommentModel->contact_method = $commentCreatePost['contact_method'];
         $productCommentModel->amazing_level = $commentCreatePost['amazing_level'];
         if (!$productCommentModel->validateProductId()) {
             $errorMessage = '该产品不存在,请确认后重新提交';
         }
         if (!$productCommentModel->validateContactMethod()) {
             $errorMessage = '联系方式超出长度,请简略填写联系信息(小于50个字)';
         }
         if (!$productCommentModel->validateSiteMark()) {
             $errorMessage = '评分不在范围之内,请重新输入或与管理员联系';
         }
         $this->layout = false;
         if ($errorMessage === null) {
             $productModel = Product::model()->findByPk($productCommentModel->product_id);
             $productModel->product_mark_sum = $productModel->product_mark * $productModel->product_mark_times + $productCommentModel->amazing_level;
             ++$productModel->product_mark_times;
             $productModel->product_mark = $productModel->product_mark_sum / $productModel->product_mark_times;
             if ($productCommentModel->save() && $productModel->save()) {
                 $this->render('_customerCreate', array('response' => 'success', 'message' => $productCommentModel->create_time));
             }
         } else {
             $this->render('_customerCreate', array('response' => 'failure', 'message' => $errorMessage));
         }
     }
 }
 public function actionCommentCreate()
 {
     if (isset($_POST['commentJSON']) && isJSON($_POST['commentJSON'])) {
         $productCommentModel = new ProductComment();
         $errorMessage = null;
         $commentCreatePost = json_decode($_POST['commentJSON'], true);
         $productCommentModel->text = isset($commentCreatePost['text']) ? $commentCreatePost['text'] : '';
         $productCommentModel->create_time = date("Y-m-d H:i:s");
         $productCommentModel->product_id = isset($commentCreatePost['product_id']) ? $commentCreatePost['product_id'] : '';
         $productCommentModel->contact_method = isset($commentCreatePost['contact_method']) ? $commentCreatePost['contact_method'] : '';
         $productCommentModel->amazing_level = isset($commentCreatePost['amazing_level']) ? $commentCreatePost['amazing_level'] : 5;
         if (!$productCommentModel->validateProductId($productCommentModel->product_id)) {
             $errorMessage = Yii::t('productComment', 'The product is not exist, please try again!');
         }
         if (!$productCommentModel->validateContactMethod()) {
             $errorMessage = Yii::t('productComment', 'Contact method is too long, please shorten it!');
         }
         if (!$productCommentModel->validateSiteMark()) {
             $errorMessage = Yii::t('productComment', 'The mark is out of range, please try again!');
         }
         $this->layout = false;
         if ($errorMessage === null) {
             $productModel = Product::model()->findByAttributes(array('product_id' => $productCommentModel->product_id));
             $productModel->product_mark_sum = $productModel->product_mark * $productModel->product_marked_times + $productCommentModel->amazing_level;
             ++$productModel->product_marked_times;
             $productModel->product_mark = $productModel->product_mark_sum / $productModel->product_marked_times;
             if ($productCommentModel->save() && $productModel->save()) {
                 $this->render('_customerCreate', array('response' => 'success', 'message' => $productCommentModel->create_time));
             }
         } else {
             $this->render('_customerCreate', array('response' => 'failure', 'message' => $errorMessage));
         }
     }
 }