<?php
class ClassCountry extends ClassConfig {
    public function __construct(){}
    public function __destruct(){}
    
    public function listActiveCountries($order=''){
        $this->_order = $order;
        $oPDOLink = ClassConfig::databaseConnect();
        $sql="
        SELECT *
        FROM core_country
        WHERE is_active=TRUE
        ";
        if($this->_order != ''){
            $sql .= " ORDER BY ". $this->_order;
        }
        $execSQL = $oPDOLink->prepare($sql);
        $execSQL->execute(array());
        $rows = $execSQL->fetchAll(PDO::FETCH_ASSOC);
        return $rows;
    }
    
    public function listCountries($order=''){
        $this->_order = $order;
        $oPDOLink = ClassConfig::databaseConnect();
        $sql="
        SELECT *
        FROM core_country
        ";
        if($this->_order != ''){
            $sql .= " ORDER BY ". $this->_order;
        }
        $execSQL = $oPDOLink->prepare($sql);
        $execSQL->execute(array());
        $rows = $execSQL->fetchAll(PDO::FETCH_ASSOC);
        return $rows;
    }
    
    public function getCitiesFromCountryAndPostCode($country_id, $postcode){
        $this->_countryId = $country_id;
        $this->_postcode = $postcode;
        $oPDOLink = ClassConfig::databaseConnect();
        $sql="
        SELECT *
        FROM core_city
        WHERE core_country_id=:country_id
            AND postcode=:postcode 
        ";
        $execSQL = $oPDOLink->prepare($sql);
        $execSQL->execute(array(':country_id'=>$this->_countryId, ':postcode'=>$this->_postcode));
        $rows = $execSQL->fetchAll(PDO::FETCH_ASSOC);
        return $rows;
    }
    
    public function getCountryNameById($country_id){
        $this->_countryId = $country_id;
        
        foreach($_SESSION['countries'] as $country){
            if($country['id'] == $this->_countryId){
                return $country['name'];
            } else {
                continue;
            }
        }
    }
    
    public function getCountryById($country_id){
        $this->_countryId = $country_id;
        
        foreach($_SESSION['countries'] as $country){
            if($country['id'] == $this->_countryId){
                return $country;
            } else {
                continue;
            }
        }
    }
    
  public function getFlagByCountryId($country_id){
    $this->_countryId = $country_id;
    
    foreach($_SESSION['countries'] as $country){
      if($country['id'] == $this->_countryId){
        return $country['code'];
      } else {
        continue;
      }
    }
  }
    
  public function getCountryIdByCode($code){
    $this->_code = $code;

    foreach($_SESSION['countries'] as $country){
      if($country['code'] == $this->_code){
        return $country['id'];
      } else {
        continue;
      }
    }
  }
  
  public function getCurrencyIdByCountryId($country_id){
    $this->_countryId = $country_id;

    foreach($_SESSION['countries'] as $country){
      if($country['id'] == $this->_countryId){
        return $country['core_currency_id'];
      } else {
        continue;
      }
    }
  }
  
}
