BxBaseLabel.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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', [
  100. 'class' => '',
  101. 'bx_if:show_icon' => [
  102. 'condition' => false,
  103. 'content' => []
  104. ],
  105. 'label' => $sLabel,
  106. 'bx_if:show_input' => [
  107. 'condition' => true,
  108. 'content' => [
  109. 'label' => $sLabel,
  110. 'field' => $sField,
  111. ]
  112. ]
  113. ]);
  114. }
  115. public function getLabelPlaceholder($sPlaceholder)
  116. {
  117. return $this->_oTemplate->parseHtmlByName('label_select_field_item.html', [
  118. 'class' => 'val-placeholder',
  119. 'bx_if:show_icon' => [
  120. 'condition' => true,
  121. 'content' => []
  122. ],
  123. 'label' => _t($sPlaceholder),
  124. 'bx_if:show_input' => [
  125. 'condition' => false,
  126. 'content' => []
  127. ]
  128. ]);
  129. }
  130. /**
  131. * Get 'Browse Labels' block.
  132. */
  133. public function getLabelsBrowse($aParams = array())
  134. {
  135. $bShowEmpty = isset($aParams['show_empty']) ? (bool)$aParams['show_empty'] : false;
  136. $iParentId = $iLevel = 0;
  137. $sContent = $this->_getLabelsBrowse($iParentId, $iLevel);
  138. if(empty($sContent))
  139. return $bShowEmpty ? MsgBox(_t('_Empty')) : '';
  140. return $this->_oTemplate->parseHtmlByName('label_browse.html', array(
  141. 'js_code' => $this->getJsCodeView(),
  142. 'content' => $sContent
  143. ));
  144. }
  145. protected function _getLabelsBrowse($iParentId, $iLevel)
  146. {
  147. $aLabels = $this->getLabels(array('type' => 'parent', 'parent' => $iParentId));
  148. if(empty($aLabels) || !is_array($aLabels))
  149. return '';
  150. $sJsObject = $this->getJsObjectName();
  151. $aTmplVarsLabels = array();
  152. foreach($aLabels as $aLabel) {
  153. $sSublist = $this->_getLabelsBrowse($aLabel['id'], $iLevel + 1);
  154. $bSublist = !empty($sSublist);
  155. $aTmplVarsLabels[] = array(
  156. 'href' => $this->getLabelUrl($aLabel['value']),
  157. 'title' => $aLabel['value'],
  158. 'title_attr' => bx_html_attribute($aLabel['value']),
  159. 'bx_if:show_sublist_link' => array(
  160. 'condition' => $bSublist,
  161. 'content' => array(
  162. 'js_object' => $sJsObject,
  163. )
  164. ),
  165. 'bx_if:show_sublist' => array(
  166. 'condition' => $bSublist,
  167. 'content' => array(
  168. 'js_object' => $sJsObject,
  169. 'content' => $sSublist
  170. ),
  171. )
  172. );
  173. }
  174. return $this->_oTemplate->parseHtmlByName('label_browse_level.html', array(
  175. 'level' => $iLevel,
  176. 'bx_repeat:labels' => $aTmplVarsLabels
  177. ));
  178. }
  179. /**
  180. * Get 'List' custom form element.
  181. */
  182. public function getLabelsList(&$aInput, &$oForm)
  183. {
  184. $bOpened = false;
  185. $iParentId = $iLevel = 0;
  186. $sContent = $this->_getLabelsList($iParentId, $iLevel, $bOpened, $aInput, $oForm);
  187. if(empty($sContent))
  188. $sContent = MsgBox(_t('_Empty'));
  189. return $this->_oTemplate->parseHtmlByName('label_select_list.html', array(
  190. 'content' => $sContent
  191. ));
  192. }
  193. protected function _getLabelsList($iParentId, $iLevel, &$bOpened, &$aInput, &$oForm)
  194. {
  195. $aLabels = $this->getLabels(array('type' => 'parent', 'parent' => $iParentId));
  196. if(empty($aLabels) || !is_array($aLabels))
  197. return '';
  198. $sJsObject = $this->getJsObjectName();
  199. $bInputValue = !empty($aInput['value']) && is_array($aInput['value']);
  200. $aCheckbox = $aInput;
  201. $aCheckbox['type'] = 'checkbox';
  202. $aCheckbox['name'] .= '[]';
  203. $aTmplVarsLabels = array();
  204. foreach($aLabels as $aLabel) {
  205. $sHtmlId = 'sys_label_' . $aLabel['value'];
  206. $bChecked = $bInputValue && in_array($aLabel['value'], $aInput['value']);
  207. if($bChecked)
  208. $bOpened = true;
  209. $aCheckbox['value'] = $aLabel['value'];
  210. $aCheckbox['checked'] = $bChecked ? 1 : 0;
  211. $aCheckbox['attrs']['id'] = $sHtmlId;
  212. $bSublistOpened = false;
  213. $sSublist = $this->_getLabelsList($aLabel['id'], $iLevel + 1, $bSublistOpened, $aInput, $oForm);
  214. $bSublist = !empty($sSublist);
  215. if($bSublistOpened)
  216. $bOpened = true;
  217. $aTmplVarsLabels[] = array(
  218. 'checkbox' => $oForm->genInputCheckbox($aCheckbox, true),
  219. 'html_id_label' => $sHtmlId,
  220. 'title' => $aLabel['value'],
  221. 'title_attr' => bx_html_attribute($aLabel['value']),
  222. 'bx_if:show_sublist_link' => array(
  223. 'condition' => $bSublist,
  224. 'content' => array(
  225. 'js_object' => $sJsObject,
  226. 'bx_if:show_class_opened' => array(
  227. 'condition' => $bSublistOpened,
  228. 'content' => array()
  229. ),
  230. 'bx_if:show_class_closed' => array(
  231. 'condition' => !$bSublistOpened,
  232. 'content' => array()
  233. )
  234. )
  235. ),
  236. 'bx_if:show_sublist' => array(
  237. 'condition' => $bSublist,
  238. 'content' => array(
  239. 'js_object' => $sJsObject,
  240. 'bx_if:show_class_opened' => array(
  241. 'condition' => $bSublistOpened,
  242. 'content' => array()
  243. ),
  244. 'content' => $sSublist
  245. ),
  246. )
  247. );
  248. }
  249. return $this->_oTemplate->parseHtmlByName('label_select_list_level.html', array(
  250. 'level' => $iLevel,
  251. 'bx_repeat:labels' => $aTmplVarsLabels
  252. ));
  253. }
  254. /**
  255. * Get "Contexts' List" custom form element.
  256. */
  257. public function getLabelsListContext(&$aInput, &$oForm)
  258. {
  259. $iProfileId = bx_get_logged_profile_id();
  260. $sJsObject = $this->getJsObjectName();
  261. $bInputValue = !empty($aInput['value']) && is_array($aInput['value']);
  262. $aCheckbox = $aInput;
  263. $aCheckbox['type'] = 'checkbox';
  264. $aCheckbox['name'] .= '[]';
  265. $aLabels = $this->getLabelsContext($iProfileId);
  266. $aTmplVarsLabels = [];
  267. foreach($aLabels as $aLabel) {
  268. $sLabelValue = $aLabel['value'];
  269. $sHtmlId = 'sys_label_' . $sLabelValue;
  270. $bChecked = $bInputValue && in_array($sLabelValue, $aInput['value']);
  271. $aCheckbox['value'] = $sLabelValue;
  272. $aCheckbox['checked'] = $bChecked ? 1 : 0;
  273. $aCheckbox['attrs']['id'] = $sHtmlId;
  274. $aTmplVarsLabels[] = [
  275. 'checkbox' => $oForm->genInput($aCheckbox),
  276. 'html_id_label' => $sHtmlId,
  277. 'title' => $sLabelValue,
  278. 'title_attr' => bx_html_attribute($sLabelValue),
  279. 'bx_if:show_sublist_link' => [
  280. 'condition' => false,
  281. 'content' => []
  282. ],
  283. 'bx_if:show_sublist' => [
  284. 'condition' => false,
  285. 'content' => [],
  286. ]
  287. ];
  288. }
  289. $sContent = $this->_oTemplate->parseHtmlByName('label_select_list_level.html', [
  290. 'level' => 0,
  291. 'bx_repeat:labels' => $aTmplVarsLabels
  292. ]);
  293. if(empty($sContent))
  294. $sContent = MsgBox(_t('_Empty'));
  295. return $this->_oTemplate->parseHtmlByName('label_select_list.html', [
  296. 'content' => $sContent
  297. ]);
  298. }
  299. public function addCssJs($sType, $bDynamicMode = false)
  300. {
  301. if(!$bDynamicMode && bx_is_dynamic_request())
  302. $bDynamicMode = true;
  303. $sInclude = '';
  304. $bTypeForm = $sType == 'form';
  305. if(!$bDynamicMode) {
  306. if($bTypeForm)
  307. $this->_oTemplate->addJs(array(
  308. 'jquery-ui/jquery-ui.min.js',
  309. ));
  310. $this->_oTemplate->addJs(array(
  311. 'BxDolLabel.js'
  312. ));
  313. }
  314. if($bTypeForm)
  315. $sInclude .= $this->_oTemplate->addCss(array(
  316. 'forms.css',
  317. ), $bDynamicMode);
  318. $sInclude .= $this->_oTemplate->addCss(array(
  319. 'label.css'
  320. ), $bDynamicMode);
  321. return $bDynamicMode ? $sInclude : '';
  322. }
  323. }
  324. /** @} */