BxDolFavorite.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. define('BX_DOL_FAVORITE_USAGE_BLOCK', 'block');
  10. define('BX_DOL_FAVORITE_USAGE_INLINE', 'inline');
  11. define('BX_DOL_FAVORITE_USAGE_DEFAULT', BX_DOL_FAVORITE_USAGE_BLOCK);
  12. /**
  13. * Track any object favorites automatically
  14. *
  15. * Add record to sys_object_favorite table to track object favorites,
  16. * to record favorites just create this class instance with your object id,
  17. * for example:
  18. * @code
  19. * BxDolFavorite::getObjectInstance('my_system', 25); // 25 - is object id
  20. * @endcode
  21. *
  22. * Description of sys_object_favorite table fields:
  23. * @code
  24. * `name` - system name, it is better to use unique module prefix here, lowercase and all spaces are underscored
  25. * `table_track` - table to track favorites
  26. * `is_on` - is the system activated
  27. * `trigger_table` - table where you need to update favorites field
  28. * `trigger_field_id` - table field id to unique determine object
  29. * `trigger_field_count` - table field where total favorites number is stored
  30. * `class_name` - your custom class name, if you overrride default class
  31. * `class_file` - your custom class path
  32. * @endcode
  33. *
  34. * Structure of the track table is the following:
  35. * @code
  36. * CREATE TABLE `my_favorites_track` (
  37. * `object_id` int(11) NOT NULL default '0', -- this field type must be exact as your object id type
  38. * `author_id` int(11) NOT NULL default '0', -- favoring user profile id
  39. * `date` int(11) NOT NULL default '0', -- timestamp of last recorded view
  40. * KEY `id` (`object_id`,`author_id`)
  41. * );
  42. * @endcode
  43. *
  44. */
  45. class BxDolFavorite extends BxDolObject
  46. {
  47. protected $_sBaseUrl;
  48. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  49. {
  50. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  51. if(empty($this->_sSystem))
  52. return;
  53. $this->_oQuery = new BxDolFavoriteQuery($this);
  54. $this->_sBaseUrl = BxDolPermalinks::getInstance()->permalink($this->_aSystem['base_url']);
  55. if(get_mb_substr($this->_sBaseUrl, 0, 4) != 'http')
  56. $this->_sBaseUrl = BX_DOL_URL_ROOT . $this->_sBaseUrl;
  57. }
  58. /**
  59. * get favorites object instanse
  60. * @param $sSys favorite object name
  61. * @param $iId associated content id, where favorite is available
  62. * @param $iInit perform initialization
  63. * @return null on error, or ready to use class instance
  64. */
  65. public static function getObjectInstance($sSys, $iId, $iInit = true)
  66. {
  67. if(isset($GLOBALS['bxDolClasses']['BxDolFavorite!' . $sSys . $iId]))
  68. return $GLOBALS['bxDolClasses']['BxDolFavorite!' . $sSys . $iId];
  69. $aSystems = self::getSystems();
  70. if(!isset($aSystems[$sSys]))
  71. return null;
  72. $sClassName = 'BxTemplFavorite';
  73. if(!empty($aSystems[$sSys]['class_name'])) {
  74. $sClassName = $aSystems[$sSys]['class_name'];
  75. if(!empty($aSystems[$sSys]['class_file']))
  76. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  77. }
  78. $o = new $sClassName($sSys, $iId, $iInit);
  79. return ($GLOBALS['bxDolClasses']['BxDolFavorite!' . $sSys . $iId] = $o);
  80. }
  81. public static function &getSystems()
  82. {
  83. $sKey = 'bx_dol_cache_memory_favorite_systems';
  84. if(!isset($GLOBALS[$sKey]))
  85. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_favorite', 'getAllWithKey', '
  86. SELECT
  87. `id` as `id`,
  88. `name` AS `name`,
  89. `table_track` AS `table_track`,
  90. `is_on` AS `is_on`,
  91. `is_undo` AS `is_undo`,
  92. `is_public` AS `is_public`,
  93. `base_url` AS `base_url`,
  94. `trigger_table` AS `trigger_table`,
  95. `trigger_field_id` AS `trigger_field_id`,
  96. `trigger_field_author` AS `trigger_field_author`,
  97. `trigger_field_count` AS `trigger_field_count`,
  98. `class_name` AS `class_name`,
  99. `class_file` AS `class_file`
  100. FROM `sys_objects_favorite`', 'name');
  101. return $GLOBALS[$sKey];
  102. }
  103. public static function onAuthorDelete ($iAuthorId)
  104. {
  105. $aSystems = self::getSystems();
  106. foreach($aSystems as $sSystem => $aSystem)
  107. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  108. return true;
  109. }
  110. /**
  111. * Actions functions
  112. */
  113. public function actionFavorite()
  114. {
  115. return echoJson($this->_doFavorite());
  116. }
  117. public function actionGetFavoritedBy()
  118. {
  119. if (!$this->isEnabled())
  120. return '';
  121. if(!$this->isAllowedFavoriteView(true))
  122. return $this->msgErrAllowedFavoriteView();
  123. return $this->_getFavoritedBy();
  124. }
  125. /**
  126. * Permissions functions
  127. */
  128. public function isAllowedFavorite($isPerformAction = false)
  129. {
  130. if(isAdmin())
  131. return true;
  132. return $this->checkAction('favorite', $isPerformAction);
  133. }
  134. public function msgErrAllowedFavorite()
  135. {
  136. return $this->checkActionErrorMsg('favorite');
  137. }
  138. public function isAllowedFavoriteView($isPerformAction = false)
  139. {
  140. if(!$this->isPublic())
  141. return false;
  142. if(isAdmin())
  143. return true;
  144. return $this->checkAction('favorite_view', $isPerformAction);
  145. }
  146. public function msgErrAllowedFavoriteView()
  147. {
  148. return $this->checkActionErrorMsg('favorite_view');
  149. }
  150. /**
  151. * Auxiliary functions
  152. */
  153. public function isUndo()
  154. {
  155. return (int)$this->_aSystem['is_undo'] == 1;
  156. }
  157. public function isPublic()
  158. {
  159. return (int)$this->_aSystem['is_public'] == 1;
  160. }
  161. /**
  162. * Internal functions
  163. */
  164. protected function _doFavorite()
  165. {
  166. if (!$this->isEnabled())
  167. return array('code' => 1, 'message' => _t('_favorite_err_not_enabled'));
  168. $iObjectId = $this->getId();
  169. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  170. $iAuthorId = $this->_getAuthorId();
  171. $bUndo = $this->isUndo();
  172. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  173. $bPerformUndo = $bPerformed && $bUndo ? true : false;
  174. if(!$bPerformUndo && !$this->isAllowedFavorite())
  175. return array('code' => 2, 'message' => $this->msgErrAllowedFavorite());
  176. if($bPerformed && !$bUndo)
  177. return array('code' => 3, 'message' => _t('_favorite_err_duplicate_favorite'));
  178. if(!$this->_oQuery->{($bPerformUndo ? 'un' : '') . 'doFavorite'}($iObjectId, $iAuthorId))
  179. return array('code' => 4, 'message' => _t('_favorite_err_cannot_perform_action'));
  180. if(!$bPerformUndo)
  181. $this->isAllowedFavorite(true);
  182. $this->_triggerValue($bPerformUndo ? -1 : 1);
  183. bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'favorite', $iObjectId, $iAuthorId, array('favorite_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId));
  184. bx_alert('favorite', ($bPerformUndo ? 'un' : '') . 'do', 0, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
  185. $aFavorite = $this->_oQuery->getFavorite($iObjectId);
  186. return array(
  187. 'eval' => $this->getJsObjectName() . '.onFavorite(oData, oElement)',
  188. 'code' => 0,
  189. 'count' => $aFavorite['count'],
  190. 'countf' => (int)$aFavorite['count'] > 0 ? $this->_getLabelCounter($aFavorite['count']) : '',
  191. 'label_icon' => $this->_getIconDoFavorite(!$bPerformed),
  192. 'label_title' => _t($this->_getTitleDoFavorite(!$bPerformed)),
  193. 'disabled' => !$bPerformed && !$bUndo
  194. );
  195. }
  196. protected function _getIconDoFavorite($bPerformed)
  197. {
  198. return $bPerformed && $this->isUndo() ? 'far fa-bookmark' : 'fas fa-bookmark';
  199. }
  200. protected function _getTitleDoFavorite($bPerformed)
  201. {
  202. return $bPerformed && $this->isUndo() ? '_favorite_do_unfavorite' : '_favorite_do_favorite';
  203. }
  204. }
  205. /** @} */