BxDolScore.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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_SCORE_USAGE_BLOCK', 'block');
  11. define('BX_DOL_SCORE_USAGE_INLINE', 'inline');
  12. define('BX_DOL_SCORE_USAGE_DEFAULT', BX_DOL_SCORE_USAGE_BLOCK);
  13. define('BX_DOL_SCORE_DO_UP', 'up');
  14. define('BX_DOL_SCORE_DO_DOWN', 'down');
  15. /**
  16. * Score for any content
  17. *
  18. * Related classes:
  19. * - BxDolScoreQuery - vote database queries
  20. * - BxBaseScore - vote base representation
  21. * - BxTemplScore - 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 = BxDolScore::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 BxDolScore extends BxDolObject
  75. {
  76. protected $_aElementDefaults;
  77. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  78. {
  79. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  80. if(empty($this->_sSystem))
  81. return;
  82. $this->_oQuery = new BxDolScoreQuery($this);
  83. }
  84. /**
  85. * get votes object instanse
  86. * @param $sSys vote object name
  87. * @param $iId associated content id, where vote is available
  88. * @param $iInit perform initialization
  89. * @return null on error, or ready to use class instance
  90. */
  91. public static function getObjectInstance($sSys, $iId, $iInit = true, $oTemplate = false)
  92. {
  93. if(isset($GLOBALS['bxDolClasses']['BxDolScore!' . $sSys . $iId]))
  94. return $GLOBALS['bxDolClasses']['BxDolScore!' . $sSys . $iId];
  95. $aSystems = self::getSystems();
  96. if(!isset($aSystems[$sSys]))
  97. return null;
  98. $sClassName = 'BxTemplScore';
  99. if(!empty($aSystems[$sSys]['class_name'])) {
  100. $sClassName = $aSystems[$sSys]['class_name'];
  101. if(!empty($aSystems[$sSys]['class_file']))
  102. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  103. }
  104. $o = new $sClassName($sSys, $iId, $iInit, $oTemplate);
  105. return ($GLOBALS['bxDolClasses']['BxDolScore!' . $sSys . $iId] = $o);
  106. }
  107. public static function &getSystems()
  108. {
  109. $sKey = 'bx_dol_cache_memory_score_systems';
  110. if(!isset($GLOBALS[$sKey]))
  111. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_score', 'getAllWithKey', '
  112. SELECT
  113. `id` as `id`,
  114. `name` AS `name`,
  115. `table_main` AS `table_main`,
  116. `table_track` AS `table_track`,
  117. `post_timeout` AS `post_timeout`,
  118. `is_on` AS `is_on`,
  119. `trigger_table` AS `trigger_table`,
  120. `trigger_field_id` AS `trigger_field_id`,
  121. `trigger_field_author` AS `trigger_field_author`,
  122. `trigger_field_score` AS `trigger_field_score`,
  123. `trigger_field_cup` AS `trigger_field_cup`,
  124. `trigger_field_cdown` AS `trigger_field_cdown`,
  125. `class_name` AS `class_name`,
  126. `class_file` AS `class_file`
  127. FROM `sys_objects_score`', 'name');
  128. return $GLOBALS[$sKey];
  129. }
  130. public static function onAuthorDelete ($iAuthorId)
  131. {
  132. $aSystems = self::getSystems();
  133. foreach($aSystems as $sSystem => $aSystem)
  134. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  135. return true;
  136. }
  137. public function isPerformed($iObjectId, $iAuthorId, $iAuthorIp = 0)
  138. {
  139. return parent::isPerformed($iObjectId, $iAuthorId) && !$this->_oQuery->isPostTimeoutEnded($iObjectId, $iAuthorIp);
  140. }
  141. public function getObjectAuthorId($iObjectId = 0)
  142. {
  143. if(empty($this->_aSystem['trigger_field_author']))
  144. return 0;
  145. return $this->_oQuery->getObjectAuthorId($iObjectId ? $iObjectId : $this->getId());
  146. }
  147. /**
  148. * Interface functions for outer usage
  149. */
  150. public function getStatCounterUp()
  151. {
  152. $aScore = $this->_oQuery->getScore($this->getId());
  153. return $aScore['count_up'];
  154. }
  155. public function getStatCounterDown()
  156. {
  157. $aScore = $this->_oQuery->getScore($this->getId());
  158. return $aScore['count_down'];
  159. }
  160. public function getStatScore()
  161. {
  162. $aScore = $this->_oQuery->getScore($this->getId());
  163. return $aScore['score'];
  164. }
  165. /**
  166. * Actions functions
  167. */
  168. public function actionVoteUp()
  169. {
  170. return $this->_doVote(BX_DOL_SCORE_DO_UP);
  171. }
  172. public function actionVoteDown()
  173. {
  174. return $this->_doVote(BX_DOL_SCORE_DO_DOWN);
  175. }
  176. public function actionGetVotedBy()
  177. {
  178. if (!$this->isEnabled())
  179. return '';
  180. $aParams = $this->_decodeElementParams(bx_process_input(bx_get('element_params')));
  181. return $this->_getVotedBy($aParams);
  182. }
  183. /**
  184. * Permissions functions
  185. */
  186. public function isAllowedVote($isPerformAction = false)
  187. {
  188. if(isAdmin())
  189. return true;
  190. return $this->checkAction('vote', $isPerformAction);
  191. }
  192. public function msgErrAllowedVote()
  193. {
  194. return $this->checkActionErrorMsg('vote');
  195. }
  196. /**
  197. * Internal functions
  198. */
  199. protected function _doVote($sType)
  200. {
  201. if(!$this->isEnabled())
  202. return echoJson(array('code' => 1, 'message' => _t('_sys_score_err_not_enabled')));
  203. if(!$this->isAllowedVote(true))
  204. return echoJson(array('code' => 2, 'message' => $this->msgErrAllowedVote()));
  205. $aParams = $this->_decodeElementParams(bx_process_input(bx_get('element_params')));
  206. $iObjectId = $this->getId();
  207. $iObjectAuthorId = $this->getObjectAuthorId($iObjectId);
  208. $iAuthorId = $this->_getAuthorId();
  209. $iAuthorIp = $this->_getAuthorIp();
  210. $bVoted = $this->isPerformed($iObjectId, $iAuthorId, $iAuthorIp);
  211. if($bVoted)
  212. return echoJson(array('code' => 3, 'message' => _t('_sys_score_err_duplicate_vote')));
  213. $iId = $this->_oQuery->putVote($iObjectId, $iAuthorId, $iAuthorIp, $sType);
  214. if($iId === false)
  215. return echoJson(array('code' => 5));
  216. $this->_trigger();
  217. $sTypeUc = ucfirst($sType);
  218. bx_alert($this->_sSystem, 'doVote' . $sTypeUc, $iObjectId, $iAuthorId, array('score_id' => $iId, 'score_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId));
  219. bx_alert('score', 'do' . $sTypeUc, $iId, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
  220. $aScore = $this->_oQuery->getScore($iObjectId);
  221. $iCup = (int)$aScore['count_up'];
  222. $iCdown = (int)$aScore['count_down'];
  223. echoJson(array(
  224. 'code' => 0,
  225. 'type' => $sType,
  226. 'score' => $aScore['score'],
  227. 'scoref' => $iCup > 0 || $iCdown > 0 ? $this->_getLabelCounter($aScore['score']) : '',
  228. 'cup' => $iCup,
  229. 'cdown' => $iCdown,
  230. 'counter' => $this->getCounter($aParams),
  231. 'label_icon' => $this->_getIconDo($sType),
  232. 'label_title' => _t($this->_getTitleDo($sType)),
  233. 'disabled' => !$bVoted,
  234. ));
  235. }
  236. /**
  237. * Note. By default image based controls aren't used.
  238. * Therefore it can be overwritten in custom template.
  239. */
  240. protected function _getImageDo($sType)
  241. {
  242. $sResult = '';
  243. switch($sType) {
  244. case BX_DOL_SCORE_DO_UP:
  245. $sResult = '';
  246. break;
  247. case BX_DOL_SCORE_DO_DOWN:
  248. $sResult = '';
  249. break;
  250. }
  251. return $sResult;
  252. }
  253. protected function _getIconDo($sType)
  254. {
  255. $sResult = '';
  256. switch($sType) {
  257. case BX_DOL_SCORE_DO_UP:
  258. $sResult = 'arrow-up';
  259. break;
  260. case BX_DOL_SCORE_DO_DOWN:
  261. $sResult = 'arrow-down';
  262. break;
  263. }
  264. return $sResult;
  265. }
  266. protected function _getTitleDo($sType)
  267. {
  268. $sResult = '';
  269. switch($sType) {
  270. case BX_DOL_SCORE_DO_UP:
  271. $sResult = '_sys_score_do_up';
  272. break;
  273. case BX_DOL_SCORE_DO_DOWN:
  274. $sResult = '_sys_score_do_down';
  275. break;
  276. }
  277. return $sResult;
  278. }
  279. protected function _getTitleDoBy()
  280. {
  281. return '_sys_score_do_by';
  282. }
  283. protected function _encodeElementParams($aParams)
  284. {
  285. if(empty($aParams) || !is_array($aParams))
  286. return '';
  287. return urlencode(base64_encode(serialize($aParams)));
  288. }
  289. protected function _decodeElementParams($sParams, $bMergeWithDefaults = true)
  290. {
  291. $aParams = array();
  292. if(!empty($sParams))
  293. $aParams = unserialize(base64_decode(urldecode($sParams)));
  294. if(empty($aParams) || !is_array($aParams))
  295. $aParams = array();
  296. if($bMergeWithDefaults)
  297. $aParams = array_merge($this->_aElementDefaults, $aParams);
  298. return $aParams;
  299. }
  300. }
  301. /** @} */