BxDolVote.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. bx_import('BxDolAcl');
  10. define('BX_DOL_VOTE_TYPE_STARS', 'stars');
  11. define('BX_DOL_VOTE_TYPE_LIKES', 'likes');
  12. define('BX_DOL_VOTE_USAGE_BLOCK', 'block');
  13. define('BX_DOL_VOTE_USAGE_INLINE', 'inline');
  14. define('BX_DOL_VOTE_USAGE_DEFAULT', BX_DOL_VOTE_USAGE_BLOCK);
  15. /**
  16. * @page objects
  17. * @section votes Votes
  18. * @ref BxDolVote
  19. */
  20. /**
  21. * Vote for any content
  22. *
  23. * Related classes:
  24. * - BxDolVoteQuery - vote database queries
  25. * - BxBaseVote - vote base representation
  26. * - BxTemplVote - custom template representation
  27. *
  28. * AJAX vote for any content. Stars and Plus based representations are supported.
  29. *
  30. * To add vote section to your feature you need to add a record to 'sys_objects_vote' table:
  31. *
  32. * - ID - autoincremented id for internal usage
  33. * - Name - your unique module name, with vendor prefix, lowercase and spaces are underscored
  34. * - TableMain - table name where summary votigs are stored
  35. * - TableTrack - table name where each vote is stored
  36. * - PostTimeout - number of seconds to not allow duplicate vote
  37. * - MinValue - min vote value, 1 by default
  38. * - MaxValue - max vote value, 5 by default
  39. * - IsUndo - is Undo enabled for Plus based votes
  40. * - IsOn - is this vote object enabled
  41. * - TriggerTable - table to be updated upon each vote
  42. * - TriggerFieldId - TriggerTable table field with unique record id, primary key
  43. * - TriggerFieldRate - TriggerTable table field with average rate
  44. * - TriggerFieldRateCount - TriggerTable table field with votes count
  45. * - ClassName - your custom class name, if you overrride default class
  46. * - ClassFile - your custom class path
  47. *
  48. * You can refer to BoonEx modules for sample record in this table.
  49. *
  50. *
  51. *
  52. * @section example Example of usage:
  53. * To get Star based vote you need to have different values for MinValue and MaxValue (for example 1 and 5)
  54. * and IsUndo should be equal to 0. To get Plus(Like) based vote you need to have equal values
  55. * for MinValue and MaxValue (for example 1) and IsUndo should be equal to 1. After filling the other
  56. * paramenters in the table you can show vote in any place, using the following code:
  57. * @code
  58. * $o = BxDolVote::getObjectInstance('system object name', $iYourEntryId);
  59. * if (!$o->isEnabled()) return '';
  60. * echo $o->getElementBlock();
  61. * @endcode
  62. *
  63. *
  64. * @section acl Memberships/ACL:
  65. * - vote
  66. *
  67. *
  68. *
  69. * @section alerts Alerts:
  70. * Alerts type/unit - every module has own type/unit, it equals to ObjectName.
  71. * The following alerts are rised:
  72. *
  73. * - rate - comment was posted
  74. * - $iObjectId - entry id
  75. * - $iSenderId - rater user id
  76. * - $aExtra['rate'] - rate
  77. *
  78. */
  79. class BxDolVote extends BxDolObject
  80. {
  81. protected $_oTemplate;
  82. protected $_bLike = true;
  83. protected $_sType = BX_DOL_VOTE_TYPE_LIKES;
  84. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  85. {
  86. parent::__construct($sSystem, $iId, $iInit);
  87. if(empty($this->_sSystem))
  88. return;
  89. $this->_oQuery = new BxDolVoteQuery($this);
  90. if ($oTemplate)
  91. $this->_oTemplate = $oTemplate;
  92. else
  93. $this->_oTemplate = BxDolTemplate::getInstance();
  94. $this->_bLike = $this->isLikeMode();
  95. $this->_sType = $this->_bLike ? BX_DOL_VOTE_TYPE_LIKES : BX_DOL_VOTE_TYPE_STARS;
  96. }
  97. /**
  98. * get votes object instanse
  99. * @param $sSys vote object name
  100. * @param $iId associated content id, where vote is available
  101. * @param $iInit perform initialization
  102. * @return null on error, or ready to use class instance
  103. */
  104. public static function getObjectInstance($sSys, $iId, $iInit = true, $oTemplate = false)
  105. {
  106. if(isset($GLOBALS['bxDolClasses']['BxDolVote!' . $sSys . $iId]))
  107. return $GLOBALS['bxDolClasses']['BxDolVote!' . $sSys . $iId];
  108. $aSystems = self::getSystems();
  109. if(!isset($aSystems[$sSys]))
  110. return null;
  111. $sClassName = 'BxTemplVote';
  112. if(!empty($aSystems[$sSys]['class_name'])) {
  113. $sClassName = $aSystems[$sSys]['class_name'];
  114. if(!empty($aSystems[$sSys]['class_file']))
  115. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  116. }
  117. $o = new $sClassName($sSys, $iId, $iInit, $oTemplate);
  118. return ($GLOBALS['bxDolClasses']['BxDolVote!' . $sSys . $iId] = $o);
  119. }
  120. public static function &getSystems()
  121. {
  122. if(!isset($GLOBALS['bx_dol_vote_systems']))
  123. $GLOBALS['bx_dol_vote_systems'] = BxDolDb::getInstance()->fromCache('sys_objects_vote', 'getAllWithKey', '
  124. SELECT
  125. `ID` as `id`,
  126. `Name` AS `name`,
  127. `TableMain` AS `table_main`,
  128. `TableTrack` AS `table_track`,
  129. `PostTimeout` AS `post_timeout`,
  130. `MinValue` AS `min_value`,
  131. `MaxValue` AS `max_value`,
  132. `IsUndo` AS `is_undo`,
  133. `IsOn` AS `is_on`,
  134. `TriggerTable` AS `trigger_table`,
  135. `TriggerFieldId` AS `trigger_field_id`,
  136. `TriggerFieldAuthor` AS `trigger_field_author`,
  137. `TriggerFieldRate` AS `trigger_field_rate`,
  138. `TriggerFieldRateCount` AS `trigger_field_count`,
  139. `ClassName` AS `class_name`,
  140. `ClassFile` AS `class_file`
  141. FROM `sys_objects_vote`', 'name');
  142. return $GLOBALS['bx_dol_vote_systems'];
  143. }
  144. public function isUndo()
  145. {
  146. return (int)$this->_aSystem['is_undo'] == 1;
  147. }
  148. public function isLikeMode()
  149. {
  150. $iMinValue = $this->getMinValue();
  151. $iMaxValue = $this->getMaxValue();
  152. return $iMinValue == $iMaxValue;
  153. }
  154. public function getMinValue()
  155. {
  156. return (int)$this->_aSystem['min_value'];
  157. }
  158. public function getMaxValue()
  159. {
  160. return (int)$this->_aSystem['max_value'];
  161. }
  162. public function getObjectAuthorId($iObjectId = 0)
  163. {
  164. if(empty($this->_aSystem['trigger_field_author']))
  165. return 0;
  166. return $this->_oQuery->getObjectAuthorId($iObjectId ? $iObjectId : $this->getId());
  167. }
  168. /**
  169. * Interface functions for outer usage
  170. */
  171. public function getStatCounter()
  172. {
  173. $aVote = $this->_oQuery->getVote($this->getId());
  174. return $aVote['count'];
  175. }
  176. public function getStatRate()
  177. {
  178. $aVote = $this->_oQuery->getVote($this->getId());
  179. return $aVote['rate'];
  180. }
  181. /**
  182. * Actions functions
  183. */
  184. public function actionVote()
  185. {
  186. if(!$this->isEnabled()) {
  187. echoJson(array('code' => 1));
  188. return;
  189. }
  190. $iObjectId = $this->getId();
  191. $iObjectAuthorId = $this->getObjectAuthorId($iObjectId);
  192. $iAuthorId = $this->_getAuthorId();
  193. $iAuthorIp = $this->_getAuthorIp();
  194. $bUndo = $this->isUndo();
  195. $bLikeMode = $this->isLikeMode();
  196. $bVoted = $this->_oQuery->isPerformed($iObjectId, $iAuthorId);
  197. $bPerformUndo = $bVoted && $bUndo ? true : false;
  198. if(!$bPerformUndo && !$this->isAllowedVote(true)) {
  199. echoJson(array('code' => 2, 'msg' => $this->msgErrAllowedVote()));
  200. return;
  201. }
  202. if((!$bLikeMode && !$this->_oQuery->isPostTimeoutEnded($iObjectId, $iAuthorIp)) || ($bLikeMode && $bVoted && !$bUndo)) {
  203. echoJson(array('code' => 3, 'msg' => _t('_vote_err_duplicate_vote')));
  204. return;
  205. }
  206. $iValue = bx_get('value');
  207. if($iValue === false) {
  208. echoJson(array('code' => 4));
  209. return;
  210. }
  211. $iValue = bx_process_input($iValue, BX_DATA_INT);
  212. $iMinValue = $this->getMinValue();
  213. if($iValue < $iMinValue)
  214. $iValue = $iMinValue;
  215. $iMaxValue = $this->getMaxValue();
  216. if($iValue > $iMaxValue)
  217. $iValue = $iMaxValue;
  218. $iId = $this->_oQuery->putVote($iObjectId, $iAuthorId, $iAuthorIp, $iValue, $bPerformUndo);
  219. if($iId === false) {
  220. echoJson(array('code' => 5));
  221. return;
  222. }
  223. $this->_trigger();
  224. $oZ = new BxDolAlerts($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'doVote', $iObjectId, $iAuthorId, array('vote_id' => $iId, 'vote_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId, 'value' => $iValue));
  225. $oZ->alert();
  226. $oZ = new BxDolAlerts('vote', ($bPerformUndo ? 'un' : '') . 'do', $iId, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId, 'value' => $iValue));
  227. $oZ->alert();
  228. $aVote = $this->_oQuery->getVote($iObjectId);
  229. echoJson(array(
  230. 'code' => 0,
  231. 'rate' => $aVote['rate'],
  232. 'count' => $aVote['count'],
  233. 'countf' => (int)$aVote['count'] > 0 ? $this->_getLabelCounter($aVote['count']) : '',
  234. 'label_icon' => $bLikeMode ? $this->_getIconDoLike(!$bVoted) : '',
  235. 'label_title' => $bLikeMode ? _t($this->_getTitleDoLike(!$bVoted)) : '',
  236. 'disabled' => !$bVoted && !$bUndo,
  237. ));
  238. }
  239. public function actionGetVotedBy()
  240. {
  241. if (!$this->isEnabled())
  242. return '';
  243. return $this->_getVotedBy();
  244. }
  245. /**
  246. * Permissions functions
  247. */
  248. public function isAllowedVote($isPerformAction = false)
  249. {
  250. if(isAdmin())
  251. return true;
  252. return $this->checkAction('vote', $isPerformAction);
  253. }
  254. public function msgErrAllowedVote()
  255. {
  256. return $this->checkActionErrorMsg('vote');
  257. }
  258. /**
  259. * Internal functions
  260. */
  261. protected function _getIconDoLike($bVoted)
  262. {
  263. return $bVoted && $this->isUndo() ? 'thumbs-down' : 'thumbs-up';
  264. }
  265. protected function _getTitleDoLike($bVoted)
  266. {
  267. return $bVoted && $this->isUndo() ? '_vote_do_unlike' : '_vote_do_like';
  268. }
  269. }
  270. /** @} */