00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Adminhtml_Block_Dashboard_Graph extends Mage_Adminhtml_Block_Dashboard_Abstract
00036 {
00037 protected $_allSeries = array();
00038 protected $_axisLabels = array();
00039 protected $_axisMaps = array();
00040
00041 protected $_dataRows = array();
00042
00043 protected $_simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
00044 protected $_extendedEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
00045
00046 const API_URL = 'http://chart.apis.google.com/chart';
00047
00048 protected $_width = '587';
00049 protected $_height = '300';
00050
00051 protected $_encoding = 'e';
00052
00053 protected $_htmlId = '';
00054
00055 public function __construct()
00056 {
00057 parent::__construct();
00058 $this->setTemplate('dashboard/graph.phtml');
00059 }
00060
00061 protected function _getTabTemplate()
00062 {
00063 return 'dashboard/graph.phtml';
00064 }
00065
00066 public function setDataRows($rows)
00067 {
00068 $this->_dataRows = (array)$rows;
00069 }
00070
00071 public function addSeries($seriesId, array $options)
00072 {
00073 $this->_allSeries[$seriesId] = $options;
00074 }
00075
00076 public function getSeries($seriesId)
00077 {
00078 if (isset($this->_allSeries[$seriesId])) {
00079 return $this->_allSeries[$seriesId];
00080 } else {
00081 return false;
00082 }
00083 }
00084
00085 public function getAllSeries()
00086 {
00087 return $this->_allSeries;
00088 }
00089
00090 public function getChartUrl($directUrl = true)
00091 {
00092 $params = array(
00093 'cht' => 'lc',
00094 'chf' => 'bg,s,f4f4f4|c,lg,90,ffffff,0.1,ededed,0',
00095 'chm' => 'B,f4d4b2,0,0,0',
00096 'chco' => 'db4814'
00097 );
00098
00099 $this->_allSeries = $this->getRowsData($this->_dataRows);
00100
00101 foreach ($this->_axisMaps as $axis => $attr){
00102 $this->setAxisLabels($axis, $this->getRowsData($attr, true));
00103 }
00104
00105 $gmtOffset = Mage::getSingleton('core/date')->getGmtOffset();
00106
00107 list ($dateStart, $dateEnd) = Mage::getResourceModel('reports/order_collection')
00108 ->getDateRange($this->getDataHelper()->getParam('period'), '', '', true);
00109
00110 $dates = array();
00111 $datas = array();
00112
00113 while($dateStart->compare($dateEnd) < 0){
00114 switch ($this->getDataHelper()->getParam('period')) {
00115 case '24h':
00116 $d = $dateStart->toString('yyyy-MM-dd HH:00');
00117 $dateStart->addHour(1);
00118 break;
00119 case '7d':
00120 case '1m':
00121 $d = $dateStart->toString('yyyy-MM-dd');
00122 $dateStart->addDay(1);
00123 break;
00124 case '1y':
00125 case '2y':
00126 $d = $dateStart->toString('yyyy-MM');
00127 $dateStart->addMonth(1);
00128 break;
00129 }
00130 foreach ($this->getAllSeries() as $index=>$serie) {
00131 if (in_array($d, $this->_axisLabels['x'])) {
00132 $datas[$index][] = (float)array_shift($this->_allSeries[$index]);
00133 } else {
00134 $datas[$index][] = 0;
00135 }
00136 }
00137 $dates[] = $d;
00138 }
00139
00140
00141
00142
00143 if (count($dates) > 8 && count($dates) < 15) {
00144 $c = 1;
00145 } else if (count($dates) >= 15){
00146 $c = 2;
00147 } else {
00148 $c = 0;
00149 }
00150
00151
00152
00153 $i=0;
00154 foreach ($dates as $k => $d) {
00155 if ($i == $c) {
00156 $dates[$k] = $d;
00157 $i = 0;
00158 } else {
00159 $dates[$k] = '';
00160 $i++;
00161 }
00162 }
00163
00164 $this->_axisLabels['x'] = $dates;
00165 $this->_allSeries = $datas;
00166
00167
00168 if ($this->_encoding == "s") {
00169
00170 $params['chd'] = "s:";
00171 $dataDelimiter = "";
00172 $dataSetdelimiter = ",";
00173 $dataMissing = "_";
00174 } else {
00175
00176 $params['chd'] = "e:";
00177 $dataDelimiter = "";
00178 $dataSetdelimiter = ",";
00179 $dataMissing = "__";
00180 }
00181
00182
00183 foreach ($this->getAllSeries() as $index => $serie) {
00184 $localmaxlength[$index] = sizeof($serie);
00185 $localmaxvalue[$index] = max($serie);
00186 $localminvalue[$index] = min($serie);
00187 }
00188
00189 if (is_numeric($this->_max)) {
00190 $maxvalue = $this->_max;
00191 } else {
00192 $maxvalue = max($localmaxvalue);
00193 }
00194 if (is_numeric($this->_min)) {
00195 $minvalue = $this->_min;
00196 } else {
00197 $minvalue = min($localminvalue);
00198 }
00199
00200 $maxlength = max($localmaxlength);
00201 if ($minvalue >= 0 && $maxvalue >= 0) {
00202 $miny = 0;
00203 if ($maxvalue > 10) {
00204 $p = pow(10, $this->_getPow($maxvalue));
00205 $maxy = (ceil($maxvalue/$p))*$p;
00206 $yLabels = range($miny, $maxy, $p);
00207 } else {
00208 $maxy = ceil($maxvalue+1);
00209 $yLabels = range($miny, $maxy, 1);
00210 }
00211 $yrange = $maxy;
00212 $yorigin = 0;
00213 }
00214
00215 $chartdata = array();
00216
00217 foreach ($this->getAllSeries() as $index => $serie) {
00218 $thisdataarray = $serie;
00219 if ($this->_encoding == "s") {
00220
00221 for ($j = 0; $j < sizeof($thisdataarray); $j++) {
00222 $currentvalue = $thisdataarray[$j];
00223 if (is_numeric($currentvalue)) {
00224 $ylocation = round((strlen($this->_simpleEncoding)-1) * ($yorigin + $currentvalue) / $yrange);
00225 array_push($chartdata, substr($this->_simpleEncoding, $ylocation, 1) . $dataDelimiter);
00226 } else {
00227 array_push($chartdata, $dataMissing . $dataDelimiter);
00228 }
00229 }
00230
00231 } else {
00232
00233 for ($j = 0; $j < sizeof($thisdataarray); $j++) {
00234 $currentvalue = $thisdataarray[$j];
00235 if (is_numeric($currentvalue)) {
00236 if ($yrange) {
00237 $ylocation = (4095 * ($yorigin + $currentvalue) / $yrange);
00238 } else {
00239 $ylocation = 0;
00240 }
00241 $firstchar = floor($ylocation / 64);
00242 $secondchar = $ylocation % 64;
00243 $mappedchar = substr($this->_extendedEncoding, $firstchar, 1) . substr($this->_extendedEncoding, $secondchar, 1);
00244 array_push($chartdata, $mappedchar . $dataDelimiter);
00245 } else {
00246 array_push($chartdata, $dataMissing . $dataDelimiter);
00247 }
00248 }
00249
00250 }
00251 array_push($chartdata, $dataSetdelimiter);
00252 }
00253 $buffer = implode('', $chartdata);
00254
00255 $buffer = rtrim($buffer, $dataSetdelimiter);
00256 $buffer = rtrim($buffer, $dataDelimiter);
00257 $buffer = str_replace(($dataDelimiter . $dataSetdelimiter), $dataSetdelimiter, $buffer);
00258
00259 $params['chd'] .= $buffer;
00260
00261 $labelBuffer = "";
00262 $valueBuffer = array();
00263 $rangeBuffer = "";
00264
00265 if (sizeof($this->_axisLabels) > 0) {
00266 $params['chxt'] = implode(',', array_keys($this->_axisLabels));
00267 $indexid = 0;
00268 foreach ($this->_axisLabels as $idx=>$labels){
00269 if ($idx == 'x') {
00270
00271
00272
00273 foreach ($this->_axisLabels[$idx] as $_index=>$_label) {
00274 if ($_label != '') {
00275 switch ($this->getDataHelper()->getParam('period')) {
00276 case '24h':
00277 $this->_axisLabels[$idx][$_index] = $this->formatTime($_label, 'short', false);
00278 break;
00279 case '7d':
00280 case '1m':
00281 $this->_axisLabels[$idx][$_index] = $this->formatDate($_label);
00282 break;
00283 case '1y':
00284 case '2y':
00285 $formats = Mage::app()->getLocale()->getTranslationList('datetime');
00286 $format = isset($formats['yyMM']) ? $formats['yyMM'] : 'MM/yyyy';
00287 $format = str_replace(array("yyyy", "yy", "MM"), array("Y", "y", "m"), $format);
00288 $this->_axisLabels[$idx][$_index] = date($format, strtotime($_label));
00289 break;
00290 }
00291 } else {
00292 $this->_axisLabels[$idx][$_index] = '';
00293 }
00294
00295 }
00296
00297 $tmpstring = implode('|', $this->_axisLabels[$idx]);
00298
00299 $valueBuffer[] = $indexid . ":|" . $tmpstring;
00300 if (sizeof($this->_axisLabels[$idx]) > 1) {
00301 $deltaX = 100/(sizeof($this->_axisLabels[$idx])-1);
00302 } else {
00303 $deltaX = 100;
00304 }
00305 } else if ($idx == 'y') {
00306 $valueBuffer[] = $indexid . ":|" . implode('|', $yLabels);
00307 if (sizeof($yLabels)-1) {
00308 $deltaY = 100/(sizeof($yLabels)-1);
00309 } else {
00310 $deltaY = 100;
00311 }
00312
00313 $rangeBuffer = $indexid . "," . $miny . "," . $maxy . "|";
00314 }
00315 $indexid++;
00316 }
00317 $params['chxl'] = implode('|', $valueBuffer);
00318 };
00319
00320
00321 $params['chs'] = $this->getWidth().'x'.$this->getHeight();
00322
00323 if (isset($deltaX) && isset($deltaY)) {
00324 $params['chg'] = $deltaX . ',' . $deltaY . ',1,0';
00325 }
00326
00327
00328 if ($directUrl) {
00329 $p = array();
00330 foreach ($params as $name => $value) {
00331 $p[] = $name . '=' .urlencode($value);
00332 }
00333 return self::API_URL . '?' . implode('&', $p);
00334 } else {
00335 $params = array('ga' => urlencode(base64_encode(serialize($params))));
00336 return $this->getUrl('*/*/tunnel', $params);
00337 }
00338 }
00339
00340 protected function getRowsData($attributes, $single = false)
00341 {
00342 $items = $this->getCollection()->getItems();
00343 $options = array();
00344 foreach ($items as $item){
00345 if ($single) {
00346 $options[] = $item->getData($attributes);
00347 } else {
00348 foreach ((array)$attributes as $attr){
00349 $options[$attr][] = $item->getData($attr);
00350 }
00351 }
00352 }
00353 return $options;
00354 }
00355
00356
00357 public function setAxisLabels($axis, $labels)
00358 {
00359 $this->_axisLabels[$axis] = $labels;
00360 }
00361
00362
00363 public function setHtmlId($htmlId)
00364 {
00365 $this->_htmlId = $htmlId;
00366 }
00367
00368
00369 public function getHtmlId()
00370 {
00371 return $this->_htmlId;
00372 }
00373
00374
00375 protected function _getPow($number)
00376 {
00377 $pow = 0;
00378 while ($number >= 10) {
00379 $number = $number/10;
00380 $pow++;
00381 }
00382 return $pow;
00383 }
00384
00385
00386 protected function getWidth()
00387 {
00388 return $this->_width;
00389 }
00390
00391
00392 protected function getHeight()
00393 {
00394 return $this->_height;
00395 }
00396 }