예제 #1
0
 public static function postReview($userId, $typeId, $entityId, $value, $text = false, $entity = false, $rm = false, $fb = '?', $source = '', $anonymous = '?', $tw = '?')
 {
     $prevValue = false;
     $guessAnonymous = $anonymous && $anonymous == '?' ? true : false;
     $guessFB = $fb && $fb == '?' ? true : false;
     $guessTW = $tw && $tw == '?' ? true : false;
     if ($guessAnonymous) {
         $user = sfGuardUserPeer::retrieveByPK($userId);
         if ($user) {
             $anonymous = $user->getProfile()->getAnonymous();
         }
     }
     if (!$entityId || !$value) {
         throw new Exception("Not enough parameters.");
     }
     if ($value != -1 && $value != 1) {
         throw new Exception("Invalid data for 'value'.");
     }
     // Check if already exists
     $c = new Criteria();
     if (!$typeId) {
         $c->add(SfReviewPeer::SF_REVIEW_ID, $entityId);
     } else {
         $c->add(SfReviewPeer::ENTITY_ID, $entityId);
     }
     $c->add(SfReviewPeer::SF_GUARD_USER_ID, $userId);
     $c->add(SfReviewPeer::SF_REVIEW_TYPE_ID, $typeId ? $typeId : null);
     $review = SfReviewPeer::doSelectOne($c);
     if (!$review) {
         $review = new SfReview();
         if (!$typeId) {
             $review->setSfReviewId($entityId);
         } else {
             $review->setEntityId($entityId);
         }
         $review->setSfReviewTypeId($typeId ? $typeId : null);
         $review->setSfGuardUserId($userId);
         $review->setCreatedAt(new DateTime());
         $review->setSource($source);
     } else {
         if ($rm && $value == $review->getValue() && $review->getIsActive()) {
             $review->setIsActive(false);
         } else {
             $review->setIsActive(true);
         }
         $review->setModifiedAt(new DateTime());
     }
     $review->setValue($value);
     if ($text) {
         $aText = SfVoUtil::cutToLength($text, self::MAX_LENGTH, '');
         $aText = strip_tags($aText);
         $review->setText($aText);
     }
     $review->setSfReviewStatusId(1);
     $review->setIpAddress($_SERVER['REMOTE_ADDR']);
     $review->setCookie(sfContext::getInstance()->getRequest()->getCookie('symfony'));
     $review->setCulture(sfContext::getInstance()->getUser()->getCulture());
     if (!$guessFB) {
         $review->setToFb($fb);
     }
     if (!$guessTW) {
         $review->setToTw($tw);
     }
     if (!$guessAnonymous || $review->isNew()) {
         $review->setAnonymous($anonymous);
     }
     try {
         $review->save();
         if (!$typeId) {
             $parentReview = SfReviewPeer::retrieveByPk($entityId);
             $parentReview->setBalance(SfReviewManager::getBalanceByReviewId($entityId));
             $parentReview->save();
         }
         if (!$entity) {
             if (!$typeId) {
                 $aEntityId = $parentReview->getEntityId();
                 $aTypeId = $parentReview->getSfReviewTypeId();
             } else {
                 $aTypeId = $typeId;
                 $aEntityId = $entityId;
             }
             $reviewType = SfReviewTypePeer::retrieveByPK($aTypeId);
             $peer = $reviewType->getModel() . 'Peer';
             $entity = $peer::retrieveByPK($aEntityId);
         }
         $entity->updateCalcs();
         $entity->save();
     } catch (Exception $e) {
         throw new Exception('Error writing review.');
     }
     return $review->getIsActive() ? $review : false;
 }