Quick Search:

View

Revision:

Diff

Diff from 1860 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/banner.php

Annotated File View

hpdl
1
1 <?php
2 /*
mattice
151
3   $Id: banner.php 1860 2009-03-06 23:25:01Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
1860
8   Copyright (c) 2007 osCommerce
hpdl
1
9
hpdl
1498
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License v2 (1991)
12   as published by the Free Software Foundation.
hpdl
1
13 */
14
hpdl
1860
15 /**
16  * The osC_Banner class manages the banners shown throughout the online store
17  */
18
hpdl
1
19   class osC_Banner {
20
hpdl
1860
21 /**
22  * Controls whether banners should be shown multiple times on the page
23  *
24  * @var boolean
25  * @access private
26  */
hpdl
1
27
hpdl
1860
28     private $_show_duplicates_in_group = false;
hpdl
1
29
hpdl
1860
30 /**
31  * A placeholder that keeps the banner ID in memory when checking if a banner exists before showing it
32  *
33  * @var id
34  * @access private
35  */
hpdl
1
36
hpdl
1860
37     private $_exists_id;
38
39 /**
40  * An array containing the banner IDs already shown on the page
41  *
42  * @var array
43  * @access private
44  */
45
46     private $_shown_ids = array();
47
48 /**
49  * Constructor
50  *
51  * @access public
52  */
53
54     public function __construct() {
55       if ( SERVICE_BANNER_SHOW_DUPLICATE == 'True' ) {
56         $this->_show_duplicates_in_group = true;
hpdl
1
57       }
58     }
59
hpdl
1860
60 /**
61  * Activate a banner that has been on schedule
62  *
63  * @param int $id The ID of the banner to activate
64  * @access public
65  * @return boolean
66  */
hpdl
1
67
hpdl
1860
68     public function activate($id) {
69       return $this->_setStatus($id, true);
hpdl
1
70     }
71
hpdl
1860
72 /**
73  * Activate all banners on schedule
74  *
75  * @access public
76  */
77
78     public function activateAll() {
hpdl
1
79       global $osC_Database;
80
81       $Qbanner = $osC_Database->query('select banners_id, date_scheduled from :table_banners where date_scheduled != ""');
82       $Qbanner->bindTable(':table_banners', TABLE_BANNERS);
83       $Qbanner->execute();
84
hpdl
1860
85       while ( $Qbanner->next() ) {
86         if ( osC_DateTime::getNow() >= $Qbanner->value('date_scheduled') ) {
87           $this->activate($Qbanner->valueInt('banners_id'));
hpdl
1
88         }
89       }
90     }
91
hpdl
1860
92 /**
93  * Deactivate a banner
94  *
95  * @param int $id The ID of the banner to deactivate
96  * @access public
97  * @return boolean
98  */
99
100     public function expire($id) {
101       return $this->_setStatus($id, false);
hpdl
1
102     }
103
hpdl
1860
104 /**
105  * Deactivate all banners that have passed their schedule
106  *
107  * @access public
108  */
109
110     public function expireAll() {
hpdl
1
111       global $osC_Database;
112
113       $Qbanner = $osC_Database->query('select b.banners_id, b.expires_date, b.expires_impressions, sum(bh.banners_shown) as banners_shown from :table_banners b, :table_banners_history bh where b.status = 1 and b.banners_id = bh.banners_id group by b.banners_id');
114       $Qbanner->bindTable(':table_banners', TABLE_BANNERS);
115       $Qbanner->bindTable(':table_banners_history', TABLE_BANNERS_HISTORY);
116       $Qbanner->execute();
117
hpdl
1860
118       while ( $Qbanner->next() ) {
119         if ( !osc_empty($Qbanner->value('expires_date')) ) {
120           if ( osC_DateTime::getNow() >= $Qbanner->value('expires_date') ) {
121             $this->expire($Qbanner->valueInt('banners_id'));
hpdl
1
122           }
hpdl
1860
123         } elseif ( !osc_empty($Qbanner->valueInt('expires_impressions')) ) {
124           if ( ($Qbanner->valueInt('expires_impressions') > 0) && ($Qbanner->valueInt('banners_shown') >= $Qbanner->valueInt('expires_impressions')) ) {
125             $this->expire($Qbanner->valueInt('banners_id'));
126           }
hpdl
1
127         }
128       }
129     }
130
hpdl
1860
131 /**
132  * Check if an existing banner is active
133  *
134  * @param int $id The ID of the banner to check
135  * @access public
136  * @return boolean
137  */
138
139     public function isActive($id) {
hpdl
1
140       global $osC_Database;
141
hpdl
1860
142       $Qbanner = $osC_Database->query('select status from :table_banners where banners_id = :banners_id');
hpdl
1
143       $Qbanner->bindTable(':table_banners', TABLE_BANNERS);
144       $Qbanner->bindInt(':banners_id', $id);
145       $Qbanner->execute();
146
hpdl
1860
147       return ( $Qbanner->valueInt('status') === 1 );
hpdl
1
148     }
149
hpdl
1860
150 /**
151  * Check if banners exist in a group. If banners exists, select a random entry and assign its ID to $_exists_id.
152  *
153  * @param string $group The group to check in
154  * @access public
155  * @return boolean
156  */
157
158     public function exists($group) {
hpdl
1
159       global $osC_Database;
160
161       $Qbanner = $osC_Database->query('select banners_id from :table_banners where status = 1 and banners_group = :banners_group');
162
hpdl
1860
163       if ( ($this->_show_duplicates_in_group === false) && (sizeof($this->_shown_ids) > 0) ) {
hpdl
1
164         $Qbanner->appendQuery('and banners_id not in (:banner_ids)');
165         $Qbanner->bindRaw(':banner_ids', implode(',', $this->_shown_ids));
166       }
167
168       $Qbanner->bindTable(':table_banners', TABLE_BANNERS);
169       $Qbanner->bindValue(':banners_group', $group);
170       $Qbanner->executeRandom();
171
hpdl
1860
172       if ( $Qbanner->numberOfRows() > 0 ) {
hpdl
1
173         $this->_exists_id = $Qbanner->valueInt('banners_id');
174
175         return true;
176       }
177
178       return false;
179     }
180
hpdl
1860
181 /**
182  * Display a banner. If no ID is passed, the value defined in $_exists_id is used.
183  *
184  * @param int $id The ID of the banner to show
185  * @access public
186  * @return string
187  */
188
189     public function display($id = null) {
hpdl
1
190       global $osC_Database;
191
hpdl
1860
192       $banner_string = '';
hpdl
1
193
hpdl
1860
194       if ( empty($id) && isset($this->_exists_id) && is_numeric($this->_exists_id) ) {
hpdl
1
195         $id = $this->_exists_id;
196
197         unset($this->_exists_id);
198       }
199
200       $Qbanner = $osC_Database->query('select * from :table_banners where banners_id = :banners_id and status = 1');
201       $Qbanner->bindTable(':table_banners', TABLE_BANNERS);
202       $Qbanner->bindInt(':banners_id', $id);
203       $Qbanner->execute();
204
hpdl
1860
205       if ( $Qbanner->numberOfRows() > 0 ) {
206         if ( !osc_empty($Qbanner->value('banners_html_text')) ) {
hpdl
1
207           $banner_string = $Qbanner->value('banners_html_text');
208         } else {
hpdl
754
209           $banner_string = osc_link_object(osc_href_link(FILENAME_REDIRECT, 'action=banner&goto=' . $Qbanner->valueInt('banners_id')), osc_image(DIR_WS_IMAGES . $Qbanner->value('banners_image'), $Qbanner->value('banners_title')), 'target="_blank"');
hpdl
1
210         }
211
212         $this->_updateDisplayCount($Qbanner->valueInt('banners_id'));
213
hpdl
1860
214         if ( $this->_show_duplicates_in_group === false ) {
hpdl
1
215           $this->_shown_ids[] = $Qbanner->valueInt('banners_id');
216         }
217       }
218
219       return $banner_string;
220     }
221
hpdl
1860
222 /**
223  * Return the URL assigned to the banner
224  *
225  * @param int $id The ID of the banner
226  * @param boolean $increment_click_flag A flag to state if the banner click count should be incremented
227  * @access public
228  * @return string
229  */
230
231     public function getURL($id, $increment_click_flag = false) {
hpdl
1
232       global $osC_Database;
233
hpdl
1860
234       $url = '';
hpdl
1
235
236       $Qbanner = $osC_Database->query('select banners_url from :table_banners where banners_id = :banners_id and status = 1');
237       $Qbanner->bindTable(':table_banners', TABLE_BANNERS);
238       $Qbanner->bindInt(':banners_id', $id);
239       $Qbanner->execute();
240
hpdl
1860
241       if ( $Qbanner->numberOfRows() > 0 ) {
hpdl
1
242         $url = $Qbanner->value('banners_url');
243
hpdl
1860
244         if ( $increment_click_flag === true ) {
hpdl
1
245           $this->_updateClickCount($id);
246         }
247       }
248
249       return $url;
250     }
251
hpdl
1860
252 /**
253  * Sets the status of a banner
254  *
255  * @param int $id The ID of the banner to set the status to
256  * @param boolean $active_flag A flag that enables or disables the banner
257  * @access private
258  * @return boolean
259  */
hpdl
1
260
hpdl
1860
261     private function _setStatus($id, $active_flag) {
hpdl
1
262       global $osC_Database;
263
hpdl
1860
264       if ( $active_flag === true ) {
hpdl
1
265         $Qbanner = $osC_Database->query('update :table_banners set status = 1, date_status_change = now(), date_scheduled = NULL where banners_id = :banners_id');
266       } else {
267         $Qbanner = $osC_Database->query('update :table_banners set status = 0, date_status_change = now() where banners_id = :banners_id');
268       }
269
hpdl
1860
270       $Qbanner->bindTable(':table_banners', TABLE_BANNERS);
271       $Qbanner->bindInt(':banners_id', $id);
272       $Qbanner->execute();
273
274       return ( $Qbanner->affectedRows() === 1 );
hpdl
1
275     }
276
hpdl
1860
277 /**
278  * Increment the display count of the banner
279  *
280  * @param int $id The ID of the banner
281  * @access private
282  */
283
284     private function _updateDisplayCount($id) {
hpdl
1
285       global $osC_Database;
286
287       $Qcheck = $osC_Database->query('select count(*) as count from :table_banners_history where banners_id = :banners_id and date_format(banners_history_date, "%Y%m%d") = date_format(now(), "%Y%m%d")');
288       $Qcheck->bindTable(':table_banners_history', TABLE_BANNERS_HISTORY);
289       $Qcheck->bindInt(':banners_id', $id);
290       $Qcheck->execute();
291
hpdl
1860
292       if ( $Qcheck->valueInt('count') > 0 ) {
hpdl
1
293         $Qbanner = $osC_Database->query('update :table_banners_history set banners_shown = banners_shown + 1 where banners_id = :banners_id and date_format(banners_history_date, "%Y%m%d") = date_format(now(), "%Y%m%d")');
294       } else {
295         $Qbanner = $osC_Database->query('insert into :table_banners_history (banners_id, banners_shown, banners_history_date) values (:banners_id, 1, now())');
296       }
hpdl
1860
297
hpdl
1
298       $Qbanner->bindTable(':table_banners_history', TABLE_BANNERS_HISTORY);
299       $Qbanner->bindInt(':banners_id', $id);
300       $Qbanner->execute();
301     }
302
hpdl
1860
303 /**
304  * Increment the click count of the banner
305  *
306  * @param int $id The ID of the banner
307  * @access private
308  */
309
310     private function _updateClickCount($id) {
hpdl
1
311       global $osC_Database;
312
313       $Qcheck = $osC_Database->query('select count(*) as count from :table_banners_history where banners_id = :banners_id and date_format(banners_history_date, "%Y%m%d") = date_format(now(), "%Y%m%d")');
314       $Qcheck->bindTable(':table_banners_history', TABLE_BANNERS_HISTORY);
315       $Qcheck->bindInt(':banners_id', $id);
316       $Qcheck->execute();
317
hpdl
1860
318       if ( $Qcheck->valueInt('count') > 0 ) {
hpdl
1
319         $Qbanner = $osC_Database->query('update :table_banners_history set banners_clicked = banners_clicked + 1 where banners_id = :banners_id and date_format(banners_history_date, "%Y%m%d") = date_format(now(), "%Y%m%d")');
320       } else {
321         $Qbanner = $osC_Database->query('insert into :table_banners_history (banners_id, banners_clicked, banners_history_date) values (:banners_id, 1, now())');
322       }
hpdl
1860
323
hpdl
1
324       $Qbanner->bindTable(':table_banners_history', TABLE_BANNERS_HISTORY);
325       $Qbanner->bindInt(':banners_id', $id);
326       $Qbanner->execute();
327     }
328   }
329 ?>