BxBaseLabel.php 11 KB

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