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