BxDolVote.php 12 KB

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