BxNtfsTemplate.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 Notifications Notifications
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  12. class BxNtfsTemplate extends BxBaseModNotificationsTemplate
  13. {
  14. function __construct(&$oConfig, &$oDb)
  15. {
  16. parent::__construct($oConfig, $oDb);
  17. }
  18. public function getInclude($bIncludeCss = true, $mixedIncludeJs = false)
  19. {
  20. if($bIncludeCss)
  21. $this->getCss();
  22. $sResult = '';
  23. if(is_string($mixedIncludeJs) && !empty($mixedIncludeJs)) {
  24. $this->getJs();
  25. $sResult = $this->getJsCode($mixedIncludeJs);
  26. }
  27. return $sResult;
  28. }
  29. public function getViewBlock($aParams)
  30. {
  31. return $this->parseHtmlByName('block_view.html', array(
  32. 'html_id_view_block' => $this->_oConfig->getHtmlIds('view', 'block'),
  33. 'html_id_events' => $this->_oConfig->getHtmlIds('view', 'events'),
  34. 'style_prefix' => $this->_oConfig->getPrefix('style'),
  35. 'content' => $this->getPosts($aParams),
  36. 'js_content' => $this->getJsCode('view', array(
  37. 'oRequestParams' => array(
  38. 'type' => $aParams['type'],
  39. 'owner_id' => $aParams['owner_id'],
  40. 'start' => $aParams['start'],
  41. 'per_page' => $aParams['per_page'],
  42. 'modules' => $aParams['modules']
  43. )
  44. ))
  45. ));
  46. }
  47. public function getSettingsBlock($sDeliveryType, $aParams = array())
  48. {
  49. $CNF = &$this->_oConfig->CNF;
  50. $iUserId = !empty($aParams['user_id']) ? (int)$aParams['user_id'] : bx_get_logged_profile_id();
  51. $sGrid = !empty($aParams['grid']) ? $aParams['grid'] : $CNF['OBJECT_GRID_SETTINGS_COMMON'];
  52. $oTemplate = !empty($aParams['template']) ? $aParams['template'] : $this;
  53. $oGrid = BxDolGrid::getObjectInstance($sGrid, $oTemplate);
  54. if(!$oGrid)
  55. return '';
  56. $oGrid->setDeliveryType($sDeliveryType);
  57. if(method_exists($oGrid, 'setUserId'))
  58. $oGrid->setUserId($iUserId);
  59. return $oGrid->getCode();
  60. }
  61. public function getPosts($aParams)
  62. {
  63. $sJsObject = $this->_oConfig->getJsObject('view');
  64. $aParamsDb = $aParams;
  65. $aParamsDb['per_page'] = 3 * $aParamsDb['per_page'];
  66. $aEvents = $this->_oDb->getEvents($aParamsDb);
  67. if(empty($aEvents))
  68. return $this->getEmpty();
  69. $aTmplVarsEvents = array();
  70. foreach($aEvents as $aEvent) {
  71. $sEvent = $this->getPost($aEvent, $aParams);
  72. if(empty($sEvent))
  73. continue;
  74. $aTmplVarsEvents[] = array('event' => $sEvent);
  75. if(count($aTmplVarsEvents) >= ($aParams['per_page'] + 1))
  76. break;
  77. }
  78. $oPaginate = new BxTemplPaginate(array(
  79. 'start' => $aParams['start'],
  80. 'per_page' => $aParams['per_page'],
  81. 'page_url' => $this->_oConfig->getViewUrl(),
  82. 'on_change_page' => $sJsObject . ".changePage(this, {start}, {per_page})"
  83. ));
  84. $oPaginate->setNumFromDataArray($aTmplVarsEvents);
  85. return $this->parseHtmlByName('events.html', array(
  86. 'style_prefix' => $this->_oConfig->getPrefix('style'),
  87. 'bx_repeat:events' => $aTmplVarsEvents,
  88. 'paginate' => $oPaginate->getSimplePaginate()
  89. ));
  90. }
  91. /**
  92. * Enter description here ...
  93. * @example Available keys are:
  94. * 1. owner_name and owner_link
  95. * 2. entry_caption and entry_url
  96. * 3. subentry_url and subentry_sample
  97. *
  98. */
  99. public function getPost(&$aEvent, $aBrowseParams = array())
  100. {
  101. $oModule = $this->getModule();
  102. if(!empty($aEvent['content']) && is_string($aEvent['content']))
  103. $aEvent['content'] = unserialize($aEvent['content']);
  104. if((int)$aEvent['processed'] == 0)
  105. $this->_processContent($aEvent);
  106. if((int)$aEvent['processed'] == 0 || empty($aEvent['content']))
  107. return '';
  108. $sParam = 'perform_privacy_check';
  109. if(!isset($aBrowseParams[$sParam]) || $aBrowseParams[$sParam] === true) {
  110. $oPrivacyInt = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'));
  111. if(!$oPrivacyInt->check($aEvent['id']))
  112. return '';
  113. $oPrivacyExt = $oModule->_oConfig->getPrivacyObject($aEvent['type'] . '_' . $aEvent['action']);
  114. if($oPrivacyExt !== false && !$oPrivacyExt->check($aEvent['id']))
  115. return '';
  116. $sService = 'check_allowed_with_content';
  117. if(BxDolRequest::serviceExists($aEvent['type'], $sService) && BxDolService::call($aEvent['type'], $sService, array('view', $this->_getContentObjectId($aEvent))) !== CHECK_ACTION_RESULT_ALLOWED)
  118. return '';
  119. }
  120. $oOwner = $oModule->getObjectUser($aEvent['owner_id']);
  121. $aEvent['content']['owner_name'] = strmaxtextlen($oOwner->getDisplayName(), $this->_oConfig->getOwnerNameMaxLen());
  122. $aEvent['content']['owner_link'] = $oOwner->getUrl();
  123. $aEvent['content']['owner_icon'] = $oOwner->getThumb();
  124. if(!empty($aEvent['content']['entry_caption'])) {
  125. $sEntryCaption = bx_process_output($aEvent['content']['entry_caption'], BX_DATA_TEXT);
  126. if(get_mb_substr($sEntryCaption, 0, 1) == '_')
  127. $sEntryCaption = _t($sEntryCaption);
  128. $aEvent['content']['entry_caption'] = strmaxtextlen($sEntryCaption, $this->_oConfig->getContentMaxLen());
  129. }
  130. $iObjectOwner = (int)$aEvent['object_owner_id'];
  131. if(empty($iObjectOwner) && !empty($aEvent['content']['entry_author']))
  132. $iObjectOwner = (int)$aEvent['content']['entry_author'];
  133. $oObjectOwner = $oModule->getObjectUser($iObjectOwner);
  134. if($oObjectOwner) {
  135. $aEvent['content']['object_owner_name'] = strmaxtextlen($oObjectOwner->getDisplayName(), $this->_oConfig->getOwnerNameMaxLen());
  136. $aEvent['content']['object_owner_link'] = $oObjectOwner->getUrl();
  137. $aEvent['content']['object_owner_icon'] = $oObjectOwner->getThumb();
  138. }
  139. foreach($aEvent['content'] as $sKey => $sValue)
  140. if(substr($sValue, 0, 1) == '_')
  141. $aEvent['content'][$sKey] = _t($sValue);
  142. $bEventParsed = false;
  143. bx_alert($this->_oConfig->getName(), 'get_notification', 0, 0, array('event' => &$aEvent, 'event_parsed' => &$bEventParsed, 'owner' => &$oOwner));
  144. if (!$bEventParsed){
  145. $aEvent['content_parsed'] = _t(!empty($aEvent['content']['lang_key']) ? $aEvent['content']['lang_key'] : $this->_getContentLangKey($aEvent));
  146. $aEvent['content_parsed'] = $this->parseHtmlByContent($aEvent['content_parsed'], $aEvent['content'], array('{', '}'));
  147. }
  148. // returns parsed content for React Jot
  149. if(isset($aBrowseParams['return_parsed_content']))
  150. return $aEvent;
  151. return $this->parseHtmlByName('event.html', array (
  152. 'html_id' => $this->_oConfig->getHtmlIds('view', 'event') . $aEvent['id'],
  153. 'style_prefix' => $this->_oConfig->getPrefix('style'),
  154. 'js_object' => $this->_oConfig->getJsObject('view'),
  155. 'class' => !empty($aBrowseParams['last_read']) && $aEvent['id'] > $aBrowseParams['last_read'] ? ' bx-def-color-bg-box-active' : '',
  156. 'id' => $aEvent['id'],
  157. 'author_unit' => $this->_isInContext($aEvent) && $oObjectOwner ? $oObjectOwner->getUnit(0, array('template' => 'unit_wo_info_links')) : $oOwner->getUnit(0, array('template' => 'unit_wo_info_links')),
  158. 'link' => $this->_getContentLink($aEvent),
  159. 'content' => $aEvent['content_parsed'],
  160. 'date' => bx_time_js($aEvent['date']),
  161. ));
  162. }
  163. public function getNotificationEmail(&$aEvent)
  164. {
  165. $sEvent = $this->getPost($aEvent, array('perform_privacy_check' => false));
  166. if(empty($sEvent) || empty($aEvent['content_parsed']))
  167. return false;
  168. $aContent = &$aEvent['content'];
  169. return array(
  170. 'content' => $this->parseHtmlByName('et_new_event.html', array(
  171. 'icon_url' => !empty($aContent['owner_icon']) ? $aContent['owner_icon'] : $this->getIconUrl('std-icon.svg'),
  172. 'content_url' => $this->_getContentLink($aEvent),
  173. 'content' => $aEvent['content_parsed'],
  174. 'date' => bx_process_output($aEvent['date'], BX_DATA_DATETIME_TS),
  175. )),
  176. 'settings' => !empty($aContent['settings']['email']) ? $aContent['settings']['email'] : array()
  177. );
  178. }
  179. public function getNotificationPush(&$aEvent)
  180. {
  181. $sEvent = $this->getPost($aEvent, array('perform_privacy_check' => false));
  182. if(empty($sEvent) || empty($aEvent['content_parsed']))
  183. return false;
  184. $sMessage = preg_replace('/<\/?[a-zA-Z0-9=\'":;\(\)\s_-]+>/i', '"', $aEvent['content_parsed']);
  185. if($sMessage)
  186. $sMessage = BxTemplFunctions::getInstance()->getStringWithLimitedLength(html_entity_decode($sMessage), $this->_oConfig->getPushMaxLen());
  187. if(empty($sMessage))
  188. return false;
  189. $aContent = &$aEvent['content'];
  190. return array(
  191. 'content' => array(
  192. 'url' => $this->_getContentLink($aEvent),
  193. 'message' => $sMessage,
  194. 'icon' => !empty($aContent['owner_icon']) ? $aContent['owner_icon'] : ''
  195. ),
  196. 'settings' => !empty($aContent['settings']['push']) ? $aContent['settings']['push'] : array()
  197. );
  198. }
  199. public function getEmpty($bVisible = true)
  200. {
  201. return $this->parseHtmlByName('empty.html', array(
  202. 'style_prefix' => $this->_oConfig->getPrefix('style'),
  203. 'visible' => $bVisible ? 'block' : 'none',
  204. 'content' => MsgBox(_t('_bx_ntfs_txt_msg_no_results'))
  205. ));
  206. }
  207. protected function _processContent(&$aEvent)
  208. {
  209. $aContent = $this->_getContent($aEvent);
  210. if(empty($aContent) || !is_array($aContent))
  211. return;
  212. $aSet = array();
  213. if(!empty($aContent['entry_author'])) {
  214. $aSet['object_owner_id'] = (int)$aContent['entry_author'];
  215. unset($aContent['entry_author']);
  216. }
  217. if(!empty($aContent['entry_privacy'])) {
  218. $aSet['allow_view_event_to'] = $aContent['entry_privacy'];
  219. $aEvent['allow_view_event_to'] = $aContent['entry_privacy'];
  220. unset($aContent['entry_privacy']);
  221. }
  222. $aEvent['content'] = $aContent;
  223. $aEvent['processed'] = 1;
  224. $aSet = array_merge($aSet, array(
  225. 'content' => serialize($aEvent['content']),
  226. 'processed' => 1
  227. ));
  228. $this->_oDb->updateEvent($aSet, array('id' => $aEvent['id']));
  229. return;
  230. }
  231. protected function _getContent(&$aEvent)
  232. {
  233. $sHandler = $aEvent['type'] . '_' . $aEvent['action'];
  234. if(!$this->_oConfig->isHandler($sHandler))
  235. return array();
  236. $aHandler = $this->_oConfig->getHandlers($sHandler);
  237. if(!empty($aHandler['module_name']) && !empty($aHandler['module_class']) && !empty($aHandler['module_method']))
  238. return BxDolService::call($aHandler['module_name'], $aHandler['module_method'], array($aEvent), $aHandler['module_class']);
  239. $sMethod = 'display' . bx_gen_method_name($aHandler['alert_unit'] . '_' . $aHandler['alert_action']);
  240. if(!method_exists($this, $sMethod))
  241. return array();
  242. return $this->$sMethod($aEvent);
  243. }
  244. protected function _getContentObjectId(&$aEvent)
  245. {
  246. return !empty($aEvent['content']['object_id']) ? (int)$aEvent['content']['object_id'] : (int)$aEvent['object_id'];
  247. }
  248. protected function _getContentLangKey(&$aEvent)
  249. {
  250. if(!empty($aEvent['subobject_id']))
  251. return '_bx_ntfs_txt_subobject_added';
  252. if($this->_isInContext($aEvent))
  253. return '_bx_ntfs_txt_object_added_in_context';
  254. return '_bx_ntfs_txt_object_added';
  255. }
  256. protected function _getContentLink(&$aEvent)
  257. {
  258. return !empty($aEvent['subobject_id']) && !empty($aEvent['content']['subentry_url']) ? $aEvent['content']['subentry_url'] : $aEvent['content']['entry_url'];
  259. }
  260. protected function _isInContext(&$aEvent)
  261. {
  262. return (int)$aEvent['object_privacy_view'] < 0;
  263. }
  264. }
  265. /** @} */