OXID eShop CE  4.10.2
 All Classes Namespaces Files Functions Variables Pages
report_conversion_rate.php
Go to the documentation of this file.
1 <?php
2 
3 if (!class_exists("report_conversion_rate")) {
9  class Report_conversion_rate extends report_base
10  {
11 
17  protected $_sThisTemplate = "report_conversion_rate.tpl";
18 
24  public function drawReport()
25  {
26  $oDb = oxDb::getDb();
27 
28  $oSmarty = $this->getSmarty();
29  $sTimeFrom = $oDb->quote(date("Y-m-d H:i:s", strtotime($oSmarty->_tpl_vars['time_from'])));
30  $sTimeTo = $oDb->quote(date("Y-m-d H:i:s", strtotime($oSmarty->_tpl_vars['time_to'])));
31 
32  // orders
33  if ($oDb->getOne("select * from oxlogs where oxtime >= $sTimeFrom and oxtime <= $sTimeTo")) {
34  return true;
35  }
36 
37  // orders
38  if ($oDb->getOne("select 1 from oxorder where oxorderdate >= $sTimeFrom and oxorderdate <= $sTimeTo")) {
39  return true;
40  }
41  }
42 
46  public function visitor_month()
47  {
48  $myConfig = $this->getConfig();
49  $oDb = oxDb::getDb();
50 
51  $aDataX = array();
52  $aDataY = array();
53 
54  $dTimeTo = strtotime(oxRegistry::getConfig()->getRequestParameter("time_to"));
55  $dTimeFrom = mktime(23, 59, 59, date("m", $dTimeTo) - 12, date("d", $dTimeTo), date("Y", $dTimeTo));
56 
57  $sTimeTo = $oDb->quote(date("Y-m-d H:i:s", $dTimeTo));
58  $sTimeFrom = $oDb->quote(date("Y-m-d H:i:s", $dTimeFrom));
59 
60  // orders
61  $sSQL = "select oxtime, count(*) as nrof from oxlogs where oxtime >= $sTimeFrom and oxtime <= $sTimeTo group by oxsessid";
62  $aTemp = array();
63  for ($i = 1; $i <= 12; $i++) {
64  $aTemp[date("m/Y", mktime(23, 59, 59, date("m", $dTimeFrom) + $i, date("d", $dTimeFrom), date("Y", $dTimeFrom)))] = 0;
65  }
66 
67  $rs = $oDb->execute($sSQL);
68  if ($rs != false && $rs->recordCount() > 0) {
69  while (!$rs->EOF) {
70  $aTemp[date("m/Y", strtotime($rs->fields[0]))]++;
71  $rs->moveNext();
72  }
73  }
74 
75  $aDataX2 = array();
76  $aDataX3 = array();
77 
78  foreach ($aTemp as $key => $value) {
79  $aDataX[$key] = $value;
80  $aDataX2[$key] = 0;
81  $aDataX3[$key] = 0;
82  $aDataY[] = $key;
83  }
84 
85  // orders
86  $sSQL = "select oxorderdate from oxorder where oxorderdate >= $sTimeFrom and oxorderdate <= $sTimeTo order by oxorderdate";
87  $rs = $oDb->execute($sSQL);
88  if ($rs != false && $rs->recordCount() > 0) {
89  while (!$rs->EOF) {
90  $sKey = date("m/Y", strtotime($rs->fields[0]));
91  if (isset($aDataX2[$sKey])) {
92  $aDataX2[$sKey]++;
93  }
94  $rs->moveNext();
95  }
96  }
97 
98  header("Content-type: image/png");
99 
100  // New graph with a drop shadow
101  $graph = new Graph(800, 600, "auto");
102 
103  $graph->setBackgroundImage($myConfig->getImageDir(true) . "/reportbgrnd.jpg", BGIMG_FILLFRAME);
104 
105  // Use a "text" X-scale
106  $graph->setScale("textlin");
107  $graph->setY2Scale("lin");
108  $graph->y2axis->setColor("red");
109 
110  // Label align for X-axis
111  $graph->xaxis->setLabelAlign('center', 'top', 'right');
112 
113  // Label align for Y-axis
114  $graph->yaxis->setLabelAlign('right', 'bottom');
115 
116  $graph->setShadow();
117  // Description
118  $graph->xaxis->setTickLabels($aDataY);
119 
120 
121  // Set title and subtitle
122  $graph->title->set("Monat");
123 
124  // Use built in font
125  $graph->title->setFont(FF_FONT1, FS_BOLD);
126 
127  $aDataFinalX = array();
128  foreach ($aDataX as $dData) {
129  $aDataFinalX[] = $dData;
130  }
131 
132  // Create the bar plot
133  $l2plot = new LinePlot($aDataFinalX);
134  $l2plot->setColor("navy");
135  $l2plot->setWeight(2);
136  $l2plot->setLegend("Besucher");
137  //$l1plot->SetBarCenter();
138  $l2plot->value->setColor("navy");
139  $l2plot->value->setFormat('% d');
140  $l2plot->value->hideZero();
141  $l2plot->value->show();
142 
143  $aDataFinalX2 = array();
144  foreach ($aDataX2 as $dData) {
145  $aDataFinalX2[] = $dData;
146  }
147 
148  // Create the bar plot
149  $l3plot = new LinePlot($aDataFinalX2);
150  $l3plot->setColor("orange");
151  $l3plot->setWeight(2);
152  $l3plot->setLegend("Bestellungen");
153  //$l1plot->SetBarCenter();
154  $l3plot->value->setColor('orange');
155  $l3plot->value->setFormat('% d');
156  $l3plot->value->hideZero();
157  $l3plot->value->show();
158 
159  //conversion rate graph
160  $l1datay = array();
161  for ($iCtr = 0; $iCtr < count($aDataFinalX); $iCtr++) {
162  if ($aDataFinalX[$iCtr] != 0 && $aDataFinalX2[$iCtr] != 0) {
163  $l1datay[] = 100 / ($aDataFinalX[$iCtr] / $aDataFinalX2[$iCtr]);
164  } else {
165  $l1datay[] = 0;
166  }
167  }
168 
169  $l1plot = new LinePlot($l1datay);
170  $l1plot->setColor("red");
171  $l1plot->setWeight(2);
172  $l1plot->setLegend("Conversion rate (%)");
173  $l1plot->value->setColor('red');
174  $l1plot->value->setFormat('% 0.2f%%');
175  $l1plot->value->hideZero();
176  $l1plot->value->show();
177 
178  // Create the grouped bar plot1
179  $graph->addY2($l1plot);
180  $graph->add($l2plot);
181  $graph->add($l3plot);
182 
183  // Finally output the image
184  $graph->stroke();
185 
186  }
187 
191  public function visitor_week()
192  {
193  $myConfig = $this->getConfig();
194  $oDb = oxDb::getDb();
195 
196  $aDataX = array();
197  $aDataX2 = array();
198  $aDataX3 = array();
199  $aDataY = array();
200 
201  $sTimeTo = $oDb->quote(date("Y-m-d H:i:s", strtotime(oxRegistry::getConfig()->getRequestParameter("time_to"))));
202  $sTimeFrom = $oDb->quote(date("Y-m-d H:i:s", strtotime(oxRegistry::getConfig()->getRequestParameter("time_from"))));
203 
204  $sSQL = "select oxtime, count(*) as nrof from oxlogs where oxtime >= $sTimeFrom and oxtime <= $sTimeTo group by oxsessid order by oxtime";
205  $aTemp = array();
206  $rs = $oDb->execute($sSQL);
207  if ($rs != false && $rs->recordCount() > 0) {
208  while (!$rs->EOF) {
209  //$aTemp[date( "W", strtotime( $rs->fields[0]))]++;
210  $aTemp[oxRegistry::get("oxUtilsDate")->getWeekNumber($myConfig->getConfigParam('iFirstWeekDay'), strtotime($rs->fields[0]))]++;
211  $rs->moveNext();
212  }
213  }
214 
215 
216  // initializing
217  list($iFrom, $iTo) = $this->getWeekRange();
218  for ($i = $iFrom; $i < $iTo; $i++) {
219  $aDataX[$i] = 0;
220  $aDataX2[$i] = 0;
221  $aDataX3[$i] = 0;
222  $aDataY[] = "KW " . $i;
223  }
224 
225  foreach ($aTemp as $key => $value) {
226  $aDataX[$key] = $value;
227  $aDataX2[$key] = 0;
228  $aDataX3[$key] = 0;
229  $aDataY[] = "KW " . $key;
230  }
231 
232  // buyer
233  $sSQL = "select oxorderdate from oxorder where oxorderdate >= $sTimeFrom and oxorderdate <= $sTimeTo order by oxorderdate";
234  $rs = $oDb->execute($sSQL);
235  if ($rs != false && $rs->recordCount() > 0) {
236  while (!$rs->EOF) {
237  $sKey = oxRegistry::get("oxUtilsDate")->getWeekNumber($myConfig->getConfigParam('iFirstWeekDay'), strtotime($rs->fields[0]));
238  if (isset($aDataX2[$sKey])) {
239  $aDataX2[$sKey]++;
240  }
241  $rs->moveNext();
242  }
243  }
244 
245  // newcustomer
246  $sSQL = "select oxtime, oxsessid from oxlogs where oxtime >= $sTimeFrom and oxtime <= $sTimeTo group by oxsessid order by oxtime";
247  $rs = $oDb->execute($sSQL);
248  if ($rs != false && $rs->recordCount() > 0) {
249  while (!$rs->EOF) {
250  $sKey = oxRegistry::get("oxUtilsDate")->getWeekNumber($myConfig->getConfigParam('iFirstWeekDay'), strtotime($rs->fields[0]));
251  if (isset($aDataX3[$sKey])) {
252  $aDataX3[$sKey]++;
253  }
254  $rs->moveNext();
255  }
256  }
257 
258  header("Content-type: image/png");
259 
260  // New graph with a drop shadow
261  $graph = new Graph(max(800, count($aDataX) * 80), 600);
262 
263  $graph->setBackgroundImage($myConfig->getImageDir(true) . "/reportbgrnd.jpg", BGIMG_FILLFRAME);
264 
265  // Use a "text" X-scale
266  $graph->setScale("textlin");
267  $graph->setY2Scale("lin");
268  $graph->y2axis->setColor("red");
269 
270  // Label align for X-axis
271  $graph->xaxis->setLabelAlign('center', 'top', 'right');
272 
273  // Label align for Y-axis
274  $graph->yaxis->setLabelAlign('right', 'bottom');
275 
276  $graph->setShadow();
277  // Description
278  $graph->xaxis->setTickLabels($aDataY);
279 
280 
281  // Set title and subtitle
282  $graph->title->set("Woche");
283 
284  // Use built in font
285  $graph->title->setFont(FF_FONT1, FS_BOLD);
286 
287  $aDataFinalX = array();
288  foreach ($aDataX as $dData) {
289  $aDataFinalX[] = $dData;
290  }
291 
292  // Create the bar plot
293  $l2plot = new LinePlot($aDataFinalX);
294  $l2plot->setColor("navy");
295  $l2plot->setWeight(2);
296  $l2plot->setLegend("Besucher");
297  $l2plot->value->setColor("navy");
298  $l2plot->value->setFormat('% d');
299  $l2plot->value->hideZero();
300  $l2plot->value->show();
301 
302  $aDataFinalX2 = array();
303  foreach ($aDataX2 as $dData) {
304  $aDataFinalX2[] = $dData;
305  }
306 
307  // Create the bar plot
308  $l3plot = new LinePlot($aDataFinalX2);
309  $l3plot->setColor("orange");
310  $l3plot->setWeight(2);
311  $l3plot->setLegend("Bestellungen");
312  //$l1plot->SetBarCenter();
313  $l3plot->value->setColor("orange");
314  $l3plot->value->setFormat('% d');
315  $l3plot->value->hideZero();
316  $l3plot->value->show();
317 
318  //conversion rate graph
319  $l1datay = array();
320  for ($iCtr = 0; $iCtr < count($aDataFinalX); $iCtr++) {
321  if ($aDataFinalX[$iCtr] != 0 && $aDataFinalX2[$iCtr] != 0) {
322  $l1datay[] = 100 / ($aDataFinalX[$iCtr] / $aDataFinalX2[$iCtr]);
323  } else {
324  $l1datay[] = 0;
325  }
326  }
327  $l1plot = new LinePlot($l1datay);
328  $l1plot->setColor("red");
329  $l1plot->setWeight(2);
330  $l1plot->setLegend("Conversion rate (%)");
331  $l1plot->value->setColor('red');
332  $l1plot->value->setFormat('% 0.4f%%');
333  $l1plot->value->hideZero();
334  $l1plot->value->show();
335 
336  // Create the grouped bar plot
337  $graph->addY2($l1plot);
338  $graph->add($l2plot);
339  $graph->add($l3plot);
340 
341  // Finally output the image
342  $graph->stroke();
343  }
344  }
345 }