<?php
/*
  $Id$

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

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  class bm_add_header_currencies {
    var $code = 'bm_add_header_currencies';
    var $group = 'boxes';
    var $title;
    var $description;
    var $sort_order;
    var $enabled = false;
    var $pages;	

    function bm_add_header_currencies() {
      $this->title = MODULE_BOXES_CURRENCIES_HEADER_FOOTER_TITLE;
      $this->description = MODULE_BOXES_CURRENCIES_HEADER_FOOTER_DESCRIPTION;

      if ( defined('MODULE_BOXES_CURRENCIES_HEADER_FOOTER_STATUS') ) {
        $this->sort_order = MODULE_BOXES_CURRENCIES_HEADER_FOOTER_SORT_ORDER;
        $this->enabled = (MODULE_BOXES_CURRENCIES_HEADER_FOOTER_STATUS == 'True');
        $this->pages = MODULE_BOXES_CURRENCIES_HEADER_FOOTER_DISPLAY_PAGES;
        $this->group = 'boxes_above_header';
      }
    }

    function execute() {
      global $PHP_SELF, $currencies, $HTTP_GET_VARS, $request_type, $currency, $oscTemplate, $current_curr_key;

      if (substr(basename($PHP_SELF), 0, 8) != 'checkout') {
        if (isset($currencies) && is_object($currencies) && (count($currencies->currencies) > 1)) {
         
          reset($currencies->currencies);
          foreach($currencies->currencies as $key => $value ) {
            if ( $key == $currency ) {
              $current_curr_key = $key;
							$current_curr_symbol_left = $value['symbol_left'];
							$current_curr_symbol_right = $value['symbol_right'];
							$current_curr_value = $value['value'];
							$current_curr_title = $value['title'];
              break;
            }
          }
		  		
          reset($currencies->currencies);
          $currencies_array = array();
          $url = '';
				  while (list($key, $value) = each($currencies->currencies)) {
						if ($current_curr_key == $key )	{
							$current_curr = 'activ';
						}else{
							$current_curr = '';
						}
						$url = 'currency='.$key;
						$currencies_string .= ' <li class="'.$current_curr.'"><a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('currency')) .'currency='.$key).'">'.$value['symbol_left'].''.$value['title'].''.$value['symbol_right'].'</a></li>';
          }

          $hidden_get_variables = '';
          reset($HTTP_GET_VARS);
          while (list($key, $value) = each($HTTP_GET_VARS)) {
            if ( is_string($value) && ($key != 'currency') && ($key != tep_session_name()) && ($key != 'x') && ($key != 'y') ) {
              $hidden_get_variables .= tep_draw_hidden_field($key, $value);
            }
          }
						
          $data = '
				
					<div id="box_currencies" class="box_currencies">'. "\n".
		  		  '		<div class="drop_down">'. "\n".
						'	     ' . tep_draw_form('currencies', tep_href_link(basename($PHP_SELF), '', $request_type, false), 'get', '') . "\n" .
       //   '        '<label class="fl_left">'. MODULE_BOXES_CURRENCIES_HEADER_FOOTER_BOX_TITLE.': </label>'. "\n".
			 			'				<label>'.$current_curr_symbol_left.''.$current_curr_title.''.$current_curr_symbol_right.'<i></i></label>'. "\n" .
						'	  '. $hidden_get_variables . tep_hide_session_id() . ''. "\n".
						'			<ul>'.$currencies_string. '</ul>'."\n".
          	'	  </form></div>'. "\n".
				  '</div>'. "\n";
        
          $oscTemplate->addBlock($data, $this->group);
        }
      }
    }

    function isEnabled() {
      return $this->enabled;
    }

    function check() {
      return defined('MODULE_BOXES_CURRENCIES_HEADER_FOOTER_STATUS');
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Currencies Module', 'MODULE_BOXES_CURRENCIES_HEADER_FOOTER_STATUS', 'True', 'Do you want to add the module to your shop?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Placement', 'MODULE_BOXES_CURRENCIES_HEADER_FOOTER_CONTENT_PLACEMENT', 'Above Header Block', 'Should the module be loaded in the Above Header Block?', '6', '1', 'tep_cfg_select_option(array(\'Above Header Block\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_BOXES_CURRENCIES_HEADER_FOOTER_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display in pages.', 'MODULE_BOXES_CURRENCIES_HEADER_FOOTER_DISPLAY_PAGES', 'all', 'select pages where this box should be displayed. ', '6', '0','tep_cfg_select_pages(' , now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array('MODULE_BOXES_CURRENCIES_HEADER_FOOTER_STATUS', 'MODULE_BOXES_CURRENCIES_HEADER_FOOTER_CONTENT_PLACEMENT', 'MODULE_BOXES_CURRENCIES_HEADER_FOOTER_SORT_ORDER','MODULE_BOXES_CURRENCIES_HEADER_FOOTER_DISPLAY_PAGES');
    }
  }
  
?>