BxDolFavorite.php 8.8 KB

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