PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/osCommerce/OM/Core/Site/Shop/Banner.php

http://github.com/osCommerce/oscommerce
PHP | 333 lines | 150 code | 74 blank | 109 comment | 29 complexity | b98fc7201efaa5c003d75cbcc879d957 MD5 | raw file
  1. <?php
  2. /**
  3. * osCommerce Online Merchant
  4. *
  5. * @copyright Copyright (c) 2011 osCommerce; http://www.oscommerce.com
  6. * @license BSD License; http://www.oscommerce.com/bsdlicense.txt
  7. */
  8. namespace osCommerce\OM\Core\Site\Shop;
  9. use osCommerce\OM\Core\DateTime;
  10. use osCommerce\OM\Core\HTML;
  11. use osCommerce\OM\Core\Registry;
  12. /**
  13. * The Banner class manages the banners shown throughout the online store
  14. */
  15. class Banner {
  16. /**
  17. * Controls whether banners should be shown multiple times on the page
  18. *
  19. * @var boolean
  20. * @access protected
  21. */
  22. protected $_show_duplicates_in_group = false;
  23. /**
  24. * A placeholder that keeps the banner ID in memory when checking if a banner
  25. * exists before showing it
  26. *
  27. * @var id
  28. * @access protected
  29. */
  30. protected $_exists_id;
  31. /**
  32. * An array containing the banner IDs already shown on the page
  33. *
  34. * @var array
  35. * @access protected
  36. */
  37. protected $_shown_ids = array();
  38. /**
  39. * Constructor
  40. *
  41. * @access public
  42. */
  43. public function __construct() {
  44. if ( SERVICE_BANNER_SHOW_DUPLICATE == 'True' ) {
  45. $this->_show_duplicates_in_group = true;
  46. }
  47. }
  48. /**
  49. * Activate a banner that has been on schedule
  50. *
  51. * @param int $id The ID of the banner to activate
  52. * @access public
  53. * @return boolean
  54. */
  55. public function activate($id) {
  56. return $this->_setStatus($id, true);
  57. }
  58. /**
  59. * Activate all banners on schedule
  60. *
  61. * @access public
  62. */
  63. public function activateAll() {
  64. $OSCOM_PDO = Registry::get('PDO');
  65. $Qbanner = $OSCOM_PDO->query('select banners_id, date_scheduled from :table_banners where date_scheduled != ""');
  66. $Qbanner->execute();
  67. while ( $Qbanner->fetch() ) {
  68. if ( DateTime::getNow() >= $Qbanner->value('date_scheduled') ) {
  69. $this->activate($Qbanner->valueInt('banners_id'));
  70. }
  71. }
  72. }
  73. /**
  74. * Deactivate a banner
  75. *
  76. * @param int $id The ID of the banner to deactivate
  77. * @access public
  78. * @return boolean
  79. */
  80. public function expire($id) {
  81. return $this->_setStatus($id, false);
  82. }
  83. /**
  84. * Deactivate all banners that have passed their schedule
  85. *
  86. * @access public
  87. */
  88. public function expireAll() {
  89. $OSCOM_PDO = Registry::get('PDO');
  90. $Qbanner = $OSCOM_PDO->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');
  91. $Qbanner->execute();
  92. while ( $Qbanner->fetch() ) {
  93. if ( strlen($Qbanner->value('expires_date')) > 0 ) {
  94. if ( DateTime::getNow() >= $Qbanner->value('expires_date') ) {
  95. $this->expire($Qbanner->valueInt('banners_id'));
  96. }
  97. } elseif ( strlen($Qbanner->valueInt('expires_impressions')) > 0 ) {
  98. if ( ($Qbanner->valueInt('expires_impressions') > 0) && ($Qbanner->valueInt('banners_shown') >= $Qbanner->valueInt('expires_impressions')) ) {
  99. $this->expire($Qbanner->valueInt('banners_id'));
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Check if an existing banner is active
  106. *
  107. * @param int $id The ID of the banner to check
  108. * @access public
  109. * @return boolean
  110. */
  111. public function isActive($id) {
  112. $OSCOM_PDO = Registry::get('PDO');
  113. $Qbanner = $OSCOM_PDO->prepare('select status from :table_banners where banners_id = :banners_id');
  114. $Qbanner->bindInt(':banners_id', $id);
  115. $Qbanner->execute();
  116. return ( $Qbanner->valueInt('status') === 1 );
  117. }
  118. /**
  119. * Check if banners exist in a group. If banners exists, select a random entry
  120. * and assign its ID to $_exists_id.
  121. *
  122. * @param string $group The group to check in
  123. * @access public
  124. * @return boolean
  125. */
  126. public function exists($group) {
  127. $OSCOM_PDO = Registry::get('PDO');
  128. $sql_query = 'select banners_id from :table_banners where status = 1 and banners_group = :banners_group';
  129. if ( ($this->_show_duplicates_in_group === false) && (count($this->_shown_ids) > 0) ) {
  130. $sql_query .= ' and banners_id not in (' . implode(',', $this->_shown_ids) . ')';
  131. }
  132. $sql_query .= ' order by rand() limit 1';
  133. $Qbanner = $OSCOM_PDO->prepare($sql_query);
  134. $Qbanner->bindValue(':banners_group', $group);
  135. $Qbanner->execute();
  136. $result = $Qbanner->fetch();
  137. if ( $result !== false ) {
  138. $this->_exists_id = $result['banners_id'];
  139. return true;
  140. }
  141. return false;
  142. }
  143. /**
  144. * Display a banner. If no ID is passed, the value defined in $_exists_id is
  145. * used.
  146. *
  147. * @param int $id The ID of the banner to show
  148. * @access public
  149. * @return string
  150. */
  151. public function display($id = null) {
  152. $OSCOM_PDO = Registry::get('PDO');
  153. $banner_string = '';
  154. if ( empty($id) && isset($this->_exists_id) && is_numeric($this->_exists_id) ) {
  155. $id = $this->_exists_id;
  156. unset($this->_exists_id);
  157. }
  158. $Qbanner = $OSCOM_PDO->prepare('select * from :table_banners where banners_id = :banners_id and status = 1');
  159. $Qbanner->bindInt(':banners_id', $id);
  160. $Qbanner->execute();
  161. $result = $Qbanner->fetch();
  162. if ( $result !== false ) {
  163. if ( !empty($result['banners_html_text']) ) {
  164. $banner_string = $result['banners_html_text'];
  165. } else {
  166. // HPDL create Redirect action; fix banner image location
  167. $banner_string = HTML::link(OSCOM::getLink('Shop', 'Index', 'Redirect&action=banner&goto=' . (int)$result['banners_id']), HTML::image('public/' . $Qbanner->value('banners_image'), $Qbanner->value('banners_title')), 'target="_blank"');
  168. }
  169. $this->_updateDisplayCount($result['banners_id']);
  170. if ( $this->_show_duplicates_in_group === false ) {
  171. $this->_shown_ids[] = $result['banners_id'];
  172. }
  173. }
  174. return $banner_string;
  175. }
  176. /**
  177. * Return the URL assigned to the banner
  178. *
  179. * @param int $id The ID of the banner
  180. * @param boolean $increment_click_flag A flag to state if the banner click count should be incremented
  181. * @access public
  182. * @return string
  183. */
  184. public function getURL($id, $increment_click_flag = false) {
  185. $OSCOM_PDO = Registry::get('PDO');
  186. $url = '';
  187. $Qbanner = $OSCOM_PDO->prepare('select banners_url from :table_banners where banners_id = :banners_id and status = 1');
  188. $Qbanner->bindInt(':banners_id', $id);
  189. $Qbanner->execute();
  190. $result = $Qbanner->fetch();
  191. if ( $result !== false ) {
  192. $url = $result['banners_url'];
  193. if ( $increment_click_flag === true ) {
  194. $this->_updateClickCount($id);
  195. }
  196. }
  197. return $url;
  198. }
  199. /**
  200. * Sets the status of a banner
  201. *
  202. * @param int $id The ID of the banner to set the status to
  203. * @param boolean $active_flag A flag that enables or disables the banner
  204. * @access private
  205. * @return boolean
  206. */
  207. private function _setStatus($id, $active_flag) {
  208. $OSCOM_PDO = Registry::get('PDO');
  209. if ( $active_flag === true ) {
  210. $Qbanner = $OSCOM_PDO->prepare('update :table_banners set status = 1, date_status_change = now(), date_scheduled = NULL where banners_id = :banners_id');
  211. } else {
  212. $Qbanner = $OSCOM_PDO->prepare('update :table_banners set status = 0, date_status_change = now() where banners_id = :banners_id');
  213. }
  214. $Qbanner->bindInt(':banners_id', $id);
  215. $Qbanner->execute();
  216. return ( $Qbanner->rowCount() === 1 );
  217. }
  218. /**
  219. * Increment the display count of the banner
  220. *
  221. * @param int $id The ID of the banner
  222. * @access private
  223. */
  224. private function _updateDisplayCount($id) {
  225. $OSCOM_PDO = Registry::get('PDO');
  226. $Qcheck = $OSCOM_PDO->prepare('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")');
  227. $Qcheck->bindInt(':banners_id', $id);
  228. $Qcheck->execute();
  229. $result = $Qcheck->fetch();
  230. if ( ($result !== false) && ($result['count'] > 0) ) {
  231. $Qbanner = $OSCOM_PDO->prepare('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")');
  232. } else {
  233. $Qbanner = $OSCOM_PDO->prepare('insert into :table_banners_history (banners_id, banners_shown, banners_history_date) values (:banners_id, 1, now())');
  234. }
  235. $Qbanner->bindInt(':banners_id', $id);
  236. $Qbanner->execute();
  237. }
  238. /**
  239. * Increment the click count of the banner
  240. *
  241. * @param int $id The ID of the banner
  242. * @access private
  243. */
  244. private function _updateClickCount($id) {
  245. $OSCOM_PDO = Registry::get('PDO');
  246. $Qcheck = $OSCOM_PDO->prepare('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")');
  247. $Qcheck->bindInt(':banners_id', $id);
  248. $Qcheck->execute();
  249. $result = $Qcheck->fetch();
  250. if ( ($result !== false) && ($result['count'] > 0) ) {
  251. $Qbanner = $OSCOM_PDO->prepare('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")');
  252. } else {
  253. $Qbanner = $OSCOM_PDO->prepare('insert into :table_banners_history (banners_id, banners_clicked, banners_history_date) values (:banners_id, 1, now())');
  254. }
  255. $Qbanner->bindInt(':banners_id', $id);
  256. $Qbanner->execute();
  257. }
  258. }
  259. ?>