|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Styrer adresser til intranet, bruger, kunde og kontaktperson |
|
4
|
|
|
* |
|
5
|
|
|
* Klassen kan styrer flere forskellige typer af adresser. B�de for intranettet, brugere, kunder og kontaktpersoner. |
|
6
|
|
|
* Beskrivelsen af hvilke og med hvilket navn er beskrevet l�ngere nede. |
|
7
|
|
|
* |
|
8
|
|
|
* @todo Skal vi programmere intranet_id ind i klassen? Det kr�ver at den f�r Kernel. |
|
9
|
|
|
* |
|
10
|
|
|
* @package Intraface |
|
11
|
|
|
* @author Sune Jensen <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
require_once 'Intraface/functions.php'; |
|
14
|
|
|
|
|
15
|
|
|
class Intraface_Address extends Intraface_Standard |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var integer |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $belong_to_key; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var integer |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $belong_to_id; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var integer |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $id; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
public $value = array(); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
public $fields = array('name', 'address', 'postcode', 'city', 'country', 'cvr', 'email', 'website', 'phone', 'ean'); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var object error |
|
44
|
|
|
*/ |
|
45
|
|
|
public $error; |
|
46
|
|
|
|
|
47
|
|
|
protected $db; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Init: loader klassen |
|
51
|
|
|
* |
|
52
|
|
|
* Her er angivet de typer af adresser den kan h�ndtere med arrayet address_type[]. |
|
53
|
|
|
* $this-fields er felter i tabellen (db) som overf�res til array og omvendt. M�ske disse |
|
54
|
|
|
* engang skal differencieres, s� man angvier hvad feltet i tabellen skal svare til navnet i arrayet. |
|
55
|
|
|
* Klassen loader ogs� adressens felter |
|
56
|
|
|
* |
|
57
|
|
|
* @param integer $id Id on address. |
|
58
|
|
|
* |
|
59
|
|
|
* @return void |
|
|
|
|
|
|
60
|
|
|
*/ |
|
61
|
97 |
|
function __construct($id) |
|
62
|
|
|
{ |
|
63
|
97 |
|
$this->id = $id; |
|
64
|
97 |
|
$this->error = new Intraface_Error; |
|
65
|
97 |
|
$this->db = MDB2::singleton(DB_DSN); |
|
66
|
|
|
|
|
67
|
97 |
|
$this->load(); |
|
68
|
|
|
|
|
69
|
97 |
|
$this->belong_to_types = $this->getBelongToTypes(); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
97 |
|
if (PEAR::isError($this->db)) { |
|
73
|
|
|
throw new Exception("Error db singleton: ".$this->db->getUserInfo()); |
|
74
|
|
|
} |
|
75
|
97 |
|
$this->db->setFetchMode(MDB2_FETCHMODE_ASSOC); |
|
76
|
97 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns an instance of Address from belong_to and belong_to_id |
|
80
|
|
|
* |
|
81
|
|
|
* @deprecated |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $belong_to What the address belongs to, corresponding to the ones in Address::getBelongToTypes() |
|
84
|
|
|
* @param integer $belong_to_id From belong_to. NB not id on the address |
|
85
|
|
|
* |
|
86
|
|
|
* @return object Address |
|
87
|
|
|
*/ |
|
88
|
|
|
function factory($belong_to, $belong_to_id) |
|
89
|
|
|
{ |
|
90
|
|
|
$gateway = new Intraface_AddressGateway(new DB_Sql); |
|
91
|
|
|
return $gateway->findByBelongToAndId($belong_to, $belong_to_id); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns possible belong to types |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
97 |
|
public static function getBelongToTypes() |
|
100
|
|
|
{ |
|
101
|
97 |
|
return array(1 => 'intranet', |
|
102
|
97 |
|
2 => 'user', |
|
103
|
97 |
|
3 => 'contact', |
|
104
|
97 |
|
4 => 'contact_delivery', |
|
105
|
97 |
|
5 => 'contact_invoice', |
|
106
|
97 |
|
6 => 'contactperson'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Sets belong to @todo used for what? |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $belong_to Which type the address belongs to |
|
113
|
|
|
* @param integer $belong_to_id Which id for the type the address belongs to |
|
114
|
|
|
* |
|
115
|
|
|
* @return void |
|
116
|
|
|
*/ |
|
117
|
80 |
|
function setBelongTo($belong_to, $belong_to_id) |
|
118
|
|
|
{ |
|
119
|
80 |
|
if ($this->id != 0) { |
|
120
|
|
|
// is id already set, then you can not change belong_to |
|
121
|
|
|
return; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
80 |
|
$belong_to_types = $this->getBelongToTypes(); |
|
125
|
80 |
|
$this->belong_to_key = array_search($belong_to, $belong_to_types); |
|
|
|
|
|
|
126
|
80 |
|
if ($this->belong_to_key === false) { |
|
127
|
|
|
throw new Exception("Invalid address type ".$belong_to." in Address::setBelongTo()"); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
80 |
|
$this->belong_to_id = (int)$belong_to_id; |
|
131
|
80 |
|
if ($this->belong_to_id == 0) { |
|
132
|
|
|
throw new Exception("Invalid belong_to_id in Address::setBelongTo()"); |
|
133
|
|
|
} |
|
134
|
80 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Loads data to array |
|
138
|
|
|
* |
|
139
|
|
|
* @return integer |
|
140
|
|
|
*/ |
|
141
|
97 |
|
protected function load() |
|
142
|
|
|
{ |
|
143
|
97 |
|
if ($this->id == 0) { |
|
144
|
80 |
|
return 0; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
91 |
|
$result = $this->db->query("SELECT id, type, belong_to_id, ".implode(', ', $this->fields)." FROM address WHERE id = ".(int)$this->id); |
|
148
|
|
|
|
|
149
|
91 |
|
if (PEAR::isError($result)) { |
|
150
|
|
|
throw new Exception($result->getUserInfo()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
91 |
|
if ($result->numRows() > 1) { |
|
154
|
|
|
throw new Exception('There is more than one active address'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
91 |
|
if ($result->numRows() == 0) { |
|
158
|
|
|
$this->id = 0; |
|
159
|
|
|
$this->value['id'] = 0; |
|
160
|
|
|
|
|
161
|
|
|
return 0; |
|
162
|
|
|
} |
|
163
|
91 |
|
$row = $result->fetchRow(MDB2_FETCHMODE_ASSOC); |
|
164
|
|
|
|
|
165
|
91 |
|
$this->value = $row; |
|
166
|
91 |
|
$this->value['id'] = $row['id']; |
|
167
|
91 |
|
$this->value['address_id'] = $row['id']; |
|
168
|
91 |
|
$this->belong_to_key = $row['type']; |
|
169
|
91 |
|
$this->belong_to_id = $row['belong_to_id']; |
|
170
|
|
|
|
|
171
|
91 |
|
return $this->id; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Validates |
|
176
|
|
|
* |
|
177
|
|
|
* @param array $array_var Values |
|
178
|
|
|
* |
|
179
|
|
|
* @return boolean |
|
180
|
|
|
*/ |
|
181
|
2 |
|
function validate($array_var) |
|
182
|
|
|
{ |
|
183
|
2 |
|
$validator = new Intraface_Validator($this->error); |
|
184
|
2 |
|
if (empty($array_var)) { |
|
185
|
1 |
|
$this->error->set('array cannot be empty'); |
|
186
|
1 |
|
} |
|
187
|
|
|
|
|
188
|
|
|
// public $fields = array('name', 'address', 'postcode', 'city', 'country', 'cvr', 'email', 'website', 'phone', 'ean'); |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
2 |
|
settype($array_var['name'], 'string'); |
|
191
|
2 |
|
$validator->isString($array_var['name'], 'there was an error in name', ''); |
|
192
|
2 |
|
settype($array_var['address'], 'string'); |
|
193
|
2 |
|
$validator->isString($array_var['address'], 'there was an error in address', ''); |
|
194
|
2 |
|
settype($array_var['postcode'], 'string'); |
|
195
|
2 |
|
$validator->isNumeric($array_var['postcode'], 'there was an error in postcode', 'greater_than_zero'); |
|
196
|
2 |
|
settype($array_var['city'], 'string'); |
|
197
|
2 |
|
$validator->isString($array_var['city'], 'there was an error in city', ''); |
|
198
|
2 |
|
settype($array_var['country'], 'string'); |
|
199
|
2 |
|
$validator->isString($array_var['country'], 'there was an error in country', '', 'allow_empty'); |
|
200
|
2 |
|
settype($array_var['cvr'], 'string'); |
|
201
|
2 |
|
$validator->isString($array_var['cvr'], 'there was an error in cvr', '', 'allow_empty'); |
|
202
|
|
|
// E-mail is not allowed to be empty do you need that. You should probably consider some places there this is needed before you set it (eg. intranet and user address) maybe make a param more to the function determine that: 'email:allow_empty' |
|
203
|
2 |
|
settype($array_var['email'], 'string'); |
|
204
|
2 |
|
$validator->isEmail($array_var['email'], 'not a valid e-mail'); |
|
205
|
2 |
|
settype($array_var['website'], 'string'); |
|
206
|
2 |
|
$validator->isUrl($array_var['website'], 'website is not valid', '', 'allow_empty'); |
|
|
|
|
|
|
207
|
2 |
|
settype($array_var['phone'], 'string'); |
|
208
|
2 |
|
$validator->isString($array_var['phone'], 'not a valid phone number', '', 'allow_empty'); |
|
209
|
2 |
|
settype($array_var['ean'], 'string'); |
|
210
|
2 |
|
$validator->isString($array_var['ean'], 'ean location number is not valid', '', 'allow_empty'); |
|
211
|
|
|
|
|
212
|
2 |
|
if ($this->error->isError()) { |
|
213
|
1 |
|
return false; |
|
214
|
|
|
} |
|
215
|
1 |
|
return true; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Public: Denne funktion gemmer data. At gemme data vil sige, at den gamle adresse gemmes, men den nye aktiveres. |
|
220
|
|
|
* |
|
221
|
|
|
* @param array $array_var et array med felter med adressen. Se felterne i init funktionen: $this->fields |
|
222
|
|
|
* |
|
223
|
|
|
* @return bolean true or false |
|
224
|
|
|
*/ |
|
225
|
72 |
|
function save($array_var) |
|
226
|
|
|
{ |
|
227
|
|
|
// @todo validate should probably be called. Selenium debtor:testChangeContactPersonAndSender fails. |
|
228
|
72 |
|
if ($this->belong_to_key == 0 || $this->belong_to_id == 0) { |
|
229
|
|
|
throw new Exception("belong_to or belong_to_id was not set. Maybe because the provided address id was not valid. In Address::save"); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
72 |
|
$sql = ''; |
|
233
|
|
|
|
|
234
|
72 |
|
if (count($array_var) > 0) { |
|
235
|
72 |
|
if ($this->id != 0) { |
|
236
|
2 |
|
$do_update = 0; |
|
237
|
2 |
|
foreach ($this->fields as $i => $field) { |
|
238
|
2 |
View Code Duplication |
if (array_key_exists($field, $array_var) and isset($array_var[$field])) { |
|
|
|
|
|
|
239
|
2 |
|
$sql .= $field.' = "'.safeToDb($array_var[$field]).'", '; |
|
240
|
2 |
|
if ($this->get($field) != $array_var[$field]) { |
|
241
|
1 |
|
$do_update = 1; |
|
242
|
1 |
|
} |
|
243
|
2 |
|
} |
|
244
|
2 |
|
} |
|
245
|
2 |
View Code Duplication |
} else { |
|
246
|
|
|
// Kun hvis der rent faktisk gemmes nogle v�rdier opdaterer vi. hvis count($arra_var) > 0 s� m� der ogs� v�re noget at opdatere? |
|
247
|
72 |
|
$do_update = 0; |
|
248
|
72 |
|
foreach ($this->fields as $i => $field) { |
|
249
|
72 |
|
if (array_key_exists($field, $array_var) and isset($array_var[$field])) { |
|
|
|
|
|
|
250
|
72 |
|
$sql .= $field.' = "'.safeToDb($array_var[$field]).'", '; |
|
251
|
72 |
|
$do_update = 1; |
|
252
|
72 |
|
} |
|
253
|
72 |
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
72 |
|
if ($do_update == 0) { |
|
257
|
|
|
// There is nothing to save, but that is OK, so we just return 1 |
|
258
|
1 |
|
return true; |
|
259
|
|
|
} else { |
|
260
|
72 |
|
$result = $this->db->exec("UPDATE address SET active = 0 WHERE type = ".$this->belong_to_key." AND belong_to_id = ".$this->belong_to_id); |
|
261
|
72 |
|
if (PEAR::isError($result)) { |
|
262
|
|
|
throw new Exception("Error in exec: ".$result->getUserInfo()); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
72 |
|
$result = $this->db->exec("INSERT INTO address SET ".$sql." type = ".$this->belong_to_key.", belong_to_id = ".$this->belong_to_id.", active = 1, changed_date = NOW()"); |
|
266
|
72 |
|
if (PEAR::isError($result)) { |
|
267
|
|
|
throw new Exception("Error in exec: ".$result->getUserInfo()); |
|
268
|
|
|
} |
|
269
|
72 |
|
$this->id = $this->db->lastInsertId('address', 'id'); |
|
270
|
72 |
|
$this->load(); |
|
271
|
72 |
|
return true; |
|
272
|
|
|
} |
|
273
|
|
|
} else { |
|
274
|
|
|
// Der var slet ikke noget indhold i arrayet, s� vi lader v�re at opdatere, men siger, at vi gjorde. |
|
275
|
|
|
return true; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.