BxDolFeature.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_FEATURED_USAGE_BLOCK', 'block');
  10. define('BX_DOL_FEATURED_USAGE_INLINE', 'inline');
  11. define('BX_DOL_FEATURED_USAGE_DEFAULT', BX_DOL_FEATURED_USAGE_BLOCK);
  12. /**
  13. * Track any object feature automatically
  14. *
  15. * Add record to sys_object_feature table to track object feature,
  16. * to record feature just create this class instance with your object id,
  17. * for example:
  18. * @code
  19. * BxDolFeature::getObjectInstance('my_system', 25); // 25 - is object id
  20. * @endcode
  21. *
  22. * Description of sys_object_feature 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 feature
  26. * `is_on` - is the system activated
  27. * `trigger_table` - table where you need to update feature field
  28. * `trigger_field_id` - table field id to unique determine object
  29. * `trigger_field_count` - table field where the featuring date 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. */
  35. class BxDolFeature extends BxDolObject
  36. {
  37. protected $_sType;
  38. protected $_sBaseUrl;
  39. protected $_aElementDefaults;
  40. protected $_aElementDefaultsApi;
  41. protected $_aElementParamsApi; //--- Params from DefaultsApi array to be passed to Api
  42. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  43. {
  44. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  45. if(empty($this->_sSystem))
  46. return;
  47. $this->_oQuery = new BxDolFeatureQuery($this);
  48. $this->_sType = 'features';
  49. $this->_sBaseUrl = BxDolPermalinks::getInstance()->permalink($this->_aSystem['base_url']);
  50. if(get_mb_substr($this->_sBaseUrl, 0, 4) != 'http')
  51. $this->_sBaseUrl = BX_DOL_URL_ROOT . $this->_sBaseUrl;
  52. }
  53. /**
  54. * get feature object instanse
  55. * @param $sSys feature object name
  56. * @param $iId associated content id, where feature is available
  57. * @param $iInit perform initialization
  58. * @return null on error, or ready to use class instance
  59. */
  60. public static function getObjectInstance($sSys, $iId, $iInit = true)
  61. {
  62. $sKey = 'BxDolFeature!' . $sSys . $iId;
  63. if(isset($GLOBALS['bxDolClasses'][$sKey]))
  64. return $GLOBALS['bxDolClasses'][$sKey];
  65. $aSystems = self::getSystems();
  66. if(!isset($aSystems[$sSys]))
  67. return null;
  68. $sClassName = 'BxTemplFeature';
  69. if(!empty($aSystems[$sSys]['class_name'])) {
  70. $sClassName = $aSystems[$sSys]['class_name'];
  71. if(!empty($aSystems[$sSys]['class_file']))
  72. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  73. }
  74. $o = new $sClassName($sSys, $iId, $iInit);
  75. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  76. }
  77. public static function &getSystems()
  78. {
  79. $sKey = 'bx_dol_cache_memory_feature_systems';
  80. if(!isset($GLOBALS[$sKey]))
  81. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_feature', 'getAllWithKey', '
  82. SELECT
  83. `id` as `id`,
  84. `name` AS `name`,
  85. `module` AS `module`,
  86. `is_on` AS `is_on`,
  87. `is_undo` AS `is_undo`,
  88. `base_url` AS `base_url`,
  89. `trigger_table` AS `trigger_table`,
  90. `trigger_field_id` AS `trigger_field_id`,
  91. `trigger_field_author` AS `trigger_field_author`,
  92. `trigger_field_flag` AS `trigger_field_flag`,
  93. `class_name` AS `class_name`,
  94. `class_file` AS `class_file`
  95. FROM `sys_objects_feature`', 'name');
  96. return $GLOBALS[$sKey];
  97. }
  98. /**
  99. * Actions functions
  100. */
  101. public function actionFeature()
  102. {
  103. return echoJson($this->doFeature());
  104. }
  105. public function actionGetFeatureBy()
  106. {
  107. return '';
  108. }
  109. /**
  110. * Permissions functions
  111. */
  112. public function isAllowedFeature($isPerformAction = false)
  113. {
  114. if(isAdmin())
  115. return true;
  116. return $this->checkAction('feature', $isPerformAction);
  117. }
  118. public function msgErrAllowedFeature()
  119. {
  120. return $this->checkActionErrorMsg('feature');
  121. }
  122. /**
  123. * Auxiliary functions
  124. */
  125. public function isUndo()
  126. {
  127. return (int)$this->_aSystem['is_undo'] == 1;
  128. }
  129. /**
  130. * Internal functions
  131. */
  132. public function doFeature()
  133. {
  134. if(!$this->isEnabled())
  135. return array('code' => 1, 'message' => _t('_feature_err_not_enabled'));
  136. $iObjectId = $this->getId();
  137. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  138. $iAuthorId = $this->_getAuthorId();
  139. $bUndo = $this->isUndo();
  140. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  141. $bPerformUndo = $bPerformed && $bUndo ? true : false;
  142. if(!$bPerformUndo && !$this->isAllowedFeature())
  143. return array('code' => 2, 'message' => $this->msgErrAllowedFeature());
  144. if($bPerformed && !$bUndo)
  145. return array('code' => 3, 'message' => _t('_feature_err_duplicate_feature'));
  146. if(!$this->_triggerValue($bPerformUndo ? 0 : time()))
  147. return array('code' => 4, 'message' => _t('_feature_err_cannot_perform_action'));
  148. if(!$bPerformUndo)
  149. $this->isAllowedFeature(true);
  150. bx_audit(
  151. $this->getId(),
  152. $this->_aSystem['name'],
  153. '_sys_audit_action_' . ($bPerformUndo ? 'un' : '') . 'feature',
  154. $this->_prepareAuditParams()
  155. );
  156. bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'feature', $iObjectId, $iAuthorId, array('object_author_id' => $iObjectAuthorId));
  157. /**
  158. * @hooks
  159. * @hookdef hook-feature-undo 'feature', 'undo' - hook on cancel feature
  160. * - $unit_name - equals `feature`
  161. * - $action - equals `undo`
  162. * - $object_id - report id
  163. * - $sender_id - profile_id for feature's author
  164. * - $extra_params - array of additional params with the following array keys:
  165. * - `object_system` - [string] system name, ex: bx_posts
  166. * - `object_id` - [int] feature object id
  167. * - `object_author_id` - [int] author's profile_id for feature object_id
  168. * @hook @ref hook-feature-undo
  169. */
  170. /**
  171. * @hooks
  172. * @hookdef hook-feature-do 'feature', 'do' - hook on feature
  173. * - $unit_name - equals `feature`
  174. * - $action - equals `do`
  175. * - $object_id - report id
  176. * - $sender_id - profile_id for feature's author
  177. * - $extra_params - array of additional params with the following array keys:
  178. * - `object_system` - [string] system name, ex: bx_posts
  179. * - `object_id` - [int] feature object id
  180. * - `object_author_id` - [int] author's profile_id for feature object_id
  181. * @hook @ref hook-feature-do
  182. */
  183. bx_alert('feature', ($bPerformUndo ? 'un' : '') . 'do', 0, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
  184. $aResult = [
  185. 'eval' => $this->getJsObjectName() . '.onFeature(oData, oElement)',
  186. 'code' => 0,
  187. 'label_icon' => $this->_getIconDoFeature(!$bPerformed),
  188. 'label_title' => _t($this->_getTitleDoFeature(!$bPerformed)),
  189. 'featured' => !$bPerformed,
  190. 'disabled' => !$bPerformed && !$bUndo
  191. ];
  192. $aResult['api'] = [
  193. 'is_featured' => $aResult['featured'],
  194. 'is_disabled' => $aResult['disabled'],
  195. 'icon' => $aResult['label_icon'],
  196. 'title' => $aResult['label_title'],
  197. ];
  198. return $aResult;
  199. }
  200. protected function _getIconDoFeature($bPerformed)
  201. {
  202. return $bPerformed && $this->isUndo() ? 'far star' : 'star';
  203. }
  204. protected function _getTitleDoFeature($bPerformed)
  205. {
  206. return $bPerformed && $this->isUndo() ? '_feature_do_unfeature' : '_feature_do_feature';
  207. }
  208. private function _prepareAuditParams()
  209. {
  210. $sModule = $this->_aSystem['module'];
  211. $oModule = BxDolModule::getInstance($sModule);
  212. if(!$oModule)
  213. return;
  214. $CNF = $oModule->_oConfig->CNF;
  215. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $this->getId()))) : array();
  216. $AuditParams = array(
  217. 'content_title' => isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']]) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  218. 'content_info_object' => isset($CNF['OBJECT_CONTENT_INFO']) ? $CNF['OBJECT_CONTENT_INFO'] : '',
  219. );
  220. return $AuditParams;
  221. }
  222. }
  223. /** @} */