Exemplo n.º 1
0
 public function saveComment($postData = array())
 {
     $user = JFactory::getUser();
     $nowDate = JFactory::getDate()->toSql();
     $title = htmlspecialchars($postData['title']);
     $comment = htmlspecialchars($postData['comment'], ENT_NOQUOTES);
     if ($user->get('guest')) {
         $guestName = strip_tags($postData['guest_name']);
         $guestEmail = strip_tags($postData['guest_email']);
     } else {
         $guestName = '';
         $guestEmail = '';
     }
     $website = isset($postData['website']) ? strip_tags($postData['website']) : '';
     $listingId = (int) $postData['listing_id'];
     $params = JUDirectoryHelper::getParams(null, $listingId);
     $totalVotes = 0;
     $helpfulVotes = 0;
     $ipAddress = JUDirectoryFrontHelper::getIpAddress();
     $parentId = (int) $postData['parent_id'];
     $rootComment = JUDirectoryFrontHelperComment::getRootComment();
     if ($parentId == $rootComment->id) {
         $approved = JUDirectoryFrontHelperPermission::canAutoApprovalComment($listingId);
         $level = 1;
         if ($params->get('filter_comment_language', 0)) {
             $language = $postData['comment_language'];
         } else {
             $language = '*';
         }
     } else {
         $approved = JUDirectoryFrontHelperPermission::canAutoApprovalReplyComment($listingId);
         $parentComment = $this->getCommentObject($parentId);
         $level = $parentComment->level + 1;
         $language = '*';
     }
     if ($approved) {
         $approved = 1;
         $published = 1;
     } else {
         $approved = 0;
         $published = 0;
     }
     $dataComment = array('title' => $title, 'comment' => $comment, 'user_id' => $user->id, 'guest_name' => $guestName, 'guest_email' => $guestEmail, 'website' => $website, 'listing_id' => $listingId, 'created' => $nowDate, 'total_votes' => $totalVotes, 'helpful_votes' => $helpfulVotes, 'ip_address' => $ipAddress, 'approved' => $approved, 'published' => $published, 'parent_id' => $parentId, 'level' => $level, 'language' => $language);
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_judirectory/tables');
     $commentTable = JTable::getInstance('Comment', 'JUDirectoryTable');
     $commentTable->setLocation($postData['parent_id'], 'last-child');
     $commentTable->bind($dataComment);
     if (!$commentTable->check() || !$commentTable->store()) {
         $this->setError($commentTable->getError());
         return false;
     }
     $canRateListing = JUDirectoryFrontHelperPermission::canRateListing($listingId);
     if ($canRateListing && $params->get('enable_listing_rate_in_comment_form', 1) && $commentTable->parent_id == $rootComment->id) {
         $postData['approved'] = $approved;
         $criteriaArray = array();
         if (isset($postData['criteria_array'])) {
             if (JUDirectoryHelper::hasMultiRating()) {
                 $criteriaArray = $postData['criteria_array'];
                 $saveRating = $this->saveRating($postData, $listingId, $criteriaArray, $commentTable->id);
             }
         } else {
             if (isset($postData['ratingValue'])) {
                 $saveRating = $this->saveRating($postData, $listingId, $criteriaArray, $commentTable->id);
             }
         }
         if (!$saveRating) {
             $this->setError(JText::_('COM_JUDIRECTORY_SAVE_RATING_FAILED'));
             return false;
         }
     }
     if (JUDIRPROVERSION && isset($postData['subscribe']) && $postData['subscribe']) {
         $subscriptionData = array();
         $subscriptionData['user_id'] = $user->id;
         $subscriptionData['type'] = 'comment';
         $subscriptionData['comment_id'] = $commentTable->id;
         $subscriptionData['name'] = $user->id == 0 ? $guestName : $user->username;
         $subscriptionData['email'] = $user->id == 0 ? $guestEmail : $user->email;
         $subscriptionData['item_id'] = $commentTable->id;
         $subscriptionData['ip_address'] = $ipAddress;
         $subscriptionData['created'] = $nowDate;
         $subscriptionData['published'] = $user->id == 0 && $params->get('activate_subscription_by_email', 1) ? 0 : 1;
         require_once JPATH_SITE . '/components/com_judirectory/models/subscribe.php';
         JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_judirectory/models');
         $subscribeModel = JModelLegacy::getInstance('Subscribe', 'JUDirectoryModel');
         if (!$subscribeModel->add($subscriptionData)) {
             $this->setError(JText::_('COM_JUDIRECTORY_SUBSCRIBE_FAILED'));
             return false;
         }
     }
     if ($commentTable->parent_id == $rootComment->id) {
         JUDirectoryFrontHelperMail::sendEmailByEvent('comment.create', $commentTable->id);
         $logData = array('item_id' => $commentTable->id, 'listing_id' => $listingId, 'user_id' => $user->id, 'event' => 'comment.create');
         $commentSubmitType = 'create';
     } else {
         JUDirectoryFrontHelperMail::sendEmailByEvent('comment.reply', $commentTable->id);
         $logData = array('user_id' => $user->id, 'event' => 'comment.reply', 'item_id' => $commentTable->id, 'listing_id' => $listingId, 'value' => 0, 'reference' => $commentTable->parent_id);
         $commentSubmitType = 'reply';
     }
     JUDirectoryFrontHelperLog::addLog($logData);
     $dispatcher = JDispatcher::getInstance();
     JPluginHelper::importPlugin('judirectory');
     $dispatcher->trigger('onCommentSubmit', array($commentTable, $commentSubmitType));
     return $commentTable->id;
 }