示例#1
0
 /**
  * Store user vote.
  *
  * @param string                   $context
  * @param array                    $data   This is a data about user and his vote
  * @param Joomla\Registry\Registry $params The parameters of the component
  *
  * @return  null|array
  */
 public function onVote($context, &$data, $params)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     if ($app->isAdmin()) {
         return;
     }
     $doc = JFactory::getDocument();
     /**  @var $doc JDocumentHtml */
     // Check document type
     $docType = $doc->getType();
     if (strcmp('raw', $docType) !== 0) {
         return;
     }
     if (strcmp('com_userideas.vote', $context) !== 0) {
         return;
     }
     $itemId = !empty($data['id']) ? (int) $data['id'] : 0;
     $userId = !empty($data['user_id']) ? (int) $data['user_id'] : 0;
     // Save vote
     $item = new Userideas\Item\Item(JFactory::getDbo());
     $item->load($itemId);
     if (!$item->getId()) {
         return null;
     }
     $item->vote();
     // Add record to history table
     $history = new Userideas\Vote\Vote(JFactory::getDbo());
     if (!$userId) {
         $hash = $this->generateHash();
         $history->setHash($hash);
     } else {
         $history->setUserId($userId);
     }
     $history->setItemId($itemId)->setVotes(1)->store();
     // Prepare response data
     $data['response_data'] = array('user_votes' => 1, 'votes' => $item->getVotes());
 }