BxDolFeature.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 $_sBaseUrl;
  38. protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  39. {
  40. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  41. if(empty($this->_sSystem))
  42. return;
  43. $this->_oQuery = new BxDolFeatureQuery($this);
  44. $this->_sBaseUrl = BxDolPermalinks::getInstance()->permalink($this->_aSystem['base_url']);
  45. if(get_mb_substr($this->_sBaseUrl, 0, 4) != 'http')
  46. $this->_sBaseUrl = BX_DOL_URL_ROOT . $this->_sBaseUrl;
  47. }
  48. /**
  49. * get feature object instanse
  50. * @param $sSys feature object name
  51. * @param $iId associated content id, where feature is available
  52. * @param $iInit perform initialization
  53. * @return null on error, or ready to use class instance
  54. */
  55. public static function getObjectInstance($sSys, $iId, $iInit = true)
  56. {
  57. $sKey = 'BxDolFeature!' . $sSys . $iId;
  58. if(isset($GLOBALS['bxDolClasses'][$sKey]))
  59. return $GLOBALS['bxDolClasses'][$sKey];
  60. $aSystems = self::getSystems();
  61. if(!isset($aSystems[$sSys]))
  62. return null;
  63. $sClassName = 'BxTemplFeature';
  64. if(!empty($aSystems[$sSys]['class_name'])) {
  65. $sClassName = $aSystems[$sSys]['class_name'];
  66. if(!empty($aSystems[$sSys]['class_file']))
  67. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  68. }
  69. $o = new $sClassName($sSys, $iId, $iInit);
  70. return ($GLOBALS['bxDolClasses'][$sKey] = $o);
  71. }
  72. public static function &getSystems()
  73. {
  74. $sKey = 'bx_dol_cache_memory_feature_systems';
  75. if(!isset($GLOBALS[$sKey]))
  76. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_feature', 'getAllWithKey', '
  77. SELECT
  78. `id` as `id`,
  79. `name` AS `name`,
  80. `module` AS `module`,
  81. `is_on` AS `is_on`,
  82. `is_undo` AS `is_undo`,
  83. `base_url` AS `base_url`,
  84. `trigger_table` AS `trigger_table`,
  85. `trigger_field_id` AS `trigger_field_id`,
  86. `trigger_field_author` AS `trigger_field_author`,
  87. `trigger_field_flag` AS `trigger_field_flag`,
  88. `class_name` AS `class_name`,
  89. `class_file` AS `class_file`
  90. FROM `sys_objects_feature`', 'name');
  91. return $GLOBALS[$sKey];
  92. }
  93. /**
  94. * Actions functions
  95. */
  96. public function actionFeature()
  97. {
  98. return echoJson($this->_doFeature());
  99. }
  100. public function actionGetFeatureBy()
  101. {
  102. return '';
  103. }
  104. /**
  105. * Permissions functions
  106. */
  107. public function isAllowedFeature($isPerformAction = false)
  108. {
  109. if(isAdmin())
  110. return true;
  111. return $this->checkAction('feature', $isPerformAction);
  112. }
  113. public function msgErrAllowedFeature()
  114. {
  115. return $this->checkActionErrorMsg('feature');
  116. }
  117. /**
  118. * Auxiliary functions
  119. */
  120. public function isUndo()
  121. {
  122. return (int)$this->_aSystem['is_undo'] == 1;
  123. }
  124. /**
  125. * Internal functions
  126. */
  127. protected function _doFeature()
  128. {
  129. if (!$this->isEnabled())
  130. return array('code' => 1, 'message' => _t('_feature_err_not_enabled'));
  131. $iObjectId = $this->getId();
  132. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  133. $iAuthorId = $this->_getAuthorId();
  134. $bUndo = $this->isUndo();
  135. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  136. $bPerformUndo = $bPerformed && $bUndo ? true : false;
  137. if(!$bPerformUndo && !$this->isAllowedFeature())
  138. return array('code' => 2, 'message' => $this->msgErrAllowedFeature());
  139. if($bPerformed && !$bUndo)
  140. return array('code' => 3, 'message' => _t('_feature_err_duplicate_feature'));
  141. if(!$this->_triggerValue($bPerformUndo ? 0 : time()))
  142. return array('code' => 4, 'message' => _t('_feature_err_cannot_perform_action'));
  143. if(!$bPerformUndo)
  144. $this->isAllowedFeature(true);
  145. bx_audit(
  146. $this->getId(),
  147. $this->_aSystem['name'],
  148. '_sys_audit_action_' . ($bPerformUndo ? 'un' : '') . 'feature',
  149. $this->_prepareAuditParams()
  150. );
  151. bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'feature', $iObjectId, $iAuthorId, array('object_author_id' => $iObjectAuthorId));
  152. bx_alert('feature', ($bPerformUndo ? 'un' : '') . 'do', 0, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
  153. return array(
  154. 'eval' => $this->getJsObjectName() . '.onFeature(oData, oElement)',
  155. 'code' => 0,
  156. 'label_icon' => $this->_getIconDoFeature(!$bPerformed),
  157. 'label_title' => _t($this->_getTitleDoFeature(!$bPerformed)),
  158. 'disabled' => !$bPerformed && !$bUndo
  159. );
  160. }
  161. protected function _getIconDoFeature($bPerformed)
  162. {
  163. return $bPerformed && $this->isUndo() ? 'far star' : 'star';
  164. }
  165. protected function _getTitleDoFeature($bPerformed)
  166. {
  167. return $bPerformed && $this->isUndo() ? '_feature_do_unfeature' : '_feature_do_feature';
  168. }
  169. private function _prepareAuditParams()
  170. {
  171. $sModule = $this->_aSystem['module'];
  172. $oModule = BxDolModule::getInstance($sModule);
  173. if(!$oModule)
  174. return;
  175. $CNF = $oModule->_oConfig->CNF;
  176. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $this->getId()))) : array();
  177. $AuditParams = array(
  178. 'content_title' => isset($CNF['FIELD_TITLE']) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  179. 'content_info_object' => isset($CNF['OBJECT_CONTENT_INFO']) ? $CNF['OBJECT_CONTENT_INFO'] : '',
  180. );
  181. return $AuditParams;
  182. }
  183. }
  184. /** @} */