BxBaseLabel.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. 'bx_if:show_sublist_link' => array(
  135. 'condition' => $bSublist,
  136. 'content' => array(
  137. 'js_object' => $sJsObject,
  138. )
  139. ),
  140. 'bx_if:show_sublist' => array(
  141. 'condition' => $bSublist,
  142. 'content' => array(
  143. 'js_object' => $sJsObject,
  144. 'content' => $sSublist
  145. ),
  146. )
  147. );
  148. }
  149. return $this->_oTemplate->parseHtmlByName('label_browse_level.html', array(
  150. 'level' => $iLevel,
  151. 'bx_repeat:labels' => $aTmplVarsLabels
  152. ));
  153. }
  154. /**
  155. * Get 'List' custom form element.
  156. */
  157. public function getLabelsList(&$aInput, &$oForm)
  158. {
  159. $bOpened = false;
  160. $iParentId = $iLevel = 0;
  161. $sContent = $this->_getLabelsList($iParentId, $iLevel, $bOpened, $aInput, $oForm);
  162. if(empty($sContent))
  163. $sContent = MsgBox(_t('_Empty'));
  164. return $this->_oTemplate->parseHtmlByName('label_select_list.html', array(
  165. 'content' => $sContent
  166. ));
  167. }
  168. protected function _getLabelsList($iParentId, $iLevel, &$bOpened, &$aInput, &$oForm)
  169. {
  170. $aLabels = $this->getLabels(array('type' => 'parent', 'parent' => $iParentId));
  171. if(empty($aLabels) || !is_array($aLabels))
  172. return '';
  173. $sJsObject = $this->getJsObjectName();
  174. $bInputValue = !empty($aInput['value']) && is_array($aInput['value']);
  175. $aCheckbox = $aInput;
  176. $aCheckbox['type'] = 'checkbox';
  177. $aCheckbox['name'] .= '[]';
  178. $aTmplVarsLabels = array();
  179. foreach($aLabels as $aLabel) {
  180. $sHtmlId = 'sys_label_' . $aLabel['value'];
  181. $bChecked = $bInputValue && in_array($aLabel['value'], $aInput['value']);
  182. if($bChecked)
  183. $bOpened = true;
  184. $aCheckbox['value'] = $aLabel['value'];
  185. $aCheckbox['checked'] = $bChecked ? 1 : 0;
  186. $aCheckbox['attrs']['id'] = $sHtmlId;
  187. $bSublistOpened = false;
  188. $sSublist = $this->_getLabelsList($aLabel['id'], $iLevel + 1, $bSublistOpened, $aInput, $oForm);
  189. $bSublist = !empty($sSublist);
  190. if($bSublistOpened)
  191. $bOpened = true;
  192. $aTmplVarsLabels[] = array(
  193. 'checkbox' => $oForm->genInput($aCheckbox),
  194. 'html_id_label' => $sHtmlId,
  195. 'title' => $aLabel['value'],
  196. 'bx_if:show_sublist_link' => array(
  197. 'condition' => $bSublist,
  198. 'content' => array(
  199. 'js_object' => $sJsObject,
  200. 'bx_if:show_class_opened' => array(
  201. 'condition' => $bSublistOpened,
  202. 'content' => array()
  203. ),
  204. 'bx_if:show_class_closed' => array(
  205. 'condition' => !$bSublistOpened,
  206. 'content' => array()
  207. )
  208. )
  209. ),
  210. 'bx_if:show_sublist' => array(
  211. 'condition' => $bSublist,
  212. 'content' => array(
  213. 'js_object' => $sJsObject,
  214. 'bx_if:show_class_opened' => array(
  215. 'condition' => $bSublistOpened,
  216. 'content' => array()
  217. ),
  218. 'content' => $sSublist
  219. ),
  220. )
  221. );
  222. }
  223. return $this->_oTemplate->parseHtmlByName('label_select_list_level.html', array(
  224. 'level' => $iLevel,
  225. 'bx_repeat:labels' => $aTmplVarsLabels
  226. ));
  227. }
  228. public function addCssJs($sType, $bDynamicMode = false)
  229. {
  230. if(!$bDynamicMode && bx_is_dynamic_request())
  231. $bDynamicMode = true;
  232. $sInclude = '';
  233. $bTypeForm = $sType == 'form';
  234. if(!$bDynamicMode) {
  235. if($bTypeForm)
  236. $this->_oTemplate->addJs(array(
  237. 'jquery-ui/jquery.ui.core.min.js',
  238. 'jquery-ui/jquery.ui.widget.min.js',
  239. 'jquery-ui/jquery.ui.menu.min.js',
  240. 'jquery-ui/jquery.ui.autocomplete.min.js'
  241. ));
  242. $this->_oTemplate->addJs(array(
  243. 'BxDolLabel.js'
  244. ));
  245. }
  246. if($bTypeForm)
  247. $sInclude .= $this->_oTemplate->addCss(array(
  248. 'forms.css',
  249. ), $bDynamicMode);
  250. $sInclude .= $this->_oTemplate->addCss(array(
  251. 'label.css'
  252. ), $bDynamicMode);
  253. return $bDynamicMode ? $sInclude : '';
  254. }
  255. }
  256. /** @} */