BxDolFavorite.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. * `table_lists` - table with lists
  27. * `is_on` - is the system activated
  28. * `trigger_table` - table where you need to update favorites field
  29. * `trigger_field_id` - table field id to unique determine object
  30. * `trigger_field_count` - table field where total favorites number is stored
  31. * `class_name` - your custom class name, if you overrride default class
  32. * `class_file` - your custom class path
  33. * @endcode
  34. *
  35. * Structure of the track table is the following:
  36. * @code
  37. * CREATE TABLE `my_favorites_track` (
  38. * `object_id` int(11) NOT NULL default '0', -- this field type must be exact as your object id type
  39. * `author_id` int(11) NOT NULL default '0', -- favoring user profile id
  40. * `date` int(11) NOT NULL default '0', -- timestamp of last recorded view
  41. * KEY `id` (`object_id`,`author_id`)
  42. * );
  43. * @endcode
  44. *
  45. */
  46. class BxDolFavorite extends BxDolObject
  47. {
  48. protected $_sBaseUrl;
  49. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  50. {
  51. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  52. if(empty($this->_sSystem))
  53. return;
  54. $this->_oQuery = new BxDolFavoriteQuery($this);
  55. $this->_sBaseUrl = BxDolPermalinks::getInstance()->permalink($this->_aSystem['base_url']);
  56. if(get_mb_substr($this->_sBaseUrl, 0, 4) != 'http')
  57. $this->_sBaseUrl = BX_DOL_URL_ROOT . $this->_sBaseUrl;
  58. $this->_sFormObject = 'sys_favorite';
  59. $this->_sFormDisplayAdd = 'sys_favorite_add';
  60. $this->_sFormDisplayListEdit = 'sys_favorite_list_edit';
  61. }
  62. /**
  63. * get favorites object instanse
  64. * @param $sSys favorite object name
  65. * @param $iId associated content id, where favorite is available
  66. * @param $iInit perform initialization
  67. * @return null on error, or ready to use class instance
  68. */
  69. public static function getObjectInstance($sSys, $iId, $iInit = true)
  70. {
  71. $sKey = 'BxDolFavorite!' . $sSys . $iId;
  72. if(isset($GLOBALS['bxDolClasses'][$sKey]))
  73. return $GLOBALS['bxDolClasses'][$sKey];
  74. $aSystems = self::getSystems();
  75. if(!isset($aSystems[$sSys]))
  76. return null;
  77. $sClassName = 'BxTemplFavorite';
  78. if(!empty($aSystems[$sSys]['class_name'])) {
  79. $sClassName = $aSystems[$sSys]['class_name'];
  80. if(!empty($aSystems[$sSys]['class_file']))
  81. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  82. }
  83. $o = new $sClassName($sSys, $iId, $iInit);
  84. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  85. }
  86. public function getConditionsTrack($sMainTable, $sMainField, $iAuthorId = 0, $iListId = 0)
  87. {
  88. $aConditions = parent::getConditionsTrack($sMainTable, $sMainField, $iAuthorId);
  89. if (empty($aConditions) || !isset($this->_aSystem['table_lists']) || $this->_aSystem['table_lists'] == '')
  90. return $aConditions;
  91. $sTableTrack = isset($this->_aSystem['table_track']) ? $this->_aSystem['table_track'] : '';
  92. $aConditions['restriction']['objects_' . $this->_sSystem . '_list'] = array(
  93. 'value' => $iListId,
  94. 'field' => 'list_id',
  95. 'operator' => '=',
  96. 'table' => $sTableTrack,
  97. );
  98. return $aConditions;
  99. }
  100. public static function &getSystems()
  101. {
  102. $sKey = 'bx_dol_cache_memory_favorite_systems';
  103. if(!isset($GLOBALS[$sKey]))
  104. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_favorite', 'getAllWithKey', '
  105. SELECT
  106. `id` as `id`,
  107. `name` AS `name`,
  108. `table_track` AS `table_track`,
  109. `table_lists` AS `table_lists`,
  110. `is_on` AS `is_on`,
  111. `is_undo` AS `is_undo`,
  112. `is_public` AS `is_public`,
  113. `base_url` AS `base_url`,
  114. `trigger_table` AS `trigger_table`,
  115. `trigger_field_id` AS `trigger_field_id`,
  116. `trigger_field_author` AS `trigger_field_author`,
  117. `trigger_field_count` AS `trigger_field_count`,
  118. `class_name` AS `class_name`,
  119. `class_file` AS `class_file`
  120. FROM `sys_objects_favorite`', 'name');
  121. return $GLOBALS[$sKey];
  122. }
  123. public static function onAuthorDelete ($iAuthorId)
  124. {
  125. $aSystems = self::getSystems();
  126. foreach($aSystems as $sSystem => $aSystem)
  127. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  128. return true;
  129. }
  130. /**
  131. * Actions functions
  132. */
  133. public function actionFavorite()
  134. {
  135. return echoJson($this->_getFavorite());
  136. }
  137. public function actionEditList()
  138. {
  139. $iListId = null;
  140. if(!bx_get('list_id'))
  141. return false;
  142. $iListId = (int) bx_get('list_id');
  143. $aList = $this->_oQuery->getList(array('type' => 'id', 'list_id' => $iListId));
  144. if ($this->isAllowedEditList($aList['author_id'])){
  145. return echoJson($this->_getEditList($aList));
  146. }
  147. return false;
  148. }
  149. public function actionDeleteList()
  150. {
  151. $iListId = null;
  152. if(!bx_get('list_id'))
  153. return false;
  154. $iListId = (int) bx_get('list_id');
  155. $aList = $this->_oQuery->getList(array('type' => 'id', 'list_id' => $iListId));
  156. $oModule = BxDolModule::getInstance($this->_aSystem["name"]);
  157. $CNF = $oModule->_oConfig->CNF;
  158. if ($this->isAllowedEditList($aList['author_id'])){
  159. $this->_oQuery->deleteList($iListId);
  160. return echoJson(array('redirect' => BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . $aList['author_id'])));
  161. }
  162. return false;
  163. }
  164. public function actionGetFavoritedBy()
  165. {
  166. if (!$this->isEnabled())
  167. return '';
  168. if(!$this->isAllowedFavoriteView(true))
  169. return $this->msgErrAllowedFavoriteView();
  170. return $this->_getFavoritedBy();
  171. }
  172. /**
  173. * Permissions functions
  174. */
  175. public function isAllowedFavorite($isPerformAction = false)
  176. {
  177. if(isAdmin())
  178. return true;
  179. return $this->checkAction('favorite', $isPerformAction);
  180. }
  181. public function isAllowedEditList($iAuthorId, $isPerformAction = false)
  182. {
  183. if(isAdmin())
  184. return true;
  185. if ($iAuthorId == bx_get_logged_profile_id())
  186. return true;
  187. return false;
  188. }
  189. public function msgErrAllowedFavorite()
  190. {
  191. return $this->checkActionErrorMsg('favorite');
  192. }
  193. public function isAllowedFavoriteView($isPerformAction = false)
  194. {
  195. if(!$this->isPublic())
  196. return false;
  197. if(isAdmin())
  198. return true;
  199. return $this->checkAction('favorite_view', $isPerformAction);
  200. }
  201. public function msgErrAllowedFavoriteView()
  202. {
  203. return $this->checkActionErrorMsg('favorite_view');
  204. }
  205. /**
  206. * Auxiliary functions
  207. */
  208. public function isUndo()
  209. {
  210. return (int)$this->_aSystem['is_undo'] == 1;
  211. }
  212. public function isPublic()
  213. {
  214. return (int)$this->_aSystem['is_public'] == 1;
  215. }
  216. /**
  217. * Internal functions
  218. */
  219. protected function _getIconDoFavorite($bPerformed)
  220. {
  221. return $bPerformed && $this->isUndo() ? 'far fa-bookmark' : 'fas fa-bookmark';
  222. }
  223. protected function _getTitleDoFavorite($bPerformed)
  224. {
  225. return $bPerformed && $this->isUndo() ? '_favorite_do_unfavorite' : '_favorite_do_favorite';
  226. }
  227. protected function _getFormObject($sDisplayName)
  228. {
  229. return BxDolForm::getObjectInstance($this->_sFormObject, $sDisplayName);
  230. }
  231. }
  232. /** @} */