PageRenderTime 70ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/common/lib/Form/Class.SumMultiView.inc.php

https://github.com/xrg/a2billing
PHP | 567 lines | 427 code | 77 blank | 63 comment | 99 complexity | a29131eaba675e6a8f8725bdcb18253a MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. require_once("Class.FormViews.inc.php");
  3. class DummyRObj {
  4. public $dbg=0;
  5. public function DummyRobj($dbg){
  6. $this->dbg=$dbg;
  7. }
  8. public function debug($str){
  9. if ($this->dbg)
  10. echo $str."<br>\n";
  11. }
  12. public function NeedRaw(){
  13. return false;
  14. }
  15. };
  16. /** This is a collection of sums, in the same page:
  17. Use it to display various statistics per-something from a model.
  18. each group of sums contains one element of display information..
  19. */
  20. class SumMultiView extends FormView {
  21. public $list_class ='sumlist';
  22. public $titlerow; ///< A row spanning all columns, used for title
  23. public $ncols = null; ///< Numer of columns for titles, used as colspan
  24. public $sums = array();
  25. public $plots = array();
  26. protected function RenderHead(){
  27. }
  28. protected function performSumQuery(&$summ,&$form,&$dbhandle,&$robj){
  29. $robj->debug("ncols:" .$this->ncols);
  30. if ($form->FG_DEBUG>3)
  31. $robj->debug("SumMultiView! Building Sum query..");
  32. if (empty($summ['fns'])){
  33. $robj->debug("No sum functions!");
  34. return;
  35. }
  36. $query_fields = array();
  37. $query_outerfields = array();
  38. $query_clauses = array();
  39. $query_grps = array();
  40. $query_table = $form->model_table;
  41. $query_outertable = '';
  42. $need_raw = $robj->NeedRaw();
  43. foreach($form->model as $fld){
  44. $fld->buildSumQuery($dbhandle, $summ['fns'],
  45. $query_fields,$query_outerfields,$query_table,$query_outertable,
  46. $query_clauses, $query_grps,$form);
  47. }
  48. if (!strlen($query_table)){
  49. $robj->debug("No sum table!");
  50. return;
  51. }
  52. $QUERY = 'SELECT ';
  53. if (count($query_fields)==0) {
  54. $robj->debug("No sum query fields!");
  55. return;
  56. }
  57. if (isset($summ['clauses']))
  58. $query_clauses = array_merge($query_clauses,$summ['clauses']);
  59. $QUERY .= implode(', ', $query_fields);
  60. $QUERY .= ' FROM ' . $query_table;
  61. if (count($query_clauses))
  62. $QUERY .= ' WHERE ' . implode(' AND ', $query_clauses);
  63. if (!empty($query_grps) &&
  64. (!isset($summ['group']) || ($summ['group'] != false)) )
  65. $QUERY .= ' GROUP BY ' . implode(', ', $query_grps);
  66. //Try to see if we can order
  67. if (!empty($form->order)){
  68. if (empty($query_grps) || in_array($form->order,$query_grps))
  69. $ordert =$form->order;
  70. else {
  71. // search again for expressions
  72. foreach ($form->model as $fld)
  73. if ($fld->fieldname == $form->order){
  74. if (in_array($fld->fieldexpr,$query_grps))
  75. $ordert = $fld->fieldexpr;
  76. break;
  77. }
  78. }
  79. }
  80. elseif (!empty($summ['order']))
  81. $ordert = $summ['order'];
  82. else
  83. $ordert = null;
  84. if(!empty($ordert)){
  85. $QUERY .= " ORDER BY $ordert";
  86. if (!empty($form->sens)){
  87. if (strtolower($form->sens)=='desc')
  88. $QUERY .= " DESC";
  89. } elseif (!empty($summ['sens']) && (strtolower($summ['sens'])=='desc'))
  90. $QUERY .= " DESC";
  91. }
  92. if (!empty($summ['limit']))
  93. $QUERY .= ' LIMIT '.$summ['limit'];
  94. $needouter=false;
  95. if(!empty($query_outertable))
  96. $needouter=true;
  97. else{
  98. foreach($query_outerfields as $qof)
  99. if (!is_string($qof)){
  100. $needouter=true;
  101. break;
  102. }
  103. }
  104. if ($needouter){
  105. $qf2=array();
  106. foreach ($query_outerfields as $qof)
  107. if (is_string($qof))
  108. $qf2[]=$qof;
  109. elseif (is_array($qof)){
  110. if ($need_raw)
  111. $qf2[]=$qof[1]. ' AS '.$qof[1] .'_raw';
  112. $qf2[]=$qof[0].' AS '.$qof[1];
  113. }
  114. $QUERY = 'SELECT '.implode(', ', $qf2). ' FROM '.
  115. '('.$QUERY .') AS innerfoo '.$query_outertable;
  116. }
  117. $QUERY .= ';';
  118. if ($form->FG_DEBUG>3)
  119. $robj->debug("SUM QUERY: $QUERY");
  120. if (!empty($this->queryreplace)){
  121. $rrep=false;
  122. if (isset($this->queryreplace['query'])){
  123. $REPQRY=str_alparams($this->queryreplace['query'],
  124. array(clauses => implode(' AND ', $query_clauses),
  125. fields => implode(', ', $query_fields),
  126. grps => $query_grps,
  127. table => $query_table));
  128. if ($form->FG_DEBUG>3)
  129. $robj->debug("REP QUERY: $REPQRY");
  130. $resRep=$dbhandle->Execute($REPQRY);
  131. if (! $resRep)
  132. $robj->debug("RepQuery Failed: ". nl2br(htmlspecialchars($dbhandle->ErrorMsg())));
  133. else
  134. $rrep = $resRep->fetchRow();
  135. }
  136. if (!$rrep)
  137. $rrep = $this->queryreplace['default'];
  138. $QUERY = str_aldbparams($dbhandle,$QUERY,$rrep);
  139. if ($form->FG_DEBUG>3)
  140. $robj->debug("SUM QUERY after rep: $QUERY");
  141. }
  142. // Perform the query
  143. $res =$dbhandle->Execute($QUERY);
  144. if (! $res){
  145. $robj->debug("Query Failed: ". nl2br(htmlspecialchars($dbhandle->ErrorMsg())));
  146. return;
  147. }
  148. return $res;
  149. }
  150. public function Render(&$form){
  151. $this->RenderHead();
  152. // For convenience, ref the dbhandle locally
  153. $dbhandle = &$form->a2billing->DBHandle();
  154. if ($this->ncols ==null)
  155. $this->ncols = count($form->model);
  156. // else echo str_params(_("No %1 found!"),array($form->model_name_s),1);
  157. // Render the table anyway..
  158. ?>
  159. <TABLE cellPadding="2" cellSpacing="2" align='center' class="<?= $this->list_class?>">
  160. <thead><tr>
  161. <?php
  162. if (!empty($this->titlerow)){
  163. ?><tr class="title"><td colspan="<?= $this->ncols?>"><?= $this->titlerow ?></td></tr>
  164. <?php
  165. }
  166. foreach ($form->model as $fld)
  167. $fld->RenderListHead($form);
  168. ?>
  169. </tr></thead>
  170. <tbody>
  171. <?php
  172. $row_num = 0;
  173. foreach($this->sums as $summ) {
  174. $dro=new DummyRObj($form->FG_DEBUG);
  175. $res = $this->performSumQuery($summ,$form,$dbhandle, $dro);
  176. if (!$res)
  177. continue;
  178. if (!empty($summ['title'])){
  179. ?><tr class="sumtitle"><td colspan="<?= $this->ncols?>"><?= $summ['title'] ?></td></tr>
  180. <?php
  181. }
  182. while ($row = $res->fetchRow()){
  183. if ($row_num % 2)
  184. echo '<tr class="odd">';
  185. else echo '<tr>';
  186. foreach ($form->model as $fld)
  187. if ($fld) $fld->RenderListCell($row,$form);
  188. echo "</tr>\n";
  189. $row_num++;
  190. }
  191. }
  192. if ($row_num ==0){
  193. ?><tr><td colspan="<?= $this->ncols?>"><?= _("No sums found!") ?></td></tr>
  194. <?php
  195. }
  196. ?>
  197. </tbody>
  198. </table>
  199. <?php
  200. }
  201. public function RenderSpecial($rmode,&$form,&$robj){
  202. if ($rmode!='get-data')
  203. return;
  204. if ($robj instanceof DataObj){
  205. $dbhandle = &$form->a2billing->DBHandle();
  206. $plot = $this->plots[$robj->code];
  207. if (empty($plot))
  208. throw new Exception("Unknown plot");
  209. $row_num = 0;
  210. $res = $this->performSumQuery($plot,$form,$dbhandle,$robj);
  211. if (!$res){
  212. $robj->debug("Could not perform query!");
  213. return false;
  214. }
  215. // Store the names of fields into an array
  216. $resfs=array();
  217. for($i=0;$i<$res->FieldCount();$i++)
  218. $resfs[] = $res->FetchField($i)->name;
  219. $robj->prepare($plot,$resfs);
  220. //at the first row, we think a little more..
  221. while ($row = $res->fetchRow()){
  222. $robj->PlotRow($row);
  223. }
  224. }
  225. /* elseif ($robj instanceof DataObjXYZ){
  226. $dbhandle = &$form->a2billing->DBHandle();
  227. $plot = $this->plots[$robj->code];
  228. if (empty($plot))
  229. throw new Exception("Unknown plot");
  230. $xdata = array();
  231. $ydata = array();
  232. $xkey = $plot['x'];
  233. $ykey = $plot['y'];
  234. $x2key = $plot['x2'];
  235. if (!empty($plot['x2t']))
  236. $x2t=$plot['x2t'];
  237. else
  238. $x2t=$x2key;
  239. $row_num = 0;
  240. $res = $this->performSumQuery($plot,$form,$dbhandle,false);
  241. if (!$res) {
  242. $robj->debug("Could not perform query!");
  243. return false;
  244. }
  245. while ($row = $res->fetchRow()){
  246. $robj->PlotXYZ($row[$xkey], $row[$ykey], $row[$x2key]);
  247. if ($robj_leg instanceof DataLegend)
  248. $robj_leg->Addlegend($row[$x2key], $row[$x2t]);
  249. }
  250. } */
  251. else {
  252. throw new Exception("Unknown object to get data to..");
  253. }
  254. }
  255. public function RenderGraph2(&$form,&$graph){
  256. $gmode= $form->getpost_single('graph');
  257. if ($form->FG_DEBUG>1)
  258. echo "RenderGraph!\n";
  259. $dbhandle = &$form->a2billing->DBHandle();
  260. $tsum = $this->plots[$gmode];
  261. if (!$tsum)
  262. return false;
  263. //print_r ($tsum);
  264. $graph->title->Set($tsum[title]);
  265. $dro=new DummyRObj($form->FG_DEBUG);
  266. $res = $this->performSumQuery($tsum,$form,$dbhandle,$dro);
  267. if (! empty($tsum['subtitles'])){
  268. $graph->tabtitle->Set($tsum['subtitles']);
  269. $graph->tabtitle->SetWidth(TABTITLE_WIDTHFULL);
  270. }
  271. if (! empty($tsum['backgroundgradient']) && $tsum['backgroundgradient'])
  272. $graph->SetBackgroundGradient('#FFFFFF','#CDDEFF:0.8',GRAD_HOR,BGRAD_PLOT);
  273. if (! empty($tsum['rowcolor']) && $tsum['rowcolor']){
  274. $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#CDDEFF@0.5');
  275. $graph->xgrid->SetColor('gray@0.5');
  276. $graph->ygrid->SetColor('gray@0.5');
  277. }
  278. switch($tsum['type']){
  279. case 'bar':
  280. $xdata = array();
  281. $ydata = array();
  282. $xkey = $tsum['x'];
  283. $ykey = $tsum['y'];
  284. while ($row = $res->fetchRow()){
  285. $xdata[] = $row[$xkey];
  286. $ydata[] = $row[$ykey];
  287. }
  288. if (! empty($tsum['xlabelangle'])){
  289. $graph->xaxis->SetLabelAngle($tsum['xlabelangle']);
  290. if ($tsum['xlabelangle']<0)
  291. $graph->xaxis->SetLabelAlign('left');
  292. }
  293. if (! empty($tsum['xlabelfont']))
  294. $graph->xaxis->SetFont($tsum['xlabelfont']);
  295. else
  296. $graph->xaxis->SetFont(FF_DEJAVU);
  297. $graph->xaxis->SetTickLabels($xdata);
  298. $bplot = new BarPlot($ydata);
  299. $graph->Add($bplot);
  300. if ($form->FG_DEBUG>2){
  301. echo "X data: ";
  302. print_r($xdata);
  303. echo "\n Y data: ";
  304. print_r($ydata);
  305. }
  306. if ($form->FG_DEBUG>1)
  307. echo "Added Bar plot";
  308. break;
  309. case 'abar':
  310. /* $graph->legend->SetColor('navy');
  311. $graph->legend->SetFillColor('gray@0.8');
  312. $graph->legend->SetLineWeight(1);
  313. //$graph->legend->SetFont(FF_ARIAL,FS_BOLD,8);
  314. $graph->legend->SetShadow('gray@0.4',3);
  315. $graph->legend->SetAbsPos(15,130,'right','bottom');*/
  316. //$graph->legend->SetFont(FF_DEJAVU);
  317. $xdata = array();
  318. $ydata = array();
  319. $yleg =array(); //holds the labels for y axises
  320. $xkey = $tsum['x'];
  321. $x2key = $tsum['x2'];
  322. if (!empty($tsum['x2t']))
  323. $x2t=$tsum['x2t'];
  324. else
  325. $x2t=$x2key;
  326. $ykey = $tsum['y'];
  327. while ($row = $res->fetchRow()){
  328. // assume first order is by x-value
  329. if (empty($xdata) || (end($xdata) != $row[$xkey]))
  330. $xdata[] = $row[$xkey];
  331. // and assume second order is the x2 key..
  332. if (!isset($ydata[$row[$x2key]]))
  333. $ydata[$row[$x2key]]=array();
  334. end($xdata); // move pointer to end
  335. $ydata[$row[$x2key]][key($xdata)] = $row[$ykey];
  336. $yleg[$row[$x2key]] = $row[$x2t];
  337. }
  338. // Now, fill with zeroes all other vars..
  339. foreach($ydata as &$yd)
  340. foreach($xdata as $xk => $xv)
  341. if (!isset($yd[$xk]))
  342. $yd[$xk]=0;
  343. if (! empty($tsum['xlabelangle'])){
  344. $graph->xaxis->SetLabelAngle($tsum['xlabelangle']);
  345. if ($tsum['xlabelangle']<0)
  346. $graph->xaxis->SetLabelAlign('left');
  347. }
  348. if (! empty($tsum['xlabelfont']))
  349. $graph->xaxis->SetFont($tsum['xlabelfont']);
  350. else
  351. $graph->xaxis->SetFont(FF_DEJAVU);
  352. $graph->xaxis->SetTickLabels($xdata);
  353. $accplots=array();
  354. $colors=array();
  355. $colors[]="yellow@0.3";
  356. $colors[]="purple@0.3";
  357. $colors[]="green@0.3";
  358. $colors[]="blue@0.3";
  359. $colors[]="red@0.3";
  360. $i=0;
  361. foreach($ydata as $yk => $ycol){
  362. $accplots[]= new BarPlot($ycol);
  363. end($accplots)->SetFillColor($colors[$i++]);
  364. if (!empty($yleg[$yk]))
  365. end($accplots)->SetLegend($yleg[$yk]);
  366. else
  367. end($accplots)->SetLegend(_("(none)"));
  368. }
  369. $bplot = new AccBarPlot($accplots);
  370. $graph->Add($bplot);
  371. if ($form->FG_DEBUG>2){
  372. echo "X data: ";
  373. print_r($xdata);
  374. echo "\n Y data: ";
  375. print_r($ydata);
  376. }
  377. if ($form->FG_DEBUG>1)
  378. echo "Added Bar plot";
  379. break;
  380. default:
  381. if ($form->FG_DEBUG>1)
  382. echo "Unknown graph type: ".$tsum['type'] . "\n";
  383. }
  384. return true;
  385. }
  386. };
  387. /** A multiple column (paginated), multi-sum (queries) view */
  388. class Multi2SumView extends SumMultiView {
  389. public $page_cols = 1; ///< Number of columns to display
  390. public $page_rows = 20; ///< Maximum number of rows per column/page
  391. public $page_rows_first = 7;
  392. public $page_rows_last = 5;
  393. protected function RenderTHead(&$form, &$col, &$row, &$mrows){
  394. //if ( $n < $num_rows * $num_cols)
  395. // $n += $num_rows_first;
  396. if ( (($col++) % $this->page_cols) == 0){
  397. echo "<tr>";
  398. $row++;
  399. }
  400. echo "<td width=\"" . (100/$this->page_cols) . "%\">";
  401. if ($form->FG_DEBUG>2) echo "Col: $col/".$this->page_cols ." <br>\n";
  402. ?>
  403. <table cellPadding="2" cellSpacing="2" align='center' class="<?= $this->list_class?>">
  404. <thead><tr>
  405. <?php
  406. if (!empty($this->titlerow) && ($row ==0)){
  407. ?><tr class="title"><td colspan="<?= $this->ncols?>"><?= $this->titlerow ?></td></tr>
  408. <?php
  409. }
  410. foreach ($form->model as $fld)
  411. $fld->RenderListHead($form);
  412. ?>
  413. </tr></thead>
  414. <tbody>
  415. <?php
  416. // Set the mrows to the appropriate value:
  417. if ($row == 0)
  418. $mrows= $this->page_rows_first;
  419. else
  420. $mrows= $this->page_rows;
  421. }
  422. protected function RenderTFoot(&$col,&$row) {
  423. echo "</tbody></table></td>";
  424. if ( ($col % $this->page_cols) == 0)
  425. echo "</tr>";
  426. echo "\n";
  427. }
  428. public function Render(&$form){
  429. $this->RenderHead();
  430. // For convenience, ref the dbhandle locally
  431. $dbhandle = &$form->a2billing->DBHandle();
  432. if ($this->ncols ==null)
  433. $this->ncols = count($form->model);
  434. // else echo str_params(_("No %1 found!"),array($form->model_name_s),1);
  435. // Variables that hold the table/pagination counters
  436. $in_table = false;
  437. $pg_row = 0;
  438. $row_num = 0;
  439. $col_num = 0;
  440. $mrows = 10;
  441. echo '<table class="invoice_cols">';
  442. foreach($this->sums as $summ) {
  443. if (!$in_table){
  444. $this->RenderTHead($form,$col_num,$pg_row, $mrows);
  445. $in_table = true;
  446. }
  447. $dro=new DummyRObj($form->FG_DEBUG);
  448. $res = $this->performSumQuery($summ,$form,$dbhandle,$dro);
  449. if (!$res)
  450. continue;
  451. if (!empty($summ['title'])){
  452. ?><tr class="sumtitle"><td colspan="<?= $this->ncols?>"><?= $summ['title'] ?></td></tr>
  453. <?php
  454. }
  455. while ($row = $res->fetchRow()){
  456. if (!$in_table){
  457. $this->RenderTHead($form,$col_num,$pg_row, $mrows);
  458. $in_table = true;
  459. }
  460. if ($row_num % 2)
  461. echo '<tr class="odd">';
  462. else echo '<tr>';
  463. foreach ($form->model as $fld)
  464. if ($fld) $fld->RenderListCell($row,$form);
  465. echo "</tr>\n";
  466. $row_num++;
  467. if (($row_num % $mrows) == 0){
  468. $this->RenderTFoot($col_num, $pg_row);
  469. $in_table = false;
  470. }
  471. }
  472. }
  473. if ($row_num ==0){
  474. ?><tr><td colspan="<?= $this->ncols?>"><?= _("No sums found!") ?></td></tr>
  475. <?php
  476. }
  477. if ($in_table){
  478. //close inner and outer tables
  479. $this->RenderTFoot($col_num, $pg_row);
  480. }
  481. echo '</table>';
  482. } // fn Render
  483. };
  484. ?>