BxDolVote.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 $_aElementDefaults;
  80. protected $_aElementDefaultsApi;
  81. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  82. {
  83. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  84. if(empty($this->_sSystem))
  85. return;
  86. $this->_aVote = array();
  87. }
  88. /**
  89. * get votes object instanse
  90. * @param $sSys vote object name
  91. * @param $iId associated content id, where vote is available
  92. * @param $iInit perform initialization
  93. * @return null on error, or ready to use class instance
  94. */
  95. public static function getObjectInstance($sSys, $iId, $iInit = true, $oTemplate = false)
  96. {
  97. $sKey = 'BxDolVote!' . $sSys . $iId . ($oTemplate ? $oTemplate->getClassName() : '');
  98. if(isset($GLOBALS['bxDolClasses'][$sKey]))
  99. return $GLOBALS['bxDolClasses'][$sKey];
  100. $aSystems = self::getSystems();
  101. if(!isset($aSystems[$sSys]))
  102. return null;
  103. $sClassName = 'BxTemplVoteLikes';
  104. if(!empty($aSystems[$sSys]['class_name'])) {
  105. $sClassName = $aSystems[$sSys]['class_name'];
  106. if(!empty($aSystems[$sSys]['class_file']))
  107. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  108. }
  109. $o = new $sClassName($sSys, $iId, false, $oTemplate);
  110. if($iInit && method_exists($o, 'init'))
  111. $o->init($iId);
  112. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  113. }
  114. public static function &getSystems()
  115. {
  116. $sKey = 'bx_dol_cache_memory_vote_systems';
  117. if(!isset($GLOBALS[$sKey]))
  118. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_vote', 'getAllWithKey', '
  119. SELECT
  120. `ID` as `id`,
  121. `Name` AS `name`,
  122. `TableMain` AS `table_main`,
  123. `TableTrack` AS `table_track`,
  124. `PostTimeout` AS `post_timeout`,
  125. `MinValue` AS `min_value`,
  126. `MaxValue` AS `max_value`,
  127. `Pruning` AS `pruning`,
  128. `IsUndo` AS `is_undo`,
  129. `IsOn` AS `is_on`,
  130. `TriggerTable` AS `trigger_table`,
  131. `TriggerFieldId` AS `trigger_field_id`,
  132. `TriggerFieldAuthor` AS `trigger_field_author`,
  133. `TriggerFieldRate` AS `trigger_field_rate`,
  134. `TriggerFieldRateCount` AS `trigger_field_count`,
  135. `ClassName` AS `class_name`,
  136. `ClassFile` AS `class_file`
  137. FROM `sys_objects_vote`', 'name');
  138. return $GLOBALS[$sKey];
  139. }
  140. public static function onAuthorDelete ($iAuthorId)
  141. {
  142. $aSystems = self::getSystems();
  143. foreach($aSystems as $sSystem => $aSystem)
  144. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  145. return true;
  146. }
  147. public function getObjectAuthorId($iObjectId = 0)
  148. {
  149. if(empty($this->_aSystem['trigger_field_author']))
  150. return 0;
  151. return $this->_oQuery->getObjectAuthorId($iObjectId ? $iObjectId : $this->getId());
  152. }
  153. /**
  154. * Interface functions for outer usage
  155. */
  156. public function isUndo()
  157. {
  158. return (int)$this->_aSystem['is_undo'] == 1;
  159. }
  160. public function getMinValue()
  161. {
  162. return (int)$this->_aSystem['min_value'];
  163. }
  164. public function getMaxValue()
  165. {
  166. return (int)$this->_aSystem['max_value'];
  167. }
  168. public function getStatCounter()
  169. {
  170. $aVote = $this->_getVote();
  171. return $aVote['count'];
  172. }
  173. public function getStatRate()
  174. {
  175. $aVote = $this->_getVote();
  176. return $aVote['rate'];
  177. }
  178. /**
  179. * Actions functions
  180. */
  181. public function actionVote()
  182. {
  183. if(!$this->isEnabled())
  184. return echoJson(array('code' => 1, 'message' => _t('_vote_err_not_enabled')));
  185. $iObjectId = $this->getId();
  186. $iObjectAuthorId = $this->getObjectAuthorId($iObjectId);
  187. $iAuthorId = $this->_getAuthorId();
  188. $iAuthorIp = $this->_getAuthorIp();
  189. $bUndo = $this->isUndo();
  190. $bVoted = $this->isPerformed($iObjectId, $iAuthorId);
  191. $bPerformUndo = $bVoted && $bUndo;
  192. if(!$bPerformUndo && !$this->isAllowedVote())
  193. return echoJson(array('code' => 2, 'message' => $this->msgErrAllowedVote()));
  194. if($this->_isDuplicate($iObjectId, $iAuthorId, $iAuthorIp, $bVoted))
  195. return echoJson(array('code' => 3, 'message' => _t('_vote_err_duplicate_vote')));
  196. $aData = $this->_getVoteData();
  197. $aParams = $this->_getRequestParamsData();
  198. if($aData === false)
  199. return echoJson(array('code' => 4));
  200. $iId = $this->_putVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bPerformUndo);
  201. if($iId === false)
  202. return echoJson(array('code' => 5));
  203. if(!$bPerformUndo)
  204. $this->isAllowedVote(true);
  205. $this->_trigger();
  206. bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'doVote', $iObjectId, $iAuthorId, array_merge(array('vote_id' => $iId, 'vote_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId), $aData));
  207. bx_alert('vote', ($bPerformUndo ? 'un' : '') . 'do', $iId, $iAuthorId, array_merge(array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId), $aData));
  208. echoJson($this->_returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, !$bVoted, $aParams));
  209. }
  210. public function actionGetVotedBy()
  211. {
  212. if (!$this->isEnabled())
  213. return '';
  214. return $this->_getVotedBy();
  215. }
  216. /**
  217. * Permissions functions
  218. */
  219. public function isAllowedVote($isPerformAction = false)
  220. {
  221. if(isAdmin())
  222. return true;
  223. return $this->checkAction('vote', $isPerformAction);
  224. }
  225. public function msgErrAllowedVote()
  226. {
  227. return $this->checkActionErrorMsg('vote');
  228. }
  229. public function isAllowedVoteView($isPerformAction = false)
  230. {
  231. if(isAdmin())
  232. return true;
  233. return $this->checkAction('vote_view', $isPerformAction);
  234. }
  235. public function msgErrAllowedVoteView()
  236. {
  237. return $this->checkActionErrorMsg('vote_view');
  238. }
  239. public function isAllowedVoteViewVoters($isPerformAction = false)
  240. {
  241. if(isAdmin())
  242. return true;
  243. return $this->checkAction('vote_view_voters', $isPerformAction);
  244. }
  245. public function msgErrAllowedVoteViewVoters()
  246. {
  247. return $this->checkActionErrorMsg('vote_view_voters');
  248. }
  249. /**
  250. * Internal functions
  251. */
  252. protected function _isDuplicate($iObjectId, $iAuthorId, $iAuthorIp, $bVoted)
  253. {
  254. return false;
  255. }
  256. protected function _isCount($aVote = array())
  257. {
  258. if(empty($aVote))
  259. $aVote = $this->_getVote();
  260. return isset($aVote['count']) && (int)$aVote['count'] != 0;
  261. }
  262. protected function _getVoteData()
  263. {
  264. $iValue = bx_get('value');
  265. if($iValue === false)
  266. return false;
  267. $iValue = bx_process_input($iValue, BX_DATA_INT);
  268. $iMinValue = $this->getMinValue();
  269. if($iValue < $iMinValue)
  270. $iValue = $iMinValue;
  271. $iMaxValue = $this->getMaxValue();
  272. if($iValue > $iMaxValue)
  273. $iValue = $iMaxValue;
  274. return array('value' => $iValue);
  275. }
  276. protected function _putVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bPerformUndo)
  277. {
  278. return $this->_oQuery->putVote($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bPerformUndo);
  279. }
  280. protected function _returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bVoted, $aParams = array())
  281. {
  282. $bUndo = $this->isUndo();
  283. $aVote = $this->_getVote($iObjectId, true);
  284. return array(
  285. 'code' => 0,
  286. 'rate' => $aVote['rate'],
  287. 'count' => $aVote['count'],
  288. 'countf' => (int)$aVote['count'] > 0 ? $this->_getCounterLabel($aVote['count'], $aParams) : '',
  289. 'label_icon' => $this->_getIconDo($bVoted),
  290. 'label_title' => _t($this->_getTitleDo($bVoted)),
  291. 'disabled' => $bVoted && !$bUndo,
  292. );
  293. }
  294. protected function _prepareRequestParamsData($aParams, $aParamsAdd = array())
  295. {
  296. if(isset($aParams['is_voted']))
  297. $aParamsAdd['is_voted'] = $aParams['is_voted'];
  298. return parent::_prepareRequestParamsData($aParams, $aParamsAdd);
  299. }
  300. protected function _getVote($iObjectId = 0, $bForceGet = false)
  301. {
  302. if(!empty($this->_aVote) && !$bForceGet)
  303. return $this->_aVote;
  304. if(empty($iObjectId))
  305. $iObjectId = $this->getId();
  306. $this->_aVote = $this->_oQuery->getVote($iObjectId);
  307. return $this->_aVote;
  308. }
  309. protected function _isVote($iObjectId = 0, $bForceGet = false)
  310. {
  311. $aVote = $this->_getVote($iObjectId, $bForceGet);
  312. foreach($aVote as $sKey => $iValue)
  313. if(strpos($sKey, 'count_') !== false && !empty($iValue))
  314. return true;
  315. return false;
  316. }
  317. protected function _getTrack($iObjectId, $iAuthorId)
  318. {
  319. return $this->_oQuery->getTrack($iObjectId, $iAuthorId);
  320. }
  321. protected function _getIconDo($bVoted)
  322. {
  323. return '';
  324. }
  325. protected function _getTitleDo($bVoted)
  326. {
  327. return '';
  328. }
  329. protected function _getTitleDoBy($aParams = array())
  330. {
  331. return _t('_vote_do_by');
  332. }
  333. }
  334. /** @} */