<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

////
// Sets the status of a banner
  function tep_set_slideshow_banner_status($slideshow_banners_id, $status) {
    if ($status == '1') {
      return tep_db_query("update " . TABLE_SLIDER_BANNERS . " set status = '1', date_status_change = now(), date_scheduled = NULL where slideshow_banners_id = '" . (int)$slideshow_banners_id . "'");
    } elseif ($status == '0') {
      return tep_db_query("update " . TABLE_SLIDER_BANNERS . " set status = '0', date_status_change = now() where slideshow_banners_id = '" . (int)$slideshow_banners_id . "'");
    } else {
      return -1;
    }
  }
// Sets the target of a banner
  function tep_set_slideshow_banner_target($slideshow_banners_id, $slideshow_banners_target) {
    if ($slideshow_banners_target == '1') {
      return tep_db_query("update " . TABLE_SLIDER_BANNERS . " set slideshow_banners_target = '1', date_slideshow_banners_target_change = now(), date_scheduled = NULL where slideshow_banners_id = '" . (int)$slideshow_banners_id . "'");
    } elseif ($slideshow_banners_target == '0') {
      return tep_db_query("update " . TABLE_SLIDER_BANNERS . " set slideshow_banners_target = '0', date_slideshow_banners_target_change = now() where slideshow_banners_id = '" . (int)$slideshow_banners_id . "'");
    } else {
      return -1;
    }
  }

////
// Auto activate banners
  function tep_activate_slideshow_banners() {
    $slideshow_banners_query = tep_db_query("select slideshow_banners_id, date_scheduled from " . TABLE_SLIDER_BANNERS . " where date_scheduled != ''");
    if (tep_db_num_rows($slideshow_banners_query)) {
      while ($slideshow_banners = tep_db_fetch_array($slideshow_banners_query)) {
        if (date('Y-m-d H:i:s') >= $slideshow_banners['date_scheduled']) {
          tep_set_slideshow_banner_status($slideshow_banners['slideshow_banners_id'], '1');
        }
      }
    }
  }

////
// Auto expire banners
  function tep_expire_slideshow_banners() {
    $slideshow_banners_query = tep_db_query("select b.slideshow_banners_id, b.expires_date, b.expires_impressions, sum(bh.slideshow_banners_shown) as slideshow_banners_shown from " . TABLE_SLIDER_BANNERS . " b, " . TABLE_SLIDER_BANNERS_HISTORY . " bh where b.status = '1' and b.slideshow_banners_id = bh.slideshow_banners_id group by b.slideshow_banners_id");
    if (tep_db_num_rows($slideshow_banners_query)) {
      while ($slideshow_banners = tep_db_fetch_array($slideshow_banners_query)) {
        if (tep_not_null($slideshow_banners['expires_date'])) {
          if (date('Y-m-d H:i:s') >= $slideshow_banners['expires_date']) {
            tep_set_slideshow_banner_status($slideshow_banners['slideshow_banners_id'], '0');
          }
        } elseif (tep_not_null($slideshow_banners['expires_impressions'])) {
          if ( ($slideshow_banners['expires_impressions'] > 0) && ($slideshow_banners['slideshow_banners_shown'] >= $slideshow_banners['expires_impressions']) ) {
            tep_set_slideshow_banner_status($slideshow_banners['slideshow_banners_id'], '0');
          }
        }
      }
    }
  }

