PageRenderTime 77ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/server/components/controls/report/ArrowReportChartCtrl.php

http://arrowplatform.googlecode.com/
PHP | 280 lines | 185 code | 71 blank | 24 comment | 29 complexity | 0625b2ac204ceddfc4de1e228da0f6e0 MD5 | raw file
  1. <?php
  2. ArrowController::importModel("org.arrowplatform.model.ArrowDataSource");
  3. ArrowController::importModel("org.arrowplatform.controls.common.criteria.IArrowCriteriaUser");
  4. class ArrowReportChartCtrl extends ArrowTagContainer {
  5. protected $dataSource;
  6. private $criteria = null;
  7. private $jsData = false;
  8. public function configure(){
  9. $this->addProperties(array(
  10. "width" => "100%",
  11. "height" => 215,
  12. "title" => false,
  13. "xAxisField" => null,
  14. "style" => "",
  15. "type" => "bar",
  16. "xName" => "",
  17. "debug" => "0",
  18. "yName" => ""
  19. ));
  20. $this->addRequiredProperties(array("xAxisField"));
  21. $this->addStateProperties(array());
  22. }
  23. public function init(){
  24. $jsData = array(
  25. //"criteria" => base64_encode( serialize($this->criteria) ),
  26. "selectedFn" => $this->getProperty("selectedFn"),
  27. //"path" => "./server/standardModels/org/arrowplatform/controls/report/resources/lib2/"
  28. );
  29. $jsData["title"] = $this->getProperty("title");
  30. $jsData["type"] = $this->getProperty("type");
  31. $jsData["width"] = $this->getProperty("width");
  32. $jsData["height"] = $this->getProperty("height");
  33. $jsData["xName"] = $this->getProperty("xName");
  34. $jsData["yName"] = $this->getProperty("yName");
  35. $jsData["height"] = $this->getProperty("height");
  36. $jsData["targetId"] = 'ctrl-report-chart'.$this->getGlobalId();
  37. $jsData["objectId"] = 'ctrl-report-chart-obj'.$this->getGlobalId();
  38. $y = explode( ";", $this->getProperty("xAxisField"));
  39. $dataSource = $this->getFirstChild("ArrowCtrlDatasource");
  40. /*$criteria = $dataSource->getCriteria();
  41. if($criteria){
  42. foreach($y as $el )
  43. $criteria->addOrderBy($el);
  44. }*/
  45. $dataS = $dataSource->getData(true);
  46. //print "<pre>".ArrowOrmMysql::getLastQuery()."</pre>";
  47. //print_r( $dataS);
  48. //exit();
  49. $tmp = array();
  50. if(isset($dataS[0]) && is_array($dataS[0]) && is_array(reset($dataS[0]))){
  51. foreach($dataS as $key => $row){
  52. $tmp[$key] = array();
  53. foreach( $row as $values )
  54. $tmp[$key] = array_merge($tmp[$key], $values);
  55. }
  56. $dataS = $tmp;
  57. }
  58. $yValues = array();
  59. foreach($dataS as $row){
  60. $str = "";
  61. foreach($y as $el ) $str.=$row[$el]." ";
  62. $yValues[] = $str;
  63. }
  64. $yValues = array_unique($yValues);
  65. /*print "<pre>";
  66. print_r($yValues);
  67. print "</pre>";*/
  68. $jsData["categories"] = $yValues;
  69. $seriesData = array();
  70. foreach($yValues as $yValue){
  71. foreach($this->getChildren("ArrowReportSerieCtrl") as $serie){
  72. $field = $serie->getProperty("field");
  73. $serieName = $serie->getProperty("title");
  74. $testField = $serie->getProperty("testField");
  75. $testValue = $serie->getProperty("testValue");
  76. $seriesData[$serieName][$yValue] = 0;
  77. foreach($dataS as $row){
  78. $res = ""; foreach($y as $el) $res.=$row[$el]." ";
  79. if( $res == $yValue){
  80. if($testField && $row[$testField] != $testValue)
  81. continue;
  82. //if(!isset($seriesData[$serieName][$yValue])){
  83. $seriesData[$serieName][$yValue]+= $row[$field];
  84. //}else{
  85. //exit($serieName. " ".$yValue ." ".$testValue);
  86. //}
  87. }
  88. }
  89. }
  90. }
  91. $series = array();
  92. $i = 0;
  93. foreach($seriesData as $serieName => $values){
  94. $series[$i]["name"] = $serieName;
  95. foreach($values as $key=> $row){
  96. if($jsData["type"] == "pie")
  97. $series[$i]["data"][] = array($key, $row);// $data.= "\n\t\t\t<set value='$row' label='{$key}' />";
  98. else
  99. $series[$i]["data"][] = $row;
  100. }
  101. $i++;
  102. }
  103. $jsData["series"] = $series;
  104. $this->registerCssFile( "./server/standardModels/org/arrowplatform/controls/report/resources/ArrowReportChartCtrl.css" );
  105. $this->registerJs( get_class($this), dirname(__FILE__)."/resources/ArrowReportChartCtrl.js", "#ctrl-".$this->getId(), $jsData);
  106. $this->registerJsFile( "./server/standardModels/org/arrowplatform/controls/report/resources/lib/highcharts.src.js" );
  107. $this->registerJsFile( "./server/standardModels/org/arrowplatform/controls/report/resources/lib/exporting.js" );
  108. }
  109. public function generateBlockStart(){
  110. $str='<div class="ctrl-report-chart" style="position: relative;width: '.$this->getProperty("width").'px; height: '.$this->getProperty("height").'px;" type="'.$this->getProperty("type").'">';
  111. //$str.='<div class="ctrl-report-chart-code"><pre>'.htmlentities( $this->generateChartCode()).'</pre></div>';
  112. //$str.='<div style="position:absolute; width: 220px; height: 20px;z-index:100; background-color: white;">&nbsp;</div>';
  113. //$str.='<textarea class="ctrl-report-chart-code" style="display: none;">'.$this->generateChartCode().'</textarea>';
  114. $str.='<div id="ctrl-report-chart'.$this->getGlobalId().'">';
  115. return $str;
  116. }
  117. public function generateChartCode(){
  118. $debug = $this->getProperty("debug");
  119. $type = $this->getProperty("type");
  120. $y = explode( ";", $this->getProperty("xAxisField"));
  121. $dataSource = $this->getFirstChild("ArrowCtrlDatasource");
  122. foreach($y as $el )
  123. $dataSource->getCriteria()->addOrderBy($el);
  124. $dataS = $dataSource->getData(true);
  125. $data = "";
  126. $tmp = array();
  127. if(isset($dataS[0]) && is_array($dataS[0])){
  128. foreach($dataS as $key => $row){
  129. $tmp[$key] = array();
  130. foreach( $row as $values )
  131. $tmp[$key] = array_merge($tmp[$key], $values);
  132. }
  133. }
  134. $dataS = $tmp;
  135. if($debug){
  136. print "<pre>";
  137. print_r($dataS);
  138. print "</pre>";
  139. return;
  140. }
  141. $yValues = array();
  142. foreach($dataS as $row){
  143. $str = "";
  144. foreach($y as $el ) $str.=$row[$el]." ";
  145. $yValues[] = $str;
  146. }
  147. $yValues = array_unique($yValues);
  148. if($type != "circle" ){
  149. $data.= "\n\t\t<categories>";
  150. foreach( $yValues as $field){
  151. $data.= "\n\t\t\t<category label='{$field}' />";
  152. }
  153. $data.= "\n\t\t</categories>";
  154. }
  155. $seriesData = array();
  156. foreach($yValues as $yValue){
  157. foreach($this->getChildren("ArrowReportSerieCtrl") as $serie){
  158. $field = $serie->getProperty("field");
  159. $serieName = $serie->getProperty("title");
  160. $testField = $serie->getProperty("testField");
  161. $testValue = $serie->getProperty("testValue");
  162. $seriesData[$serieName][$yValue] = 0;
  163. foreach($dataS as $row){
  164. $res = ""; foreach($y as $el) $res.=$row[$el]." ";
  165. if( $res == $yValue){
  166. if($testField && $row[$testField] != $testValue)
  167. continue;
  168. //if(!isset($seriesData[$serieName][$yValue])){
  169. $seriesData[$serieName][$yValue]+= $row[$field];
  170. //}else{
  171. //exit($serieName. " ".$yValue ." ".$testValue);
  172. //}
  173. }
  174. }
  175. }
  176. }
  177. foreach($seriesData as $serieName => $values){
  178. if($type != "circle")
  179. $data.= "\n\t\t<dataset seriesName='{$serieName}' >";
  180. foreach($values as $key=> $row){
  181. $data.= "\n\t\t\t<set value='$row' label='{$key}' />";
  182. }
  183. if($type != "circle")
  184. $data.= "\n\t\t</dataset>";
  185. }
  186. $add = "showValues='1'";
  187. if($type == "circle")
  188. $add = "showPercentValues='1'";
  189. return "<chart caption='".$this->getProperty("title")."' xAxisName='".$this->getProperty("xName")."' yAxisName='".$this->getProperty("yName")."' {$add} >{$data}\n</chart>";
  190. }
  191. public function generateBlockEnd(){
  192. return "</div><div style=\"clear:both;\"></div></div>";
  193. }
  194. public function setCriteria( $criteria ){
  195. throw new ArrowException("[ArrowBindCtrl] Set criteria is not possible");
  196. }
  197. public function getCriteria(){
  198. if($this->criteria == null){
  199. $criteriaCont = $this->getFirstChild("ArrowCtrlCriteria");
  200. $this->criteria = $criteriaCont->getCriteria();
  201. $req = ArrowRequestContext::getDefault() ;
  202. if( isset( $req["unit"] ) ) {
  203. $this->criteria['AuthUser']->addCondition( 'units_flat', $req["unit"], Criteria::C_BIT_AND );
  204. }
  205. }
  206. return $this->criteria;
  207. }
  208. }
  209. ?>