BxBaseGrid.php 27 KB

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