////
// Display a slideshow_banner from the specified group or slideshow_banner id ($identifier)
  function tep_display_slideshow_banner($action, $identifier) {
    global $languages_id;
		
    if ($action == 'dynamic') {
      $slideshow_banners_query = tep_db_query("select count(*) as count from " . TABLE_SLIDER_BANNERS . " where status = '1' and slideshow_banners_group = '" . tep_db_input($identifier) . "'");
      $slideshow_banners = tep_db_fetch_array($slideshow_banners_query);
      if ($slideshow_banners['count'] > 0) {
				$slideshow_banner = tep_random_select("select s.slideshow_banners_id, s.slideshow_banners_title, s.slideshow_banners_image, s.slideshow_banners_target, sd.slideshow_banners_field_1, sd.slideshow_banners_field_2, sd.slideshow_banners_field_3, sd.slideshow_banners_field_4, s.slideshow_banners_image_thumb from " . TABLE_SLIDER_BANNERS . " s, " . TABLE_SLIDER_BANNERS_DESCRIPTION . " sd where s.slideshow_banners_id = sd.slideshow_banners_id and s.status = '1' and s.slideshow_banners_group = '" . tep_db_input($identifier) . "' and sd.language_id = '" . (int)$languages_id . "'");
      } else {
        return '<strong>TEP ERROR! (tep_display_banner(' . $action . ', ' . $identifier . ') -> No banners with group \'' . $identifier . '\' found!</strong>';
      }
    } elseif ($action == 'static') {
      if (is_array($identifier)) {
        $slideshow_banner = $identifier;
      } else {
        $slideshow_banner_query = tep_db_query("select s.slideshow_banners_id, s.slideshow_banners_title, s.slideshow_banners_image, s.slideshow_banners_target, sd.slideshow_banners_field_1, sd.slideshow_banners_field_2, sd.slideshow_banners_field_3, sd.slideshow_banners_field_4, s.slideshow_banners_image_thumb from " . TABLE_SLIDER_BANNERS . " s, " . TABLE_SLIDER_BANNERS_DESCRIPTION . " sd where s.slideshow_banners_id = sd.slideshow_banners_id and s.status = '1' and s.slideshow_banners_id = '" . (int)$identifier . "' and sd.language_id = '" . (int)$languages_id . "'");
        if (tep_db_num_rows($slideshow_banner_query)) {
          $slideshow_banner = tep_db_fetch_array($slideshow_banner_query);
        } else {
          return '<strong>TEP ERROR! (tep_display_banner(' . $action . ', ' . $identifier . ') -> slideshow_banner with ID \'' . $identifier . '\' not found, or status inactive</strong>';
        }
      }
    } else {
      return '<strong>TEP ERROR! (tep_display_banner(' . $action . ', ' . $identifier . ') -> Unknown $action parameter value - it must be either \'dynamic\' or \'static\'</strong>';
    }

 //   if (tep_not_null($slideshow_banner['slideshow_banners_html_text'])) {
//      $slideshow_banner_string = $slideshow_banner['slideshow_banners_html_text'];
 //   } else {
		if ($slideshow_banner['slideshow_banners_target']){
			$slideshow_banner_target = '_self';	
		}else{
			$slideshow_banner_target = '_blank';	
		}
			$slideshow_banner_link = '' . tep_href_link(FILENAME_REDIRECT, 'action=slideshow_banner&goto=' . $slideshow_banner['slideshow_banners_id']) . '" target="'.$slideshow_banner_target.'';
			$slideshow_banner_title = '
			<div class="camera_caption moveFromRight">
					<span class="slider_text_1">'.$slideshow_banner['slideshow_banners_field_1'].'</span>
					<span class="slider_text_2">'.$slideshow_banner['slideshow_banners_field_2'].'</span>					
					<span class="slider_text_3">'.$slideshow_banner['slideshow_banners_field_3'].'</span>
					<span class="slider_text_4"><a href="'.$slideshow_banner_link.'"><strong class="button_slider"><strong class="button_bg"><strong></strong></strong><strong class="button_text">'.$slideshow_banner['slideshow_banners_field_4'].'</strong></strong></a></span>
			</div>';
      $slideshow_banner_string = '<div data-src="'.DIR_WS_IMAGES . 'slider/'. $slideshow_banner['slideshow_banners_image'].'" data-thumb="'.DIR_WS_IMAGES . 'slider/' . $slideshow_banner['slideshow_banners_image'].'" class="slider_block">'.$slideshow_banner_title.'</div>';

    tep_update_slideshow_banner_display_count($slideshow_banner['slideshow_banners_id']);

    return $slideshow_banner_string;
  }
// ******************************************************************

////

