/**
  * @param Vote $vote
  * @return array
  *
  */
 public function findOne(Vote $vote)
 {
     $votedObject = $vote->getVotedObject();
     $clause = 'uid = ' . $votedObject->getIdentifier();
     $clause .= $this->getPageRepository()->enableFields($votedObject->getContentType());
     $clause .= $this->getPageRepository()->deleteClause($votedObject->getContentType());
     $object = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', $votedObject->getContentType(), $clause);
     return is_array($object) ? $object : [];
 }
示例#2
0
 /**
  * Check if $columns is valid. If it is not valid, throw an exception.
  *
  * @param Vote $vote
  * @return void
  */
 public function isValid($vote)
 {
     // Check if User is logged in
     if (!$this->getUserService()->isAuthenticated()) {
         print 'Authentication required.';
         HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_401);
     }
     if (!$vote instanceof Vote) {
         print 'I could not instantiate the Vote object.';
         HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_400);
     }
     if (empty($vote->getVotedObject()->getContentType())) {
         print 'I miss a valid content type.';
         HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_400);
     }
     $object = $this->getVotedObjectRepository()->findOne($vote);
     if (empty($object)) {
         print 'I could not retrieve the voted object.';
         HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_404);
     }
     // Check the content element that contains the voting meta information.
     $contentElementIdentifier = (int) GeneralUtility::_GP('contentElement');
     if ($contentElementIdentifier < 1) {
         print 'Invalid or missing content element parameter.';
         HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_400);
     }
     $content = $this->getContentElementService()->get($contentElementIdentifier);
     if (empty($content)) {
         print 'I could not retrieve this content element: ' . $contentElementIdentifier;
         HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_404);
     }
     $settings = $this->getContentElementService()->getSettings($contentElementIdentifier);
     if ((int) $settings['closingDate'] > 0 && (int) $settings['closingDate'] < time()) {
         print 'Sorry, the vote is closed for this content element: ' . $contentElementIdentifier;
         HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_403);
     }
     $allowedFrequency = (int) $settings['allowedFrequency'];
     $userIdentifier = $vote->getUser();
     $lastVote = $this->getVoteRepository()->findLastVote($settings['contentType'], $userIdentifier);
     if ($allowedFrequency > 0 && !empty($lastVote)) {
         if ($allowedFrequency === self::ALLOWED_ONLY_ONCE_PER_24 && time() - $lastVote['time'] < 86400) {
             print 'Sorry, you can not vote for this type of object today, please come back.';
             HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_403);
         } elseif ($allowedFrequency === self::ALLOWED_ONLY_ONCE) {
             print 'Sorry, you can vote only once for this type of object.';
             HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_403);
         }
     }
 }
示例#3
0
    /**
     * @param Vote $vote
     */
    protected function cacheRank(Vote $vote)
    {
        /* http://stackoverflow.com/a/14297055/1517316 */
        $this->getDatabaseConnection()->sql_query('SET @prev_value = NULL;');
        $this->getDatabaseConnection()->sql_query('SET @rank_count = 0;');
        $sql = sprintf('UPDATE %s
			SET %s = CASE
				WHEN @prev_value = votes THEN @rank_count
				WHEN @prev_value := votes THEN @rank_count := @rank_count + 1
				ELSE @rank_count := @rank_count + 1
			END
			%s
			ORDER BY %s DESC', $vote->getVotedObject()->getContentType(), 'rank', $this->getPossibleWhereClause($vote), $vote->getVotedObject()->getRelationalFieldName());
        $this->getDatabaseConnection()->sql_query($sql);
    }