Address.php
Go to the documentation of this file.
1<?php
10
39class Address extends Fieldset
40{
49 public function __construct($name, $parameters, $form)
50 {
51 parent::__construct($name, $parameters, $form);
52
53 $this->props = [
54 'Address1',
55 'Address2',
56 'Zip',
57 'City',
58 'Country',
59 'State',
60 ];
61 $this->propsMaxLength = [
62 'Address1' => 255,
63 'Address2' => 255,
64 'Zip' => 45,
65 'City' => 255,
66 'VatId' => 60,
67 ];
68
69
70 if (isset($parameters['props'])) {
71 $this->props = $parameters['props'];
72 }
73 foreach ($this->props as $prop) {
74 if (isset($parameters["default$prop"])) {
75 $this->defaults["default$prop"] = $parameters["default$prop"];
76 }
77 }
78
79 $this->defaults['priorityCountries'] = isset($parameters['priorityCountries'])
80 ? $parameters['priorityCountries']
81 : array();
82
83 $this->prefix = isset($parameters['prefix'])
84 ? rtrim($parameters['prefix'], '_') . '_'
85 : '';
86 }
87
97 protected function setDefaults()
98 {
99 parent::setDefaults();
100
101 $this->defaults['required'] = false;
102 $this->defaults['labelAddress1'] = _("Address 1");
103 $this->defaults['labelAddress2'] = _("Address 2");
104 $this->defaults['labelCity'] = _("City");
105 $this->defaults['labelState'] = _("State");
106 $this->defaults['labelZip'] = _("Zip");
107 $this->defaults['labelCountry'] = _("Country");
108 }
109
115 public function addChildElements()
116 {
117 parent::addChildElements();
118
119 foreach ($this->props as $prop) {
120 if (isset($this->defaults["default$prop"])) {
121 $labelVar = "label$prop";
122
123 if ($prop == "State") {
124 $this->addState($this->prefix . "state", array(
125 'label' => $this->labelState,
126 'defaultValue' => $this->defaults['defaultState'],
127 //'required' => $this->required,
128 ));
129 } else if ($prop == "Country") {
130 $this->addCountry($this->prefix . "country", array(
131 'label' => $this->labelCountry,
132 'priorityCountries' => $this->defaults['priorityCountries'],
133 'defaultValue' => $this->defaults['defaultCountry'],
134 'required' => $this->required,
135 ));
136 } else {
137 $this->addText($this->prefix . strtolower($prop), array(
138 'label' => $this->$labelVar,
139 'maxlength' => $this->propsMaxLength[$prop],
140 'defaultValue' => $this->defaults["default$prop"],
141 'required' => $prop != "Address2" && $this->required,
142 ));
143 }
144 }
145 }
146 }
147
153 public function validate()
154 {
155 if (parent::validate() && isset($this->defaultCountry) && isset($this->defaultState)) {
156 // check selected state matches the country
157 $country_states = state::getStates();
158 $country = $this->getElement("{$this->prefix}country");
159 $state = $this->getElement("{$this->prefix}state");
160 $this->valid = isset($country_states[$country->getValue()][$state->getValue()]);
161 if (!$this->valid) {
162 $state->errorMessage = _("State and Country do not match");
163 $state->valid = false;
164 }
165
166 // TODO validate zip code
167 }
168
169 return $this->valid;
170 }
171}
172
173/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
getElement($name, $includeFieldsets=false)
Gets subelement by name.
$valid
Contains element validation status/result.
Definition Element.php:30
Default address fieldset.
Definition Address.php:40
addChildElements()
adds address-inputs to fieldset
Definition Address.php:115
validate()
Validate the address data.
Definition Address.php:153
__construct($name, $parameters, $form)
multiple class constructor
Definition Address.php:49
setDefaults()
collects initial values across subclasses.
Definition Address.php:97
The fieldset class holds HTML-fieldset specific attributes and methods.
Definition Fieldset.php:51
Classes for HTML input-elements.
Definition Address.php:9