// Display a slideshow_banner from the specified group or slideshow_banner id ($identifier)
  function tep_display_bannershow_banner($action, $identifier) {
    global $languages_id;
		
    if ($action == 'dynamic') {
      $slideshow_banners_query = tep_db_query("select count(*) as count from " . TABLE_SLIDER_BANNERS . " where status = '1' and slideshow_banners_group = '" . tep_db_input($identifier) . "'");
      $slideshow_banners = tep_db_fetch_array($slideshow_banners_query);
      if ($slideshow_banners['count'] > 0) {
				$slideshow_banner = tep_random_select("select s.slideshow_banners_id, s.slideshow_banners_title, s.slideshow_banners_image, s.slideshow_banners_target, sd.slideshow_banners_field_1, sd.slideshow_banners_field_2, sd.slideshow_banners_field_3, sd.slideshow_banners_field_4, s.slideshow_banners_image_thumb from " . TABLE_SLIDER_BANNERS . " s, " . TABLE_SLIDER_BANNERS_DESCRIPTION . " sd where s.slideshow_banners_id = sd.slideshow_banners_id and s.status = '1' and s.slideshow_banners_group = '" . tep_db_input($identifier) . "' and sd.language_id = '" . (int)$languages_id . "'");
      } else {
        return '<strong>TEP ERROR! (tep_display_banner(' . $action . ', ' . $identifier . ') -> No banners with group \'' . $identifier . '\' found!</strong>';
      }
    } elseif ($action == 'static') {
      if (is_array($identifier)) {
        $slideshow_banner = $identifier;
      } else {
        $slideshow_banner_query = tep_db_query("select s.slideshow_banners_id, s.slideshow_banners_title, s.slideshow_banners_image, s.slideshow_banners_target, sd.slideshow_banners_field_1, sd.slideshow_banners_field_2, sd.slideshow_banners_field_3, sd.slideshow_banners_field_4, s.slideshow_banners_image_thumb from " . TABLE_SLIDER_BANNERS . " s, " . TABLE_SLIDER_BANNERS_DESCRIPTION . " sd where s.slideshow_banners_id = sd.slideshow_banners_id and s.status = '1' and s.slideshow_banners_id = '" . (int)$identifier . "' and sd.language_id = '" . (int)$languages_id . "'");
        if (tep_db_num_rows($slideshow_banner_query)) {
          $slideshow_banner = tep_db_fetch_array($slideshow_banner_query);
        } else {
          return '<strong>TEP ERROR! (tep_display_banner(' . $action . ', ' . $identifier . ') -> slideshow_banner with ID \'' . $identifier . '\' not found, or status inactive</strong>';
        }
      }
    } else {
      return '<strong>TEP ERROR! (tep_display_banner(' . $action . ', ' . $identifier . ') -> Unknown $action parameter value - it must be either \'dynamic\' or \'static\'</strong>';
    }

 //   if (tep_not_null($slideshow_banner['slideshow_banners_html_text'])) {
//      $slideshow_banner_string = $slideshow_banner['slideshow_banners_html_text'];
 //   } else {
		if ($slideshow_banner['slideshow_banners_target']){
			$slideshow_banner_target = '_blank';	
		}else{
			$slideshow_banner_target = '_self';	
		}
			$slideshow_banner_link = '' . tep_href_link(FILENAME_REDIRECT, 'action=slideshow_banner&goto=' . $slideshow_banner['slideshow_banners_id']) . '" target="'.$slideshow_banner_target.'';
			$slideshow_banner_title = '
			<div class="bannershow_caption">
					<span class="slider_text_1">'.$slideshow_banner['slideshow_banners_field_1'].'</span>
					<span class="slider_text_2">'.$slideshow_banner['slideshow_banners_field_2'].'</span>					
					<span class="slider_text_3">'.$slideshow_banner['slideshow_banners_field_3'].'</span>
					<span class="slider_text_4"><a href="'.$slideshow_banner_link.'"><strong class="button_bannershow"><strong class="button_bg"></strong><strong class="button_text">'.$slideshow_banner['slideshow_banners_field_4'].'</strong></strong></a></span>
			</div>';
	  if (tep_not_null($slideshow_banner['slideshow_banners_image'])) {
		$image = tep_image(DIR_WS_IMAGES . 'slider/'. $slideshow_banner['slideshow_banners_image'], $slideshow_banner['slideshow_banners_title'], '', '') ; 
	  }else{
		$image = '';  
	  }
      $slideshow_banner_string = '<div class="bannershow_block">'. $image . '<div class="bannershow_wrapper">'.$slideshow_banner_title.'</div></div>';

    tep_update_slideshow_banner_display_count($slideshow_banner['slideshow_banners_id']);

    return $slideshow_banner_string;
  }
