BxDolContentInfo.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 $_bIgnoreCacheInServiceCalls = false;
  38. protected function __construct($sSystem)
  39. {
  40. parent::__construct();
  41. if(empty($sSystem))
  42. return;
  43. $aSystems = $this->getSystems();
  44. if(!isset($aSystems[$sSystem]))
  45. return;
  46. $this->_sSystem = $sSystem;
  47. $this->_aSystem = $aSystems[$sSystem];
  48. $this->_aGrid = array();
  49. }
  50. /**
  51. * get content info object instance
  52. * @param $sSystem view object name
  53. * @return null on error, or ready to use class instance
  54. */
  55. public static function getObjectInstance($sSystem)
  56. {
  57. $sClassKey = 'BxDolContentInfo!' . $sSystem;
  58. if(isset($GLOBALS['bxDolClasses'][$sClassKey]))
  59. return $GLOBALS['bxDolClasses'][$sClassKey];
  60. $aSystems = self::getSystems();
  61. if(!isset($aSystems[$sSystem]))
  62. return null;
  63. $sClassName = 'BxDolContentInfo';
  64. if(!empty($aSystems[$sSystem]['class_name'])) {
  65. $sClassName = $aSystems[$sSystem]['class_name'];
  66. if(!empty($aSystems[$sSystem]['class_file']))
  67. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSystem]['class_file']);
  68. }
  69. $GLOBALS['bxDolClasses'][$sClassKey] = new $sClassName($sSystem);
  70. return $GLOBALS['bxDolClasses'][$sClassKey];
  71. }
  72. /**
  73. * get content info object instance (for internal usage).
  74. * @see self::getObjectInstanceByAlertAdd, self::getObjectInstanceByAlertUpdate and self::getObjectInstanceByAlertDelete
  75. * @param $sAlertType alert type (add, update, delete)
  76. * @param $sUnit alert unit
  77. * @param $sAction alert action
  78. * @return null on error, or ready to use class instance
  79. */
  80. public static function getObjectInstanceByAlertCommon($sAlertType, $sUnit, $sAction)
  81. {
  82. $sAlert = $sUnit . '_' . $sAction;
  83. $sClassKey = 'BxDolContentInfo!' . bx_gen_method_name($sAlertType . '_' . $sAlert);
  84. if(isset($GLOBALS['bxDolClasses'][$sClassKey]))
  85. return $GLOBALS['bxDolClasses'][$sClassKey];
  86. $aSystems = self::getSystemsByAlertType($sAlertType);
  87. if(!isset($aSystems[$sAlert]))
  88. return null;
  89. $sClassName = 'BxDolContentInfo';
  90. if(!empty($aSystems[$sAlert]['class_name'])) {
  91. $sClassName = $aSystems[$sAlert]['class_name'];
  92. if(!empty($aSystems[$sAlert]['class_file']))
  93. require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sAlert]['class_file']);
  94. }
  95. $GLOBALS['bxDolClasses'][$sClassKey] = new $sClassName($aSystems[$sAlert]['name']);
  96. return $GLOBALS['bxDolClasses'][$sClassKey];
  97. }
  98. public static function getObjectInstanceByAlertAdd($sUnit, $sAction)
  99. {
  100. return self::getObjectInstanceByAlertCommon('add', $sUnit, $sAction);
  101. }
  102. public static function getObjectInstanceByAlertUpdate($sUnit, $sAction)
  103. {
  104. return self::getObjectInstanceByAlertCommon('update', $sUnit, $sAction);
  105. }
  106. public static function getObjectInstanceByAlertDelete($sUnit, $sAction)
  107. {
  108. return self::getObjectInstanceByAlertCommon('delete', $sUnit, $sAction);
  109. }
  110. /**
  111. * get content info object instance (for internal usage).
  112. * @see self::getObjectInstanceByAlertAdd, self::getObjectInstanceByAlertUpdate and self::getObjectInstanceByAlertDelete
  113. * @param $sAlertType alert type (add, update, delete)
  114. * @param $sUnit alert unit
  115. * @param $sAction alert action
  116. * @return null on error, or ready to use class instance
  117. */
  118. public static function getObjectInstanceByGrid($sGrid)
  119. {
  120. $aGrids = self::getGrids();
  121. if(!isset($aGrids[$sGrid]))
  122. return null;
  123. $oObject = self::getObjectInstance($aGrids[$sGrid]['object']);
  124. $oObject->setGrid($aGrids[$sGrid]);
  125. return $oObject;
  126. }
  127. public static function &getSystems()
  128. {
  129. $sKey = 'bx_dol_cache_memory_content_info_systems';
  130. if(!isset($GLOBALS[$sKey]))
  131. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_content_info', 'getAllWithKey', '
  132. SELECT
  133. `id` as `id`,
  134. `name` AS `name`,
  135. `title` AS `title`,
  136. `alert_unit` AS `alert_unit`,
  137. `alert_action_add` AS `alert_action_add`,
  138. `alert_action_update` AS `alert_action_update`,
  139. `alert_action_delete` AS `alert_action_delete`,
  140. `class_name` AS `class_name`,
  141. `class_file` AS `class_file`
  142. FROM `sys_objects_content_info`', 'name');
  143. return $GLOBALS[$sKey];
  144. }
  145. public static function &getGrids()
  146. {
  147. $sKey = 'bx_dol_cache_memory_content_info_grids';
  148. if(!isset($GLOBALS[$sKey]))
  149. $GLOBALS[$sKey] = BxDolDb::getInstance()->fromCache('sys_objects_content_info_grids', 'getAllWithKey', '
  150. SELECT
  151. `id` as `id`,
  152. `object` AS `object`,
  153. `grid_object` AS `grid_object`,
  154. `grid_field_id` AS `grid_field_id`,
  155. `condition` AS `condition`,
  156. `selection` AS `selection`
  157. FROM `sys_content_info_grids`', 'grid_object');
  158. return $GLOBALS[$sKey];
  159. }
  160. public static function &getSystemsByAlertType($sAlertType)
  161. {
  162. $sKey = 'bx_dol_cache_memory_content_info_systems_' . $sAlertType;
  163. if(!isset($GLOBALS[$sKey])) {
  164. $aSystems = BxDolContentInfo::getSystems();
  165. foreach($aSystems as $aSystem)
  166. $GLOBALS[$sKey][$aSystem['alert_unit'] . '_' . $aSystem['alert_action_' . $sAlertType]] = $aSystem;
  167. }
  168. return $GLOBALS[$sKey];
  169. }
  170. public static function formatFields($aContentInfo)
  171. {
  172. foreach ($aContentInfo as $sKey => $mixedValue)
  173. if(is_numeric($mixedValue)) {
  174. if(strpos($mixedValue, '.') !== false)
  175. $aContentInfo[$sKey] = (float)$mixedValue;
  176. else
  177. $aContentInfo[$sKey] = (int)$mixedValue;
  178. }
  179. return $aContentInfo;
  180. }
  181. public function setGrid($aGrid)
  182. {
  183. $this->_aGrid = $aGrid;
  184. }
  185. public function setIgnoreCacheInServiceCalls($b)
  186. {
  187. $this->_bIgnoreCacheInServiceCalls = $b;
  188. }
  189. public function getName()
  190. {
  191. return $this->_sSystem;
  192. }
  193. public function getTitle()
  194. {
  195. return $this->_aSystem['title'];
  196. }
  197. public function getGrid()
  198. {
  199. return $this->_aGrid;
  200. }
  201. public function getContentAuthor ($iContentId)
  202. {
  203. return $this->_call('get_author', $iContentId);
  204. }
  205. public function getContentDateAdded ($iContentId)
  206. {
  207. return $this->_call('get_date_added', $iContentId);
  208. }
  209. public function getContentDateChanged ($iContentId)
  210. {
  211. return $this->_call('get_date_changed', $iContentId);
  212. }
  213. public function getContentTitle ($iContentId)
  214. {
  215. return $this->_call('get_title', $iContentId);
  216. }
  217. public function getContentThumb ($iContentId)
  218. {
  219. return $this->_call('get_thumb', $iContentId);
  220. }
  221. public function getContentLink ($iContentId)
  222. {
  223. return $this->_call('get_link', $iContentId);
  224. }
  225. public function getContentText ($iContentId)
  226. {
  227. return $this->_call('get_text', $iContentId);
  228. }
  229. public function getContentEmbed ($iContentId)
  230. {
  231. return $this->_call('get_embed', $iContentId);
  232. }
  233. public function getContentInfo ($iContentId, $bSearchableFieldsOnly = true)
  234. {
  235. return $this->_call('get_info', $iContentId, $bSearchableFieldsOnly);
  236. }
  237. public function getContentSearchResultUnit ($iContentId, $sUnitTemplate = '')
  238. {
  239. return $this->_call('get_search_result_unit', $iContentId, $sUnitTemplate);
  240. }
  241. public function getAll ($aParams = array())
  242. {
  243. return $this->_call('get_all', $aParams);
  244. }
  245. public function getSearchableFieldsExtended ()
  246. {
  247. return $this->_call('get_searchable_fields_extended');
  248. }
  249. public function getSortableFieldsExtended ()
  250. {
  251. return $this->_call('get_sortable_fields_extended');
  252. }
  253. public function getSearchResultExtended ($aParams, $iStart = 0, $iPerPage = 0, $bFilterMode = false)
  254. {
  255. return $this->_call('get_search_result_extended', $aParams, $iStart, $iPerPage, $bFilterMode);
  256. }
  257. protected function _call($sMethod)
  258. {
  259. if(!BxDolRequest::serviceExists($this->_sSystem, $sMethod))
  260. return false;
  261. return BxDolService::call($this->_sSystem, $sMethod, array_slice(func_get_args(), 1), 'Module', $this->_bIgnoreCacheInServiceCalls);
  262. }
  263. }
  264. /** @} */