<?php
/*

 osCommRes, Services Oline 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare 

Released under the GNU General Public License
*/

// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();

  class eventCart {
    var $contents, $total, $event_cartID;

    function eventCart() {
      $this->reset();
    }

    function restore_contents() {
       global $customer_id;

      if (!tep_session_is_registered('customer_id')) return false;

// insert current cart contents in database
      if (is_array($this->contents)) {
        reset($this->contents);
        while (list($sessions_id, ) = each($this->contents)) {
          $session_query = tep_db_query("select sessions_id from " . TABLE_CUSTOMERS_EVENT_BASKET . " where customers_id = '" . (int)$customer_id . "' and sessions_id = '" . tep_db_input($sessions_id) . "'");
          if (!tep_db_num_rows($session_query)) {
            tep_db_query("insert into " . TABLE_CUSTOMERS_EVENT_BASKET . " (customers_id, sessions_id, customers_event_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($sessions_id) . "', '" . date('Ymd') . "')");
          }
        }
      $this->cleanup();
    }
    }
    function reset($reset_database = false) {
      global $customer_id;

      $this->contents = array();
      $this->total = 0;
      $this->weight = 0;
      $this->content_type = false;

      if (tep_session_is_registered('customer_id') && ($reset_database == true)) {
        tep_db_query("delete from " . TABLE_CUSTOMERS_EVENT_BASKET . " where customers_id = '" . (int)$customer_id . "'");
      }

      unset($this->event_cartID);
      if (tep_session_is_registered('event_cartID')) tep_session_unregister('event_cartID');
    }

    function add_cart($sessions_id, $notify = true) {
      global $new_sessions_id_in_event_cart, $customer_id;
      $this->cleanup();
      //$sessions_id = tep_get_uprid($sessions_id, $attributes);
      if ($notify == true) {
        $new_sessions_id_in_event_cart = $sessions_id;
        tep_session_register('new_sessions_id_in_event_cart');
      }

        $this->contents[] = array($sessions_id);
// insert into database
        if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_EVENT_BASKET . " (customers_id, sessions_id, customers_event_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($sessions_id) . "', '" . date('Ymd') . "')");


// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
      $this->event_cartID = $this->generate_event_cart_id();
    }

    function cleanup() {
      global $customer_id;

      
      while (list($key,) = each($this->contents)) {
// remove from database
          if (tep_session_is_registered('customer_id')) {
            tep_db_query("delete from " . TABLE_CUSTOMERS_EVENT_BASKET . " where customers_id = '" . (int)$customer_id . "' and sessions_id = '" . tep_db_input($key) . "'");
        }
      }
      $this->reset();
    }

    function count_contents() {  // get total number of items in cart 
      $total_items = 0;
      if (is_array($this->contents)) {
        reset($this->contents);
	return sizeof($this->contents);
      }
    }

    function in_cart($sessions_id) {
      if (isset($this->contents[$sessions_id])) {
        return true;
      } else {
        return false;
      }
    }

    function remove($sessions_id) {
      global $customer_id;

      unset($this->contents[$sessions_id]);
// remove from database
      if (tep_session_is_registered('customer_id')) {
        tep_db_query("delete from " . TABLE_CUSTOMERS_EVENT_BASKET . " where customers_id = '" . (int)$customer_id . "' and sessions_id = '" . tep_db_input($sessions_id) . "'");
      }
// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
      $this->event_cartID = $this->generate_event_cart_id();
    }

    function remove_all() {
      $this->reset();
    }

    function get_sessions_id_list() {
      $sessions_id_list = '';
      if (is_array($this->contents)) {
        reset($this->contents);
        while (list($sessions_id, ) = each($this->contents)) {
          $sessions_id_list .= ', ' . $sessions_id;
        }
      }

      return substr($sessions_id_list, 2);
    }

    function calculate() {
      $this->total = 0;
      if (!is_array($this->contents)) return 0;

      reset($this->contents);
      while (list($sessions_id, ) = each($this->contents)) {

// sessions price
        $session_query = tep_db_query("select sessions_id, sessions_price, sessions_tax_class_id from " . TABLE_EVENTS_SESSIONS . " where sessions_id = '" . (int)$sessions_id . "'");
        if ($session = tep_db_fetch_array($session_query)) {
          $srid = $session['sessions_id'];
          $sessions_tax = tep_get_tax_rate($session['sessions_tax_class_id']);
          $sessions_price = $session['sessions_price'];

          $this->total += tep_add_tax($sessions_price, $sesions_tax);
        }

    }
    }
    function get_sessions() {
      global $languages_id;

      if (!is_array($this->contents)) return false;

      $sessions_array = array();
      for($i=0,$n=sizeof($this->contents);$i<$n;$i++){
        $sessions_id=$this->contents[$i][0];
        $sessions_query = tep_db_query("select s.sessions_id, e.events_name, s.sessions_fees, s.sessions_start_date,s.sessions_end_date,s.sessions_start_time,s.sessions_end_time,s.sessions_tax_class_id from " . TABLE_EVENTS_DESCRIPTION . " e, ". TABLE_EVENTS_SESSIONS . " s, " . TABLE_EVENTS_SESSIONS_DESCRIPTION . " sd where s.sessions_id = '" . (int)$sessions_id . "' and sd.sessions_id = s.sessions_id and s.event_id=e.events_id and sd.language_id = '" . (int)$languages_id . "' and e.language_id = '" . (int)$languages_id . "'");
        if ($sessions = tep_db_fetch_array($sessions_query)) {
          $srid = $sessions['sessions_id'];
	  
          $sessions_array[] = array('name'=>$sessions['events_name'],
	  			    'id' => $sessions['sessions_id'],
                                    'fees' => $sessions['sessions_fees'],
				    'start_date' => $sessions['sessions_start_date'],
				    'start_time' => $sessions['sessions_start_time'],
				    'end_date' => $sessions['sessions_end_date'],
				    'end_time' => $sessions['sessions_end_time'],
                                    'final_fees' => $sessions['sessions_fees'],
                                    'tax_class_id' => $sessions['sessions_tax_class_id']);
        }
      }

      return $sessions_array;
    }

    function show_total() {
      $this->calculate();

      return $this->total;
    }

    function generate_event_cart_id($length = 5) {
      return tep_create_random_value($length, 'digits');
    }

    function unserialize($broken) {
      for(reset($broken);$kv=each($broken);) {
        $key=$kv['key'];
        if (gettype($this->$key)!="user function")
              $this->$key=$kv['value'];
      }
    }
   }
?>
