BxBaseMenuUnitMeta.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 = array(), $sTemplate = 'unit_meta_item.html')
  106. {
  107. if(empty($sContent) && $sName != 'nl')
  108. return '';
  109. if(!is_array($aAttrs))
  110. $aAttrs = array();
  111. $aTags = array('span', 'a', 'button', 'sbutton', 'custom', 'nl', 'extended');
  112. $sTmplVarsClass = '';
  113. if(!empty($aAttrs['class'])) {
  114. $sTmplVarsClass = $aAttrs['class'];
  115. unset($aAttrs['class']);
  116. }
  117. $aTmplVarsAttrs = array();
  118. foreach($aAttrs as $sKey => $sValue)
  119. $aTmplVarsAttrs[] = array('key' => $sKey, 'value' => bx_html_attribute($sValue));
  120. $aTmplVars = array();
  121. foreach($aTags as $sTag) {
  122. $aTmplVarsTag = array();
  123. $bTmplVarsTag = $sTag == $sName;
  124. if($bTmplVarsTag)
  125. $aTmplVarsTag = array(
  126. 'style_prefix' => $this->_sStylePrefix,
  127. 'class' => $sTmplVarsClass,
  128. 'content' => $sContent,
  129. 'bx_repeat:attrs' => $aTmplVarsAttrs
  130. );
  131. $aTmplVars['bx_if:' . $sTag] = array(
  132. 'condition' => $bTmplVarsTag,
  133. 'content' => $aTmplVarsTag
  134. );
  135. }
  136. return $this->_oTemplate->parseHtmlByName($sTemplate, $aTmplVars);
  137. }
  138. public function getUnitMetaItemButtonOrLink($sName, $sContent, $aAttrs = array())
  139. {
  140. if(!isset($aAttrs['href']))
  141. $aAttrs['href'] = 'javascript:void(0)';
  142. return $this->getUnitMetaItem($sName, $sContent, $aAttrs);
  143. }
  144. protected function _getMenuItemNl($aItem)
  145. {
  146. return $this->getUnitMetaItemNl();
  147. }
  148. protected function _getMenuItemDefault($aItem)
  149. {
  150. $sResult = '';
  151. if(!empty($aItem['link']))
  152. $sResult = $this->getUnitMetaItemLink($aItem['title'], array(
  153. 'href' => $aItem['link']
  154. ));
  155. else if(!empty($aItem['onclick']))
  156. $sResult = $this->getUnitMetaItemButtonSmall($aItem['title'], array(
  157. 'onclick' => $aItem['onclick']
  158. ));
  159. else
  160. $sResult = $this->getUnitMetaItemText($aItem['title']);
  161. return $sResult;
  162. }
  163. protected function _getMenuItemAPI($aItem, $mixedType = 'text', $aData = [])
  164. {
  165. $sDisplayType = 'link';
  166. $sContentType = 'text';
  167. if(is_array($mixedType)) {
  168. if(isset($mixedType['display']))
  169. $sDisplayType = $mixedType['display'];
  170. if(isset($mixedType['content']))
  171. $sContentType = $mixedType['content'];
  172. }
  173. else
  174. $sContentType = $mixedType;
  175. $aItemData = array_merge([
  176. 'id' => $aItem['id'],
  177. 'name' => $aItem['name'],
  178. 'display_type' => $sDisplayType,
  179. 'content_type' => $sContentType,
  180. 'title' => $aItem['title'],
  181. 'link' => !empty($aItem['link']) && $aItem['link'] != 'javascript:void(0)' ? $aItem['link'] : ''
  182. ], $aData);
  183. return $aItemData;
  184. }
  185. protected function _buttonsCounterReset()
  186. {
  187. if($this->_iButtonsMax === false)
  188. return;
  189. $this->_iButtons = 0;
  190. }
  191. protected function _buttonsCounterIncrement()
  192. {
  193. if($this->_iButtonsMax === false)
  194. return;
  195. $this->_iButtons++;
  196. }
  197. protected function _buttonsCounterCheck()
  198. {
  199. if($this->_iButtonsMax === false)
  200. return true;
  201. return $this->_iButtons < $this->_iButtonsMax;
  202. }
  203. protected function _isVisibleInContext($aItem)
  204. {
  205. $sDiv = ',';
  206. if(empty($this->_sContext) || empty($aItem['hidden_on_cxt']))
  207. return true;
  208. //--- Hide everywhere.
  209. if($aItem['hidden_on_cxt'] == 'all')
  210. return false;
  211. //--- Hide everywhere except following list.
  212. if(substr($aItem['hidden_on_cxt'], 0, 4) == 'all!') {
  213. $aContexts = explode($sDiv, substr($aItem['hidden_on_cxt'], 4));
  214. return in_array($this->_sContext, $aContexts);
  215. }
  216. $aContexts = explode($sDiv, $aItem['hidden_on_cxt']);
  217. return !in_array($this->_sContext, $aContexts);
  218. }
  219. }
  220. /** @} */