BxDolFeature.php 6.3 KB

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