Something went wrong. Try again.
Easy Address Field is the easiest way to add an address to your entries.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312<?php
namespace studioespresso\easyaddressfield\models;
use Craft;use craft\helpers\Json;use craft\helpers\Template;use craft\web\View;use studioespresso\easyaddressfield\EasyAddressField;use yii\base\Model;
/** * Class EasyAddressFieldModel * @package studioespresso\easyaddressfield\models */class EasyAddressFieldModel extends Model{ /** * EasyAddressFieldModel constructor. * @param array $attributes * @param array $config */ public function __construct($attributes = [], array $config = []) { if (is_array($attributes)) { foreach ($attributes as $key => $value) { if (property_exists($this, $key)) { $this[$key] = $value; } } } elseif (is_object($attributes) && get_class($attributes) === self::class) { foreach ($attributes->toArray() as $key => $value) { if (property_exists($this, $key)) { $this[$key] = $value; } } } else { $attributes = Json::decodeIfJson($attributes); foreach ($attributes as $key => $value) { if (property_exists($this, $key)) { $this[$key] = $value; } } } parent::__construct($config); }
/** * @return array */ public function rules() { return [ [ [ 'id', 'name', 'street', 'street2', 'postalCode', 'city', 'state', 'country', 'latitude', 'longitude', ], 'safe', ], ]; }
/** * Address ID * * @var */ public $id;
/** * Element ID * * @var */ public $owner;
/** * Site ID * * @var */ public $site;
/** * Field ID * * @var */ public $field;
/** * The name of the address * * @var */ public $name;
/** * Latitude * * @var string */ public $latitude;
/** * Longitude * * @var string */ public $longitude;
/** * The first line of the street block. * * @var string */ public $street;
/** * The second line of the street block. * * @var string */ public $street2;
/** * The postal code. * * @var string */ public $postalCode;
/** * @var */ public $city;
/** * @var */ public $state;
/** * @var */ public $country;
/** * @return string */ public function getLatitude(): string { return $this->latitude; }
/** * @return string */ public function getLongitude(): string { return $this->longitude; }
/** * Fallback for older installs of StatikAddress * @return string */ public function lat() { Craft::$app->getDeprecator()->log(__CLASS__ . 'lat', "The 'lat' method has been renamed to 'latitude'."); return $this->latitude; }
/** * Fallback for older installs of StatikAddress * @return string */ public function long() { Craft::$app->getDeprecator()->log(__CLASS__ . 'long', "The 'long' method has been renamed to 'longitude'."); return $this->longitude; }
/** * @param $locale * @return string | null */ public function getCountryName($locale = null) { if (!$locale) { $locale = Craft::$app->getLocale(); } if ($this->country) { $name = EasyAddressField::getInstance()->countries->getCountryNameByAlpha2($this->country, $locale); return $name; } return null; }
public function getDirectionsUrl($currentLocation = false): string { if ($currentLocation) { $str = 'https://www.google.com/maps/dir/current+location/'; } else { $str = 'https://www.google.com/maps/dir//'; } $data = $this->toString(','); $str .= str_replace(' ', '+', $data);
return $str; }
/** * @param string $glue * @return string */ public function toString($glue = '+'): string { $data = array(); if (!empty($this->street)) { $data['street'] = $this->street; } if (!empty($this->street2)) { $data['street2'] = $this->street2; } if (!empty($this->postalCode)) { $data['postalCode'] = $this->postalCode; } if (!empty($this->city)) { $data['city'] = $this->city; } if (!empty($this->country)) { $data['country'] = $this->getCountryName(); }
return implode($glue, $data); }
public function modelToString($glue = '+'): string { $data = array(); if (!empty($this->name)) { $data['name'] = $this->name; } if (!empty($this->street)) { $data['street'] = $this->street; } if (!empty($this->street2)) { $data['street2'] = $this->street2; } if (!empty($this->postalCode)) { $data['postalCode'] = $this->postalCode; } if (!empty($this->city)) { $data['city'] = $this->city; } if (!empty($this->state)) { $data['state'] = $this->state; } if (!empty($this->country)) { $data['country'] = $this->getCountryName(); }
return implode($glue, $data); }
public function formatted($includeCountry = false) { $view = Craft::$app->getView(); $oldTemplateMode = $view->getTemplateMode();
$view->setTemplateMode(View::TEMPLATE_MODE_CP); $template = $view->renderTemplate('easy-address-field/_formatted', [ 'address' => $this, 'includeCountry' => $includeCountry, ]);
$view->setTemplateMode($oldTemplateMode); return Template::raw($template); }
public function isEmpty($params = []) { if ($params) { $values = $this->toArray($params); } else { $values = $this->toArray(['name', 'street', 'street2', 'postalCode', 'state', 'country']); } $values = array_filter($values); if (empty($values)) { return true; }
if (count($values) == 1 && isset($values['country'])) { return true; } else { return false; } }}