PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/components/bitrix/report.view/templates/admin_mobile/template.php

https://gitlab.com/alexprowars/bitrix
PHP | 512 lines | 429 code | 51 blank | 32 comment | 108 complexity | 2e2fdf85db8c9bea450cd14c46126ae2 MD5 | raw file
  1. <?
  2. if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
  3. if (!empty($arResult['ERROR']))
  4. {
  5. echo $arResult['ERROR'];
  6. return false;
  7. }
  8. ?>
  9. <div class="mobile-admin-report-ptitle"><?=GetMessage('REPORT_REPORT').": ".$arResult['report']['TITLE']?></div>
  10. <?
  11. // determine column data type
  12. function getResultColumnDataType(&$viewColumnInfo, &$customColumnTypes, $helperClassName)
  13. {
  14. $dataType = null;
  15. if (is_array($customColumnTypes) && array_key_exists($viewColumnInfo['fieldName'], $customColumnTypes))
  16. {
  17. $dataType = $customColumnTypes[$viewColumnInfo['fieldName']];
  18. }
  19. else
  20. {
  21. /** @var Bitrix\Main\Entity\Field[] $viewColumnInfo */
  22. $dataType = call_user_func(array($helperClassName, 'getFieldDataType'), $viewColumnInfo['field']);
  23. }
  24. if (!empty($viewColumnInfo['prcnt']))
  25. {
  26. $dataType = 'float';
  27. }
  28. else if (!empty($viewColumnInfo['aggr']))
  29. {
  30. if ($viewColumnInfo['aggr'] == 'COUNT_DISTINCT') $dataType = 'integer';
  31. else if ($viewColumnInfo['aggr'] == 'GROUP_CONCAT') $dataType = 'string';
  32. else if ($dataType == 'boolean')
  33. {
  34. if ($viewColumnInfo['aggr'] == 'MIN' || $viewColumnInfo['aggr'] == 'AVG'
  35. || $viewColumnInfo['aggr'] == 'MAX' || $viewColumnInfo['aggr'] == 'SUM'
  36. || $viewColumnInfo['aggr'] == 'COUNT_DISTINCT')
  37. {
  38. $dataType = 'integer';
  39. }
  40. }
  41. }
  42. return $dataType;
  43. }
  44. //<!-- result table -->
  45. if ($arResult['groupingMode'] === true): // show result using a grouping mode
  46. function groupingReportResultHtml(&$arParams, &$arResult, $level = 0, $arRowSet = array())
  47. {
  48. $strHtml = '';
  49. $total = array();
  50. $arViewColumns = &$arResult['viewColumns'];
  51. $arData = &$arResult['data'];
  52. // static variables
  53. static $arGroups = array();
  54. static $arColumns = array();
  55. static $nGroups = 0;
  56. static $nColumns = 0;
  57. static $arValueTypes = array();
  58. // initialize static variables
  59. if ($level === 0)
  60. {
  61. foreach($arViewColumns as $viewColumnIndex => $viewColumn)
  62. {
  63. if ($viewColumn['grouping']) $arGroups[$nGroups++] = $viewColumnIndex;
  64. else $arColumns[$nColumns++] = $viewColumnIndex;
  65. $arValueTypes[$viewColumnIndex] = getResultColumnDataType($viewColumn, $arResult['customColumnTypes'],
  66. $arResult['helperClassName']);
  67. }
  68. }
  69. $nRows = count($arRowSet);
  70. $bUseRowSet = ($nRows > 0) ? true : false;
  71. if (!$bUseRowSet) $nRows = count($arData);
  72. if ($nGroups > 0)
  73. {
  74. // grouping table header
  75. if ($level === 0)
  76. {
  77. $strHtml .=
  78. "\t".'<div class="order_infoblock">'.PHP_EOL.
  79. "\t\t".'<div class="order_infoblock_content">'.PHP_EOL.
  80. "\t\t\t".'<table class="bx_table_item_remainder">'.PHP_EOL.
  81. "\t\t\t\t".'<thead>'.PHP_EOL;
  82. $strHtml .=
  83. "\t\t\t\t\t".'<tr class="mobile-admin-report-header">'.PHP_EOL.
  84. "\t\t\t\t\t\t".'<td>'.htmlspecialcharsbx($arViewColumns[$groupColumnIndex]['humanTitle']);
  85. $bFirst = true;
  86. foreach ($arGroups as $groupColumnIndex)
  87. {
  88. if($bFirst)
  89. $strHtml .= htmlspecialcharsbx($arViewColumns[$groupColumnIndex]['humanTitle']);
  90. else
  91. $strHtml .= '<span>'.htmlspecialcharsbx($arViewColumns[$groupColumnIndex]['humanTitle']).'</span>';
  92. $bFirst = false;
  93. }
  94. $strHtml .= "\t\t\t\t\t\t".'</td>'.PHP_EOL;
  95. foreach ($arColumns as $viewColumnIndex)
  96. {
  97. $strHtml .="\t\t\t\t\t\t".'<td defaultSort="'.$arViewColumns[$viewColumnIndex]['defaultSort'].'">'.
  98. htmlspecialcharsbx($arViewColumns[$viewColumnIndex]['humanTitle'])."\t\t\t\t\t\t".'</td>'.PHP_EOL;
  99. }
  100. $strHtml .=
  101. "\t\t\t\t\t".'</tr>'.PHP_EOL;
  102. $strHtml .=
  103. "\t\t\t\t".'</thead>'.PHP_EOL;
  104. }
  105. if ($nRows > 0)
  106. {
  107. // init total
  108. if ($nColumns > 0) foreach (array_keys($arColumns) as $columnIndex) $total[$columnIndex] = null;
  109. if ($level < $nGroups)
  110. {
  111. // fill group arrays
  112. $arGroupValues = array();
  113. $arGroupValuesIndexes = array();
  114. $rowNumber = 0;
  115. $groupDataType = $arValueTypes[$arGroups[$level]];
  116. $dataIndex = null;
  117. reset($arData);
  118. while ($rowNumber++ < $nRows)
  119. {
  120. // get index
  121. if ($bUseRowSet)
  122. {
  123. $dataIndex = current($arRowSet);
  124. next($arRowSet);
  125. }
  126. else
  127. {
  128. $dataIndex = key($arData);
  129. next($arData);
  130. }
  131. // fill index and value of group
  132. $arGroupValuesIndexes[] = $dataIndex;
  133. $groupValue = $arData[$dataIndex][$arViewColumns[$arGroups[$level]]['resultName']];
  134. if ($groupDataType === 'date' || $groupDataType === 'datetime') // convert value for a sorting
  135. {
  136. $groupValue = MakeTimeStamp($groupValue, CSite::GetDateFormat('SHORT'));
  137. }
  138. // magic glue
  139. if (is_array($groupValue)) $groupValue = join(' / ', $groupValue);
  140. $arGroupValues[] = $groupValue;
  141. }
  142. // determine sort options
  143. $groupSortOption = SORT_STRING;
  144. $groupSortDirection = SORT_ASC;
  145. if (in_array($groupDataType, array('date','datetime','integer','float'))) $groupSortOption = SORT_NUMERIC;
  146. if ($arGroups[$level] == $arResult['sort_id'])
  147. {
  148. if ($arResult['sort_type'] != 'ASC') $groupSortDirection = SORT_DESC;
  149. }
  150. // sort group
  151. array_multisort($arGroupValues, $groupSortOption, $groupSortDirection,
  152. $arGroupValuesIndexes, SORT_NUMERIC, SORT_ASC);
  153. // recursive scan
  154. $prev = null;
  155. $newRowSet = array();
  156. $nGroupValues = count($arGroupValues);
  157. $nSubRows = 0;
  158. $closeTBody = false;
  159. for ($i = 0; $i < $nGroupValues; $i++)
  160. {
  161. $cur = $arGroupValues[$i];
  162. if ($i == 0) $prev = $cur;
  163. $bLastValue = ($nGroupValues - 1 == $i);
  164. if ($cur != $prev || $bLastValue)
  165. {
  166. $n = ($bLastValue && $cur != $prev) ? 2 : 1;
  167. while ($n-- > 0)
  168. {
  169. if ($bLastValue && $cur == $prev) $newRowSet[] = $arGroupValuesIndexes[$i];
  170. $arGroupingResult = groupingReportResultHtml($arParams, $arResult, $level+1, $newRowSet);
  171. $arSubTotal = $arGroupingResult['total'];
  172. $strSubHtml = $arGroupingResult['html'];
  173. unset($arGroupingResult);
  174. $newRowSet = array();
  175. if (!$bLastValue) $newRowSet[] = $arGroupValuesIndexes[$i];
  176. $prev = $cur;
  177. // show row
  178. $groupValueIndex = ($bLastValue && $n === 0) ? $i : $i - 1;
  179. $groupValueKey = $arViewColumns[$arGroups[$level]]['resultName'];
  180. $groupValue = $arData[$arGroupValuesIndexes[$groupValueIndex]][$groupValueKey];
  181. // magic glue
  182. if (is_array($groupValue)) $groupValue = join(' / ', $groupValue);
  183. if ($level == 0)
  184. {
  185. if($closeTBody)
  186. $strHtml .= "\t\t\t\t".'</tbody>'.PHP_EOL;
  187. $strHtml .= "\t\t\t\t".'<tbody>'.PHP_EOL;
  188. $closeTBody = true;
  189. $rowClass = ' class="mobile-admin-report-category"';
  190. }
  191. else
  192. $rowClass ='';
  193. $strHtml .= "\t\t\t\t\t".'<tr'.$rowClass.'>'.PHP_EOL."\t\t\t\t\t\t".'<td>';
  194. if ($level == $nGroups - 1)
  195. $strHtml .= '<span>'.$groupValue.'</span>';
  196. else
  197. $strHtml .= $groupValue;
  198. $strHtml .= "\t\t\t\t\t\t".'</td>'.PHP_EOL;
  199. foreach ($arSubTotal as $k => $subValue)
  200. {
  201. $bGroupingSubtotal = $arViewColumns[$arColumns[$k]]['grouping_subtotal'];
  202. if ($bGroupingSubtotal || $level == $nGroups - 1)
  203. {
  204. $finalSubValue = $subValue;
  205. if (method_exists($arResult['helperClassName'], 'formatResultGroupingTotal'))
  206. {
  207. // format subtotal value
  208. $subValueKey = $arViewColumns[$arColumns[$k]]['resultName'];
  209. call_user_func(
  210. array($arResult['helperClassName'], 'formatResultGroupingTotal'),
  211. array(
  212. 'k' => $subValueKey,
  213. 'v' => &$finalSubValue,
  214. 'cInfo' => &$arViewColumns[$arColumns[$k]]
  215. )
  216. );
  217. }
  218. }
  219. else
  220. {
  221. $finalSubValue = '&nbsp;';
  222. }
  223. $strHtml .=
  224. "\t\t\t\t\t\t".'<td>'.$finalSubValue.'</td>'.PHP_EOL;
  225. }
  226. $strHtml .=
  227. "\t\t\t\t\t".'</tr>'.PHP_EOL;
  228. $strHtml .= $strSubHtml;
  229. // total += subtotal
  230. if ($nColumns > 0)
  231. {
  232. foreach ($arColumns as $columnIndex => $viewColumnIndex)
  233. {
  234. $columnDataType = $arValueTypes[$viewColumnIndex];
  235. if ($columnDataType === 'integer' || $columnDataType === 'float')
  236. {
  237. if (is_string($arSubTotal[$columnIndex]))
  238. {
  239. $arSubTotal[$columnIndex] = str_replace(' ', '', $arSubTotal[$columnIndex]);
  240. }
  241. $total[$columnIndex] += $arSubTotal[$columnIndex];
  242. }
  243. }
  244. $nSubRows++;
  245. }
  246. } // while ($n-- > 0)
  247. }
  248. else $newRowSet[] = $arGroupValuesIndexes[$i];
  249. }
  250. // calculate average values
  251. if ($nSubRows > 1)
  252. {
  253. foreach ($arColumns as $columnIndex => $viewColumnIndex)
  254. {
  255. if ($arViewColumns[$viewColumnIndex]['aggr'] === 'AVG'
  256. || $arViewColumns[$viewColumnIndex]['grouping_aggr'] === 'AVG'
  257. )
  258. {
  259. $total[$columnIndex] = $total[$columnIndex] / $nSubRows;
  260. }
  261. }
  262. }
  263. }
  264. else // last level
  265. {
  266. if ($nColumns > 0)
  267. {
  268. $rowNumber = 0;
  269. while ($rowNumber++ < $nRows)
  270. {
  271. // get index
  272. if ($bUseRowSet)
  273. {
  274. $dataIndex = current($arRowSet);
  275. next($arRowSet);
  276. }
  277. else
  278. {
  279. $dataIndex = key($arData);
  280. next($arData);
  281. }
  282. // total += values
  283. foreach ($arColumns as $columnIndex => $viewColumnIndex)
  284. {
  285. $columnDataType = $arValueTypes[$viewColumnIndex];
  286. if ($nRows == 1)
  287. {
  288. $dataValueKey = $arViewColumns[$viewColumnIndex]['resultName'];
  289. $dataValue = $arData[$dataIndex][$dataValueKey];
  290. if ($columnDataType === 'integer' || $columnDataType === 'float' && is_string($dataValue))
  291. {
  292. $dataValue = str_replace(' ', '', $dataValue);
  293. }
  294. // normal value
  295. $total[$columnIndex] = $dataValue;
  296. }
  297. else if ($columnDataType === 'integer' || $columnDataType === 'float')
  298. {
  299. $dataValue = $arData[$dataIndex][$arViewColumns[$viewColumnIndex]['resultName']];
  300. if (is_string($dataValue)) $dataValue = str_replace(' ', '', $dataValue);
  301. $total[$columnIndex] += $dataValue;
  302. }
  303. }
  304. }
  305. // calculate average values
  306. if ($nRows > 1)
  307. {
  308. foreach ($arColumns as $columnIndex => $viewColumnIndex)
  309. {
  310. if ($arViewColumns[$viewColumnIndex]['aggr'] === 'AVG'
  311. || $arViewColumns[$viewColumnIndex]['grouping_aggr'] === 'AVG'
  312. )
  313. {
  314. $total[$columnIndex] = $total[$columnIndex] / $nRows;
  315. }
  316. }
  317. }
  318. }
  319. }
  320. }
  321. // show total
  322. if ($level === 0)
  323. {
  324. if (count($total) > 0)
  325. {
  326. // show total check
  327. $bShowTotal = false;
  328. foreach ($total as $k => $v)
  329. {
  330. if ($arViewColumns[$arColumns[$k]]['grouping_subtotal'])
  331. {
  332. $bShowTotal = true;
  333. break;
  334. }
  335. }
  336. if ($bShowTotal)
  337. {
  338. $strHtml .=
  339. "\t\t\t".'</tbody><tbody><tr style="background: none repeat scroll 0 0 #EDF2D4;">'.PHP_EOL.
  340. "\t\t\t\t".'<td>'.htmlspecialcharsbx(GetMessage('REPORT_TOTAL')).':</td>'.PHP_EOL;
  341. foreach ($total as $k => $v)
  342. {
  343. $bGroupingSubtotal = $arViewColumns[$arColumns[$k]]['grouping_subtotal'];
  344. if ($bGroupingSubtotal)
  345. {
  346. $finalTotalValue = $v;
  347. if (method_exists($arResult['helperClassName'], 'formatResultGroupingTotal'))
  348. {
  349. // format subtotal value
  350. $subValueKey = $arViewColumns[$arColumns[$k]]['resultName'];
  351. call_user_func(
  352. array($arResult['helperClassName'], 'formatResultGroupingTotal'),
  353. array(
  354. 'k' => $subValueKey,
  355. 'v' => &$finalTotalValue,
  356. 'cInfo' => &$arViewColumns[$arColumns[$k]]
  357. )
  358. );
  359. }
  360. }
  361. else
  362. {
  363. $finalTotalValue = '&nbsp;';
  364. }
  365. $strHtml .=
  366. "\t\t\t\t".'<td>'.$finalTotalValue.'</td>'.PHP_EOL;
  367. }
  368. $strHtml .=
  369. "\t\t\t".'</tr>'.PHP_EOL;
  370. }
  371. }
  372. $strHtml .= "\t".'</tbody></table>'.PHP_EOL.'</div></div>'.PHP_EOL;
  373. }
  374. }
  375. return array('total' => $total, 'html' => $strHtml);
  376. }
  377. $arGroupingResult = groupingReportResultHtml($arParams, $arResult);
  378. echo $arGroupingResult['html'];
  379. unset($arGroupingResult);
  380. ?>
  381. <?php
  382. // </editor-fold>
  383. ?>
  384. <?php else: // show result using a default mode?>
  385. <div class="order_infoblock">
  386. <div class="order_infoblock_title"><?=$arResult['report']['TITLE']?></div>
  387. <div class="order_infoblock_content">
  388. <table class="bx_table_item_remainder">
  389. <!-- head -->
  390. <thead>
  391. <tr>
  392. <? $i = 0; foreach($arResult['viewColumns'] as $colId => $col): ?>
  393. <?
  394. $i++;
  395. // sorting
  396. //$defaultSort = 'DESC';
  397. $defaultSort = $col['defaultSort'];
  398. ?>
  399. <td <?php
  400. ?> defaultSort="<?=$defaultSort?>">
  401. <?=htmlspecialcharsbx($col['humanTitle'])?>
  402. </td>
  403. <? endforeach; ?>
  404. </tr>
  405. </thead>
  406. <!-- data -->
  407. <tbody>
  408. <? foreach ($arResult['data'] as $row): ?>
  409. <tr>
  410. <? $i = 0; foreach($arResult['viewColumns'] as $col): ?>
  411. <?
  412. $i++;
  413. $finalValue = $row[$col['resultName']];
  414. // add link
  415. if (!empty($col['href']) && !empty($row['__HREF_'.$col['resultName']]))
  416. {
  417. if (is_array($finalValue))
  418. {
  419. // grc
  420. foreach ($finalValue as $grcIndex => $v)
  421. {
  422. $finalValue[$grcIndex] = '<a href="'
  423. .$arResult['grcData'][$col['resultName']][$grcIndex]['__HREF_'.$col['resultName']]
  424. .'">'.$v.'</a>';
  425. }
  426. }
  427. elseif(mb_strlen($row[$col['resultName']]))
  428. {
  429. $finalValue = '<a href="'.$row['__HREF_'.$col['resultName']].'">'.$row[$col['resultName']].'</a>';
  430. }
  431. }
  432. // magic glue
  433. if (is_array($finalValue))
  434. {
  435. $finalValue = join(' / ', $finalValue);
  436. }
  437. ?>
  438. <td><?=$finalValue?></td>
  439. <? endforeach; ?>
  440. </tr>
  441. <? endforeach; ?>
  442. <tr>
  443. <? $i = 0; foreach($arResult['viewColumns'] as $col): ?>
  444. <?
  445. $i++;
  446. if (array_key_exists('TOTAL_'.$col['resultName'], $arResult['total']))
  447. {
  448. $finalValue = $arResult['total']['TOTAL_'.$col['resultName']];
  449. }
  450. else $finalValue = '&mdash;';
  451. ?>
  452. <td></td>
  453. <? endforeach; ?>
  454. </tr>
  455. </tbody>
  456. </table>
  457. </div></div>
  458. <?php endif; ?>
  459. <script type="text/javascript">
  460. app.setPageTitle({title: "<?=GetMessage('REPORT_REPORT').": ".$arResult['report']['TITLE']?>"});
  461. </script>