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