BxDolVote.php 10 KB

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