BxBaseLabel.php 13 KB

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