<?php
/** 
 *
 * @copyright Copyright 2007 Paul Mathot Haarlem, The Netherlands
 * @copyright parts Copyright Zen Cart Development Team
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version v1.4.2 (or newer)
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}

function zen_group_name_add_to_session(){
  // added for compatibility only, please use zen_group_add_to_session() instead  
  zen_group_add_to_session();
}
function zen_group_add_to_session(){
  // check customers group_name and add it to the SESSION array
  // this function should be called whenever a customer logs in (also after account creation)
  // or at every page request (at the time of writing it's called on every page request)
  global $db;
  // checking isset($_SESSION['group_name'] saves a queries, but customers need to logout and in again before they notice groups setting changes 
  if(!isset($_SESSION['group_name'])){
    $group_query = $db->Execute("select customers_group_pricing from " . TABLE_CUSTOMERS . " where customers_id = '" . $_SESSION['customer_id'] . "'");
    if ($group_query->fields['customers_group_pricing'] != '0') {
        $group_discount = $db->Execute("select group_name, group_percentage from " . TABLE_GROUP_PRICING . " where
                                        group_id = '" . $group_query->fields['customers_group_pricing'] . "'");
        //$_SESSION['group_name'] =  $group_discount->fields['group_name'];
        // if groupname equals 'taxfree' for example set the tax to 0 (in functions_taxes)
        if (isset($group_discount->fields['group_name'])) {
           $_SESSION['group_name'] =  $group_discount->fields['group_name'];
        }
    }
  } 
  
}

function zen_group_name_is_valid($group_name, $strict = false){
  // added for compatibility only, please use zen_group_is_valid() instead
  return zen_group_is_valid($group_name, $strict);
}
function zen_group_is_valid($group_name, $strict = false){
  // $strict = true means that the group name has to be an exact match
  // when $strict is false, any group name starting with $group_name will be valid
  
  // for compatibility with older Zen Cart versions that don't support init_includes(?) 
  if((isset($_SESSION['customer_id']))&&(!isset($_SESSION['group_name']))) zen_group_add_to_session();
  
  if((isset($_SESSION['group_name'])&&(strlen($_SESSION['group_name']) > 0))){
    if (($strict === false)&&(strstr($_SESSION['group_name'],$group_name) == $_SESSION['group_name'])){
      return true;
    }elseif($_SESSION['group_name'] == $group_name){
      return true;
    }
  }
  
  return false;
}
?>