BxBaseLabel.php 10 KB

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