BxBaseGrid.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. * Grid representation.
  11. * @see BxDolGrid
  12. */
  13. class BxBaseGrid extends BxDolGrid
  14. {
  15. protected $_oTemplate;
  16. protected $_aPopupOptions = false;
  17. protected $_aQueryAppend = false;
  18. protected $_aQueryReset = false;
  19. protected $_aConfirmMessages = false;
  20. protected $_bSelectAll = false;
  21. protected $_isDisplayPopupOnTextOverflow = true;
  22. public function __construct ($aOptions, $oTemplate)
  23. {
  24. parent::__construct ($aOptions);
  25. if ($oTemplate)
  26. $this->_oTemplate = $oTemplate;
  27. else
  28. $this->_oTemplate = BxDolTemplate::getInstance();
  29. $this->_aQueryReset = array($this->_aOptions['filter_get'], $this->_aOptions['order_get_field'], $this->_aOptions['order_get_dir'], $this->_aOptions['paginate_get_start'], $this->_aOptions['paginate_get_per_page']);
  30. }
  31. public function performActionDisplay()
  32. {
  33. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  34. echoJson(array('grid' => $this->getCode(false)));
  35. }
  36. public function performActionReorder()
  37. {
  38. $this->_replaceMarkers ();
  39. $aOrder = bx_get($this->_sObject . '_row');
  40. $iOrder = 0;
  41. foreach ($aOrder as $mixedId)
  42. $this->_updateOrder($mixedId, ++$iOrder);
  43. echoJson(array());
  44. }
  45. public function performActionDelete()
  46. {
  47. $this->_replaceMarkers ();
  48. $iAffected = 0;
  49. $aIds = bx_get('ids');
  50. if (!$aIds || !is_array($aIds)) {
  51. echoJson(array());
  52. exit;
  53. }
  54. foreach ($aIds as $mixedId)
  55. $iAffected += $this->_delete($mixedId) ? 1 : 0;
  56. echo echoJson(array_merge(
  57. array(
  58. 'grid' => $this->getCode(false),
  59. ),
  60. $iAffected ? array() : array('msg' => _t("_sys_grid_delete_failed"))
  61. ));
  62. }
  63. public function performActionEnable()
  64. {
  65. $this->_replaceMarkers ();
  66. $aIds = bx_get('ids');
  67. if (!$aIds || !is_array($aIds)) {
  68. echoJson(array());
  69. exit;
  70. }
  71. $aAffectedIds = array ();
  72. foreach ($aIds as $mixedId)
  73. if ($this->_enable($mixedId, (int)bx_get('checked')))
  74. $aAffectedIds[] = $mixedId;
  75. $sAction = (int)bx_get('checked') ? 'enable' : 'disable';
  76. echo echoJson(array($sAction => $aAffectedIds));
  77. }
  78. public function getCode ($isDisplayHeader = true)
  79. {
  80. $this->_replaceMarkers ();
  81. if ($isDisplayHeader && empty($this->_aOptions['paginate_url'])) {
  82. // reset page query params if grid is just initialized and it uses AJAX paginate
  83. $this->resetQueryParams();
  84. }
  85. $sPaginate = '';
  86. $aData = array();
  87. $sIdWrapper = 'bx-grid-wrap-' . $this->_sObject;
  88. $sIdContainer = 'bx-grid-cont-' . $this->_sObject;
  89. $sIdTable = 'bx-grid-table-' . $this->_sObject;
  90. $sFilter = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['filter_get'])));
  91. $sOrderField = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['order_get_field'])));
  92. $sOrderDir = 0 === strcasecmp('desc', bx_get($this->_aOptions['order_get_dir'])) ? 'DESC' : 'ASC';
  93. if ($this->_aOptions['paginate_get_start'])
  94. $iStart = (int)bx_get($this->_aOptions['paginate_get_start']);
  95. else
  96. $iStart = 0;
  97. if ($this->_aOptions['paginate_get_per_page'] && (int)bx_get($this->_aOptions['paginate_get_per_page']) > 0)
  98. $iPerPage = (int)bx_get($this->_aOptions['paginate_get_per_page']);
  99. elseif ($this->_aOptions['paginate_per_page'])
  100. $iPerPage = (int)$this->_aOptions['paginate_per_page'];
  101. else
  102. $iPerPage = 10;
  103. if ($this->_aOptions['paginate_get_start']) {
  104. $aData = $this->_getData ($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage + 1);
  105. $sPageUrl = false;
  106. if (!empty($this->_aOptions['paginate_url'])) {
  107. $sPageUrl = $this->_aOptions['paginate_url'];
  108. $aParamsAppend = array();
  109. if ($sFilter) {
  110. $aParamsAppend['filter'] = bx_process_input(bx_get($this->_aOptions['filter_get']));
  111. }
  112. if ($sOrderField) {
  113. $aParamsAppend['order_field'] = bx_process_input(bx_get($this->_aOptions['order_get_field']));
  114. $aParamsAppend['order_dir'] = bx_process_input(bx_get($this->_aOptions['order_get_dir']));
  115. }
  116. if ($aParamsAppend)
  117. $sPageUrl = bx_append_url_params($sPageUrl, $aParamsAppend);
  118. }
  119. $aPaginateParams = array(
  120. 'start' => $iStart,
  121. 'per_page' => $iPerPage,
  122. 'page_url' => $sPageUrl ? $sPageUrl : "javascript:glGrids." . $this->_sObject . ".reload('{start}'); void(0);",
  123. );
  124. $oPaginate = new BxTemplPaginate($aPaginateParams, $this->_oTemplate);
  125. $oPaginate->setNumFromDataArray($aData);
  126. if (isset($this->_aOptions['paginate_simple']) && false !== $this->_aOptions['paginate_simple'])
  127. $sPaginate = $oPaginate->getSimplePaginate($this->_aOptions['paginate_simple']);
  128. else
  129. $sPaginate = $oPaginate->getPaginate();
  130. } else {
  131. $aData = $this->_getData ($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  132. }
  133. if((empty($aData) || !is_array($aData)) && isset($this->_aBrowseParams['empty_message']) && !(bool)$this->_aBrowseParams['empty_message'])
  134. return '';
  135. $sPopupOptions = '{}';
  136. if (!empty($this->_aPopupOptions) && is_array($this->_aPopupOptions))
  137. $sPopupOptions = json_encode($this->_aPopupOptions);
  138. $aQueryAppend = array_merge(is_array($this->_aQueryAppend) ? $this->_aQueryAppend : array(), is_array($this->_aMarkers) ? $this->_aMarkers : array());
  139. $sQueryAppend = '{}';
  140. if (!empty($aQueryAppend) && is_array($aQueryAppend))
  141. $sQueryAppend = json_encode($aQueryAppend);
  142. $sConfirmMessages = '{}';
  143. if (!empty($this->_aConfirmMessages) && is_array($this->_aConfirmMessages))
  144. $sConfirmMessages = json_encode($this->_aConfirmMessages);
  145. $iColumns = count($this->_aOptions['fields']);
  146. $aVarsHead = $this->_getRowHead();
  147. $aVars = array (
  148. 'object' => $this->_sObject,
  149. 'id_table' => $sIdTable,
  150. 'id_cont' => $sIdContainer,
  151. 'id_wrap' => $sIdWrapper,
  152. 'sortable' => empty($this->_aOptions['field_order']) ? 0 : 1,
  153. 'sorting' => empty($this->_aOptions['sorting_fields']) ? 0 : 1,
  154. 'sorting_field' => $sOrderField,
  155. 'sorting_dir' => $sOrderDir,
  156. 'bx_if:display_head' => array(
  157. 'condition' => !empty($aVarsHead),
  158. 'content' => array(
  159. 'bx_repeat:row_header' => $aVarsHead,
  160. 'columns' => $iColumns,
  161. )
  162. ),
  163. 'bx_repeat:rows_data' => $this->_getRowsDataDesign ($aData),
  164. 'paginate_get_start' => $this->_aOptions['paginate_get_start'],
  165. 'paginate_get_per_page' => $this->_aOptions['paginate_get_per_page'],
  166. 'start' => $iStart,
  167. 'per_page' => $iPerPage,
  168. 'filter' => bx_js_string($sFilter, BX_ESCAPE_STR_APOS),
  169. 'filter_get' => bx_js_string($this->_aOptions['filter_get']),
  170. 'order_field' => bx_js_string($sOrderField, BX_ESCAPE_STR_APOS),
  171. 'order_dir' => bx_js_string($sOrderDir, BX_ESCAPE_STR_APOS),
  172. 'order_get_field' => bx_js_string($this->_aOptions['order_get_field']),
  173. 'order_get_dir' => bx_js_string($this->_aOptions['order_get_dir']),
  174. 'popup_options' => $sPopupOptions,
  175. 'query_append' => $sQueryAppend,
  176. 'confirm_messages' => $sConfirmMessages,
  177. 'columns' => $iColumns,
  178. 'bx_if:display_footer' => array(
  179. 'condition' => !empty($this->_aOptions['actions_bulk']) || !empty($sPaginate),
  180. 'content' => array(
  181. 'bx_if:actions_bulk' => array (
  182. 'condition' => !empty($this->_aOptions['actions_bulk']),
  183. 'content' => array(
  184. 'actions_bulk' => $this->_getActions ('bulk'),
  185. ),
  186. ),
  187. 'paginate' => $sPaginate,
  188. )
  189. ),
  190. 'bx_if:display_header' => array (
  191. 'condition' => $isDisplayHeader,
  192. 'content' => array (
  193. 'bx_if:actions_independent' => array (
  194. 'condition' => !empty($this->_aOptions['actions_independent']),
  195. 'content' => array(
  196. 'actions_independent' => $this->_getActions ('independent'),
  197. ),
  198. ),
  199. 'bx_if:filter' => array (
  200. 'condition' => !empty($this->_aOptions['filter_fields']) || !empty($this->_aOptions['filter_fields_translatable']),
  201. 'content' => array(
  202. 'controls' => $this->_getFilterControls(),
  203. ),
  204. ),
  205. ),
  206. ),
  207. );
  208. $this->_addJsCss();
  209. return $this->_oTemplate->parseHtmlByName('grid.html', $aVars);
  210. }
  211. /**
  212. * Reset query params, like filter and page number
  213. */
  214. public function resetQueryParams()
  215. {
  216. foreach ($this->_aQueryReset as $sKey) {
  217. unset($_GET[$sKey]);
  218. unset($_POST[$sKey]);
  219. }
  220. }
  221. protected function _getRowHead ()
  222. {
  223. $aRet = array();
  224. foreach ($this->_aOptions['fields'] as $sKey => $a) {
  225. $sMethod = '_getCellHeaderDefault';
  226. $sCustomMethod = '_getCellHeader' . $this->_genMethodName($sKey);
  227. if (method_exists($this, $sCustomMethod))
  228. $sMethod = $sCustomMethod;
  229. $aRet[] = array('header_cell' => $this->$sMethod($sKey, $a));
  230. }
  231. return $aRet;
  232. }
  233. protected function _getCellHeaderDefault ($sKey, $aField)
  234. {
  235. $sHeader = bx_process_output($aField['title']);
  236. if (!empty($this->_aOptions['sorting_fields']) && is_array($this->_aOptions['sorting_fields']) && in_array($sKey, $this->_aOptions['sorting_fields'])) {
  237. $sHeader = '<a href="javascript:void(0);" class="bx-grid-sort-handle">' . $sHeader . '</a><span class="bx-grid-sort-indi"></span>';
  238. $aField['attr_head']['bx_grid_sort_head'] = $sKey;
  239. }
  240. $sAttr = $this->_convertAttrs(
  241. $aField, 'attr_head',
  242. 'bx-def-padding-sec-bottom bx-def-padding-sec-top bx-grid-header-' . $sKey, // add default classes
  243. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  244. );
  245. return $this->_getCellHeaderWrapper ($sKey, $aField, $sHeader, $sAttr);
  246. }
  247. protected function _getCellHeaderCheckbox ($sKey, $aField)
  248. {
  249. $aAttr = array(
  250. 'type' => 'checkbox',
  251. 'id' => $this->_sObject . '_check_all',
  252. 'name' => $this->_sObject . '_check_all',
  253. 'onclick' => "$('input[name=" . $this->_sObject . "_check]:not([disabled])').attr('checked', this.checked)"
  254. );
  255. if($this->_bSelectAll)
  256. $aAttr['checked'] = 'checked';
  257. $aField['attr'] = isset($aField['attr']) && is_array($aField['attr']) ? array_merge($aAttr, $aField['attr']) : $aAttr;
  258. $sAttrHead = $this->_convertAttrs(
  259. $aField, 'attr_head',
  260. 'bx-def-padding-sec-bottom bx-def-padding-sec-top bx-grid-header-' . $sKey, // add default classes
  261. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  262. );
  263. return $this->_getCellHeaderWrapper ($sKey, $aField, ' <input ' . $this->_convertAttrs($aField, 'attr') . ' /> ', $sAttrHead);
  264. }
  265. protected function _getCellHeaderWrapper ($sKey, $aField, $sHeader, $sAttr)
  266. {
  267. return '<th ' . $sAttr . '>' . $sHeader . '</th>';
  268. }
  269. /**
  270. * Check if the whole row is disabled.
  271. * When row is disabled - checkbox is not selectable, actions aren't clickable and text is grayed out.
  272. * By default all rows aren't disabled.
  273. * @param $aRow row array
  274. * @return boolean
  275. */
  276. protected function _isRowDisabled($aRow)
  277. {
  278. if (isset($aRow[$this->_aOptions['field_active']]) && !$this->_switcherState2Checked($aRow[$this->_aOptions['field_active']]))
  279. return true;
  280. return false;
  281. }
  282. /**
  283. * Determine how actions are disabled when whole row is disabled.
  284. * @param $aRow row array
  285. * @return null - disable/enable actions when row is disabled/enabled, true - actions are always disabled, false - actions are always enabled
  286. */
  287. protected function _getActionsDisabledBehavior($aRow)
  288. {
  289. return null;
  290. }
  291. /**
  292. * Check if the checkbox is disabled.
  293. * @param $aRow row array
  294. * @return boolean
  295. */
  296. protected function _isCheckboxDisabled($aRow)
  297. {
  298. return $this->_isRowDisabled($aRow);
  299. }
  300. /**
  301. * Is checkbox checked by default ?
  302. * By default no one checkbox is selected.
  303. * @return boolean
  304. */
  305. protected function _isCheckboxSelected($mixedValue, $sKey, $aField, $aRow)
  306. {
  307. return $this->_bSelectAll;
  308. }
  309. /**
  310. * Is switcher on by default ?
  311. * By default no one switcher is on.
  312. * @return boolean
  313. */
  314. protected function _isSwitcherOn($mixedValue, $sKey, $aField, $aRow)
  315. {
  316. return $this->_switcherState2Checked($mixedValue);
  317. }
  318. /**
  319. * Convert switcher checked status to the actual value ?
  320. * @return boolean
  321. */
  322. protected function _switcherChecked2State($isChecked)
  323. {
  324. return $isChecked ? 1 : 0;
  325. }
  326. /**
  327. * Convert switcher value to checked(boolean) value ?
  328. * @return boolean
  329. */
  330. protected function _switcherState2Checked($mixedState)
  331. {
  332. return $mixedState ? true : false;
  333. }
  334. protected function _getRowsDataDesign (&$aData)
  335. {
  336. $aGrid = array();
  337. if(empty($aData))
  338. $aGrid[] = array(
  339. 'id_row' => 0,
  340. 'row_class' => 'bx-grid-table-row-empty',
  341. 'row' => '<td class="bx-def-padding-sec-topbottom" colspan="' . count($this->_aOptions['fields']) . '">' . MsgBox(_t('_Empty')) . '</td>'
  342. );
  343. else
  344. foreach ($aData as $aRow)
  345. $aGrid[] = array(
  346. 'id_row' => $this->_getRowId($aRow),
  347. 'row_class' => $this->_isRowDisabled($aRow) ? 'bx-grid-table-row-disabled bx-def-font-grayed' : '',
  348. 'row' => $this->_getRowDesign($aRow)
  349. );
  350. return $aGrid;
  351. }
  352. protected function _getRowId($mixedRow)
  353. {
  354. if(is_string($mixedRow))
  355. $sId = rand(1, getrandmax());
  356. else
  357. $sId = $mixedRow[$this->_aOptions['field_id']];
  358. return $this->_sObject . '_row_' . $sId;
  359. }
  360. protected function _getRowDesign($mixedRow)
  361. {
  362. $sRow = '';
  363. if(is_string($mixedRow))
  364. $sRow = '<td class="bx-def-padding-sec-topbottom bx-def-font-bold" colspan="' . count($this->_aOptions['fields']) . '">' . $mixedRow . '</td>';
  365. else
  366. foreach($this->_aOptions['fields'] as $sKey => $aField)
  367. $sRow .= $this->_getCellDesign($sKey, $aField, $mixedRow);
  368. return $sRow;
  369. }
  370. protected function _getCellDesign($sKey, $aField, $aRow)
  371. {
  372. $sMethod = '_getCellDefault';
  373. if ($this->_aOptions['field_order'] == $sKey)
  374. $sMethod = '_getCellOrder';
  375. $sCustomMethod = '_getCell' . $this->_genMethodName($sKey);
  376. if (method_exists($this, $sCustomMethod))
  377. $sMethod = $sCustomMethod;
  378. $mixedValue = $this->_getCellData($sKey, $aField, $aRow);
  379. if ($aField['translatable'])
  380. $mixedValue = _t($mixedValue);
  381. $mixedValue = $this->_limitMaxLength($mixedValue, $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  382. return $this->$sMethod($mixedValue, $sKey, $aField, $aRow);
  383. }
  384. protected function _getCellCheckbox ($mixedValue, $sKey, $aField, $aRow)
  385. {
  386. $sAttr = $this->_convertAttrs(
  387. $aField, 'attr_cell',
  388. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  389. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  390. );
  391. $sDisabled = ($this->_isCheckboxDisabled($aRow) ? 'disabled="disabled"' : '');
  392. $sSelected = ($this->_isCheckboxSelected($mixedValue, $sKey, $aField, $aRow) ? 'checked="checked"' : '');
  393. $sVal = $aRow[$this->_aOptions['field_id']];
  394. return '<td ' . $sAttr . '><input type="checkbox" name="'. $this->_sObject . '_check" value="' . $sVal . '" ' . $sDisabled . ' ' . $sSelected . '/></td>';
  395. }
  396. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  397. {
  398. $sAttr = $this->_convertAttrs(
  399. $aField, 'attr_cell',
  400. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  401. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  402. );
  403. $oForm = new BxTemplFormView(array(), $this->_oTemplate);
  404. $oForm->addCssJs();
  405. $aInput = array(
  406. 'type' => 'switcher',
  407. 'name' => $this->_sObject . '_switch_' . $aRow[$this->_aOptions['field_id']],
  408. 'caption' => '',
  409. 'attrs' => array (
  410. 'bx_grid_action_single' => 'enable',
  411. 'bx_grid_action_confirm' => '',
  412. 'bx_grid_action_data' => $aRow[$this->_aOptions['field_id']],
  413. ),
  414. 'value' => $aRow[$this->_aOptions['field_id']],
  415. 'checked' => $this->_isSwitcherOn(isset($aRow[$this->_aOptions['field_active']]) ? $aRow[$this->_aOptions['field_active']] : false, $sKey, $aField, $aRow),
  416. );
  417. $sSwitcher = $oForm->genInput($aInput);
  418. return '<td ' . $sAttr . '>' . $sSwitcher . '</td>';
  419. }
  420. protected function _getCellOrder ($mixedValue, $sKey, $aField, $aRow)
  421. {
  422. $sAttr = $this->_convertAttrs(
  423. $aField, 'attr_cell',
  424. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  425. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  426. );
  427. return '<td ' . $sAttr . '><div id="' . $this->_sObject . '_cell_' . $aRow[$this->_aOptions['field_id']] . '" class="bx-grid-drag-handle"><i class="sys-icon align-justify"></i></div></td>';
  428. }
  429. protected function _getCellActions ($mixedValue, $sKey, $aField, $aRow)
  430. {
  431. $sAttr = $this->_convertAttrs(
  432. $aField, 'attr_cell',
  433. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  434. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  435. );
  436. $mixedDisabledBehavior = $this->_getActionsDisabledBehavior($aRow);
  437. $sActions = $this->_getActions('single', $aRow[$this->_aOptions['field_id']], false, null === $mixedDisabledBehavior ? $this->_isRowDisabled($aRow) : $mixedDisabledBehavior, null !== $mixedDisabledBehavior, $aRow);
  438. return '<td ' . $sAttr . '><div class="bx-grid-cell-single-actions-wrapper bx-def-margin-thd-neg">' . $sActions . '</div></td>';
  439. }
  440. protected function _getCellDefault ($mixedValue, $sKey, $aField, $aRow)
  441. {
  442. $sAttr = $this->_convertAttrs(
  443. $aField, 'attr_cell',
  444. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  445. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  446. );
  447. return '<td ' . $sAttr . '>' . $mixedValue . '</td>';
  448. }
  449. protected function _getActions ($sType, $sActionData = false, $isSmall = false, $isDisabled = false, $isPermanentState = false, $aRow = array())
  450. {
  451. $sActionsType = 'actions_' . $sType;
  452. if (empty($this->_aOptions[$sActionsType]) || !is_array($this->_aOptions[$sActionsType]))
  453. return '';
  454. $sRet = '';
  455. foreach ($this->_aOptions[$sActionsType] as $sKey => $a) {
  456. $sFunc = '_getAction' . $this->_genMethodName($sKey);
  457. if (!method_exists($this, $sFunc))
  458. $sFunc = empty($a) ? '_getActionDivider' : '_getActionDefault';
  459. if (!isset($a['attr']['bx_grid_action'])) {
  460. $a['attr']['bx_grid_action_' . $sType] = $sKey;
  461. if (!empty($sActionData))
  462. $a['attr']['bx_grid_action_data'] = $sActionData;
  463. if (isset($a['confirm']))
  464. $a['attr']['bx_grid_action_confirm'] = $a['confirm'] ? 1 : 0;
  465. $a['attr']['bx_grid_action_reset_paginate'] = false === strpos($sKey, 'delete') ? 0 : 1; // reset paginate after deleting row
  466. }
  467. if ($isPermanentState && !isset($a['attr']['bx_grid_permanent_state']))
  468. $a['attr']['bx_grid_permanent_state'] = 1;
  469. $sRet .= $this->$sFunc($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  470. }
  471. return $sRet;
  472. }
  473. protected function _getActionDefault ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  474. {
  475. if ($a['icon_only'] && empty($a['attr']['title']) && !empty($a['title']))
  476. $a['attr']['title'] = $a['title'];
  477. $sAttr = $this->_convertAttrs(
  478. $a, 'attr',
  479. 'bx-btn bx-def-margin-thd' . ($isSmall ? ' bx-btn-small' : '') . ($isDisabled ? ' bx-btn-disabled' : '') // add default classes
  480. );
  481. $sIcon = '';
  482. $sImage = '';
  483. if (!empty($a['icon'])) {
  484. if (false === strpos($a['icon'], '.'))
  485. $sIcon = '<i class="sys-icon ' . $a['icon'] . '"></i>';
  486. elseif ($sIconUrl = $this->_oTemplate->getIconUrl($a['icon']))
  487. $sImage = '<img style="background-image:url(' . $sIconUrl . ');" src="' . $this->_oTemplate->getIconUrl('spacer.gif') .'" />';
  488. }
  489. return '<button ' . $sAttr . '>' . $sIcon . $sImage . ($a['icon_only'] || empty($a['title']) ? '' : '<u>' . $a['title'] . '</u>') . '</button>';
  490. }
  491. protected function _getActionDivider ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  492. {
  493. return '<div class="bx-grid-actions-divider bx-def-margin-sec-left"> | </div>';
  494. }
  495. protected function _getFilterControls ()
  496. {
  497. $oForm = new BxTemplStudioFormView(array());
  498. $aInput = array(
  499. 'type' => 'text',
  500. 'name' => 'keyword',
  501. 'attrs' => array(
  502. 'id' => 'bx-grid-search-' . $this->_sObject
  503. )
  504. );
  505. $this->_oTemplate->addCss('forms.css');
  506. return $oForm->genRow($aInput);
  507. }
  508. protected function _limitMaxLength ($mixedValue, $sKey, $aField, $aRow, $isDisplayPopupOnTextOverflow, $bReturnString = true)
  509. {
  510. if ($aField['chars_limit'] > 0)
  511. $mixedValue = BxTemplFunctions::getInstance()->getStringWithLimitedLength($mixedValue, $aField['chars_limit'], $isDisplayPopupOnTextOverflow, $bReturnString);
  512. return $mixedValue;
  513. }
  514. protected function _convertAttrs ($aField, $sAttrName, $sClasses = false, $sStyles = false)
  515. {
  516. return bx_convert_array2attrs(
  517. isset($aField[$sAttrName]) && is_array($aField[$sAttrName]) ? $aField[$sAttrName] : array(),
  518. $sClasses,
  519. $sStyles
  520. );
  521. }
  522. protected function _updateOrder($mixedId, $iOrder)
  523. {
  524. $oDb = BxDolDb::getInstance();
  525. $sTable = $this->_aOptions['table'];
  526. $sFieldId = $this->_aOptions['field_id'];
  527. $sFieldOrder = $this->_aOptions['field_order'];
  528. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `{$sFieldOrder}` = ? WHERE `{$sFieldId}` = ?", $iOrder, $mixedId);
  529. return $oDb->query($sQuery);
  530. }
  531. protected function _delete ($mixedId)
  532. {
  533. $oDb = BxDolDb::getInstance();
  534. $sTable = $this->_aOptions['table'];
  535. $sFieldId = $this->_aOptions['field_id'];
  536. $sQuery = $oDb->prepare("DELETE FROM `{$sTable}` WHERE `{$sFieldId}` = ?", $mixedId);
  537. return $oDb->query($sQuery);
  538. }
  539. protected function _enable ($mixedId, $isChecked)
  540. {
  541. $oDb = BxDolDb::getInstance();
  542. $sTable = $this->_aOptions['table'];
  543. $sFieldId = $this->_aOptions['field_id'];
  544. $sFieldActive = $this->_aOptions['field_active'];
  545. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `$sFieldActive` = ? WHERE `{$sFieldId}` = ?", $this->_switcherChecked2State($isChecked), $mixedId);
  546. return $oDb->query($sQuery);
  547. }
  548. protected function _addJsCss()
  549. {
  550. if ($this->_aOptions['field_order']) {
  551. $aUiLangs = BxBaseFormView::getJsUiLangs ();
  552. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  553. $this->_oTemplate->addJs(array(
  554. 'jquery-ui/jquery-ui.custom.min.js',
  555. 'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
  556. ));
  557. $this->_oTemplate->addCss('jquery-ui/jquery-ui.css');
  558. }
  559. $this->_oTemplate->addJs('BxDolGrid.js');
  560. $this->_oTemplate->addCss('grid.css');
  561. $this->_oTemplate->addJsTranslation('_sys_grid_confirmation');
  562. }
  563. }
  564. /** @} */