BxBaseView.php 12 KB

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