PageRenderTime 27ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/classes/Address.php

https://github.com/Dimyan/muhovyaz_ps
PHP | 329 lines | 194 code | 50 blank | 85 comment | 11 complexity | 1c00bef9d60948363ffebffda6d1b1e2 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2011 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2011 PrestaShop SA
  23. * @version Release: $Revision: 6594 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. class AddressCore extends ObjectModel
  28. {
  29. /** @var integer Customer id which address belongs */
  30. public $id_customer = NULL;
  31. /** @var integer Manufacturer id which address belongs */
  32. public $id_manufacturer = NULL;
  33. /** @var integer Supplier id which address belongs */
  34. public $id_supplier = NULL;
  35. /** @var integer Country id */
  36. public $id_country;
  37. /** @var integer State id */
  38. public $id_state;
  39. /** @var string Country name */
  40. public $country;
  41. /** @var string Alias (eg. Home, Work...) */
  42. public $alias;
  43. /** @var string Company (optional) */
  44. public $company;
  45. /** @var string Lastname */
  46. public $lastname;
  47. /** @var string Firstname */
  48. public $firstname;
  49. /** @var string Address first line */
  50. public $address1;
  51. /** @var string Address second line (optional) */
  52. public $address2;
  53. /** @var string Postal code */
  54. public $postcode;
  55. /** @var string City */
  56. public $city;
  57. /** @var string Any other useful information */
  58. public $other;
  59. /** @var string Phone number */
  60. public $phone;
  61. /** @var string Mobile phone number */
  62. public $phone_mobile;
  63. /** @var string VAT number */
  64. public $vat_number;
  65. /** @var string DNI number */
  66. public $dni;
  67. /** @var string Object creation date */
  68. public $date_add;
  69. /** @var string Object last modification date */
  70. public $date_upd;
  71. /** @var boolean True if address has been deleted (staying in database as deleted) */
  72. public $deleted = 0;
  73. protected static $_idZones = array();
  74. protected static $_idCountries = array();
  75. protected $fieldsRequired = array('id_country', 'alias', 'lastname', 'firstname', 'address1', 'city');
  76. protected $fieldsSize = array('alias' => 32, 'company' => 32, 'lastname' => 32, 'firstname' => 32,
  77. 'address1' => 128, 'address2' => 128, 'postcode' => 12, 'city' => 64,
  78. 'other' => 300, 'phone' => 16, 'phone_mobile' => 16, 'dni' => 16);
  79. protected $fieldsValidate = array('id_customer' => 'isNullOrUnsignedId', 'id_manufacturer' => 'isNullOrUnsignedId',
  80. 'id_supplier' => 'isNullOrUnsignedId', 'id_country' => 'isUnsignedId', 'id_state' => 'isNullOrUnsignedId',
  81. 'alias' => 'isGenericName', 'company' => 'isGenericName', 'lastname' => 'isName','vat_number' => 'isGenericName',
  82. 'firstname' => 'isName', 'address1' => 'isAddress', 'address2' => 'isAddress', 'postcode'=>'isPostCode',
  83. 'city' => 'isCityName', 'other' => 'isMessage',
  84. 'phone' => 'isPhoneNumber', 'phone_mobile' => 'isPhoneNumber', 'deleted' => 'isBool', 'dni' => 'isDniLite');
  85. protected $table = 'address';
  86. protected $identifier = 'id_address';
  87. protected $_includeVars = array('addressType' => 'table');
  88. protected $_includeContainer = false;
  89. protected $webserviceParameters = array(
  90. 'objectsNodeName' => 'addresses',
  91. 'fields' => array(
  92. 'id_customer' => array('xlink_resource'=> 'customers'),
  93. 'id_manufacturer' => array('xlink_resource'=> 'manufacturers'),
  94. 'id_supplier' => array('xlink_resource'=> 'suppliers'),
  95. 'id_country' => array('xlink_resource'=> 'countries'),
  96. 'id_state' => array('xlink_resource'=> 'states'),
  97. ),
  98. );
  99. /**
  100. * Build an address
  101. *
  102. * @param integer $id_address Existing address id in order to load object (optional)
  103. */
  104. public function __construct($id_address = NULL, $id_lang = NULL)
  105. {
  106. parent::__construct($id_address);
  107. /* Get and cache address country name */
  108. if ($this->id)
  109. {
  110. $result = Db::getInstance()->getRow('SELECT `name` FROM `'._DB_PREFIX_.'country_lang`
  111. WHERE `id_country` = '.(int)($this->id_country).'
  112. AND `id_lang` = '.($id_lang ? (int)($id_lang) : Configuration::get('PS_LANG_DEFAULT')));
  113. $this->country = $result['name'];
  114. }
  115. }
  116. public function add($autodate = true, $nullValues = false)
  117. {
  118. if (!parent::add($autodate, $nullValues))
  119. return false;
  120. if (Validate::isUnsignedId($this->id_customer))
  121. Customer::resetAddressCache($this->id_customer);
  122. return true;
  123. }
  124. public function delete()
  125. {
  126. if (Validate::isUnsignedId($this->id_customer))
  127. Customer::resetAddressCache($this->id_customer);
  128. if (!$this->isUsed())
  129. return parent::delete();
  130. else
  131. {
  132. $this->deleted = true;
  133. return $this->update();
  134. }
  135. }
  136. /**
  137. * Returns fields required for an address in an array hash
  138. * @return array hash values
  139. */
  140. public static function getFieldsValidate()
  141. {
  142. $tmp_addr = new Address();
  143. $out = ($tmp_addr->fieldsValidate);
  144. unset($tmp_addr);
  145. return $out;
  146. }
  147. public function getFields()
  148. {
  149. parent::validateFields();
  150. if (isset($this->id))
  151. $fields['id_address'] = (int)($this->id);
  152. $fields['id_customer'] = is_null($this->id_customer) ? 0 : (int)($this->id_customer);
  153. $fields['id_manufacturer'] = is_null($this->id_manufacturer) ? 0 : (int)($this->id_manufacturer);
  154. $fields['id_supplier'] = is_null($this->id_supplier) ? 0 : (int)($this->id_supplier);
  155. $fields['id_country'] = (int)($this->id_country);
  156. $fields['id_state'] = (int)($this->id_state);
  157. $fields['alias'] = pSQL($this->alias);
  158. $fields['company'] = pSQL($this->company);
  159. $fields['lastname'] = pSQL($this->lastname);
  160. $fields['firstname'] = pSQL($this->firstname);
  161. $fields['address1'] = pSQL($this->address1);
  162. $fields['address2'] = pSQL($this->address2);
  163. $fields['postcode'] = pSQL($this->postcode);
  164. $fields['city'] = pSQL($this->city);
  165. $fields['other'] = pSQL($this->other);
  166. $fields['phone'] = pSQL($this->phone);
  167. $fields['phone_mobile'] = pSQL($this->phone_mobile);
  168. $fields['vat_number'] = pSQL($this->vat_number);
  169. $fields['dni'] = pSQL($this->dni);
  170. $fields['deleted'] = (int)($this->deleted);
  171. $fields['date_add'] = pSQL($this->date_add);
  172. $fields['date_upd'] = pSQL($this->date_upd);
  173. return $fields;
  174. }
  175. public function validateController($htmlentities = true)
  176. {
  177. $errors = parent::validateController($htmlentities);
  178. if (!Configuration::get('VATNUMBER_CHECKING'))
  179. return $errors;
  180. include_once(_PS_MODULE_DIR_.'vatnumber/vatnumber.php');
  181. if (class_exists('VatNumber', false))
  182. return array_merge($errors, VatNumber::WebServiceCheck($this->vat_number));
  183. return $errors;
  184. }
  185. /**
  186. * Get zone id for a given address
  187. *
  188. * @param integer $id_address Address id for which we want to get zone id
  189. * @return integer Zone id
  190. */
  191. public static function getZoneById($id_address)
  192. {
  193. if (isset(self::$_idZones[$id_address]))
  194. return self::$_idZones[$id_address];
  195. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
  196. SELECT s.`id_zone` AS id_zone_state, c.`id_zone`
  197. FROM `'._DB_PREFIX_.'address` a
  198. LEFT JOIN `'._DB_PREFIX_.'country` c ON c.`id_country` = a.`id_country`
  199. LEFT JOIN `'._DB_PREFIX_.'state` s ON s.`id_state` = a.`id_state`
  200. WHERE a.`id_address` = '.(int)($id_address));
  201. self::$_idZones[$id_address] = (int)((int)($result['id_zone_state']) ? $result['id_zone_state'] : $result['id_zone']);
  202. return self::$_idZones[$id_address];
  203. }
  204. /**
  205. * Check if country is active for a given address
  206. *
  207. * @param integer $id_address Address id for which we want to get country status
  208. * @return integer Country status
  209. */
  210. public static function isCountryActiveById($id_address)
  211. {
  212. if (!$result = Db::getInstance()->getRow('
  213. SELECT c.`active`
  214. FROM `'._DB_PREFIX_.'address` a
  215. LEFT JOIN `'._DB_PREFIX_.'country` c ON c.`id_country` = a.`id_country`
  216. WHERE a.`id_address` = '.(int)($id_address)))
  217. return false;
  218. return ($result['active']);
  219. }
  220. /**
  221. * Check if address is used (at least one order placed)
  222. *
  223. * @return integer Order count for this address
  224. */
  225. public function isUsed()
  226. {
  227. $result = Db::getInstance()->getRow('
  228. SELECT COUNT(`id_order`) AS used
  229. FROM `'._DB_PREFIX_.'orders`
  230. WHERE `id_address_delivery` = '.(int)($this->id).'
  231. OR `id_address_invoice` = '.(int)($this->id));
  232. return isset($result['used']) ? $result['used'] : false;
  233. }
  234. /**
  235. * @param int $id_address
  236. * @return int
  237. * @deprecated
  238. */
  239. static public function getManufacturerIdByAddress($id_address)
  240. {
  241. Tools::displayAsDeprecated();
  242. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
  243. SELECT `id_manufacturer` FROM `'._DB_PREFIX_.'address`
  244. WHERE `id_address` = '.(int)($id_address));
  245. return isset($result['id_manufacturer']) ? $result['id_manufacturer'] : false;
  246. }
  247. static public function getCountryAndState($id_address)
  248. {
  249. if (isset(self::$_idCountries[$id_address]))
  250. return self::$_idCountries[$id_address];
  251. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
  252. SELECT `id_country`, `id_state`, `vat_number`, `postcode` FROM `'._DB_PREFIX_.'address`
  253. WHERE `id_address` = '.(int)($id_address));
  254. self::$_idCountries[$id_address] = $result;
  255. return $result;
  256. }
  257. /**
  258. * Specify if an address is already in base
  259. *
  260. * @param $id_address Address id
  261. * @return boolean
  262. */
  263. static public function addressExists($id_address)
  264. {
  265. $row = Db::getInstance()->getRow('
  266. SELECT `id_address`
  267. FROM '._DB_PREFIX_.'address a
  268. WHERE a.`id_address` = '.(int)($id_address));
  269. return isset($row['id_address']);
  270. }
  271. static public function getFirstCustomerAddressId($id_customer, $active = true)
  272. {
  273. return Db::getInstance()->getValue('
  274. SELECT `id_address`
  275. FROM `'._DB_PREFIX_.'address`
  276. WHERE `id_customer` = '.(int)($id_customer).' AND `deleted` = 0'.($active ? ' AND `active` = 1' : '')
  277. );
  278. }
  279. }