BxBaseView.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 $_bCssJsAdded;
  15. protected $_sStylePrefix;
  16. protected $_sJsObjName;
  17. protected $_aHtmlIds;
  18. protected $_aElementDefaults;
  19. protected $_sTmplNameCounter;
  20. protected $_sTmplNameDoView;
  21. protected $_sTmplNameDoViewLabel;
  22. protected $_sTmplNameByList;
  23. public function __construct($sSystem, $iId, $iInit = 1)
  24. {
  25. parent::__construct($sSystem, $iId, $iInit);
  26. $this->_bCssJsAdded = false;
  27. $this->_sStylePrefix = 'bx-view';
  28. $this->_sJsObjName = 'oView' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  29. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  30. $this->_aHtmlIds = array(
  31. 'main' => 'bx-view-' . $sHtmlId,
  32. 'counter' => 'bx-view-counter-' . $sHtmlId,
  33. 'by_popup' => 'bx-view-by-popup-' . $sHtmlId
  34. );
  35. $this->_aElementDefaults = array(
  36. 'show_do_view_as_button' => false,
  37. 'show_do_view_as_button_small' => false,
  38. 'show_do_view_icon' => true,
  39. 'show_do_view_label' => false,
  40. 'show_counter' => true
  41. );
  42. $this->_sTmplNameCounter = 'view_counter.html';
  43. $this->_sTmplNameDoView = 'view_do_view.html';
  44. $this->_sTmplNameDoViewLabel = 'view_do_view_label.html';
  45. $this->_sTmplNameByList = 'view_by_list.html';
  46. }
  47. public function addCssJs($bDynamicMode = false)
  48. {
  49. if($bDynamicMode || $this->_bCssJsAdded)
  50. return;
  51. $this->_oTemplate->addJs(array('BxDolView.js'));
  52. $this->_oTemplate->addCss(array('view.css'));
  53. $this->_bCssJsAdded = true;
  54. }
  55. public function getJsObjectName()
  56. {
  57. return $this->_sJsObjName;
  58. }
  59. public function getJsScript($bDynamicMode = false)
  60. {
  61. $aParams = array(
  62. 'sObjName' => $this->_sJsObjName,
  63. 'sSystem' => $this->getSystemName(),
  64. 'iAuthorId' => $this->_getAuthorId(),
  65. 'iObjId' => $this->getId(),
  66. 'sRootUrl' => BX_DOL_URL_ROOT,
  67. 'sStylePrefix' => $this->_sStylePrefix,
  68. 'aHtmlIds' => $this->_aHtmlIds
  69. );
  70. $sCode = $this->_sJsObjName . " = new BxDolView(" . json_encode($aParams) . ");";
  71. if($bDynamicMode) {
  72. $sCode = "var " . $this->_sJsObjName . " = null;
  73. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('BxDolView.js'), BX_ESCAPE_STR_APOS) . "', function(data, textStatus, jqxhr) {
  74. bx_get_style('" . bx_js_string($this->_oTemplate->getCssUrl('view.css'), BX_ESCAPE_STR_APOS) . "');
  75. " . $sCode . "
  76. }); ";
  77. }
  78. else
  79. $sCode = "var " . $sCode;
  80. $this->addCssJs($bDynamicMode);
  81. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  82. }
  83. public function getCounter($aParams = array())
  84. {
  85. $sJsObject = $this->getJsObjectName();
  86. $bShowDoViewAsButtonSmall = isset($aParams['show_do_view_as_button_small']) && $aParams['show_do_view_as_button_small'] == true;
  87. $bShowDoViewAsButton = !$bShowDoViewAsButtonSmall && isset($aParams['show_do_view_as_button']) && $aParams['show_do_view_as_button'] == true;
  88. $bAllowedViewViewViewers = $this->isAllowedViewViewViewers();
  89. $aView = $this->_oQuery->getView($this->getId());
  90. $sClass = $this->_sStylePrefix . '-counter';
  91. if($bShowDoViewAsButtonSmall)
  92. $sClass .= ' bx-btn-small-height';
  93. if($bShowDoViewAsButton)
  94. $sClass .= ' bx-btn-height';
  95. $sContent = (int)$aView['count'] > 0 ? $this->_getLabelCounter($aView['count']) : '';
  96. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameCounter, array(
  97. 'bx_if:show_text' => 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. ),
  104. 'content' => $sContent
  105. )
  106. ),
  107. 'bx_if:show_link' => array(
  108. 'condition' => $bAllowedViewViewViewers,
  109. 'content' => array(
  110. 'bx_repeat:attrs' => array(
  111. array('key' => 'id', 'value' => $this->_aHtmlIds['counter']),
  112. array('key' => 'class', 'value' => $sClass),
  113. array('key' => 'href', 'value' => 'javascript:void(0)'),
  114. array('key' => 'onclick', 'value' => 'javascript:' . $sJsObject . '.toggleByPopup(this)'),
  115. array('key' => 'title', 'value' => _t('_view_do_view_by'))
  116. ),
  117. 'content' => $sContent
  118. )
  119. )
  120. ));
  121. }
  122. public function getElementBlock($aParams = array())
  123. {
  124. $aParams['usage'] = BX_DOL_VIEW_USAGE_BLOCK;
  125. return $this->getElement($aParams);
  126. }
  127. public function getElementInline($aParams = array())
  128. {
  129. $aParams['usage'] = BX_DOL_VIEW_USAGE_INLINE;
  130. return $this->getElement($aParams);
  131. }
  132. public function getElement($aParams = array())
  133. {
  134. $aParams = array_merge($this->_aElementDefaults, $aParams);
  135. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  136. $bAllowedView = $this->isAllowedView();
  137. $bAllowedViewView = $this->isAllowedViewView();
  138. $bShowDoView = !isset($aParams['show_do_view']) || $aParams['show_do_view'] == true;
  139. $bShowDoViewAsButtonSmall = isset($aParams['show_do_view_as_button_small']) && $aParams['show_do_view_as_button_small'] == true;
  140. $bShowDoViewAsButton = !$bShowDoViewAsButtonSmall && isset($aParams['show_do_view_as_button']) && $aParams['show_do_view_as_button'] == true;
  141. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true && $bAllowedViewView;
  142. $iObjectId = $this->getId();
  143. $iAuthorId = $this->_getAuthorId();
  144. $aView = $this->_oQuery->getView($iObjectId);
  145. $bCount = (int)$aView['count'] != 0;
  146. if(!$bAllowedView && (!$bAllowedViewView || !$bCount))
  147. return '';
  148. //--- Do View
  149. $bTmplVarsDoView = $bShowDoView && ($bCount || $bAllowedView);
  150. $aTmplVarsDoView = array();
  151. if($bTmplVarsDoView)
  152. $aTmplVarsDoView = array(
  153. 'style_prefix' => $this->_sStylePrefix,
  154. 'do_view' => $this->_getDoView($aParams, $bAllowedView),
  155. );
  156. //--- Counter
  157. $bTmplVarsCounter = $bShowCounter && ($bCount || $bAllowedView);
  158. $aTmplVarsCounter = array();
  159. if($bTmplVarsCounter)
  160. $aTmplVarsCounter = array(
  161. 'style_prefix' => $this->_sStylePrefix,
  162. 'bx_if:show_hidden' => array(
  163. 'condition' => !$bCount,
  164. 'content' => array()
  165. ),
  166. 'counter' => $this->getCounter($aParams)
  167. );
  168. if(!$bTmplVarsDoView && !$bTmplVarsCounter)
  169. return '';
  170. $sTmplName = 'view_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VIEW_USAGE_DEFAULT) . '.html';
  171. return $this->_oTemplate->parseHtmlByName($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->parseHtmlByName($this->_sTmplNameDoView, array(
  200. 'style_prefix' => $this->_sStylePrefix,
  201. 'class' => $sClass,
  202. 'title' => _t('_view_do_view'),
  203. 'bx_if:show_onclick' => array(
  204. 'condition' => !$bDisabled,
  205. 'content' => array(
  206. 'js_object' => $this->getJsObjectName()
  207. )
  208. ),
  209. 'do_view' => $this->_getLabelDo($aParams),
  210. ));
  211. }
  212. protected function _getLabelCounter($iCount)
  213. {
  214. return _t('_view_counter', $iCount);
  215. }
  216. protected function _getLabelDo($aParams = array())
  217. {
  218. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoViewLabel, array(
  219. 'bx_if:show_icon' => array(
  220. 'condition' => isset($aParams['show_do_view_icon']) && $aParams['show_do_view_icon'] == true,
  221. 'content' => array(
  222. 'name' => $this->_getIconDo()
  223. )
  224. ),
  225. 'bx_if:show_text' => array(
  226. 'condition' => isset($aParams['show_do_view_label']) && $aParams['show_do_view_label'] == true,
  227. 'content' => array(
  228. 'text' => _t($this->_getTitleDo())
  229. )
  230. )
  231. ));
  232. }
  233. protected function _getViewedBy()
  234. {
  235. $aTmplUsers = array();
  236. $aUserIds = $this->_oQuery->getPerformedBy($this->getId());
  237. foreach($aUserIds as $iUserId) {
  238. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  239. $aTmplUsers[] = array(
  240. 'style_prefix' => $this->_sStylePrefix,
  241. 'user_unit' => $sUserUnit
  242. );
  243. }
  244. if(empty($aTmplUsers))
  245. $aTmplUsers = MsgBox(_t('_Empty'));
  246. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  247. 'style_prefix' => $this->_sStylePrefix,
  248. 'bx_repeat:list' => $aTmplUsers
  249. ));
  250. }
  251. }
  252. /** @} */