BxBaseView.php 12 KB

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