BxDolFavorite.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 $_sType;
  49. protected $_sBaseUrl;
  50. protected $_aFavorite;
  51. protected $_aElementDefaults;
  52. protected $_aElementDefaultsApi;
  53. protected $_aElementParamsApi; //--- Params from DefaultsApi array to be passed to Api
  54. protected $_sFormObject;
  55. protected $_sFormDisplayAdd;
  56. protected $_sFormDisplayListEdit;
  57. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  58. {
  59. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  60. if(empty($this->_sSystem))
  61. return;
  62. $this->_oQuery = new BxDolFavoriteQuery($this);
  63. $this->_sType = 'favorites';
  64. $this->_sBaseUrl = BxDolPermalinks::getInstance()->permalink($this->_aSystem['base_url']);
  65. if(get_mb_substr($this->_sBaseUrl, 0, 4) != 'http')
  66. $this->_sBaseUrl = BX_DOL_URL_ROOT . $this->_sBaseUrl;
  67. $this->_aFavorite = [];
  68. $this->_sFormObject = 'sys_favorite';
  69. $this->_sFormDisplayAdd = 'sys_favorite_add';
  70. $this->_sFormDisplayListEdit = 'sys_favorite_list_edit';
  71. }
  72. /**
  73. * get favorites object instanse
  74. * @param $sSys favorite object name
  75. * @param $iId associated content id, where favorite is available
  76. * @param $iInit perform initialization
  77. * @return null on error, or ready to use class instance
  78. */
  79. public static function getObjectInstance($sSys, $iId, $iInit = true)
  80. {
  81. $sKey = 'BxDolFavorite!' . $sSys . $iId;
  82. if(isset($GLOBALS['bxDolClasses'][$sKey]))
  83. return $GLOBALS['bxDolClasses'][$sKey];
  84. $aSystems = self::getSystems();
  85. if(!isset($aSystems[$sSys]))
  86. return null;
  87. $sClassName = 'BxTemplFavorite';
  88. if(!empty($aSystems[$sSys]['class_name'])) {
  89. $sClassName = $aSystems[$sSys]['class_name'];
  90. if(!empty($aSystems[$sSys]['class_file']))
  91. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  92. }
  93. $o = new $sClassName($sSys, $iId, $iInit);
  94. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  95. }
  96. public function getConditionsTrack($sMainTable, $sMainField, $iAuthorId = 0, $iListId = 0)
  97. {
  98. $aConditions = parent::getConditionsTrack($sMainTable, $sMainField, $iAuthorId);
  99. if (empty($aConditions) || !isset($this->_aSystem['table_lists']) || $this->_aSystem['table_lists'] == '')
  100. return $aConditions;
  101. $sTableTrack = isset($this->_aSystem['table_track']) ? $this->_aSystem['table_track'] : '';
  102. $aConditions['restriction']['objects_' . $this->_sSystem . '_list'] = array(
  103. 'value' => $iListId,
  104. 'field' => 'list_id',
  105. 'operator' => '=',
  106. 'table' => $sTableTrack,
  107. );
  108. return $aConditions;
  109. }
  110. public static function &getSystems()
  111. {
  112. $sKey = 'bx_dol_cache_memory_favorite_systems';
  113. if(!isset($GLOBALS[$sKey]))
  114. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_favorite', 'getAllWithKey', '
  115. SELECT
  116. `id` as `id`,
  117. `name` AS `name`,
  118. `table_track` AS `table_track`,
  119. `table_lists` AS `table_lists`,
  120. `pruning` AS `pruning`,
  121. `is_on` AS `is_on`,
  122. `is_undo` AS `is_undo`,
  123. `is_public` AS `is_public`,
  124. `base_url` AS `base_url`,
  125. `trigger_table` AS `trigger_table`,
  126. `trigger_field_id` AS `trigger_field_id`,
  127. `trigger_field_author` AS `trigger_field_author`,
  128. `trigger_field_count` AS `trigger_field_count`,
  129. `class_name` AS `class_name`,
  130. `class_file` AS `class_file`
  131. FROM `sys_objects_favorite`', 'name');
  132. return $GLOBALS[$sKey];
  133. }
  134. public static function onAuthorDelete ($iAuthorId)
  135. {
  136. $aSystems = self::getSystems();
  137. foreach($aSystems as $sSystem => $aSystem)
  138. self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
  139. return true;
  140. }
  141. /**
  142. * Actions functions
  143. */
  144. public function actionFavorite()
  145. {
  146. return echoJson($this->favorite());
  147. }
  148. public function actionEditList()
  149. {
  150. $iListId = null;
  151. if(!bx_get('list_id'))
  152. return false;
  153. $iListId = (int) bx_get('list_id');
  154. $aList = $this->_oQuery->getList(array('type' => 'id', 'list_id' => $iListId));
  155. if ($this->isAllowedEditList($aList['author_id'])){
  156. return echoJson($this->_getEditList($aList));
  157. }
  158. return false;
  159. }
  160. public function actionDeleteList()
  161. {
  162. $iListId = null;
  163. if(!bx_get('list_id'))
  164. return false;
  165. $iListId = (int) bx_get('list_id');
  166. $aList = $this->_oQuery->getList(array('type' => 'id', 'list_id' => $iListId));
  167. $oModule = BxDolModule::getInstance($this->_aSystem["name"]);
  168. $CNF = $oModule->_oConfig->CNF;
  169. if ($this->isAllowedEditList($aList['author_id'])){
  170. $this->_oQuery->deleteList($iListId);
  171. return echoJson(array('redirect' => BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . $aList['author_id'])));
  172. }
  173. return false;
  174. }
  175. public function actionGetFavoritedBy()
  176. {
  177. if (!$this->isEnabled())
  178. return '';
  179. if(!$this->isAllowedFavoriteView(true))
  180. return $this->msgErrAllowedFavoriteView();
  181. return $this->_getFavoritedBy();
  182. }
  183. /**
  184. * Permissions functions
  185. */
  186. public function isAllowedFavorite($isPerformAction = false)
  187. {
  188. if(isAdmin())
  189. return true;
  190. return $this->checkAction('favorite', $isPerformAction);
  191. }
  192. public function isAllowedEditList($iAuthorId, $isPerformAction = false)
  193. {
  194. if(isAdmin())
  195. return true;
  196. if ($iAuthorId == bx_get_logged_profile_id())
  197. return true;
  198. return false;
  199. }
  200. public function msgErrAllowedFavorite()
  201. {
  202. return $this->checkActionErrorMsg('favorite');
  203. }
  204. public function isAllowedFavoriteView($isPerformAction = false)
  205. {
  206. if(!$this->isPublic())
  207. return false;
  208. if(isAdmin())
  209. return true;
  210. return $this->checkAction('favorite_view', $isPerformAction);
  211. }
  212. public function msgErrAllowedFavoriteView()
  213. {
  214. return $this->checkActionErrorMsg('favorite_view');
  215. }
  216. /**
  217. * Auxiliary functions
  218. */
  219. public function isUndo()
  220. {
  221. return (int)$this->_aSystem['is_undo'] == 1;
  222. }
  223. public function isPublic()
  224. {
  225. return (int)$this->_aSystem['is_public'] == 1;
  226. }
  227. /**
  228. * Internal functions
  229. */
  230. public function doFavorite()
  231. {
  232. if(!$this->isEnabled())
  233. return ['code' => 1, 'message' => _t('_feature_err_not_enabled')];
  234. $iObjectId = $this->getId();
  235. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  236. $iAuthorId = $this->_getAuthorId();
  237. $bUndo = $this->isUndo();
  238. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  239. $bPerformUndo = $bPerformed && $bUndo ? true : false;
  240. if(!$bPerformUndo && !$this->isAllowedFavorite())
  241. return ['code' => 2, 'message' => $this->msgErrAllowedFavorite()];
  242. if($bPerformed && !$bUndo)
  243. return ['code' => 3, 'message' => _t('_favorite_err_duplicate_favorite')];
  244. if(!$this->_oQuery->{($bPerformUndo ? 'un' : '') . 'doFavorite'}($iObjectId, $iAuthorId))
  245. return ['code' => 4, 'message' => _t('_favorite_err_cannot_perform_action')];
  246. if(!$bPerformUndo)
  247. $this->isAllowedFavorite(true);
  248. $this->_triggerValue($bPerformUndo ? -1 : 1);
  249. bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'favorite', $iObjectId, $iAuthorId, ['favorite_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId]);
  250. /**
  251. * @hooks
  252. * @hookdef hook-report-undo 'favorite', 'favorite' - hook on add new object to favorites lists or remove object from favorites lists
  253. * - $unit_name - equals `favorite`
  254. * - $action - can be do/undo
  255. * - $object_id - not used
  256. * - $sender_id - profile_id for favorite's author
  257. * - $extra_params - array of additional params with the following array keys:
  258. * - `object_system` - [string] system name, ex: bx_posts
  259. * - `object_id` - [int] reported object id
  260. * - `object_author_id` - [int] author's profile_id for reported object_id
  261. * @hook @ref hook-favorite-undo
  262. */
  263. bx_alert('favorite', ($bPerformUndo ? 'un' : '') . 'do', 0, $iAuthorId, ['object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId]);
  264. $aFavorite = $this->_oQuery->getFavorite($iObjectId);
  265. $aResult = [
  266. 'eval' => $this->getJsObjectName() . '.onFavorite(oData, oElement)',
  267. 'code' => 0,
  268. 'count' => $aFavorite['count'],
  269. 'countf' => (int)$aFavorite['count'] > 0 ? $this->_getCounterLabel($aFavorite['count']) : '',
  270. 'label_icon' => $this->_getIconDoFavorite(!$bPerformed),
  271. 'label_title' => _t($this->_getTitleDoFavorite(!$bPerformed)),
  272. 'favorited' => !$bPerformed,
  273. 'disabled' => !$bPerformed && !$bUndo
  274. ];
  275. $aResult['api'] = [
  276. 'is_favorited' => $aResult['favorited'],
  277. 'is_disabled' => $aResult['disabled'],
  278. 'icon' => $aResult['label_icon'],
  279. 'title' => $aResult['label_title'],
  280. 'counter' => $aFavorite
  281. ];
  282. return $aResult;
  283. }
  284. protected function _getFavorite($iObjectId = 0, $bForceGet = false)
  285. {
  286. if(!empty($this->_aFavorite) && !$bForceGet)
  287. return $this->_aFavorite;
  288. if(empty($iObjectId))
  289. $iObjectId = $this->getId();
  290. $this->_aFavorite = $this->_oQuery->getFavorite($iObjectId);
  291. return $this->_aFavorite;
  292. }
  293. protected function _getIconDoFavorite($bPerformed)
  294. {
  295. return $bPerformed && $this->isUndo() ? 'far fa-bookmark' : 'fas fa-bookmark';
  296. }
  297. protected function _getTitleDoFavorite($bPerformed)
  298. {
  299. return $bPerformed && $this->isUndo() ? '_favorite_do_unfavorite' : '_favorite_do_favorite';
  300. }
  301. protected function _getFormObject($sDisplayName)
  302. {
  303. return BxDolForm::getObjectInstance($this->_sFormObject, $sDisplayName);
  304. }
  305. }
  306. /** @} */