BxBaseGrid.php 32 KB

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