BxBaseView.php 11 KB

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