BxBaseView.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * @see BxDolView
  11. */
  12. class BxBaseView extends BxDolView
  13. {
  14. protected $_sTmplNameByList;
  15. protected $_sTmplContentDoViewLabel;
  16. protected $_bCssJsAdded;
  17. protected $_sStylePrefix;
  18. protected $_sJsObjName;
  19. protected $_aHtmlIds;
  20. protected $_aElementDefaults;
  21. public function __construct($sSystem, $iId, $iInit = 1)
  22. {
  23. parent::__construct($sSystem, $iId, $iInit);
  24. $this->_bCssJsAdded = false;
  25. $this->_sStylePrefix = 'bx-view';
  26. $this->_sJsObjName = 'oView' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  27. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  28. $this->_aHtmlIds = array(
  29. 'main' => 'bx-view-' . $sHtmlId,
  30. 'counter' => 'bx-view-counter-' . $sHtmlId,
  31. 'by_popup' => 'bx-view-by-popup-' . $sHtmlId
  32. );
  33. $this->_aElementDefaults = array(
  34. 'show_do_view_as_button' => false,
  35. 'show_do_view_as_button_small' => false,
  36. 'show_do_view_icon' => true,
  37. 'show_do_view_label' => false,
  38. 'show_counter' => true,
  39. 'show_counter_only' => true,
  40. 'show_counter_label_icon' => false,
  41. 'show_counter_label_text' => true,
  42. 'show_script' => true
  43. );
  44. $this->_sTmplNameByList = 'view_by_list.html';
  45. $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('view_element_block.html');
  46. $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('view_element_inline.html');
  47. $this->_sTmplContentCounter = $this->_oTemplate->getHtml('view_counter.html');
  48. $this->_sTmplContentCounterLabel = $this->_oTemplate->getHtml('view_counter_label.html');
  49. $this->_sTmplContentDoViewLabel = $this->_oTemplate->getHtml('view_do_view_label.html');
  50. }
  51. public function getJsObjectName()
  52. {
  53. return $this->_sJsObjName;
  54. }
  55. public function getJsScript($bDynamicMode = false)
  56. {
  57. $sJsObjName = $this->getJsObjectName();
  58. $aParams = array(
  59. 'sObjName' => $sJsObjName,
  60. 'sSystem' => $this->getSystemName(),
  61. 'iAuthorId' => $this->_getAuthorId(),
  62. 'iObjId' => $this->getId(),
  63. 'sRootUrl' => BX_DOL_URL_ROOT,
  64. 'sStylePrefix' => $this->_sStylePrefix,
  65. 'aHtmlIds' => $this->_aHtmlIds
  66. );
  67. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new BxDolView(" . json_encode($aParams) . ");";
  68. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  69. }
  70. public function getCounter($aParams = [])
  71. {
  72. $aParams = array_merge($this->_aElementDefaults, $aParams);
  73. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  74. $bShowDoViewAsButtonSmall = isset($aParams['show_do_view_as_button_small']) && $aParams['show_do_view_as_button_small'] == true;
  75. $bShowDoViewAsButton = !$bShowDoViewAsButtonSmall && isset($aParams['show_do_view_as_button']) && $aParams['show_do_view_as_button'] == true;
  76. $bShowScript = !isset($aParams['show_script']) || $aParams['show_script'] == true;
  77. $bAllowedViewViewViewers = $this->isAllowedViewViewViewers();
  78. $sClass = 'sys-action-counter';
  79. if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
  80. $sClass .= ' sys-ac-only';
  81. $sClass .= ' ' . $this->_sStylePrefix . '-counter';
  82. if($bShowDoViewAsButtonSmall)
  83. $sClass .= ' bx-btn-small-height';
  84. if($bShowDoViewAsButton)
  85. $sClass .= ' bx-btn-height';
  86. $sContent = '';
  87. $aView = $this->_oQuery->getView($this->getId());
  88. if((int)$aView['count'] > 0)
  89. $sContent = $this->_getCounterLabel($aView['count'], $aParams);
  90. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounter(), array(
  91. 'html_id' => $this->_aHtmlIds['counter'],
  92. 'style_prefix' => $this->_sStylePrefix,
  93. 'bx_if:show_text' => array(
  94. 'condition' => !$bAllowedViewViewViewers,
  95. 'content' => array(
  96. 'class' => $sClass,
  97. 'bx_repeat:attrs' => array(
  98. array('key' => 'id', 'value' => $this->_aHtmlIds['counter']),
  99. ),
  100. 'content' => $sContent
  101. )
  102. ),
  103. 'bx_if:show_link' => array(
  104. 'condition' => $bAllowedViewViewViewers,
  105. 'content' => array(
  106. 'class' => $sClass,
  107. 'bx_repeat:attrs' => array(
  108. array('key' => 'id', 'value' => $this->_aHtmlIds['counter']),
  109. array('key' => 'href', 'value' => 'javascript:void(0)'),
  110. array('key' => 'onclick', 'value' => 'javascript:' . $this->getJsObjectName() . '.toggleByPopup(this)'),
  111. array('key' => 'title', 'value' => _t('_view_do_view_by'))
  112. ),
  113. 'content' => $sContent
  114. )
  115. ),
  116. 'script' => $bShowScript ? $this->getJsScript($bDynamicMode) : ''
  117. ));
  118. }
  119. public function getElementBlock($aParams = array())
  120. {
  121. $aParams['usage'] = BX_DOL_VIEW_USAGE_BLOCK;
  122. return $this->getElement($aParams);
  123. }
  124. public function getElementInline($aParams = array())
  125. {
  126. $aParams['usage'] = BX_DOL_VIEW_USAGE_INLINE;
  127. return $this->getElement($aParams);
  128. }
  129. public function getElement($aParams = array())
  130. {
  131. $aParams = array_merge($this->_aElementDefaults, $aParams);
  132. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  133. $bAllowedView = $this->isAllowedView();
  134. $bAllowedViewView = $this->isAllowedViewView();
  135. $bShowDoView = !isset($aParams['show_do_view']) || $aParams['show_do_view'] == true;
  136. $bShowDoViewAsButtonSmall = isset($aParams['show_do_view_as_button_small']) && $aParams['show_do_view_as_button_small'] == true;
  137. $bShowDoViewAsButton = !$bShowDoViewAsButtonSmall && isset($aParams['show_do_view_as_button']) && $aParams['show_do_view_as_button'] == true;
  138. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true && $bAllowedViewView;
  139. $iObjectId = $this->getId();
  140. $iAuthorId = $this->_getAuthorId();
  141. $aView = $this->_oQuery->getView($iObjectId);
  142. $bCount = (int)$aView['count'] != 0;
  143. if(!$bAllowedView && (!$bAllowedViewView || !$bCount))
  144. return '';
  145. //--- Do View
  146. $bTmplVarsDoView = $bShowDoView && ($bCount || $bAllowedView);
  147. $aTmplVarsDoView = array();
  148. if($bTmplVarsDoView)
  149. $aTmplVarsDoView = array(
  150. 'style_prefix' => $this->_sStylePrefix,
  151. 'do_view' => $this->_getDoView($aParams, $bAllowedView),
  152. );
  153. //--- Counter
  154. $bTmplVarsCounter = $bShowCounter && ($bCount || $bAllowedView);
  155. $aTmplVarsCounter = array();
  156. if($bTmplVarsCounter)
  157. $aTmplVarsCounter = array(
  158. 'style_prefix' => $this->_sStylePrefix,
  159. 'bx_if:show_hidden' => array(
  160. 'condition' => !$bCount,
  161. 'content' => array()
  162. ),
  163. 'counter' => $this->getCounter(array_merge($aParams, [
  164. 'show_counter_only' => false,
  165. 'show_script' => false
  166. ]))
  167. );
  168. if(!$bTmplVarsDoView && !$bTmplVarsCounter)
  169. return '';
  170. $sTmplName = $this->{'_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VIEW_USAGE_DEFAULT)}();
  171. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  172. 'style_prefix' => $this->_sStylePrefix,
  173. 'html_id' => $this->_aHtmlIds['main'],
  174. 'class' => $this->_sStylePrefix . ($bShowDoViewAsButton ? '-button' : '') . ($bShowDoViewAsButtonSmall ? '-button-small' : ''),
  175. 'count' => $aView['count'],
  176. 'bx_if:show_do_view' => array(
  177. 'condition' => $bTmplVarsDoView,
  178. 'content' => $aTmplVarsDoView
  179. ),
  180. 'bx_if:show_counter' => array(
  181. 'condition' => $bTmplVarsCounter,
  182. 'content' => $aTmplVarsCounter
  183. ),
  184. 'script' => $this->getJsScript($bDynamicMode)
  185. ));
  186. }
  187. protected function _getDoView($aParams = array(), $bAllowedView = true)
  188. {
  189. $bShowDoViewAsButtonSmall = isset($aParams['show_do_view_as_button_small']) && $aParams['show_do_view_as_button_small'] == true;
  190. $bShowDoViewAsButton = !$bShowDoViewAsButtonSmall && isset($aParams['show_do_view_as_button']) && $aParams['show_do_view_as_button'] == true;
  191. $bDisabled = !$bAllowedView;
  192. $sClass = '';
  193. if($bShowDoViewAsButton)
  194. $sClass = 'bx-btn';
  195. else if ($bShowDoViewAsButtonSmall)
  196. $sClass = 'bx-btn bx-btn-small';
  197. if($bDisabled)
  198. $sClass .= $bShowDoViewAsButton || $bShowDoViewAsButtonSmall ? ' bx-btn-disabled' : 'bx-view-disabled';
  199. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDo($aParams), array(
  200. 'class' => $this->_sStylePrefix . '-do-view ' . $sClass,
  201. 'title' => _t('_view_do_view')
  202. ));
  203. }
  204. protected function _getCounterLabel($iCount, $aParams = array())
  205. {
  206. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  207. 'style_prefix' => $this->_sStylePrefix,
  208. 'bx_if:show_icon' => array(
  209. 'condition' => !isset($aParams['show_counter_label_icon']) || $aParams['show_counter_label_icon'] === true,
  210. 'content' => array(
  211. 'style_prefix' => $this->_sStylePrefix,
  212. 'icon' => isset($aParams['custom_icon']) && $aParams['custom_icon'] != '' ? $aParams['custom_icon'] : $this->_oTemplate->getImageAuto($this->_getIconDo())
  213. )
  214. ),
  215. 'bx_if:show_text' => array(
  216. 'condition' => !isset($aParams['show_counter_label_text']) || $aParams['show_counter_label_text'] === true,
  217. 'content' => array(
  218. 'style_prefix' => $this->_sStylePrefix,
  219. 'text' => _t(isset($aParams['caption']) ? $aParams['caption'] : '_view_counter', $iCount)
  220. )
  221. )
  222. ));
  223. }
  224. protected function _getLabelDo($aParams = array())
  225. {
  226. return $this->_oTemplate->parseHtmlByContent($this->_sTmplContentDoViewLabel, array(
  227. 'style_prefix' => $this->_sStylePrefix,
  228. 'bx_if:show_icon' => array(
  229. 'condition' => isset($aParams['show_do_view_icon']) && $aParams['show_do_view_icon'] == true,
  230. 'content' => array(
  231. 'style_prefix' => $this->_sStylePrefix,
  232. 'icon' => $this->_oTemplate->getImageAuto($this->_getIconDo())
  233. )
  234. ),
  235. 'bx_if:show_text' => array(
  236. 'condition' => isset($aParams['show_do_view_label']) && $aParams['show_do_view_label'] == true,
  237. 'content' => array(
  238. 'style_prefix' => $this->_sStylePrefix,
  239. 'text' => _t($this->_getTitleDo())
  240. )
  241. )
  242. ));
  243. }
  244. protected function _getViewedBy($iStart = 0, $iPerPage = 0)
  245. {
  246. if(empty($iPerPage))
  247. $iPerPage = $this->_aSystem['per_page_default'];
  248. $aUsers = $this->_oQuery->getPerformedBy($this->getId(), $iStart, $iPerPage + 1);
  249. $oPaginate = new BxTemplPaginate(array(
  250. 'on_change_page' => $this->getJsObjectName() . '.getUsers(this, {start}, {per_page})',
  251. 'start' => $iStart,
  252. 'per_page' => $iPerPage,
  253. ));
  254. $oPaginate->setNumFromDataArray($aUsers);
  255. $aTmplUsers = array();
  256. foreach($aUsers as $aUser) {
  257. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit, $sUserUnitWoInfo) = $this->_getAuthorInfo($aUser['id']);
  258. $aTmplUsers[] = array(
  259. 'style_prefix' => $this->_sStylePrefix,
  260. 'user_unit' => $sUserUnitWoInfo,
  261. 'user_url' => $sUserUrl,
  262. 'user_title' => bx_html_attribute($sUserName),
  263. 'user_name' => $sUserName,
  264. 'bx_if:show_user_info' => array(
  265. 'condition' => (int)$aUser['id'] == 0 && (int)$aUser['count'] > 0,
  266. 'content' => array(
  267. 'style_prefix' => $this->_sStylePrefix,
  268. 'user_info' => _t('_view_do_view_by_counter', $aUser['count'])
  269. )
  270. )
  271. );
  272. }
  273. if(empty($aTmplUsers))
  274. $aTmplUsers = MsgBox(_t('_Empty'));
  275. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  276. 'style_prefix' => $this->_sStylePrefix,
  277. 'bx_repeat:list' => $aTmplUsers,
  278. 'paginate' => $oPaginate->getSimplePaginate()
  279. ));
  280. }
  281. }
  282. /** @} */