Пример #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']) : '';

		$docId        = (int) $postData['doc_id'];
		$params       = JUDownloadHelper::getParams(null, $docId);
		$totalVotes   = 0;
		$helpfulVotes = 0;
		$ipAddress    = JUDownloadFrontHelper::getIpAddress();
		$parentId     = (int) $postData['parent_id'];
		$rootComment  = JUDownloadFrontHelperComment::getRootComment();

		
		if ($parentId == $rootComment->id)
		{
			
			$approved = JUDownloadFrontHelperPermission::canAutoApprovalComment($docId);
			$level    = 1;
			if ($params->get('filter_comment_language', 0))
			{
				$language = $postData['comment_language'];
			}
			else
			{
				$language = '*';
			}
		}
		else
		{
			
			$approved      = JUDownloadFrontHelperPermission::canAutoApprovalReplyComment($docId);
			$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,
		                     'doc_id'        => $docId, '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_judownload/tables');
		$commentTable = JTable::getInstance('Comment', 'JUDownloadTable');
		$commentTable->setLocation($postData['parent_id'], 'last-child');
		$commentTable->bind($dataComment);

		
		if (!$commentTable->check() || !$commentTable->store())
		{
			$this->setError($commentTable->getError());

			return false;
		}

		
		$canRateDocument = JUDownloadFrontHelperPermission::canRateDocument($docId);
		if ($canRateDocument && $params->get('enable_doc_rate_in_comment_form', 1) && ($commentTable->parent_id == $rootComment->id))
		{
			$postData['approved'] = $approved;
			$criteriaArray        = array();

			
			if (isset($postData['criteria_array']))
			{
				if (JUDownloadHelper::hasMultiRating())
				{
					$criteriaArray = $postData['criteria_array'];
					$saveRating    = $this->saveRating($postData, $docId, $criteriaArray, $commentTable->id);
				}
			}
			
			else
			{
				if (isset($postData['ratingValue']))
				{
					$saveRating = $this->saveRating($postData, $docId, $criteriaArray, $commentTable->id);
				}
			}

			if (!$saveRating)
			{
				$this->setError(JText::_('COM_JUDOWNLOAD_SAVE_RATING_FAILED'));

				return false;
			}
		}

		
		if (JUDLPROVERSION && 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_judownload/models/subscribe.php';
			JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_judownload/models');
			$subscribeModel = JModelLegacy::getInstance('Subscribe', 'JUDownloadModel');
			if (!$subscribeModel->add($subscriptionData))
			{
				$this->setError(JText::_('COM_JUDOWNLOAD_SUBSCRIBE_FAILED'));

				return false;
			}
		}

		
		if ($commentTable->parent_id == $rootComment->id)
		{
			
			JUDownloadFrontHelperMail::sendEmailByEvent('comment.create', $commentTable->id);
			
			$logData = array(
				'item_id' => $commentTable->id,
				'doc_id'  => $docId,
				'user_id' => $user->id,
				'event'   => 'comment.create'
			);

			$commentSubmitType = 'create';
		}
		
		else
		{
			
			JUDownloadFrontHelperMail::sendEmailByEvent('comment.reply', $commentTable->id);

			
			$logData           = array(
				'user_id'   => $user->id,
				'event'     => 'comment.reply',
				'item_id'   => $commentTable->id,
				'doc_id'    => $docId,
				'value'     => 0,
				'reference' => $commentTable->parent_id,
			);
			$commentSubmitType = 'reply';
		}

		JUDownloadFrontHelperLog::addLog($logData);

		
		$dispatcher = JDispatcher::getInstance();
		JPluginHelper::importPlugin('judownload');

		$dispatcher->trigger('onCommentSubmit', array($commentTable, $commentSubmitType));

		return $commentTable->id;
	}