BxDolVote.php 11 KB

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