BxBaseLabel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. return array('eval' => $sJsObject . '.onSelectLabels(oData);', 'content' => $this->getElementLabels(array(
  88. 'name' => $sName,
  89. 'value' => $aLabels,
  90. )));
  91. }
  92. $sContent = BxTemplFunctions::getInstance()->transBox($this->_aHtmlIds['labels_select_popup'], $this->_oTemplate->parseHtmlByName('label_select_popup.html', array(
  93. 'js_object' => $sJsObject,
  94. 'form' => $oForm->getCode(),
  95. 'form_id' => $oForm->getId()
  96. )));
  97. return array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false)));
  98. }
  99. public function getLabel($sField, $sLabel)
  100. {
  101. return $this->_oTemplate->parseHtmlByName('label_select_field_item.html', array(
  102. 'field' => $sField,
  103. 'label' => $sLabel
  104. ));
  105. }
  106. /**
  107. * Get 'Browse Labels' block.
  108. */
  109. public function getLabelsBrowse($aParams = array())
  110. {
  111. $bShowEmpty = isset($aParams['show_empty']) ? (bool)$aParams['show_empty'] : false;
  112. $iParentId = $iLevel = 0;
  113. $sContent = $this->_getLabelsBrowse($iParentId, $iLevel);
  114. if(empty($sContent))
  115. return $bShowEmpty ? MsgBox(_t('_Empty')) : '';
  116. return $this->_oTemplate->parseHtmlByName('label_browse.html', array(
  117. 'js_code' => $this->getJsCodeView(),
  118. 'content' => $sContent
  119. ));
  120. }
  121. protected function _getLabelsBrowse($iParentId, $iLevel)
  122. {
  123. $aLabels = $this->getLabels(array('type' => 'parent', 'parent' => $iParentId));
  124. if(empty($aLabels) || !is_array($aLabels))
  125. return '';
  126. $sJsObject = $this->getJsObjectName();
  127. $aTmplVarsLabels = array();
  128. foreach($aLabels as $aLabel) {
  129. $sSublist = $this->_getLabelsBrowse($aLabel['id'], $iLevel + 1);
  130. $bSublist = !empty($sSublist);
  131. $aTmplVarsLabels[] = array(
  132. 'href' => $this->getLabelUrl($aLabel['value']),
  133. 'title' => $aLabel['value'],
  134. 'title_attr' => bx_html_attribute($aLabel['value']),
  135. 'bx_if:show_sublist_link' => array(
  136. 'condition' => $bSublist,
  137. 'content' => array(
  138. 'js_object' => $sJsObject,
  139. )
  140. ),
  141. 'bx_if:show_sublist' => array(
  142. 'condition' => $bSublist,
  143. 'content' => array(
  144. 'js_object' => $sJsObject,
  145. 'content' => $sSublist
  146. ),
  147. )
  148. );
  149. }
  150. return $this->_oTemplate->parseHtmlByName('label_browse_level.html', array(
  151. 'level' => $iLevel,
  152. 'bx_repeat:labels' => $aTmplVarsLabels
  153. ));
  154. }
  155. /**
  156. * Get 'List' custom form element.
  157. */
  158. public function getLabelsList(&$aInput, &$oForm)
  159. {
  160. $bOpened = false;
  161. $iParentId = $iLevel = 0;
  162. $sContent = $this->_getLabelsList($iParentId, $iLevel, $bOpened, $aInput, $oForm);
  163. if(empty($sContent))
  164. $sContent = MsgBox(_t('_Empty'));
  165. return $this->_oTemplate->parseHtmlByName('label_select_list.html', array(
  166. 'content' => $sContent
  167. ));
  168. }
  169. protected function _getLabelsList($iParentId, $iLevel, &$bOpened, &$aInput, &$oForm)
  170. {
  171. $aLabels = $this->getLabels(array('type' => 'parent', 'parent' => $iParentId));
  172. if(empty($aLabels) || !is_array($aLabels))
  173. return '';
  174. $sJsObject = $this->getJsObjectName();
  175. $bInputValue = !empty($aInput['value']) && is_array($aInput['value']);
  176. $aCheckbox = $aInput;
  177. $aCheckbox['type'] = 'checkbox';
  178. $aCheckbox['name'] .= '[]';
  179. $aTmplVarsLabels = array();
  180. foreach($aLabels as $aLabel) {
  181. $sHtmlId = 'sys_label_' . $aLabel['value'];
  182. $bChecked = $bInputValue && in_array($aLabel['value'], $aInput['value']);
  183. if($bChecked)
  184. $bOpened = true;
  185. $aCheckbox['value'] = $aLabel['value'];
  186. $aCheckbox['checked'] = $bChecked ? 1 : 0;
  187. $aCheckbox['attrs']['id'] = $sHtmlId;
  188. $bSublistOpened = false;
  189. $sSublist = $this->_getLabelsList($aLabel['id'], $iLevel + 1, $bSublistOpened, $aInput, $oForm);
  190. $bSublist = !empty($sSublist);
  191. if($bSublistOpened)
  192. $bOpened = true;
  193. $aTmplVarsLabels[] = array(
  194. 'checkbox' => $oForm->genInput($aCheckbox),
  195. 'html_id_label' => $sHtmlId,
  196. 'title' => $aLabel['value'],
  197. 'title_attr' => bx_html_attribute($aLabel['value']),
  198. 'bx_if:show_sublist_link' => array(
  199. 'condition' => $bSublist,
  200. 'content' => array(
  201. 'js_object' => $sJsObject,
  202. 'bx_if:show_class_opened' => array(
  203. 'condition' => $bSublistOpened,
  204. 'content' => array()
  205. ),
  206. 'bx_if:show_class_closed' => array(
  207. 'condition' => !$bSublistOpened,
  208. 'content' => array()
  209. )
  210. )
  211. ),
  212. 'bx_if:show_sublist' => 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. 'content' => $sSublist
  221. ),
  222. )
  223. );
  224. }
  225. return $this->_oTemplate->parseHtmlByName('label_select_list_level.html', array(
  226. 'level' => $iLevel,
  227. 'bx_repeat:labels' => $aTmplVarsLabels
  228. ));
  229. }
  230. public function addCssJs($sType, $bDynamicMode = false)
  231. {
  232. if(!$bDynamicMode && bx_is_dynamic_request())
  233. $bDynamicMode = true;
  234. $sInclude = '';
  235. $bTypeForm = $sType == 'form';
  236. if(!$bDynamicMode) {
  237. if($bTypeForm)
  238. $this->_oTemplate->addJs(array(
  239. 'jquery-ui/jquery.ui.core.min.js',
  240. 'jquery-ui/jquery.ui.widget.min.js',
  241. 'jquery-ui/jquery.ui.menu.min.js',
  242. 'jquery-ui/jquery.ui.autocomplete.min.js'
  243. ));
  244. $this->_oTemplate->addJs(array(
  245. 'BxDolLabel.js'
  246. ));
  247. }
  248. if($bTypeForm)
  249. $sInclude .= $this->_oTemplate->addCss(array(
  250. 'forms.css',
  251. ), $bDynamicMode);
  252. $sInclude .= $this->_oTemplate->addCss(array(
  253. 'label.css'
  254. ), $bDynamicMode);
  255. return $bDynamicMode ? $sInclude : '';
  256. }
  257. }
  258. /** @} */