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