1
0

BxBaseView.php 12 KB

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