BxBaseLabel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. * Labels objects representation.
  11. *
  12. * @see BxDolLabel
  13. */
  14. class BxBaseLabel extends BxDolLabel
  15. {
  16. protected $_oTemplate;
  17. protected $_sJsObjClass;
  18. protected $_sJsObjName;
  19. protected $_aHtmlIds;
  20. public function __construct($oTemplate = false)
  21. {
  22. parent::__construct();
  23. if($oTemplate)
  24. $this->_oTemplate = $oTemplate;
  25. else
  26. $this->_oTemplate = BxDolTemplate::getInstance();
  27. $this->_sJsObjClass = 'BxDolLabel';
  28. $this->_sJsObjName = 'oBxDolLabel';
  29. $this->_aHtmlIds = array(
  30. 'labels_element' => 'sys-labels-',
  31. 'labels_select_popup' => 'sys-labels-select-popup',
  32. );
  33. }
  34. public function getJsObjectName()
  35. {
  36. return $this->_sJsObjName;
  37. }
  38. public function getJsCodeForm($bDynamicMode = false)
  39. {
  40. return $this->getJsCode('form', $bDynamicMode);
  41. }
  42. public function getJsCodeView($bDynamicMode = false)
  43. {
  44. return $this->getJsCode('view', $bDynamicMode);
  45. }
  46. public function getJsCode($sType, $bDynamicMode = false)
  47. {
  48. if(!$bDynamicMode && bx_is_dynamic_request())
  49. $bDynamicMode = true;
  50. $sCodeObject = $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode(array(
  51. 'sObjName' => $this->_sJsObjName,
  52. 'sRootUrl' => BX_DOL_URL_ROOT,
  53. 'aHtmlIds' => $this->_aHtmlIds
  54. )) . ");";
  55. if($bDynamicMode) {
  56. $sCode = "var " . $this->_sJsObjName . " = null;";
  57. if($sType == 'form')
  58. $sCode .= "if(typeof(jQuery.ui.position) == 'undefined')
  59. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('jquery-ui/jquery-ui.min.js'), BX_ESCAPE_STR_APOS) . "');";
  60. $sCode .= "if(window['" . $this->_sJsObjName . "'] === null)
  61. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('BxDolLabel.js'), BX_ESCAPE_STR_APOS) . "', function(data, textStatus, jqxhr) {
  62. " . $sCodeObject . "
  63. });";
  64. }
  65. else
  66. $sCode = "if(window['" . $this->_sJsObjName . "'] == undefined) var " . $sCodeObject;
  67. return $this->addCssJs($sType, $bDynamicMode) . $this->_oTemplate->_wrapInTagJsCode($sCode);
  68. }
  69. protected function selectLabels($aValues = array())
  70. {
  71. $sJsObject = $this->getJsObjectName();
  72. $oForm = BxDolForm::getObjectInstance($this->_sForm, $this->_sFormDisplaySelect);
  73. $oForm->initChecker($aValues);
  74. if($oForm->isSubmittedAndValid()) {
  75. $sName = $oForm->getCleanValue('name');
  76. $aLabels = array();
  77. if(($aLabelsSearch = $oForm->getCleanValue('search')) !== false)
  78. $aLabels = array_merge($aLabels, $aLabelsSearch);
  79. if(($aLabelsList = $oForm->getCleanValue('list')) !== false)
  80. $aLabels = array_merge($aLabels, $aLabelsList);
  81. $aLabels = array_unique($aLabels);
  82. $sLabels = '';
  83. if(!empty($aLabels) && is_array($aLabels))
  84. foreach($aLabels as $sLabel)
  85. $sLabels .= $this->getLabel($sName, $sLabel);
  86. return array('eval' => $sJsObject . '.onSelectLabels(oData);', 'name' => $sName, 'content' => $sLabels);
  87. }
  88. $sContent = BxTemplFunctions::getInstance()->transBox($this->_aHtmlIds['labels_select_popup'], $this->_oTemplate->parseHtmlByName('label_select_popup.html', array(
  89. 'js_object' => $sJsObject,
  90. 'form' => $oForm->getCode(),
  91. 'form_id' => $oForm->getId()
  92. )));
  93. return array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false)));
  94. }
  95. public function getLabel($sField, $sLabel)
  96. {
  97. return $this->_oTemplate->parseHtmlByName('label_select_field_item.html', array(
  98. 'class' => '',
  99. 'label' => $sLabel,
  100. 'bx_if:show_input' => array(
  101. 'condition' => true,
  102. 'content' => array(
  103. 'label' => $sLabel,
  104. 'field' => $sField,
  105. )
  106. )
  107. ));
  108. }
  109. public function getLabelPlaceholder($sPlaceholder)
  110. {
  111. return $this->_oTemplate->parseHtmlByName('label_select_field_item.html', array(
  112. 'class' => 'val-placeholder',
  113. 'label' => _t($sPlaceholder),
  114. 'bx_if:show_input' => array(
  115. 'condition' => false,
  116. 'content' => array()
  117. )
  118. ));
  119. }
  120. /**
  121. * Get 'Browse Labels' block.
  122. */
  123. public function getLabelsBrowse($aParams = array())
  124. {
  125. $bShowEmpty = isset($aParams['show_empty']) ? (bool)$aParams['show_empty'] : false;
  126. $iParentId = $iLevel = 0;
  127. $sContent = $this->_getLabelsBrowse($iParentId, $iLevel);
  128. if(empty($sContent))
  129. return $bShowEmpty ? MsgBox(_t('_Empty')) : '';
  130. return $this->_oTemplate->parseHtmlByName('label_browse.html', array(
  131. 'js_code' => $this->getJsCodeView(),
  132. 'content' => $sContent
  133. ));
  134. }
  135. protected function _getLabelsBrowse($iParentId, $iLevel)
  136. {
  137. $aLabels = $this->getLabels(array('type' => 'parent', 'parent' => $iParentId));
  138. if(empty($aLabels) || !is_array($aLabels))
  139. return '';
  140. $sJsObject = $this->getJsObjectName();
  141. $aTmplVarsLabels = array();
  142. foreach($aLabels as $aLabel) {
  143. $sSublist = $this->_getLabelsBrowse($aLabel['id'], $iLevel + 1);
  144. $bSublist = !empty($sSublist);
  145. $aTmplVarsLabels[] = array(
  146. 'href' => $this->getLabelUrl($aLabel['value']),
  147. 'title' => $aLabel['value'],
  148. 'title_attr' => bx_html_attribute($aLabel['value']),
  149. 'bx_if:show_sublist_link' => array(
  150. 'condition' => $bSublist,
  151. 'content' => array(
  152. 'js_object' => $sJsObject,
  153. )
  154. ),
  155. 'bx_if:show_sublist' => array(
  156. 'condition' => $bSublist,
  157. 'content' => array(
  158. 'js_object' => $sJsObject,
  159. 'content' => $sSublist
  160. ),
  161. )
  162. );
  163. }
  164. return $this->_oTemplate->parseHtmlByName('label_browse_level.html', array(
  165. 'level' => $iLevel,
  166. 'bx_repeat:labels' => $aTmplVarsLabels
  167. ));
  168. }
  169. /**
  170. * Get 'List' custom form element.
  171. */
  172. public function getLabelsList(&$aInput, &$oForm)
  173. {
  174. $bOpened = false;
  175. $iParentId = $iLevel = 0;
  176. $sContent = $this->_getLabelsList($iParentId, $iLevel, $bOpened, $aInput, $oForm);
  177. if(empty($sContent))
  178. $sContent = MsgBox(_t('_Empty'));
  179. return $this->_oTemplate->parseHtmlByName('label_select_list.html', array(
  180. 'content' => $sContent
  181. ));
  182. }
  183. protected function _getLabelsList($iParentId, $iLevel, &$bOpened, &$aInput, &$oForm)
  184. {
  185. $aLabels = $this->getLabels(array('type' => 'parent', 'parent' => $iParentId));
  186. if(empty($aLabels) || !is_array($aLabels))
  187. return '';
  188. $sJsObject = $this->getJsObjectName();
  189. $bInputValue = !empty($aInput['value']) && is_array($aInput['value']);
  190. $aCheckbox = $aInput;
  191. $aCheckbox['type'] = 'checkbox';
  192. $aCheckbox['name'] .= '[]';
  193. $aTmplVarsLabels = array();
  194. foreach($aLabels as $aLabel) {
  195. $sHtmlId = 'sys_label_' . $aLabel['value'];
  196. $bChecked = $bInputValue && in_array($aLabel['value'], $aInput['value']);
  197. if($bChecked)
  198. $bOpened = true;
  199. $aCheckbox['value'] = $aLabel['value'];
  200. $aCheckbox['checked'] = $bChecked ? 1 : 0;
  201. $aCheckbox['attrs']['id'] = $sHtmlId;
  202. $bSublistOpened = false;
  203. $sSublist = $this->_getLabelsList($aLabel['id'], $iLevel + 1, $bSublistOpened, $aInput, $oForm);
  204. $bSublist = !empty($sSublist);
  205. if($bSublistOpened)
  206. $bOpened = true;
  207. $aTmplVarsLabels[] = array(
  208. 'checkbox' => $oForm->genInput($aCheckbox),
  209. 'html_id_label' => $sHtmlId,
  210. 'title' => $aLabel['value'],
  211. 'title_attr' => bx_html_attribute($aLabel['value']),
  212. 'bx_if:show_sublist_link' => array(
  213. 'condition' => $bSublist,
  214. 'content' => array(
  215. 'js_object' => $sJsObject,
  216. 'bx_if:show_class_opened' => array(
  217. 'condition' => $bSublistOpened,
  218. 'content' => array()
  219. ),
  220. 'bx_if:show_class_closed' => array(
  221. 'condition' => !$bSublistOpened,
  222. 'content' => array()
  223. )
  224. )
  225. ),
  226. 'bx_if:show_sublist' => array(
  227. 'condition' => $bSublist,
  228. 'content' => array(
  229. 'js_object' => $sJsObject,
  230. 'bx_if:show_class_opened' => array(
  231. 'condition' => $bSublistOpened,
  232. 'content' => array()
  233. ),
  234. 'content' => $sSublist
  235. ),
  236. )
  237. );
  238. }
  239. return $this->_oTemplate->parseHtmlByName('label_select_list_level.html', array(
  240. 'level' => $iLevel,
  241. 'bx_repeat:labels' => $aTmplVarsLabels
  242. ));
  243. }
  244. public function addCssJs($sType, $bDynamicMode = false)
  245. {
  246. if(!$bDynamicMode && bx_is_dynamic_request())
  247. $bDynamicMode = true;
  248. $sInclude = '';
  249. $bTypeForm = $sType == 'form';
  250. if(!$bDynamicMode) {
  251. if($bTypeForm)
  252. $this->_oTemplate->addJs(array(
  253. 'jquery-ui/jquery-ui.min.js',
  254. ));
  255. $this->_oTemplate->addJs(array(
  256. 'BxDolLabel.js'
  257. ));
  258. }
  259. if($bTypeForm)
  260. $sInclude .= $this->_oTemplate->addCss(array(
  261. 'forms.css',
  262. ), $bDynamicMode);
  263. $sInclude .= $this->_oTemplate->addCss(array(
  264. 'label.css'
  265. ), $bDynamicMode);
  266. return $bDynamicMode ? $sInclude : '';
  267. }
  268. }
  269. /** @} */