// ******************************************************************

////
// Check to see if a slideshow_banner exists
  function tep_slideshow_banner_exists($action, $identifier) {
    if ($action == 'dynamic') {
			return tep_random_select("select s.slideshow_banners_id, s.slideshow_banners_title, s.slideshow_banners_image, s.slideshow_banners_target, sd.slideshow_banners_field_1, sd.slideshow_banners_field_2, sd.slideshow_banners_field_3, sd.slideshow_banners_field_4 from " . TABLE_SLIDER_BANNERS . " s, " . TABLE_SLIDER_BANNERS_DESCRIPTION . " sd where s.status = '1' and s.slideshow_banners_group = '" . tep_db_input($identifier) . "' and s.slideshow_banners_id = sd.slideshow_banners_id and sd.language_id = '" . (int)$languages_id . "'");
    } elseif ($action == 'static') {
      $slideshow_banner_query = tep_db_query("select s.slideshow_banners_id, s.slideshow_banners_title, s.slideshow_banners_image, s.slideshow_banners_target, sd.slideshow_banners_field_1, sd.slideshow_banners_field_2, sd.slideshow_banners_field_3, sd.slideshow_banners_field_4 from " . TABLE_SLIDER_BANNERS . " s, " . TABLE_SLIDER_BANNERS_DESCRIPTION . " sd where s.status = '1' and s.slideshow_banners_id = '" . (int)$identifier . "' and s.slideshow_banners_id = sd.slideshow_banners_id and sd.language_id = '" . (int)$languages_id . "'");
      return tep_db_fetch_array($slideshow_banner_query);
    } else {
      return false;
    }
  }

////
// Update the slideshow_banner display statistics
  function tep_update_slideshow_banner_display_count($slideshow_banner_id) {
    $slideshow_banner_check_query = tep_db_query("select count(*) as count from " . TABLE_SLIDER_BANNERS_HISTORY . " where slideshow_banners_id = '" . (int)$slideshow_banner_id . "' and date_format(slideshow_banners_history_date, '%Y%m%d') = date_format(now(), '%Y%m%d')");
    $slideshow_banner_check = tep_db_fetch_array($slideshow_banner_check_query);

    if ($slideshow_banner_check['count'] > 0) {
      tep_db_query("update " . TABLE_SLIDER_BANNERS_HISTORY . " set slideshow_banners_shown = slideshow_banners_shown + 1 where slideshow_banners_id = '" . (int)$slideshow_banner_id . "' and date_format(slideshow_banners_history_date, '%Y%m%d') = date_format(now(), '%Y%m%d')");
    } else {
      tep_db_query("insert into " . TABLE_SLIDER_BANNERS_HISTORY . " (slideshow_banners_id, slideshow_banners_shown, slideshow_banners_history_date) values ('" . (int)$slideshow_banner_id . "', 1, now())");
    }
  }

////
// Update the slideshow_banner click statistics
  function tep_update_slideshow_banner_click_count($slideshow_banner_id) {
    tep_db_query("update " . TABLE_SLIDER_BANNERS_HISTORY . " set slideshow_banners_clicked = slideshow_banners_clicked + 1 where slideshow_banners_id = '" . (int)$slideshow_banner_id . "' and date_format(slideshow_banners_history_date, '%Y%m%d') = date_format(now(), '%Y%m%d')");
  }
  
  
////
// build slideshow_banner groups
  function osc_build_slideshow_banners_group($selected_slideshow_banners) {
    $selected_slideshow_banners = explode(':', $selected_slideshow_banners);
    $size = sizeof($selected_slideshow_banners);
    if ($size == 1) {
      $new_slideshow_banner_search = " slideshow_banners_group = '" . $selected_slideshow_banners[0] . "'";
    } else {
      for ($i=0, $n=$size; $i<$n; $i+=1) {
        $new_slideshow_banner_search .= " slideshow_banners_group = '" . $selected_slideshow_banners[$i] . "'";
        if ($i+1 < $n) {
          $new_slideshow_banner_search .= ' or ';
        }
      }
    }
    if ($new_slideshow_banner_search != '') {
      $new_slideshow_banner_search = ' and (' . $new_slideshow_banner_search . ')';
    }
    return $new_slideshow_banner_search;
  }

?>
