1
0

BxBaseStudioGrid.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 UnaView UNA Studio Representation classes
  7. * @ingroup UnaStudio
  8. * @{
  9. */
  10. class BxBaseStudioGrid extends BxDolStudioGrid
  11. {
  12. function __construct($aOptions, $oTemplate = false)
  13. {
  14. parent::__construct($aOptions, $oTemplate);
  15. }
  16. /**
  17. * Studio grid action glyphs are Lucide: a font-icon name from the action's data is drawn by the Lucide iconset (through its
  18. * Font Awesome map, Studio's own picks first); an icon Lucide lacks keeps the font glyph.
  19. */
  20. protected $_aActionIcons = ['pencil-alt' => 'square-pen'];
  21. protected function _getActionDefault ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  22. {
  23. if(!$this->_bIsApi && !empty($a['icon']) && preg_match('/^[a-z0-9-]+$/i', $a['icon'])) {
  24. $sName = isset($this->_aActionIcons[$a['icon']]) ? $this->_aActionIcons[$a['icon']] : $a['icon'];
  25. if(($sSvg = $this->_getIconLucide($sName)) !== false)
  26. $a['icon'] = $sSvg;
  27. }
  28. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  29. }
  30. /**
  31. * The row drag handle is a Lucide grip in every Studio grid.
  32. */
  33. protected function _getCellOrder ($mixedValue, $sKey, $aField, $aRow)
  34. {
  35. if($this->_bIsApi || ($sSvg = $this->_getIconLucide('grip-vertical')) === false)
  36. return parent::_getCellOrder($mixedValue, $sKey, $aField, $aRow);
  37. $sAttr = $this->_convertAttrs(
  38. $aField, 'attr_cell',
  39. 'bx-def-padding-sec-bottom bx-def-padding-sec-top',
  40. isset($aField['width']) ? 'width:' . $aField['width'] : false
  41. );
  42. return '<td ' . $sAttr . '><div id="' . $this->_sObject . '_cell_' . $aRow[$this->_aOptions['field_id']] . '" class="bx-grid-drag-handle">' . $sSvg . '</div></td>';
  43. }
  44. /**
  45. * Inline Lucide SVG for a Lucide or Font Awesome icon name, or false when Lucide has no such icon.
  46. */
  47. protected function _getIconLucide($sName, $aAttrs = [])
  48. {
  49. if(empty($sName) || !preg_match('/^[a-z0-9-]+$/i', $sName) || !($oIconset = BxDolIconset::getObjectInstance('sys_lucide')))
  50. return false;
  51. return $oIconset->getIconHtml($sName, $aAttrs);
  52. }
  53. function getJsObject()
  54. {
  55. return '';
  56. }
  57. public function getModulesSelectOneArray($sGetItemsMethod, $bShowCustom = true, $bShowSystem = true)
  58. {
  59. if(empty($sGetItemsMethod))
  60. return '';
  61. $aInputModules = array(
  62. 'type' => 'select',
  63. 'name' => 'module',
  64. 'attrs' => array(
  65. 'id' => 'bx-grid-module-' . $this->_sObject,
  66. 'aria-label' => _t('_adm_grid_lbl_module'),
  67. 'onChange' => 'javascript:' . $this->getJsObject() . '.onChangeModule()'
  68. ),
  69. 'value' => $this->sModule,
  70. 'values' => $this->getModules($bShowCustom, $bShowSystem)
  71. );
  72. $aCounter = array();
  73. $this->oDb->$sGetItemsMethod(array('type' => 'counter_by_modules'), $aCounter, false);
  74. foreach($aInputModules['values'] as $sKey => $sValue)
  75. $aInputModules['values'][$sKey] = $aInputModules['values'][$sKey] . " (" . (isset($aCounter[$sKey]) ? $aCounter[$sKey] : "0") . ")";
  76. return $aInputModules;
  77. }
  78. public function getModulesSelectOne($sGetItemsMethod, $bShowCustom = true, $bShowSystem = true)
  79. {
  80. $aInputModules = $this->getModulesSelectOneArray($sGetItemsMethod, $bShowCustom, $bShowSystem);
  81. $aInputModules['values'] = array_merge(array('' => _t('_adm_txt_select_module')), $aInputModules['values']);
  82. $oForm = new BxTemplStudioFormView(array());
  83. return $oForm->genRow($aInputModules);
  84. }
  85. public function getSearchInput()
  86. {
  87. return parent::_getSearchInput();
  88. }
  89. protected function _getFilterOnChange()
  90. {
  91. return $this->getJsObject() . '.onChangeFilter()';
  92. }
  93. protected function _getItem($sDbMethod = '')
  94. {
  95. $aIds = bx_get('ids');
  96. if(!$aIds || !is_array($aIds)) {
  97. $iId = (int)bx_get('id');
  98. if(!$iId)
  99. return false;
  100. $aIds = array($iId);
  101. }
  102. $iId = $aIds[0];
  103. $aItem = array();
  104. $this->oDb->$sDbMethod(array('type' => 'by_id', 'value' => $iId), $aItem, false);
  105. if(!is_array($aItem) || empty($aItem))
  106. return false;
  107. return $aItem;
  108. }
  109. protected function _getIconPreview($iId, $sIconImage = '', $sIcon = '')
  110. {
  111. $bIconImage = !empty($sIconImage);
  112. $aIcons = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon($sIcon);
  113. $sIconHtml = $aIcons[2] . $aIcons[3] . $aIcons[4];
  114. $bIconHtml = !empty($sIconHtml) && !$bIconImage;
  115. return $this->_oTemplate->parseHtmlByName('item_icon_preview.html', [
  116. 'id' => $iId,
  117. 'bx_if:show_icon_empty' => [
  118. 'condition' => !$bIconImage && !$bIconHtml,
  119. 'content' => []
  120. ],
  121. 'bx_if:show_icon_image' => [
  122. 'condition' => $bIconImage,
  123. 'content' => [
  124. 'js_object' => $this->getJsObject(),
  125. 'url' => $sIconImage,
  126. 'id' => $iId
  127. ]
  128. ],
  129. 'bx_if:show_icon_html' => [
  130. 'condition' => $bIconHtml,
  131. 'content' => [
  132. 'icon' => $sIconHtml
  133. ]
  134. ]
  135. ]);
  136. }
  137. protected function _getIds()
  138. {
  139. if(($aIds = bx_get('ids')) && is_array($aIds))
  140. return reset($aIds);
  141. if(($iId = bx_get('id')) !== false)
  142. return (int)$iId;
  143. return false;
  144. }
  145. }
  146. /** @} */