BxBaseMenuUnitMeta.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. * Menu representation.
  11. * @see BxDolMenu
  12. */
  13. class BxBaseMenuUnitMeta extends BxTemplMenuCustom
  14. {
  15. protected $_sStylePrefix;
  16. protected $_sContext;
  17. protected $_iButtons;
  18. protected $_iButtonsMax;
  19. public function __construct ($aObject, $oTemplate)
  20. {
  21. parent::__construct ($aObject, $oTemplate);
  22. $this->_sStylePrefix = 'sys-meta';
  23. $this->_sContext = '';
  24. $this->_iButtons = 0;
  25. $this->_iButtonsMax = false;
  26. }
  27. public function setContext($sContext)
  28. {
  29. $this->_sContext = $sContext;
  30. }
  31. public function getUnitMetaItemLink($sContent, $aAttrs = array())
  32. {
  33. return $this->getUnitMetaItemButtonOrLink('a', $sContent, $aAttrs);
  34. }
  35. public function getUnitMetaItemText($sContent, $aAttrs = array())
  36. {
  37. return $this->getUnitMetaItem('span', $sContent, $aAttrs);
  38. }
  39. public function getUnitMetaItemButton($sContent, $aAttrs = array())
  40. {
  41. if(!$this->_buttonsCounterCheck())
  42. return false;
  43. $sResult = $this->getUnitMetaItemButtonOrLink('button', $sContent, $aAttrs);
  44. if(!empty($sResult))
  45. $this->_buttonsCounterIncrement();
  46. return $sResult;
  47. }
  48. public function getUnitMetaItemButtonSmall($sContent, $aAttrs = array())
  49. {
  50. if(!$this->_buttonsCounterCheck())
  51. return false;
  52. $sResult = $this->getUnitMetaItemButtonOrLink('sbutton', $sContent, $aAttrs);
  53. if(!empty($sResult))
  54. $this->_buttonsCounterIncrement();
  55. return $sResult;
  56. }
  57. public function getUnitMetaItemNl($sContent = '')
  58. {
  59. return $this->getUnitMetaItem('nl', $sContent);
  60. }
  61. public function getUnitMetaItemCustom($sContent)
  62. {
  63. return $this->getUnitMetaItem('custom', $sContent);
  64. }
  65. public function getUnitMetaItemExtended($sContent = '', $sIcon = '', $sUrl = '', $bIsNoBg = false, $bIsNoPadding = false )
  66. {
  67. $aTmplVarsEx = [];
  68. if ($sContent == '' && $sIcon == '')
  69. return ;
  70. $aTmplVarsEx['bx_if:icon'] = [
  71. 'condition' => ($sIcon != ''),
  72. 'content' => ['icon' => $this->getMenuIconHtml($sIcon)]
  73. ];
  74. $aTmplVarsEx['bx_if:noicon'] = [
  75. 'condition' => ($sIcon == ''),
  76. 'content' => []
  77. ];
  78. $aTmplVarsEx['bx_if:a'] = [
  79. 'condition' => ($sUrl != ''),
  80. 'content' => ['link' => $sUrl]
  81. ];
  82. $aTmplVarsEx['bx_if:a2'] = [
  83. 'condition' => ($sUrl != ''),
  84. 'content' => []
  85. ];
  86. $aTmplVarsEx['bx_if:text'] = [
  87. 'condition' => ($sContent != ''),
  88. 'content' => ['content' => $sContent]
  89. ];
  90. $aTmplVars['bx_if:extended'] = [
  91. 'condition' => true,
  92. 'content' => $aTmplVarsEx
  93. ];
  94. $aTags = array('span', 'a', 'button', 'sbutton', 'custom', 'nl');
  95. foreach($aTags as $sTag) {
  96. $aTmplVars['bx_if:' . $sTag] = array(
  97. 'condition' => false,
  98. 'content' => []
  99. );
  100. }
  101. $aTmplVars['class'] = $bIsNoBg ? 'bx-menu-meta-item-ex-no-bg' : '';
  102. $aTmplVars['class'] = $bIsNoPadding ? 'bx-menu-meta-item-ex-no-pad' : '';
  103. return $this->_oTemplate->parseHtmlByName('unit_meta_item_ex.html', $aTmplVars);
  104. }
  105. public function getUnitMetaItem($sName, $sContent, $aAttrs = [], $sTemplate = 'unit_meta_item.html')
  106. {
  107. if(empty($sContent) && $sName != 'nl')
  108. return '';
  109. return $this->_oTemplate->parseTag($sName, $sContent, $aAttrs, [
  110. 'template_name' => $sTemplate,
  111. 'template_vars' => [
  112. 'style_prefix' => $this->_sStylePrefix
  113. ]
  114. ]);
  115. }
  116. public function getUnitMetaItemButtonOrLink($sName, $sContent, $aAttrs = array())
  117. {
  118. if(!isset($aAttrs['href']))
  119. $aAttrs['href'] = 'javascript:void(0)';
  120. return $this->getUnitMetaItem($sName, $sContent, $aAttrs);
  121. }
  122. protected function _getMenuItemNl($aItem)
  123. {
  124. return $this->getUnitMetaItemNl();
  125. }
  126. protected function _getMenuItemDefault($aItem)
  127. {
  128. $sResult = '';
  129. if(!empty($aItem['link']))
  130. $sResult = $this->getUnitMetaItemLink($aItem['title'], array(
  131. 'href' => $aItem['link']
  132. ));
  133. else if(!empty($aItem['onclick']))
  134. $sResult = $this->getUnitMetaItemButtonSmall($aItem['title'], array(
  135. 'onclick' => $aItem['onclick']
  136. ));
  137. else
  138. $sResult = $this->getUnitMetaItemText($aItem['title']);
  139. return $sResult;
  140. }
  141. protected function _getMenuItemAPI($aItem, $mixedType = 'text', $aData = [])
  142. {
  143. $sDisplayType = 'link';
  144. $sContentType = 'text';
  145. if(is_array($mixedType)) {
  146. if(isset($mixedType['display']))
  147. $sDisplayType = $mixedType['display'];
  148. if(isset($mixedType['content']))
  149. $sContentType = $mixedType['content'];
  150. }
  151. else
  152. $sContentType = $mixedType;
  153. $aItemData = array_merge([
  154. 'id' => $aItem['id'],
  155. 'name' => $aItem['name'],
  156. 'display_type' => $sDisplayType,
  157. 'content_type' => $sContentType,
  158. 'title' => $aItem['title'],
  159. 'link' => !empty($aItem['link']) && $aItem['link'] != 'javascript:void(0)' ? $aItem['link'] : '',
  160. 'primary' => !empty($aItem['primary']),
  161. ], $aData);
  162. return $aItemData;
  163. }
  164. protected function _getMenuItemElementAPI($aItem, $aElement)
  165. {
  166. return [
  167. 'id' => $aItem['id'],
  168. 'name' => $aItem['name'],
  169. 'display_type' => 'element',
  170. 'data' => $aElement
  171. ];
  172. }
  173. protected function _buttonsCounterReset()
  174. {
  175. if($this->_iButtonsMax === false)
  176. return;
  177. $this->_iButtons = 0;
  178. }
  179. protected function _buttonsCounterIncrement()
  180. {
  181. if($this->_iButtonsMax === false)
  182. return;
  183. $this->_iButtons++;
  184. }
  185. protected function _buttonsCounterCheck()
  186. {
  187. if($this->_iButtonsMax === false)
  188. return true;
  189. return $this->_iButtons < $this->_iButtonsMax;
  190. }
  191. protected function _isVisibleInContext($aItem)
  192. {
  193. $sDiv = ',';
  194. if(empty($this->_sContext) && empty($aItem['hidden_on_cxt']))
  195. return true;
  196. //--- Hide everywhere.
  197. if($aItem['hidden_on_cxt'] == 'all')
  198. return false;
  199. //--- Hide everywhere except following list.
  200. if(substr($aItem['hidden_on_cxt'], 0, 4) == 'all!') {
  201. $aContexts = explode($sDiv, substr($aItem['hidden_on_cxt'], 4));
  202. return in_array($this->_sContext, $aContexts);
  203. }
  204. $aContexts = explode($sDiv, $aItem['hidden_on_cxt']);
  205. return !in_array($this->_sContext, $aContexts);
  206. }
  207. }
  208. /** @} */