BxDolContentInfo.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. /**
  10. * Get content info from Content modules
  11. *
  12. * Add record to sys_objects_content_info table to provide unified access to module's content,
  13. * to get content info just create an instance of this class and request content info by id,
  14. * for example:
  15. * @code
  16. * BxDolContentInfo::getObjectInstance('my_system')->getInfo(25); // 25 - is object id
  17. * @endcode
  18. *
  19. * Description of sys_objects_content_info table fields:
  20. * @code
  21. * `name` - system name, it is better to use unique module prefix here, lowercase and all spaces are underscored
  22. * `alert_unit` - unit name of the alert which will be fired
  23. * `alert_action_add` - action name of the alert which is fired after a content was added
  24. * `alert_action_update` - action name of the alert which is fired after a content was updated
  25. * `alert_action_delete` - action name of the alert which is fired after a content was deleted
  26. * `class_name` - your custom class name, if you overrride default class
  27. * `class_file` - your custom class path
  28. * @endcode
  29. *
  30. */
  31. class BxDolContentInfo extends BxDolFactory implements iBxDolFactoryObject
  32. {
  33. protected $_oQuery;
  34. protected $_sSystem;
  35. protected $_aSystem;
  36. protected $_aGrid;
  37. protected function __construct($sSystem)
  38. {
  39. parent::__construct();
  40. if(empty($sSystem))
  41. return;
  42. $aSystems = $this->getSystems();
  43. if(!isset($aSystems[$sSystem]))
  44. return;
  45. $this->_sSystem = $sSystem;
  46. $this->_aSystem = $aSystems[$sSystem];
  47. $this->_aGrid = array();
  48. }
  49. /**
  50. * get content info object instance
  51. * @param $sSystem view object name
  52. * @return null on error, or ready to use class instance
  53. */
  54. public static function getObjectInstance($sSystem)
  55. {
  56. $sClassKey = 'BxDolContentInfo!' . $sSystem;
  57. if(isset($GLOBALS['bxDolClasses'][$sClassKey]))
  58. return $GLOBALS['bxDolClasses'][$sClassKey];
  59. $aSystems = self::getSystems();
  60. if(!isset($aSystems[$sSystem]))
  61. return null;
  62. $sClassName = 'BxDolContentInfo';
  63. if(!empty($aSystems[$sSystem]['class_name'])) {
  64. $sClassName = $aSystems[$sSystem]['class_name'];
  65. if(!empty($aSystems[$sSystem]['class_file']))
  66. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSystem]['class_file']);
  67. }
  68. $GLOBALS['bxDolClasses'][$sClassKey] = new $sClassName($sSystem);
  69. return $GLOBALS['bxDolClasses'][$sClassKey];
  70. }
  71. /**
  72. * get content info object instance (for internal usage).
  73. * @see self::getObjectInstanceByAlertAdd, self::getObjectInstanceByAlertUpdate and self::getObjectInstanceByAlertDelete
  74. * @param $sAlertType alert type (add, update, delete)
  75. * @param $sUnit alert unit
  76. * @param $sAction alert action
  77. * @return null on error, or ready to use class instance
  78. */
  79. public static function getObjectInstanceByAlertCommon($sAlertType, $sUnit, $sAction)
  80. {
  81. $sAlert = $sUnit . '_' . $sAction;
  82. $sClassKey = 'BxDolContentInfo!' . bx_gen_method_name($sAlertType . '_' . $sAlert);
  83. if(isset($GLOBALS['bxDolClasses'][$sClassKey]))
  84. return $GLOBALS['bxDolClasses'][$sClassKey];
  85. $aSystems = self::getSystemsByAlertType($sAlertType);
  86. if(!isset($aSystems[$sAlert]))
  87. return null;
  88. $sClassName = 'BxDolContentInfo';
  89. if(!empty($aSystems[$sAlert]['class_name'])) {
  90. $sClassName = $aSystems[$sAlert]['class_name'];
  91. if(!empty($aSystems[$sAlert]['class_file']))
  92. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sAlert]['class_file']);
  93. }
  94. $GLOBALS['bxDolClasses'][$sClassKey] = new $sClassName($aSystems[$sAlert]['name']);
  95. return $GLOBALS['bxDolClasses'][$sClassKey];
  96. }
  97. public static function getObjectInstanceByAlertAdd($sUnit, $sAction)
  98. {
  99. return self::getObjectInstanceByAlertCommon('add', $sUnit, $sAction);
  100. }
  101. public static function getObjectInstanceByAlertUpdate($sUnit, $sAction)
  102. {
  103. return self::getObjectInstanceByAlertCommon('update', $sUnit, $sAction);
  104. }
  105. public static function getObjectInstanceByAlertDelete($sUnit, $sAction)
  106. {
  107. return self::getObjectInstanceByAlertCommon('delete', $sUnit, $sAction);
  108. }
  109. /**
  110. * get content info object instance (for internal usage).
  111. * @see self::getObjectInstanceByAlertAdd, self::getObjectInstanceByAlertUpdate and self::getObjectInstanceByAlertDelete
  112. * @param $sAlertType alert type (add, update, delete)
  113. * @param $sUnit alert unit
  114. * @param $sAction alert action
  115. * @return null on error, or ready to use class instance
  116. */
  117. public static function getObjectInstanceByGrid($sGrid)
  118. {
  119. $aGrids = self::getGrids();
  120. if(!isset($aGrids[$sGrid]))
  121. return null;
  122. $oObject = self::getObjectInstance($aGrids[$sGrid]['object']);
  123. $oObject->setGrid($aGrids[$sGrid]);
  124. return $oObject;
  125. }
  126. public static function &getSystems()
  127. {
  128. $sKey = 'bx_dol_cache_memory_content_info_systems';
  129. if(!isset($GLOBALS[$sKey]))
  130. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_content_info', 'getAllWithKey', '
  131. SELECT
  132. `id` as `id`,
  133. `name` AS `name`,
  134. `title` AS `title`,
  135. `alert_unit` AS `alert_unit`,
  136. `alert_action_add` AS `alert_action_add`,
  137. `alert_action_update` AS `alert_action_update`,
  138. `alert_action_delete` AS `alert_action_delete`,
  139. `class_name` AS `class_name`,
  140. `class_file` AS `class_file`
  141. FROM `sys_objects_content_info`', 'name');
  142. return $GLOBALS[$sKey];
  143. }
  144. public static function &getGrids()
  145. {
  146. $sKey = 'bx_dol_cache_memory_content_info_grids';
  147. if(!isset($GLOBALS[$sKey]))
  148. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_content_info_grids', 'getAllWithKey', '
  149. SELECT
  150. `id` as `id`,
  151. `object` AS `object`,
  152. `grid_object` AS `grid_object`,
  153. `grid_field_id` AS `grid_field_id`,
  154. `condition` AS `condition`,
  155. `selection` AS `selection`
  156. FROM `sys_content_info_grids`', 'grid_object');
  157. return $GLOBALS[$sKey];
  158. }
  159. public static function &getSystemsByAlertType($sAlertType)
  160. {
  161. $sKey = 'bx_dol_cache_memory_content_info_systems_' . $sAlertType;
  162. if(!isset($GLOBALS[$sKey])) {
  163. $aSystems = BxDolContentInfo::getSystems();
  164. foreach($aSystems as $aSystem)
  165. $GLOBALS[$sKey][$aSystem['alert_unit'] . '_' . $aSystem['alert_action_' . $sAlertType]] = $aSystem;
  166. }
  167. return $GLOBALS[$sKey];
  168. }
  169. public static function formatFields($aContentInfo)
  170. {
  171. foreach ($aContentInfo as $sKey => $mixedValue)
  172. if(is_numeric($mixedValue)) {
  173. if(strpos($mixedValue, '.') !== false)
  174. $aContentInfo[$sKey] = (float)$mixedValue;
  175. else
  176. $aContentInfo[$sKey] = (int)$mixedValue;
  177. }
  178. return $aContentInfo;
  179. }
  180. public function setGrid($aGrid)
  181. {
  182. $this->_aGrid = $aGrid;
  183. }
  184. public function getName()
  185. {
  186. return $this->_sSystem;
  187. }
  188. public function getTitle()
  189. {
  190. return $this->_aSystem['title'];
  191. }
  192. public function getGrid()
  193. {
  194. return $this->_aGrid;
  195. }
  196. public function getContentAuthor ($iContentId)
  197. {
  198. return $this->_call('get_author', $iContentId);
  199. }
  200. public function getContentDateAdded ($iContentId)
  201. {
  202. return $this->_call('get_date_added', $iContentId);
  203. }
  204. public function getContentDateChanged ($iContentId)
  205. {
  206. return $this->_call('get_date_changed', $iContentId);
  207. }
  208. public function getContentTitle ($iContentId)
  209. {
  210. return $this->_call('get_title', $iContentId);
  211. }
  212. public function getContentThumb ($iContentId)
  213. {
  214. return $this->_call('get_thumb', $iContentId);
  215. }
  216. public function getContentLink ($iContentId)
  217. {
  218. return $this->_call('get_link', $iContentId);
  219. }
  220. public function getContentText ($iContentId)
  221. {
  222. return $this->_call('get_text', $iContentId);
  223. }
  224. public function getContentInfo ($iContentId, $bSearchableFieldsOnly = true)
  225. {
  226. return $this->_call('get_info', $iContentId, $bSearchableFieldsOnly);
  227. }
  228. public function getContentSearchResultUnit ($iContentId, $sUnitTemplate = '')
  229. {
  230. return $this->_call('get_search_result_unit', $iContentId, $sUnitTemplate);
  231. }
  232. public function getAll ($aParams = array())
  233. {
  234. return $this->_call('get_all', $aParams);
  235. }
  236. public function getSearchableFieldsExtended ()
  237. {
  238. return $this->_call('get_searchable_fields_extended');
  239. }
  240. public function getSearchResultExtended ($aParams, $iStart = 0, $iPerPage = 0)
  241. {
  242. return $this->_call('get_search_result_extended', $aParams, $iStart, $iPerPage);
  243. }
  244. protected function _call($sMethod)
  245. {
  246. if(!BxDolRequest::serviceExists($this->_sSystem, $sMethod))
  247. return false;
  248. return BxDolService::call($this->_sSystem, $sMethod, array_slice(func_get_args(), 1));
  249. }
  250. }
  251. /** @} */