Newwebshop/classes/Country.php

447 lines
14 KiB
PHP

<?php
/**
* Copyright seit 2024 Webshop System
*
* Länderverwaltung für das Webshop-System
* Vollständig PrestaShop-kompatibel mit erweiterten Funktionen
*
* @author Webshop System
* @license GPL v3
*/
class Country extends ObjectModel
{
/** @var int */
public $id;
/** @var int Zone id which country belongs */
public $id_zone;
/** @var int Currency id which country belongs */
public $id_currency;
/** @var string 2 letters iso code */
public $iso_code;
/** @var int international call prefix */
public $call_prefix;
/** @var string[]|string Name */
public $name;
/** @var bool Contain states */
public $contains_states;
/** @var bool Need identification number dni/nif/nie */
public $need_identification_number;
/** @var bool Need Zip Code */
public $need_zip_code;
/** @var string Zip Code Format */
public $zip_code_format;
/** @var bool Display or not the tax incl./tax excl. mention in the front office */
public $display_tax_label = true;
/** @var bool Status for delivery */
public $active = true;
protected static $_idZones = [];
public const GEOLOC_ALLOWED = 0;
public const GEOLOC_CATALOG_MODE = 1;
public const GEOLOC_FORBIDDEN = 2;
/**
* @see ObjectModel::$definition
*/
public static $definition = [
'table' => 'country',
'primary' => 'id_country',
'multilang' => true,
'fields' => [
'id_zone' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true],
'id_currency' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId'],
'call_prefix' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
'iso_code' => ['type' => self::TYPE_STRING, 'validate' => 'isLanguageIsoCode', 'required' => true, 'size' => 3],
'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'contains_states' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true],
'need_identification_number' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true],
'need_zip_code' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'zip_code_format' => ['type' => self::TYPE_STRING, 'validate' => 'isZipCodeFormat', 'size' => 12],
'display_tax_label' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true],
/* Lang fields */
'name' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 64],
],
'associations' => [
'zone' => ['type' => self::HAS_ONE],
'currency' => ['type' => self::HAS_ONE],
],
];
protected static $cache_iso_by_id = [];
protected $webserviceParameters = [
'objectsNodeName' => 'countries',
'fields' => [
'id_zone' => ['xlink_resource' => 'zones'],
'id_currency' => ['xlink_resource' => 'currencies'],
],
];
/**
* Konstruktor
*/
public function __construct($id = 1, $name = 'Deutschland', $iso_code = 'DE')
{
parent::__construct($id);
$this->name = $name;
$this->iso_code = $iso_code;
}
/**
* Deletes current Country from the database.
*/
public function delete()
{
if (!parent::delete()) {
return false;
}
return Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'cart_rule_country WHERE id_country = ' . (int) $this->id);
}
/**
* Return available countries
*/
public static function getCountries($idLang, $active = false, $containStates = false, $listStates = true)
{
$countries = [];
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT cl.*,c.*, cl.`name` country, z.`name` zone
FROM `' . _DB_PREFIX_ . 'country` c ' . Shop::addSqlAssociation('country', 'c') . '
LEFT JOIN `' . _DB_PREFIX_ . 'country_lang` cl ON (c.`id_country` = cl.`id_country` AND cl.`id_lang` = ' . (int) $idLang . ')
LEFT JOIN `' . _DB_PREFIX_ . 'zone` z ON (z.`id_zone` = c.`id_zone`)
WHERE 1' . ($active ? ' AND c.active = 1' : '') . ($containStates ? ' AND c.`contains_states` = ' . (int) $containStates : '') . '
ORDER BY cl.name ASC');
foreach ($result as $row) {
$countries[$row['id_country']] = $row;
}
if ($listStates) {
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT * FROM `' . _DB_PREFIX_ . 'state` ORDER BY `name` ASC');
foreach ($result as $row) {
if (isset($countries[$row['id_country']]) && $row['active'] == 1) {
$countries[$row['id_country']]['states'][] = $row;
}
}
}
return $countries;
}
/**
* Get countries by shop
*/
public static function getCountriesByIdShop($idShop, $idLang)
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT *
FROM `' . _DB_PREFIX_ . 'country` c
LEFT JOIN `' . _DB_PREFIX_ . 'country_shop` cs ON (cs.`id_country`= c.`id_country`)
LEFT JOIN `' . _DB_PREFIX_ . 'country_lang` cl ON (c.`id_country` = cl.`id_country` AND cl.`id_lang` = ' . (int) $idLang . ')
WHERE `id_shop` = ' . (int) $idShop);
}
/**
* Get a country ID with its iso code.
*/
public static function getByIso($isoCode, $active = false)
{
if (!Validate::isLanguageIsoCode($isoCode)) {
die(Tools::displayError('Given iso code (' . $isoCode . ') is not valid.'));
}
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(
'
SELECT `id_country`
FROM `' . _DB_PREFIX_ . 'country`
WHERE `iso_code` = \'' . pSQL(strtoupper($isoCode)) . '\''
. ($active ? ' AND active = 1' : '')
);
if (isset($result['id_country'])) {
return (int) $result['id_country'];
}
return false;
}
/**
* Get Zone ID by Country.
*/
public static function getIdZone($idCountry)
{
if (!Validate::isUnsignedId($idCountry)) {
die(Tools::displayError('Country ID is invalid.'));
}
if (isset(self::$_idZones[$idCountry])) {
return (int) self::$_idZones[$idCountry];
}
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT `id_zone`
FROM `' . _DB_PREFIX_ . 'country`
WHERE `id_country` = ' . (int) $idCountry);
if (isset($result['id_zone'])) {
self::$_idZones[$idCountry] = (int) $result['id_zone'];
return (int) $result['id_zone'];
}
return false;
}
/**
* Get a country name with its ID.
*/
public static function getNameById($idLang, $idCountry)
{
$key = 'country_getNameById_' . $idCountry . '_' . $idLang;
if (!Cache::isStored($key)) {
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
'
SELECT `name`
FROM `' . _DB_PREFIX_ . 'country_lang`
WHERE `id_country` = ' . (int) $idCountry . '
AND `id_lang` = ' . (int) $idLang
);
Cache::store($key, $result);
}
return Cache::retrieve($key);
}
/**
* Get a country iso with its ID.
*/
public static function getIsoById($idCountry)
{
if (!Validate::isUnsignedId($idCountry)) {
die(Tools::displayError('Country ID is invalid.'));
}
if (!isset(self::$cache_iso_by_id[$idCountry])) {
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
'
SELECT `iso_code`
FROM `' . _DB_PREFIX_ . 'country`
WHERE `id_country` = ' . (int) $idCountry
);
self::$cache_iso_by_id[$idCountry] = $result;
}
return self::$cache_iso_by_id[$idCountry];
}
/**
* Get a country id with its name.
*/
public static function getIdByName($idLang, $country)
{
$sql = '
SELECT `id_country`
FROM `' . _DB_PREFIX_ . 'country_lang`
WHERE `name` = \'' . pSQL($country) . '\'
AND `id_lang` = ' . (int) $idLang;
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
return (int) $result;
}
/**
* Get need zip code
*/
public static function getNeedZipCode($idCountry)
{
if (!Validate::isUnsignedId($idCountry)) {
die(Tools::displayError('Country ID is invalid.'));
}
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
'
SELECT `need_zip_code`
FROM `' . _DB_PREFIX_ . 'country`
WHERE `id_country` = ' . (int) $idCountry
);
return (bool) $result;
}
/**
* Get zip code format
*/
public static function getZipCodeFormat($idCountry)
{
if (!Validate::isUnsignedId($idCountry)) {
die(Tools::displayError('Country ID is invalid.'));
}
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
'
SELECT `zip_code_format`
FROM `' . _DB_PREFIX_ . 'country`
WHERE `id_country` = ' . (int) $idCountry
);
return $result;
}
/**
* Get countries by zone
*/
public static function getCountriesByZoneId($idZone, $idLang)
{
$countries = [];
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT cl.*, c.*, cl.`name` country
FROM `' . _DB_PREFIX_ . 'country` c
LEFT JOIN `' . _DB_PREFIX_ . 'country_lang` cl ON (c.`id_country` = cl.`id_country` AND cl.`id_lang` = ' . (int) $idLang . ')
WHERE c.`id_zone` = ' . (int) $idZone . '
AND c.active = 1
ORDER BY cl.name ASC');
foreach ($result as $row) {
$countries[$row['id_country']] = $row;
}
return $countries;
}
/**
* Check if country need DNI
*/
public function isNeedDni()
{
return (bool) $this->need_identification_number;
}
/**
* Check if country need DNI by country ID
*/
public static function isNeedDniByCountryId($idCountry)
{
if (!Validate::isUnsignedId($idCountry)) {
die(Tools::displayError('Country ID is invalid.'));
}
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
'
SELECT `need_identification_number`
FROM `' . _DB_PREFIX_ . 'country`
WHERE `id_country` = ' . (int) $idCountry
);
return (bool) $result;
}
/**
* Check if country contains states
*/
public static function containsStates($idCountry)
{
if (!Validate::isUnsignedId($idCountry)) {
die(Tools::displayError('Country ID is invalid.'));
}
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
'
SELECT `contains_states`
FROM `' . _DB_PREFIX_ . 'country`
WHERE `id_country` = ' . (int) $idCountry
);
return (bool) $result;
}
/**
* Affect zone to selection
*/
public function affectZoneToSelection($idsCountries, $idZone)
{
$countries = [];
foreach ($idsCountries as $idCountry) {
if (Validate::isUnsignedId($idCountry)) {
$countries[] = (int) $idCountry;
}
}
if (count($countries)) {
return Db::getInstance()->execute('
UPDATE `' . _DB_PREFIX_ . 'country`
SET `id_zone` = ' . (int) $idZone . '
WHERE `id_country` IN (' . implode(',', $countries) . ')');
}
return false;
}
/**
* Check zip code
*/
public function checkZipCode($zipCode)
{
if (empty($this->zip_code_format)) {
return true;
}
$zipRegexp = '/^' . $this->zip_code_format . '$/ui';
$zipRegexp = str_replace(' ', ' *', $zipRegexp);
$zipRegexp = str_replace('-', '-?', $zipRegexp);
$zipRegexp = str_replace('N', '[0-9]', $zipRegexp);
$zipRegexp = str_replace('L', '[a-zA-Z]', $zipRegexp);
$zipRegexp = str_replace('C', $this->iso_code, $zipRegexp);
return (bool) preg_match($zipRegexp, $zipCode);
}
/**
* Add module restrictions
*/
public static function addModuleRestrictions(array $shops = [], array $countries = [], array $modules = [])
{
$sql = 'INSERT INTO `' . _DB_PREFIX_ . 'module_country` (`id_module`, `id_shop`, `id_country`) VALUES ';
$sqlValues = [];
foreach ($modules as $idModule) {
foreach ($shops as $idShop) {
foreach ($countries as $idCountry) {
$sqlValues[] = '(' . (int) $idModule . ', ' . (int) $idShop . ', ' . (int) $idCountry . ')';
}
}
}
if (!empty($sqlValues)) {
$sql .= implode(', ', $sqlValues);
return Db::getInstance()->execute($sql);
}
return true;
}
/**
* Add country
*/
public function add($autoDate = true, $nullValues = false)
{
$return = parent::add($autoDate, $nullValues);
if ($return) {
$this->addModuleRestrictions([Configuration::get('PS_SHOP_DEFAULT')], [$this->id], Module::getPaymentModules());
}
return $return;
}
}