BxBaseGrid.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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 $_oFunctions;
  17. protected $_aPopupOptions = false;
  18. protected $_aQueryAppend = [];
  19. protected $_aQueryAppendExclude = false; // an array of keys which shouldn't be pathed in http requests, but can be stored (used) in 'Query Append' array.
  20. protected $_aQueryAppendExcludeApi = false;
  21. protected $_aQueryReset = false;
  22. protected $_aConfirmMessages = false;
  23. protected $_bSelectAll = false;
  24. protected $_isDisplayPopupOnTextOverflow = true;
  25. public function __construct ($aOptions, $oTemplate)
  26. {
  27. parent::__construct ($aOptions);
  28. if ($oTemplate)
  29. $this->_oTemplate = $oTemplate;
  30. else
  31. $this->_oTemplate = BxDolTemplate::getInstance();
  32. $this->_oFunctions = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate);
  33. $this->_aPopupOptions = [];
  34. $this->_aQueryAppend = [
  35. $this->_oTemplate->getCodeKey() => $this->_oTemplate->getCode()
  36. ];
  37. $this->_aQueryAppendExclude = [];
  38. $this->_aQueryAppendExcludeApi = [$this->_oTemplate->getCodeKey()];
  39. $this->_aQueryReset = [
  40. $this->_aOptions['filter_get'],
  41. $this->_aOptions['order_get_field'],
  42. $this->_aOptions['order_get_dir'],
  43. $this->_aOptions['paginate_get_start'],
  44. $this->_aOptions['paginate_get_per_page']
  45. ];
  46. $this->_aConfirmMessages = [];
  47. }
  48. public function performActionDisplay()
  49. {
  50. if($this->_bIsApi)
  51. return $this->getCodeAPI();
  52. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  53. echoJson(array(
  54. 'grid' => $this->getCode(false),
  55. 'total_count' => $this->_iTotalCount,
  56. 'total_count_f' => $this->_getCounter()
  57. ));
  58. }
  59. public function performActionReorder()
  60. {
  61. $this->_replaceMarkers ();
  62. $aOrder = bx_get($this->_sObject . '_row');
  63. $iOrder = 0;
  64. foreach ($aOrder as $mixedId)
  65. $this->_updateOrder($mixedId, ++$iOrder);
  66. return $this->_getActionResult([]);
  67. }
  68. public function performActionDelete()
  69. {
  70. $this->_replaceMarkers ();
  71. $aResult = [];
  72. $iAffected = 0;
  73. $aIds = bx_get('ids');
  74. if (!$aIds || !is_array($aIds))
  75. return $this->_getActionResult($aResult);
  76. foreach ($aIds as $mixedId)
  77. $iAffected += $this->_delete($mixedId) ? 1 : 0;
  78. if($iAffected)
  79. $aResult = !$this->_bIsApi ? ['grid' => $this->getCode(false)] : [];
  80. else
  81. $aResult = ['msg' => _t("_sys_grid_delete_failed")];
  82. return $this->_getActionResult($aResult);
  83. }
  84. public function performActionEnable($mixedChecked = null)
  85. {
  86. $this->_replaceMarkers ();
  87. $aIds = bx_get('ids');
  88. if (!$aIds || !is_array($aIds)) {
  89. echoJson(array());
  90. exit;
  91. }
  92. $iChecked = (int)($mixedChecked !== null ? $mixedChecked : bx_get('checked'));
  93. $aAffectedIds = array ();
  94. foreach ($aIds as $mixedId)
  95. if ($this->_enable($mixedId, $iChecked))
  96. $aAffectedIds[] = preg_match("/^[\d\w]+$/", $mixedId) ? $mixedId : (int)$mixedId;
  97. $sAction = $iChecked ? 'enable' : 'disable';
  98. if($this->_bIsApi)
  99. return $aAffectedIds;
  100. echo echoJson(array($sAction => $aAffectedIds));
  101. }
  102. public function performActionRevealSecret()
  103. {
  104. $this->_replaceMarkers();
  105. if (!isLogged())
  106. return $this->_getActionResult(['msg' => _t('_Access denied')]);
  107. if (!$this->_isCurrentPasswordValid(bx_get('password')))
  108. return $this->_getActionResult(['msg' => _t('_sys_form_account_input_password_current_error')]);
  109. $aIds = bx_get('ids');
  110. $sKey = bx_process_input(bx_get('field'));
  111. $mixedId = is_array($aIds) && $aIds ? reset($aIds) : false;
  112. if ($mixedId === false || $sKey === false || $sKey === '' || empty($this->_aOptions['fields'][$sKey]))
  113. return $this->_getActionResult(['msg' => _t('_sys_txt_error_occured')]);
  114. $aRow = $this->_getSecretRow($mixedId);
  115. if (!$aRow)
  116. return $this->_getActionResult(['msg' => _t('_sys_txt_error_occured')]);
  117. return $this->_getActionResult([
  118. 'secret' => [
  119. 'id' => $mixedId,
  120. 'field' => $sKey,
  121. 'value' => $this->_getSecretValue($mixedId, $sKey, $aRow),
  122. ]
  123. ]);
  124. }
  125. public function getCode ($isDisplayHeader = true)
  126. {
  127. $this->_replaceMarkers ();
  128. if ($isDisplayHeader && empty($this->_aOptions['paginate_url'])) {
  129. // reset page query params if grid is just initialized and it uses AJAX paginate
  130. $this->resetQueryParams();
  131. }
  132. $sPaginate = '';
  133. $aData = array();
  134. $sIdWrapper = 'bx-grid-wrap-' . $this->_sObject;
  135. $sIdContainer = 'bx-grid-cont-' . $this->_sObject;
  136. $sIdTable = 'bx-grid-table-' . $this->_sObject;
  137. $sFilter = $this->_getFilterValue();
  138. $sOrderField = $this->_getOrderValue();
  139. $sOrderDir = 0 === strcasecmp('desc', bx_get($this->_aOptions['order_get_dir'])) ? 'DESC' : 'ASC';
  140. $iStart = 0;
  141. if($this->_aOptions['paginate_get_start'] && ($iStartGet = (int)bx_get($this->_aOptions['paginate_get_start'])) >= 0)
  142. $iStart = $iStartGet;
  143. $iPerPage = 10;
  144. if($this->_aOptions['paginate_get_per_page'] && ($iPerPageGet = (int)bx_get($this->_aOptions['paginate_get_per_page'])) > 0)
  145. $iPerPage = $iPerPageGet;
  146. else if($this->_aOptions['paginate_per_page'])
  147. $iPerPage = (int)$this->_aOptions['paginate_per_page'];
  148. if ($this->_aOptions['paginate_get_start']) {
  149. $aData = $this->_getData ($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage + 1);
  150. $sPageUrl = false;
  151. if (!empty($this->_aOptions['paginate_url'])) {
  152. $sPageUrl = $this->_aOptions['paginate_url'];
  153. $aParamsAppend = array();
  154. if ($sFilter) {
  155. $aParamsAppend['filter'] = bx_process_input(bx_get($this->_aOptions['filter_get']));
  156. }
  157. if ($sOrderField) {
  158. $aParamsAppend['order_field'] = bx_process_input(bx_get($this->_aOptions['order_get_field']));
  159. $aParamsAppend['order_dir'] = bx_process_input(bx_get($this->_aOptions['order_get_dir']));
  160. }
  161. if ($aParamsAppend)
  162. $sPageUrl = bx_append_url_params($sPageUrl, $aParamsAppend);
  163. }
  164. $aPaginateParams = array(
  165. 'start' => $iStart,
  166. 'per_page' => $iPerPage,
  167. 'page_url' => $sPageUrl ? $sPageUrl : "javascript:glGrids." . $this->_sObject . ".reload('{start}'); void(0);",
  168. );
  169. $oPaginate = new BxTemplPaginate($aPaginateParams, $this->_oTemplate);
  170. $oPaginate->setNumFromDataArray($aData);
  171. if (isset($this->_aOptions['paginate_simple']) && false !== $this->_aOptions['paginate_simple'])
  172. $sPaginate = $oPaginate->getSimplePaginate($this->_aOptions['paginate_simple']);
  173. else
  174. $sPaginate = $oPaginate->getPaginate();
  175. } else {
  176. $aData = $this->_getData ($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  177. }
  178. if((empty($aData) || !is_array($aData)) && isset($this->_aBrowseParams['empty_message']) && !(bool)$this->_aBrowseParams['empty_message'])
  179. return '';
  180. $sPopupOptions = '{}';
  181. if (!empty($this->_aPopupOptions) && is_array($this->_aPopupOptions))
  182. $sPopupOptions = json_encode($this->_aPopupOptions);
  183. $aQueryAppend = array_merge(is_array($this->_aQueryAppend) ? $this->_aQueryAppend : array(), is_array($this->_aMarkers) ? $this->_aMarkers : array());
  184. if(!empty($this->_aQueryAppendExclude) && is_array($this->_aQueryAppendExclude))
  185. $aQueryAppend = array_diff_key($aQueryAppend, array_flip($this->_aQueryAppendExclude));
  186. $sQueryAppend = '{}';
  187. if (!empty($aQueryAppend) && is_array($aQueryAppend))
  188. $sQueryAppend = json_encode($aQueryAppend);
  189. $sConfirmMessages = '{}';
  190. if (!empty($this->_aConfirmMessages) && is_array($this->_aConfirmMessages))
  191. $sConfirmMessages = json_encode($this->_aConfirmMessages);
  192. $iColumns = count($this->_aOptions['fields']);
  193. $aVarsHead = $this->_getRowHead();
  194. $sClassHeader = '';
  195. if($this->_aOptions['show_total_count'] == 0)
  196. $sClassHeader .= ' bx-gh-no-counter';
  197. $aVars = array (
  198. 'object' => $this->_sObject,
  199. 'csrf_token' => BxDolForm::getCsrfToken(),
  200. 'id_table' => $sIdTable,
  201. 'id_cont' => $sIdContainer,
  202. 'id_wrap' => $sIdWrapper,
  203. 'class_table_wrapper' => $this->_aOptions['responsive'] ? 'bx-grid-table-wrapper-responsive' : '',
  204. 'sortable' => empty($this->_aOptions['field_order']) ? 0 : 1,
  205. 'sorting' => empty($this->_getOrderFields()) ? 0 : 1,
  206. 'sorting_field' => $sOrderField,
  207. 'sorting_dir' => $sOrderDir,
  208. 'bx_if:display_head' => array(
  209. 'condition' => !empty($aVarsHead),
  210. 'content' => array(
  211. 'bx_repeat:row_header' => $aVarsHead,
  212. 'columns' => $iColumns,
  213. )
  214. ),
  215. 'bx_repeat:rows_data' => $this->_getRowsDataDesign ($aData),
  216. 'paginate_get_start' => $this->_aOptions['paginate_get_start'],
  217. 'paginate_get_per_page' => $this->_aOptions['paginate_get_per_page'],
  218. 'start' => $iStart,
  219. 'per_page' => $iPerPage,
  220. 'filter' => bx_js_string($sFilter, BX_ESCAPE_STR_APOS),
  221. 'filter_get' => bx_js_string($this->_aOptions['filter_get']),
  222. 'order_field' => bx_js_string($sOrderField, BX_ESCAPE_STR_APOS),
  223. 'order_dir' => bx_js_string($sOrderDir, BX_ESCAPE_STR_APOS),
  224. 'order_get_field' => bx_js_string($this->_aOptions['order_get_field']),
  225. 'order_get_dir' => bx_js_string($this->_aOptions['order_get_dir']),
  226. 'popup_options' => $sPopupOptions,
  227. 'query_append' => $sQueryAppend,
  228. 'confirm_messages' => $sConfirmMessages,
  229. 'columns' => $iColumns,
  230. 'bx_if:display_footer' => array(
  231. 'condition' => !empty($this->_aOptions['actions_bulk']) || !empty($sPaginate),
  232. 'content' => array(
  233. 'bx_if:actions_bulk' => array (
  234. 'condition' => !empty($this->_aOptions['actions_bulk']),
  235. 'content' => array(
  236. 'actions_bulk' => $this->_getActions ('bulk'),
  237. ),
  238. ),
  239. 'paginate' => $sPaginate,
  240. )
  241. ),
  242. 'bx_if:display_header' => array (
  243. 'condition' => $isDisplayHeader,
  244. 'content' => array (
  245. 'class' => $sClassHeader,
  246. 'bx_if:actions_independent' => array (
  247. 'condition' => !empty($this->_aOptions['actions_independent']),
  248. 'content' => array(
  249. 'actions_independent' => $this->_getActions ('independent'),
  250. ),
  251. ),
  252. 'bx_if:filter' => array (
  253. 'condition' => !empty($this->_aOptions['filter_fields']) || !empty($this->_aOptions['filter_fields_translatable']),
  254. 'content' => array(
  255. 'controls' => $this->_getFilterControls(),
  256. ),
  257. ),
  258. 'bx_if:counter' => array (
  259. 'condition' => $this->_aOptions['show_total_count'] == 1,
  260. 'content' => array(
  261. 'counter' => $this->_getCounter(),
  262. ),
  263. ),
  264. ),
  265. ),
  266. );
  267. $this->_addJsCss();
  268. return $this->_oTemplate->parseHtmlByName('grid.html', $aVars);
  269. }
  270. /**
  271. * Get grid code API.
  272. * @return array
  273. */
  274. public function getCodeAPI($bForceReturn = false)
  275. {
  276. $this->_replaceMarkers();
  277. $sFilter = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['filter_get'])));
  278. $sOrderField = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['order_get_field'])));
  279. $sOrderDir = 0 === strcasecmp('desc', bx_get($this->_aOptions['order_get_dir'])) ? 'DESC' : 'ASC';
  280. $iStart = 0;
  281. if($this->_aOptions['paginate_get_start'] && ($iStartGet = (int)bx_get($this->_aOptions['paginate_get_start'])) >= 0)
  282. $iStart = $iStartGet;
  283. $iPerPage = 10;
  284. if($this->_aOptions['paginate_get_per_page'] && ($iPerPageGet = (int)bx_get($this->_aOptions['paginate_get_per_page'])) > 0)
  285. $iPerPage = $iPerPageGet;
  286. else if($this->_aOptions['paginate_per_page'])
  287. $iPerPage = (int)$this->_aOptions['paginate_per_page'];
  288. $aData = $this->_getData($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  289. if(!empty($aData) && is_array($aData))
  290. $aData = $this->decodeDataAPI($aData);
  291. $aQueryAppend = [];
  292. if(!empty($this->_aQueryAppend) && is_array($this->_aQueryAppend))
  293. $aQueryAppend = array_merge($aQueryAppend, $this->_aQueryAppend);
  294. if(!empty($this->_aMarkers) && is_array($this->_aMarkers))
  295. $aQueryAppend = array_merge($aQueryAppend, $this->_aMarkers);
  296. if(!empty($this->_aQueryAppendExclude) && is_array($this->_aQueryAppendExclude))
  297. $aQueryAppend = array_diff_key($aQueryAppend, array_flip($this->_aQueryAppendExclude));
  298. if(!empty($this->_aQueryAppendExcludeApi) && is_array($this->_aQueryAppendExcludeApi))
  299. $aQueryAppend = array_diff_key($aQueryAppend, array_flip($this->_aQueryAppendExcludeApi));
  300. return [
  301. 'settings' => [
  302. 'object' => $this->_aOptions['object'],
  303. 'field_id' => $this->_aOptions['field_id'],
  304. 'start' => $iStart,
  305. 'per_page' => $iPerPage,
  306. 'filters' => $this->_getFilterControlsAPI(),
  307. 'query_append' => $aQueryAppend
  308. ],
  309. 'header' => $this->_getRowHeadAPI(),
  310. 'data' => defined('BX_API_PAGE') && !$bForceReturn ? [] : $aData,
  311. 'actions' => [
  312. 'independent' => $this->_getActionsAPI('independent'),
  313. 'bulk' => $this->_getActionsAPI('bulk')
  314. ]
  315. ];
  316. }
  317. public function decodeDataAPI($aData)
  318. {
  319. $sKeyId = $this->_aOptions['field_id'];
  320. $sMethodDefault = '_getCellDefault';
  321. $aDataRv = [];
  322. foreach($aData as $iKey => $aRow) {
  323. $aDataRv[$iKey] = [$sKeyId => $aData[$iKey][$sKeyId]];
  324. foreach($this->_aOptions['fields'] as $sKey => $aField) {
  325. $sMethod = '_getCell' . $this->_genMethodName($sKey);
  326. if(!method_exists($this, $sMethod))
  327. $sMethod = !empty($aField['secret']) ? '_getCellSecret' : $sMethodDefault;
  328. if(($aCell = $this->$sMethod(isset($aData[$iKey][$sKey]) ? $aData[$iKey][$sKey] : _t('_undefined'), $sKey, $aField, $aRow)))
  329. $aDataRv[$iKey][$sKey] = $aCell;
  330. }
  331. }
  332. return $aDataRv;
  333. }
  334. public function getFormBlockAPI($oForm, $sAction, $iId = 0)
  335. {
  336. return [
  337. bx_api_get_block('form', $oForm->getCodeAPI(), [
  338. 'ext' => [
  339. 'name' => $this->_oModule->getName(),
  340. 'title' => $this->getFormBlockTitleAPI($sAction, $iId),
  341. 'request' => ['url' => $this->getFormCallBackUrlAPI($sAction, $iId), 'immutable' => true]]
  342. ]
  343. )
  344. ];
  345. }
  346. public function getFormBlockTitleAPI($sAction, $iId)
  347. {
  348. return '';
  349. }
  350. public function getFormCallBackUrlAPI($sAction, $iId)
  351. {
  352. return '';
  353. }
  354. /**
  355. * Reset query params, like filter and page number
  356. */
  357. public function resetQueryParams()
  358. {
  359. foreach ($this->_aQueryReset as $sKey) {
  360. unset($_GET[$sKey]);
  361. unset($_POST[$sKey]);
  362. }
  363. }
  364. protected function _getRowHead()
  365. {
  366. $aRet = array();
  367. foreach ($this->_aOptions['fields'] as $sKey => $a) {
  368. $sMethod = '_getCellHeaderDefault';
  369. $sCustomMethod = '_getCellHeader' . $this->_genMethodName($sKey);
  370. if (method_exists($this, $sCustomMethod))
  371. $sMethod = $sCustomMethod;
  372. $aRet[] = array('header_cell' => $this->$sMethod($sKey, $a));
  373. }
  374. return $aRet;
  375. }
  376. protected function _getRowHeadAPI()
  377. {
  378. $bResize = false;
  379. $aHeader = [];
  380. foreach($this->_aOptions['fields'] as $sKey => $aField) {
  381. if($sKey == 'checkbox' && (int)$aField['width'] < 8)
  382. $bResize = true;
  383. $sMethod = '_getCellHeader' . $this->_genMethodName($sKey);
  384. if(($aHeaderItem = $this->{method_exists($this, $sMethod) ? $sMethod : '_getCellHeaderDefault'}($sKey, $aField)))
  385. $aHeader[] = $aHeaderItem;
  386. }
  387. if(($sKey = 'checkbox') && $bResize)
  388. $this->_resizeCellAPI($sKey, $this->_aOptions['fields'][$sKey], $aHeader);
  389. return $aHeader;
  390. }
  391. protected function _resizeCellAPI($sKey, $aField, &$aHeader)
  392. {
  393. $iMin = 8;
  394. $iAdd = $iMin - (int)$aField['width'];
  395. $iThreshold = $iMin + $iAdd;
  396. $iReduce = 0;
  397. array_walk($aHeader, function($aItem) use ($iThreshold, &$iReduce) {
  398. if((int)$aItem['width'] >= $iThreshold)
  399. $iReduce += 1;
  400. });
  401. $fSub = round($iAdd / $iReduce, 2);
  402. array_walk($aHeader, function(&$aItem) use ($sKey, $iThreshold, $iAdd, $fSub) {
  403. if($aItem['name'] == $sKey)
  404. $aItem['width'] = (float)$aItem['width'] + $iAdd;
  405. else if((int)$aItem['width'] >= $iThreshold)
  406. $aItem['width'] = (float)$aItem['width'] - $fSub;
  407. else
  408. return;
  409. $aItem['width'] .= '%';
  410. });
  411. }
  412. protected function _getCellHeaderDefault ($sKey, $aField)
  413. {
  414. if($this->_bIsApi)
  415. return [
  416. 'name' => bx_process_output($aField['name']),
  417. 'title' => bx_process_output($aField['title']),
  418. 'width' => $aField['width']
  419. ];
  420. $sHeader = bx_process_output($aField['title']);
  421. if (($aSortingFields = $this->_getOrderFields()) && in_array($sKey, $aSortingFields)) {
  422. $sHeader = '<a href="javascript:void(0);" class="bx-grid-sort-handle">' . $sHeader . '</a><span class="bx-grid-sort-indi"></span>';
  423. $aField['attr_head']['bx_grid_sort_head'] = $sKey;
  424. }
  425. $sAttr = $this->_convertAttrs(
  426. $aField, 'attr_head',
  427. 'bx-def-padding-sec-bottom bx-def-padding-sec-top bx-grid-header-' . $sKey, // add default classes
  428. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  429. );
  430. return $this->_getCellHeaderWrapper ($sKey, $aField, $sHeader, $sAttr);
  431. }
  432. protected function _getCellHeaderCheckbox ($sKey, $aField)
  433. {
  434. if($this->_bIsApi)
  435. return $this->_getCellHeaderDefault($sKey, $aField);
  436. $aAttr = array(
  437. 'type' => 'checkbox',
  438. 'id' => $this->_sObject . '_check_all',
  439. 'name' => $this->_sObject . '_check_all',
  440. 'onclick' => "$('input[name=" . $this->_sObject . "_check]:not([disabled])').attr('checked', this.checked)"
  441. );
  442. if($this->_bSelectAll)
  443. $aAttr['checked'] = 'checked';
  444. $aField['attr'] = isset($aField['attr']) && is_array($aField['attr']) ? array_merge($aAttr, $aField['attr']) : $aAttr;
  445. $sAttrHead = $this->_convertAttrs(
  446. $aField, 'attr_head',
  447. 'bx-def-padding-sec-bottom bx-def-padding-sec-top bx-grid-header-' . $sKey, // add default classes
  448. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  449. );
  450. return $this->_getCellHeaderWrapper ($sKey, $aField, ' <input ' . $this->_convertAttrs($aField, 'attr') . ' /> ', $sAttrHead);
  451. }
  452. protected function _getCellHeaderWrapper ($sKey, $aField, $sHeader, $sAttr)
  453. {
  454. return '<th ' . $sAttr . '>' . $sHeader . '</th>';
  455. }
  456. /**
  457. * Check if the whole row is disabled.
  458. * When row is disabled - checkbox is not selectable, actions aren't clickable and text is grayed out.
  459. * By default all rows aren't disabled.
  460. * @param $aRow row array
  461. * @return boolean
  462. */
  463. protected function _isRowDisabled($aRow)
  464. {
  465. if (isset($aRow[$this->_aOptions['field_active']]) && !$this->_switcherState2Checked($aRow[$this->_aOptions['field_active']]))
  466. return true;
  467. return false;
  468. }
  469. /**
  470. * Determine how actions are disabled when whole row is disabled.
  471. * @param $aRow row array
  472. * @return null - disable/enable actions when row is disabled/enabled, true - actions are always disabled, false - actions are always enabled
  473. */
  474. protected function _getActionsDisabledBehavior($aRow)
  475. {
  476. return null;
  477. }
  478. /**
  479. * Check if the checkbox is disabled.
  480. * @param $aRow row array
  481. * @return boolean
  482. */
  483. protected function _isCheckboxDisabled($aRow)
  484. {
  485. return $this->_isRowDisabled($aRow);
  486. }
  487. /**
  488. * Is checkbox checked by default ?
  489. * By default no one checkbox is selected.
  490. * @return boolean
  491. */
  492. protected function _isCheckboxSelected($mixedValue, $sKey, $aField, $aRow)
  493. {
  494. return $this->_bSelectAll;
  495. }
  496. /**
  497. * Is switcher on by default ?
  498. * By default no one switcher is on.
  499. * @return boolean
  500. */
  501. protected function _isSwitcherOn($mixedValue, $sKey, $aField, $aRow)
  502. {
  503. return $this->_switcherState2Checked($mixedValue);
  504. }
  505. /**
  506. * Convert switcher checked status to the actual value ?
  507. * @return boolean
  508. */
  509. protected function _switcherChecked2State($isChecked)
  510. {
  511. return $isChecked ? 1 : 0;
  512. }
  513. /**
  514. * Convert switcher value to checked(boolean) value ?
  515. * @return boolean
  516. */
  517. protected function _switcherState2Checked($mixedState)
  518. {
  519. return $mixedState ? true : false;
  520. }
  521. protected function _getRowsDataDesign (&$aData)
  522. {
  523. $aGrid = array();
  524. if(empty($aData))
  525. $aGrid[] = array(
  526. 'id_row' => 0,
  527. 'row_class' => 'bx-grid-table-row-empty',
  528. 'row' => '<td class="bx-def-padding-sec-topbottom" colspan="' . count($this->_aOptions['fields']) . '">' . MsgBox(_t('_Empty')) . '</td>'
  529. );
  530. else
  531. foreach ($aData as $aRow)
  532. $aGrid[] = array(
  533. 'id_row' => $this->_getRowId($aRow),
  534. 'row_class' => $this->_isRowDisabled($aRow) ? 'bx-grid-table-row-disabled bx-def-font-grayed' : '',
  535. 'row' => $this->_getRowDesign($aRow)
  536. );
  537. return $aGrid;
  538. }
  539. protected function _getRowId($mixedRow)
  540. {
  541. if(is_string($mixedRow))
  542. $sId = rand(1, getrandmax());
  543. else
  544. $sId = $mixedRow[$this->_aOptions['field_id']];
  545. return $this->_sObject . '_row_' . $sId;
  546. }
  547. protected function _getRowDesign($mixedRow)
  548. {
  549. $sRow = '';
  550. if(is_string($mixedRow))
  551. $sRow = '<td class="bx-grid-cell bx-gc-string bx-def-padding-sec-topbottom bx-def-font-bold" colspan="' . count($this->_aOptions['fields']) . '">' . $mixedRow . '</td>';
  552. else
  553. foreach($this->_aOptions['fields'] as $sKey => $aField)
  554. $sRow .= $this->_getCellDesign($sKey, $aField, $mixedRow);
  555. return $sRow;
  556. }
  557. protected function _getCellDesign($sKey, $aField, $aRow)
  558. {
  559. $sMethod = '_getCellDefault';
  560. if ($this->_aOptions['field_order'] == $sKey)
  561. $sMethod = '_getCellOrder';
  562. $sCustomMethod = '_getCell' . $this->_genMethodName($sKey);
  563. if (method_exists($this, $sCustomMethod))
  564. $sMethod = $sCustomMethod;
  565. else if (!empty($aField['secret']))
  566. $sMethod = '_getCellSecret';
  567. $mixedValue = $this->_getCellData($sKey, $aField, $aRow);
  568. if ($aField['translatable'])
  569. $mixedValue = _t($mixedValue);
  570. if ($sMethod != '_getCellSecret')
  571. $mixedValue = $this->_limitMaxLength($mixedValue, $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  572. return $this->$sMethod($mixedValue, $sKey, $aField, $aRow);
  573. }
  574. protected function _getCellCheckbox ($mixedValue, $sKey, $aField, $aRow)
  575. {
  576. if($this->_bIsApi)
  577. return ['type' => 'checkbox', 'data' => $aRow[$this->_aOptions['field_id']]];
  578. $sAttr = $this->_convertAttrs(
  579. $aField, 'attr_cell',
  580. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  581. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  582. );
  583. $sDisabled = ($this->_isCheckboxDisabled($aRow) ? 'disabled="disabled"' : '');
  584. $sSelected = ($this->_isCheckboxSelected($mixedValue, $sKey, $aField, $aRow) ? 'checked="checked"' : '');
  585. $sVal = $aRow[$this->_aOptions['field_id']];
  586. return '<td ' . $sAttr . '><input type="checkbox" name="'. $this->_sObject . '_check" value="' . $sVal . '" ' . $sDisabled . ' ' . $sSelected . '/></td>';
  587. }
  588. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  589. {
  590. if($this->_bIsApi)
  591. return ['type' => 'switcher', 'data' => $aRow[$this->_aOptions['field_active']], 'fld' => $this->_aOptions['field_active']];
  592. $sAttr = $this->_convertAttrs(
  593. $aField, 'attr_cell',
  594. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  595. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  596. );
  597. $oForm = new BxTemplFormView(array(), $this->_oTemplate);
  598. $oForm->addCssJs();
  599. $aInput = array(
  600. 'type' => 'switcher',
  601. 'name' => $this->_sObject . '_switch_' . $aRow[$this->_aOptions['field_id']],
  602. 'caption' => '',
  603. 'attrs' => array (
  604. 'bx_grid_action_single' => 'enable',
  605. 'bx_grid_action_confirm' => '',
  606. 'bx_grid_action_data' => $aRow[$this->_aOptions['field_id']],
  607. ),
  608. 'value' => $aRow[$this->_aOptions['field_id']],
  609. 'checked' => $this->_isSwitcherOn(isset($aRow[$this->_aOptions['field_active']]) ? $aRow[$this->_aOptions['field_active']] : false, $sKey, $aField, $aRow),
  610. );
  611. $sSwitcher = $oForm->genInput($aInput);
  612. return '<td ' . $sAttr . '>' . $sSwitcher . '</td>';
  613. }
  614. protected function _getCellOrder ($mixedValue, $sKey, $aField, $aRow)
  615. {
  616. if($this->_bIsApi)
  617. return ['type' => 'order', 'data'=> $mixedValue];
  618. $sAttr = $this->_convertAttrs(
  619. $aField, 'attr_cell',
  620. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  621. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  622. );
  623. 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>';
  624. }
  625. protected function _getCellActions ($mixedValue, $sKey, $aField, $aRow)
  626. {
  627. $sAttr = $this->_convertAttrs(
  628. $aField, 'attr_cell',
  629. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  630. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  631. );
  632. $mixedDisabledBehavior = $this->_getActionsDisabledBehavior($aRow);
  633. $mixedActions = $this->_getActions('single', $aRow[$this->_aOptions['field_id']], false, null === $mixedDisabledBehavior ? $this->_isRowDisabled($aRow) : $mixedDisabledBehavior, null !== $mixedDisabledBehavior, $aRow);
  634. if($this->_bIsApi)
  635. return ['type' => 'actions', 'data' => $mixedActions];
  636. return '<td ' . $sAttr . '><div class="bx-grid-cell-single-actions-wrapper bx-def-margin-thd-neg">' . $mixedActions . '</div></td>';
  637. }
  638. protected function _getCellDefault ($mixedValue, $sKey, $aField, $aRow)
  639. {
  640. if($this->_bIsApi) {
  641. $sType = $mixedValue['type'] ?? 'text';
  642. if(isset($mixedValue['type']))
  643. unset($mixedValue['type']);
  644. return [
  645. 'type' => $sType,
  646. 'value'=> (int)$aField['translatable'] ? _t($mixedValue) : $mixedValue
  647. ];
  648. }
  649. $sAttr = $this->_convertAttrs(
  650. $aField, 'attr_cell',
  651. 'bx-grid-cell' . (!empty($aField['name']) ? ' bx-gc-' . $aField['name'] : '') . ' bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  652. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  653. );
  654. return '<td ' . $sAttr . '>' . $mixedValue . '</td>';
  655. }
  656. /**
  657. * Display a cell as a masked secret with an eye icon to reveal it after password confirmation.
  658. * @see BxDolGrid grid_display_secret_cell
  659. */
  660. protected function _getCellSecret ($mixedValue, $sKey, $aField, $aRow)
  661. {
  662. $sRaw = '';
  663. if (isset($aRow[$sKey]) && $aRow[$sKey] !== '' && $aRow[$sKey] !== null)
  664. $sRaw = (string)$aRow[$sKey];
  665. else if ($mixedValue !== '' && $mixedValue !== null && $mixedValue !== false)
  666. $sRaw = (string)$mixedValue;
  667. $sMasked = $sRaw !== '' ? bx_gen_secret($sRaw) : '';
  668. if ($this->_bIsApi)
  669. return ['type' => 'secret', 'value' => $sMasked];
  670. $sAttr = $this->_convertAttrs(
  671. $aField, 'attr_cell',
  672. 'bx-grid-cell' . (!empty($aField['name']) ? ' bx-gc-' . $aField['name'] : '') . ' bx-def-padding-sec-bottom bx-def-padding-sec-top',
  673. isset($aField['width']) ? 'width:' . $aField['width'] : false
  674. );
  675. $sToggle = '';
  676. if ($sRaw !== '' && isLogged()) {
  677. $mixedId = $aRow[$this->_aOptions['field_id']];
  678. $sToggle = '<a href="javascript:void(0);" class="bx-grid-secret-toggle" title="' . bx_html_attribute(_t('_sys_grid_secret_reveal')) . '" bx_grid_secret_id="' . bx_html_attribute($mixedId) . '" bx_grid_secret_field="' . bx_html_attribute($sKey) . '"><i class="sys-icon eye"></i></a>';
  679. }
  680. return '<td ' . $sAttr . '><div class="bx-grid-secret"><span class="bx-grid-secret-value" data-masked="' . bx_html_attribute($sMasked) . '">' . bx_process_output($sMasked) . '</span>' . $sToggle . '</div></td>';
  681. }
  682. protected function _getSecretRow($mixedId)
  683. {
  684. $aRows = $this->_getDataUpdated([$mixedId]);
  685. if (!$aRows || !is_array($aRows))
  686. return false;
  687. $sFieldId = $this->_aOptions['field_id'];
  688. foreach ($aRows as $aRow) {
  689. if (isset($aRow[$sFieldId]) && (string)$aRow[$sFieldId] === (string)$mixedId)
  690. return $aRow;
  691. }
  692. return reset($aRows);
  693. }
  694. /**
  695. * Return the secret value for a revealed cell. Override when the value is encrypted or computed.
  696. */
  697. protected function _getSecretValue($mixedId, $sKey, $aRow)
  698. {
  699. return isset($aRow[$sKey]) ? $aRow[$sKey] : '';
  700. }
  701. protected function _isCurrentPasswordValid($sPassword)
  702. {
  703. if ($sPassword === false || $sPassword === '')
  704. return false;
  705. if (!class_exists('BxFormAccountCheckerHelper'))
  706. bx_import('BxBaseFormAccount');
  707. $oChecker = new BxFormAccountCheckerHelper();
  708. return true === $oChecker->checkPasswordCurrent($sPassword);
  709. }
  710. protected function _getActions ($sType, $sActionData = false, $isSmall = false, $isDisabled = false, $isPermanentState = false, $aRow = array())
  711. {
  712. $sActionsType = 'actions_' . $sType;
  713. if (empty($this->_aOptions[$sActionsType]) || !is_array($this->_aOptions[$sActionsType]))
  714. return '';
  715. $mixedResult = $this->_bIsApi ? [] : '';
  716. foreach ($this->_aOptions[$sActionsType] as $sKey => $a) {
  717. if(!$a['active'] || (!$a['title'] && !$a['icon']))
  718. continue;
  719. $sFunc = '_getAction' . $this->_genMethodName($sKey);
  720. if (!method_exists($this, $sFunc))
  721. $sFunc = empty($a) ? '_getActionDivider' : '_getActionDefault';
  722. if (!isset($a['attr']['bx_grid_action'])) {
  723. $a['attr']['bx_grid_action_' . $sType] = $sKey;
  724. if (!empty($sActionData))
  725. $a['attr']['bx_grid_action_data'] = $sActionData;
  726. if (isset($a['confirm']))
  727. $a['attr']['bx_grid_action_confirm'] = $a['confirm'] ? 1 : 0;
  728. $a['attr']['bx_grid_action_reset_paginate'] = false === strpos($sKey, 'delete') ? 0 : 1; // reset paginate after deleting row
  729. }
  730. if ($isPermanentState && !isset($a['attr']['bx_grid_permanent_state']))
  731. $a['attr']['bx_grid_permanent_state'] = 1;
  732. if ($this->_bIsApi)
  733. $mixedResult[] = $this->$sFunc($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  734. else
  735. $mixedResult .= $this->$sFunc($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  736. }
  737. return $mixedResult;
  738. }
  739. protected function _getActionsAPI ($sType)
  740. {
  741. $sActionsType = 'actions_' . $sType;
  742. if(empty($this->_aOptions[$sActionsType]) || !is_array($this->_aOptions[$sActionsType]))
  743. return [];
  744. foreach ($this->_aOptions[$sActionsType] as $sKey => $aAction) {
  745. $sMethod = '_getAction' . $this->_genMethodName($sKey);
  746. if(!method_exists($this, $sMethod))
  747. $sMethod = '_getActionDefault';
  748. if(!isset($aAction['attr']))
  749. $aAction['attr'] = [];
  750. if(($_aAction = $this->$sMethod($sType, $sKey, $aAction)))
  751. $this->_aOptions[$sActionsType][$sKey] = $_aAction;
  752. }
  753. return $this->_aOptions[$sActionsType];
  754. }
  755. protected function _getActionDelete($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  756. {
  757. if($this->_bIsApi)
  758. return array_merge($a, ['name' => $sKey, 'type' => 'callback', 'on_callback' => 'hide_row']);
  759. return $this->_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  760. }
  761. protected function _getActionDefault ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  762. {
  763. if ($a['icon_only'] && empty($a['attr']['title']) && !empty($a['title']))
  764. $a['attr']['title'] = $a['title'];
  765. if ($this->_bIsApi) {
  766. $sParams = '';
  767. if($sType == 'single' && ($sFieldId = $this->_aOptions['field_id']) && isset($aRow[$sFieldId]))
  768. $sParams = '&id=' . $aRow[$sFieldId];
  769. return array_merge($a, ['name' => $sKey, 'type' => 'modal', 'action' => $sKey, 'params' => $sParams]);
  770. }
  771. $sAttr = $this->_convertAttrs(
  772. $a, 'attr',
  773. 'bx-btn bx-def-margin-thd' . ($isSmall ? ' bx-btn-small' : '') . ($isDisabled ? ' bx-btn-disabled' : '') // add default classes
  774. );
  775. $sIcon = '';
  776. $sImage = '';
  777. if (!empty($a['icon'])) {
  778. list($sIconFont, $sIconUrl, $sIconA, $sIconHtml) = $this->_oFunctions->getIcon($a['icon']);
  779. if ($sIconFont)
  780. $sIcon = '<i class="sys-icon ' . $sIconFont . '"></i>';
  781. else if($sIconUrl)
  782. $sImage = '<img style="background-image:url(' . $sIconUrl . ');" src="' . $this->_oTemplate->getIconUrl('spacer.gif') .'" />';
  783. else if($sIconHtml)
  784. $sImage = $sIconHtml;
  785. }
  786. return '<button ' . $sAttr . '>' . $sIcon . $sImage . ($a['icon_only'] || empty($a['title']) ? '' : '<u>' . $a['title'] . '</u>') . '</button>';
  787. }
  788. protected function _getActionDivider ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  789. {
  790. return '<div class="bx-grid-actions-divider bx-def-margin-sec-left"> | </div>';
  791. }
  792. protected function _getActionResult($aResult)
  793. {
  794. return $this->_bIsApi ? $aResult : echoJson($aResult);
  795. }
  796. protected function _getFilterControls ()
  797. {
  798. $oForm = new BxTemplFormView([]);
  799. $aInput = array(
  800. 'type' => 'text',
  801. 'name' => 'keyword',
  802. 'attrs' => array(
  803. 'id' => 'bx-grid-search-' . $this->_sObject
  804. )
  805. );
  806. $this->_oTemplate->addCss('forms.css');
  807. return $oForm->genRow($aInput);
  808. }
  809. protected function _getFilterControlsAPI($aFilters = [])
  810. {
  811. if(!empty($this->_aOptions['filter_fields']) || !empty($this->_aOptions['filter_fields_translatable']))
  812. $aFilters['search'] = [];
  813. return $aFilters;
  814. }
  815. protected function _getFilterOnChange()
  816. {
  817. return '';
  818. }
  819. protected function _getFilterSelectOne($sFilterName, $sFilterValue, $aFilterValues, $mixedAddSelectOne = true, $bAsArray = false)
  820. {
  821. if(empty($sFilterName))
  822. return '';
  823. $aInputValues = [];
  824. if(($mixedAddSelectOne === true && ($sKey = '_Select_one')) || (is_string($mixedAddSelectOne) && ($sKey = $mixedAddSelectOne)))
  825. $aInputValues[''] = _t($sKey);
  826. foreach($aFilterValues as $mixedKey => $mixedValue)
  827. if(is_array($mixedValue) && isset($mixedValue['key'], $mixedValue['value']))
  828. $aInputValues[$mixedValue['key']] = _t($mixedValue['value']);
  829. else
  830. $aInputValues[$mixedKey] = _t($mixedValue);
  831. $aInputSelect = [
  832. 'type' => 'select',
  833. 'name' => $sFilterName,
  834. 'attrs' => [
  835. 'id' => 'bx-grid-' . $sFilterName . '-' . $this->_sObject,
  836. 'onChange' => ($sOnChange = $this->_getFilterOnChange()) ? 'javascript:' . $sOnChange : '',
  837. ],
  838. 'value' => $sFilterValue,
  839. 'values' => $aInputValues
  840. ];
  841. if($bAsArray)
  842. return $aInputSelect;
  843. $oForm = new BxTemplFormView([]);
  844. return $oForm->genRow($aInputSelect);
  845. }
  846. protected function _getFilterDatePicker($sFilterName, $sFilterValue, $bAsArray = false)
  847. {
  848. if(empty($sFilterName))
  849. return '';
  850. $aInputDatePicker = [
  851. 'type' => 'datepicker',
  852. 'name' => $sFilterName,
  853. 'attrs' => [
  854. 'id' => 'bx-grid-' . $sFilterName . '-' . $this->_sObject,
  855. 'onChange' => ($sOnChange = $this->_getFilterOnChange()) ? 'javascript:' . $sOnChange : '',
  856. ],
  857. 'value' => $sFilterValue,
  858. ];
  859. if($bAsArray)
  860. return $aInputDatePicker;
  861. $oForm = new BxTemplFormView([]);
  862. return $oForm->genRow($aInputDatePicker);
  863. }
  864. protected function _getFilterButton($bAsArray = false)
  865. {
  866. $sOnChange = $this->_getFilterOnChange();
  867. $aInputButton = [
  868. 'type' => 'button',
  869. 'name' => 'button',
  870. 'attrs' => [
  871. 'id' => 'bx-grid-button-' . $this->_sObject,
  872. 'onClick' => 'javascript:' . $sOnChange,
  873. ],
  874. 'value' => _t('_Search'),
  875. ];
  876. if($bAsArray)
  877. return $aInputButton;
  878. $oForm = new BxTemplFormView([]);
  879. return $oForm->genRow($aInputButton);
  880. }
  881. protected function _getFilterLabel($sFilterValue, $bAsArray = false)
  882. {
  883. $aInputLabel = [
  884. 'type' => 'value',
  885. 'value' => $sFilterValue,
  886. 'tr_attrs' => ['class' => 'bx-grid-controls-filter-label'],
  887. ];
  888. if($bAsArray)
  889. return $aInputLabel;
  890. $oForm = new BxTemplFormView([]);
  891. return $oForm->genRow($aInputLabel);
  892. }
  893. protected function _getSearchInput($bAsArray = false)
  894. {
  895. $sOnChange = $this->_getFilterOnChange();
  896. $aInputSearch = [
  897. 'type' => 'text',
  898. 'name' => 'search',
  899. 'attrs' => [
  900. 'id' => 'bx-grid-search-' . $this->_sObject,
  901. 'onKeyup' => 'javascript:$(this).off(\'keyup focusout\'); ' . $sOnChange,
  902. 'onBlur' => 'javascript:' . $sOnChange,
  903. ]
  904. ];
  905. if($bAsArray)
  906. return $aInputSearch;
  907. $oForm = new BxTemplFormView([]);
  908. return $oForm->genRow($aInputSearch);
  909. }
  910. protected function _getCounter()
  911. {
  912. return _t('_sys_grid_total_count', $this->_iTotalCount);
  913. }
  914. protected function _limitMaxLength ($mixedValue, $sKey, $aField, $aRow, $isDisplayPopupOnTextOverflow, $bReturnString = true)
  915. {
  916. if((int)$aField['chars_limit'] <= 0)
  917. return $mixedValue;
  918. return $this->_oFunctions->getStringWithLimitedLength([strip_tags($mixedValue), $mixedValue], $aField['chars_limit'], $isDisplayPopupOnTextOverflow, $bReturnString);
  919. }
  920. protected function _convertAttrs ($aField, $sAttrName, $sClasses = false, $sStyles = false)
  921. {
  922. if (!empty($aField['hidden_on'])) {
  923. $aHiddenOn = array(
  924. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  925. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  926. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  927. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  928. );
  929. foreach ($aHiddenOn as $iHiddenOn => $sClass)
  930. if ((int)$aField['hidden_on'] & $iHiddenOn)
  931. $sClasses .= ' ' . $sClass;
  932. }
  933. return bx_convert_array2attrs(
  934. isset($aField[$sAttrName]) && is_array($aField[$sAttrName]) ? $aField[$sAttrName] : array(),
  935. $sClasses,
  936. $sStyles
  937. );
  938. }
  939. protected function _updateOrder($mixedId, $iOrder)
  940. {
  941. $oDb = BxDolDb::getInstance();
  942. $sTable = $this->_aOptions['table'];
  943. $sFieldId = $this->_aOptions['field_id'];
  944. $sFieldOrder = $this->_aOptions['field_order'];
  945. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `{$sFieldOrder}` = ? WHERE `{$sFieldId}` = ?", $iOrder, $mixedId);
  946. return $oDb->query($sQuery);
  947. }
  948. protected function _delete ($mixedId)
  949. {
  950. $oDb = BxDolDb::getInstance();
  951. $sTable = $this->_aOptions['table'];
  952. $sFieldId = $this->_aOptions['field_id'];
  953. $sQuery = $oDb->prepare("DELETE FROM `{$sTable}` WHERE `{$sFieldId}` = ?", $mixedId);
  954. return $oDb->query($sQuery);
  955. }
  956. protected function _enable ($mixedId, $isChecked)
  957. {
  958. $oDb = BxDolDb::getInstance();
  959. $sTable = $this->_aOptions['table'];
  960. $sFieldId = $this->_aOptions['field_id'];
  961. $sFieldActive = $this->_aOptions['field_active'];
  962. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `$sFieldActive` = ? WHERE `{$sFieldId}` = ?", $this->_switcherChecked2State($isChecked), $mixedId);
  963. return $oDb->query($sQuery);
  964. }
  965. protected function _addJsCss()
  966. {
  967. if ($this->_aOptions['field_order']) {
  968. $this->_oTemplate->addJs(array(
  969. 'jquery-ui/jquery-ui.custom.min.js',
  970. 'URI.min.js',
  971. ));
  972. }
  973. $this->_oTemplate->addJs('BxDolGrid.js');
  974. $this->_oTemplate->addCss('grid.css');
  975. $this->_oTemplate->addJsTranslation([
  976. '_sys_grid_confirmation',
  977. '_sys_grid_secret_reveal',
  978. '_sys_grid_secret_hide',
  979. '_sys_grid_secret_password_prompt',
  980. ]);
  981. }
  982. }
  983. /** @} */