BxBaseGrid.php 35 KB

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