BxDolFeature.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. if(isset($GLOBALS['bxDolClasses']['BxDolFeature!' . $sSys . $iId]))
  58. return $GLOBALS['bxDolClasses']['BxDolFeature!' . $sSys . $iId];
  59. $aSystems = self::getSystems();
  60. if(!isset($aSystems[$sSys]))
  61. return null;
  62. $sClassName = 'BxTemplFeature';
  63. if(!empty($aSystems[$sSys]['class_name'])) {
  64. $sClassName = $aSystems[$sSys]['class_name'];
  65. if(!empty($aSystems[$sSys]['class_file']))
  66. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
  67. }
  68. $o = new $sClassName($sSys, $iId, $iInit);
  69. return ($GLOBALS['bxDolClasses']['BxDolFeature!' . $sSys . $iId] = $o);
  70. }
  71. public static function &getSystems()
  72. {
  73. $sKey = 'bx_dol_cache_memory_feature_systems';
  74. if(!isset($GLOBALS[$sKey]))
  75. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_feature', 'getAllWithKey', '
  76. SELECT
  77. `id` as `id`,
  78. `name` AS `name`,
  79. `is_on` AS `is_on`,
  80. `is_undo` AS `is_undo`,
  81. `base_url` AS `base_url`,
  82. `trigger_table` AS `trigger_table`,
  83. `trigger_field_id` AS `trigger_field_id`,
  84. `trigger_field_author` AS `trigger_field_author`,
  85. `trigger_field_flag` AS `trigger_field_flag`,
  86. `class_name` AS `class_name`,
  87. `class_file` AS `class_file`
  88. FROM `sys_objects_feature`', 'name');
  89. return $GLOBALS[$sKey];
  90. }
  91. /**
  92. * Actions functions
  93. */
  94. public function actionFeature()
  95. {
  96. return echoJson($this->_doFeature());
  97. }
  98. public function actionGetFeatureBy()
  99. {
  100. return '';
  101. }
  102. /**
  103. * Permissions functions
  104. */
  105. public function isAllowedFeature($isPerformAction = false)
  106. {
  107. if(isAdmin())
  108. return true;
  109. return $this->checkAction('feature', $isPerformAction);
  110. }
  111. public function msgErrAllowedFeature()
  112. {
  113. return $this->checkActionErrorMsg('feature');
  114. }
  115. /**
  116. * Auxiliary functions
  117. */
  118. public function isUndo()
  119. {
  120. return (int)$this->_aSystem['is_undo'] == 1;
  121. }
  122. /**
  123. * Internal functions
  124. */
  125. protected function _doFeature()
  126. {
  127. if (!$this->isEnabled())
  128. return array('code' => 1, 'message' => _t('_feature_err_not_enabled'));
  129. $iObjectId = $this->getId();
  130. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  131. $iAuthorId = $this->_getAuthorId();
  132. $bUndo = $this->isUndo();
  133. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  134. $bPerformUndo = $bPerformed && $bUndo ? true : false;
  135. if(!$bPerformUndo && !$this->isAllowedFeature())
  136. return array('code' => 2, 'message' => $this->msgErrAllowedFeature());
  137. if($bPerformed && !$bUndo)
  138. return array('code' => 3, 'message' => _t('_feature_err_duplicate_feature'));
  139. if(!$this->_triggerValue($bPerformUndo ? 0 : time()))
  140. return array('code' => 4, 'message' => _t('_feature_err_cannot_perform_action'));
  141. if(!$bPerformUndo)
  142. $this->isAllowedFeature(true);
  143. bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'feature', $iObjectId, $iAuthorId, array('object_author_id' => $iObjectAuthorId));
  144. bx_alert('feature', ($bPerformUndo ? 'un' : '') . 'do', 0, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
  145. return array(
  146. 'eval' => $this->getJsObjectName() . '.onFeature(oData, oElement)',
  147. 'code' => 0,
  148. 'label_icon' => $this->_getIconDoFeature(!$bPerformed),
  149. 'label_title' => _t($this->_getTitleDoFeature(!$bPerformed)),
  150. 'disabled' => !$bPerformed && !$bUndo
  151. );
  152. }
  153. protected function _getIconDoFeature($bPerformed)
  154. {
  155. return $bPerformed && $this->isUndo() ? 'star-o' : 'star';
  156. }
  157. protected function _getTitleDoFeature($bPerformed)
  158. {
  159. return $bPerformed && $this->isUndo() ? '_feature_do_unfeature' : '_feature_do_feature';
  160. }
  161. }
  162. /** @} */