1
0

BxBaseLabel.php 11 KB

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