Commit | Line | Data |
---|---|---|
0b6c8b36 SJ |
1 | <?php |
2 | /*************************************************************************** | |
3 | * Copyright (C) 2003-2010 Polytechnique.org * | |
4 | * http://opensource.polytechnique.org/ * | |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | /** Class Phone is meant to perform most of the access to the table profile_phones. | |
23 | * | |
24 | * profile_phone describes a Phone, which can be related to an Address, | |
25 | * a Job, a Profile or a Company: | |
26 | * - for a Profile: | |
27 | * - `link_type` is set to 'user' | |
28 | * - `link_id` is set to 0 | |
29 | * - `pid` is set to the id of the related Profile (in profiles) | |
30 | * | |
31 | * - for a Company: | |
32 | * - `link_type` is set to 'hq' | |
33 | * - `link_id` is set to the id of the related Company (in profile_job_enum) | |
34 | * - `pid` is set to 0 | |
35 | * | |
36 | * - for an Address (this only applies to a personal address) | |
37 | * - `link_type` is set to 'address' | |
38 | * - `link_id` is set to the related Address `id` (in profile_addresses) | |
39 | * - `pid` is set to the related Address `pid` (in both profiles and profile_addresses) | |
40 | * | |
41 | * - for a Job: | |
42 | * - `link_type` is set to 'pro' | |
43 | * - `link_id` is set to the related Job `id` (not `jobid`) (in profile_job) | |
44 | * - `pid` is set to the related Job `pid` (in both profiles and profile_job) | |
45 | * | |
46 | * Thus a Phone can be linked to a Company, a Profile, a Job, or a Profile-related Address. | |
47 | */ | |
48 | class Phone | |
49 | { | |
50 | const TYPE_FAX = 'fax'; | |
51 | const TYPE_FIXED = 'fixed'; | |
52 | const TYPE_MOBILE = 'mobile'; | |
53 | ||
54 | const LINK_JOB = 'pro'; | |
55 | const LINK_ADDRESS = 'address'; | |
56 | const LINK_PROFILE = 'user'; | |
57 | const LINK_COMPANY = 'hq'; | |
58 | ||
59 | /** The following fields, but $error, all correspond to the fields of the | |
60 | * database table profile_phones. | |
61 | */ | |
62 | private $id = 0; | |
63 | private $pid = 0; | |
64 | private $search = ''; | |
65 | private $link_type = 'user'; | |
66 | private $link_id = 0; | |
67 | // The following fields are the fields of the form in the profile edition. | |
68 | private $type = 'fixed'; | |
69 | public $display = ''; | |
70 | private $pub = 'private'; | |
71 | public $comment = ''; | |
72 | private $error = false; | |
73 | ||
74 | public function __construct(array $data = array()) | |
75 | { | |
76 | if (count($data) > 0) { | |
77 | foreach ($data as $key => $val) { | |
78 | $this->$key = $val; | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
83 | public function linkType() | |
84 | { | |
85 | return $this->link_type; | |
86 | } | |
87 | ||
88 | public function linkId() | |
89 | { | |
90 | return $this->link_id; | |
91 | } | |
92 | ||
93 | public function id() | |
94 | { | |
95 | return $this->id; | |
96 | } | |
97 | ||
98 | public function pid() | |
99 | { | |
100 | return $this->pid; | |
101 | } | |
102 | ||
103 | public function search() | |
104 | { | |
105 | return $this->search; | |
106 | } | |
107 | ||
108 | /** Returns the unique ID of a phone. | |
109 | * This ID will allow to link it to an address, a user or a job. | |
110 | * The format is address_addressId_phoneId (where phoneId is the id | |
111 | * of the phone in the list of those associated with the address). | |
112 | */ | |
113 | public function uniqueId() { | |
114 | return $this->link_type . '_' . $this->link_id . '_' . $this->id; | |
115 | } | |
116 | ||
117 | public function hasFlags($flags) { | |
118 | return $this->hasType($flags) && $this->hasLink($flags); | |
119 | } | |
120 | ||
121 | /** Returns true if this phone's type matches the flags. | |
122 | */ | |
123 | public function hasType($flags) { | |
124 | $flags = $flags & Profile::PHONE_TYPE_ANY; | |
125 | return ( | |
126 | ($flags == Profile::PHONE_TYPE_ANY) | |
127 | || | |
128 | (($flags & Profile::PHONE_TYPE_FAX) && $this->type == self::TYPE_FAX) | |
129 | || | |
130 | (($flags & Profile::PHONE_TYPE_FIXED) && $this->type == self::TYPE_FIXED) | |
131 | || | |
132 | (($flags & Profile::PHONE_TYPE_MOBILE) && $this->type == self::TYPE_MOBILE) | |
133 | ); | |
134 | } | |
135 | ||
136 | /** User-friendly accessible version of the type. | |
137 | */ | |
138 | public function displayType($short = false) | |
139 | { | |
140 | switch ($this->type) { | |
141 | case Phone::TYPE_FIXED: | |
142 | return $short ? 'Tél' : 'Fixe'; | |
143 | case Phone::TYPE_FAX: | |
144 | return 'Fax'; | |
145 | case Phone::TYPE_MOBILE: | |
146 | return $short ? 'Mob' : 'Mobile'; | |
147 | default: | |
148 | return $this->type; | |
149 | } | |
150 | } | |
151 | ||
152 | /** Returns true if this phone's link matches the flags. | |
153 | */ | |
154 | public function hasLink($flags) | |
155 | { | |
156 | $flags = $flags & Profile::PHONE_LINK_ANY; | |
157 | return ( | |
158 | ($flags == Profile::PHONE_LINK_ANY) | |
159 | || | |
160 | (($flags & Profile::PHONE_LINK_COMPANY) && $this->link_type == self::LINK_COMPANY) | |
161 | || | |
162 | (($flags & Profile::PHONE_LINK_JOB) && $this->link_type == self::LINK_JOB) | |
163 | || | |
164 | (($flags & Profile::PHONE_LINK_ADDRESS) && $this->link_type == self::LINK_ADDRESS) | |
165 | || | |
166 | (($flags & Profile::PHONE_LINK_PROFILE) && $this->link_type == self::LINK_PROFILE) | |
167 | ); | |
168 | } | |
169 | ||
170 | /* Properly formats the search phone, based on actual display phone. | |
171 | * | |
172 | * Computes a base form of the phone number with the international prefix. | |
173 | * This number only contains digits, thus does not begin with the '+' sign. | |
174 | * Numbers starting with 0 (or '(0)') are considered as French. | |
175 | * This assumes that non-French numbers have their international prefix. | |
176 | */ | |
177 | private function formatSearch() | |
178 | { | |
179 | $tel = trim($this->display); | |
180 | // Number starting with "(0)" is a French number. | |
181 | if (substr($tel, 0, 3) === '(0)') { | |
182 | $tel = '33' . $tel; | |
183 | } | |
184 | // Removes all "(0)" often used as local prefix. | |
185 | $tel = str_replace('(0)', '', $tel); | |
186 | // Removes all non-digit chars. | |
187 | $tel = preg_replace('/[^0-9]/', '', $tel); | |
188 | ||
189 | if (substr($tel, 0, 2) === '00') { | |
190 | // Removes prefix for international calls. | |
191 | $tel = substr($tel, 2); | |
192 | } else if (substr($tel, 0, 1) === '0') { | |
193 | // Number starting with 0 is a French number. | |
194 | $tel = '33' . substr($tel, 1); | |
195 | } | |
196 | $this->search = $tel; | |
197 | } | |
198 | ||
199 | // Properly formats the display phone, it requires the search phone to be already formatted. | |
200 | private function formatDisplay($format = array()) | |
201 | { | |
202 | $tel = $this->search; | |
203 | $ret = ''; | |
204 | $telLength = strlen($tel); | |
205 | // Try to find the country by trying to find a matching prefix of 1, 2 or 3 digits. | |
206 | if ((!isset($format['phoneprf'])) || ($format['phoneprf'] == '')) { | |
207 | $res = XDB::query('SELECT phonePrefix AS phoneprf, phoneFormat AS format | |
208 | FROM geoloc_countries | |
209 | WHERE phonePrefix = {?} OR phonePrefix = {?} OR phonePrefix = {?} | |
210 | LIMIT 1', | |
211 | substr($tel, 0, 1), substr($tel, 0, 2), substr($tel, 0, 3)); | |
212 | if ($res->numRows() == 0) { | |
213 | // No country found, does not format more than prepending a '+' sign. | |
214 | $this->error = true; | |
215 | $this->display = '+' . $tel; | |
216 | return; | |
217 | } | |
218 | $format = $res->fetchOneAssoc(); | |
219 | } | |
220 | if ($format['format'] == '') { | |
221 | // If the country does not have a phone number format, the number will be displayed | |
222 | // as "+prefix ## ## ## ##...". | |
555a4512 | 223 | $format['format'] = '(+p)'; |
0b6c8b36 SJ |
224 | } |
225 | ||
226 | /* Formats the phone number according t the template with these rules: | |
227 | * - p is replaced by the international prefix, | |
228 | * - # is replaced by one digit, | |
229 | * - other chars are left intact. | |
230 | * If the number is longer than the format, remaining digits are | |
231 | * appended by blocks of two digits separated by spaces. | |
232 | * The last block can have 3 digits to avoid a final single-digit block. | |
233 | */ | |
234 | $j = 0; | |
235 | $i = strlen($format['phoneprf']); | |
236 | $lengthFormat = strlen($format['format']); | |
237 | while (($i < $telLength) && ($j < $lengthFormat)) { | |
238 | if ($format['format'][$j] == '#') { | |
239 | $ret .= $tel[$i]; | |
240 | ++$i; | |
241 | } else if ($format['format'][$j] == 'p') { | |
242 | $ret .= $format['phoneprf']; | |
243 | } else { | |
244 | $ret .= $format['format'][$j]; | |
245 | } | |
246 | ++$j; | |
247 | } | |
248 | for (; $i < $telLength - 1; $i += 2) { | |
249 | $ret .= ' ' . substr($tel, $i, 2); | |
250 | } | |
251 | // Appends last left alone numbers to the last block. | |
252 | if ($i < $telLength) { | |
253 | $ret .= substr($tel, $i); | |
254 | } | |
255 | $this->display = $ret; | |
256 | } | |
257 | ||
258 | ||
259 | public function format($format = array()) | |
260 | { | |
261 | if (!($this->type == Phone::TYPE_FIXED | |
262 | || $this->type == Phone::TYPE_MOBILE | |
263 | || $this->type == Phone::TYPE_FAX)) { | |
264 | $this->type = Phone::TYPE_FIXED; | |
265 | } | |
266 | $this->formatSearch(); | |
267 | $this->formatDisplay($format); | |
268 | return !$this->error; | |
269 | } | |
270 | ||
271 | public function toFormArray() | |
272 | { | |
273 | return array( | |
274 | 'type' => $this->type, | |
275 | 'display' => $this->display, | |
276 | 'pub' => $this->pub, | |
277 | 'comment' => $this->comment, | |
278 | 'error' => $this->error | |
279 | ); | |
280 | } | |
281 | ||
282 | private function toString() | |
283 | { | |
284 | return 'type : ' . $this->type .', numéro : ' . $this->display | |
285 | . ', commentaire : « ' . $this->comment . ' », affichage : ' . $this->pub; | |
286 | } | |
287 | ||
288 | private function isEmpty() | |
289 | { | |
290 | return (!$this->search || $this->search == ''); | |
291 | } | |
292 | ||
293 | public function save() | |
294 | { | |
295 | $this->format(); | |
296 | if (!$this->isEmpty()) { | |
297 | XDB::execute('INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type, | |
298 | search_tel, display_tel, pub, comment) | |
299 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})', | |
300 | $this->pid, $this->link_type, $this->link_id, $this->id, $this->type, | |
301 | $this->search, $this->display, $this->pub, $this->comment); | |
302 | } | |
303 | } | |
304 | ||
305 | static public function deletePhones($pid, $link_type, $link_id = null) | |
306 | { | |
307 | $where = ''; | |
308 | if (!is_null($link_id)) { | |
309 | $where = XDB::format(' AND link_id = {?}', $link_id); | |
310 | } | |
311 | XDB::execute('DELETE FROM profile_phones | |
312 | WHERE pid = {?} AND link_type = {?}' . $where, | |
313 | $pid, $link_type); | |
314 | } | |
315 | ||
316 | /** Saves phones into the database. | |
317 | * @param $data: an array of form formatted phones. | |
318 | * @param $pid, $link_type, $link_id: pid, link_type and link_id concerned by the update. | |
319 | */ | |
320 | static public function savePhones(array $data, $pid, $link_type, $link_id = null) | |
321 | { | |
322 | foreach ($data as $id => $value) { | |
323 | $value['id'] = $id; | |
324 | if (!is_null($pid)) { | |
325 | $value['pid'] = $pid ; | |
326 | } | |
327 | if (!is_null($link_type)) { | |
328 | $value['link_type'] = $link_type ; | |
329 | } | |
330 | if (!is_null($link_id)) { | |
331 | $value['link_id'] = $link_id ; | |
332 | } | |
333 | $phone = new Phone($value); | |
334 | $phone->save(); | |
335 | } | |
336 | } | |
337 | ||
338 | static private function formArrayWalk(array $data, $function, &$success = true, $requiresEmptyPhone = false) | |
339 | { | |
340 | $phones = array(); | |
341 | foreach ($data as $item) { | |
342 | $phone = new Phone($item); | |
42ec0fe2 | 343 | $success = (!$phone->error && ($phone->format() || $phone->isEmpty()) && $success); |
0b6c8b36 SJ |
344 | if (!$phone->isEmpty()) { |
345 | $phones[] = call_user_func(array($phone, $function)); | |
346 | } | |
347 | } | |
348 | if (count($phones) == 0 && $requiresEmptyPhone) { | |
349 | $phone = new Phone(); | |
350 | $phones[] = call_user_func(array($phone, $function)); | |
351 | } | |
352 | return $phones; | |
353 | } | |
354 | ||
355 | // Formats an array of form phones into an array of form formatted phones. | |
356 | static public function formatFormArray(array $data, &$success = true) | |
357 | { | |
358 | return self::formArrayWalk($data, 'toFormArray', $success, true); | |
359 | } | |
360 | ||
361 | static public function formArrayToString(array $data) | |
362 | { | |
363 | return implode(' ; ', self::formArrayWalk($data, 'toString')); | |
364 | } | |
365 | ||
366 | static public function iterate(array $pids = array(), array $link_types = array(), | |
367 | array $link_ids = array(), array $pubs = array()) | |
368 | { | |
369 | return new PhoneIterator($pids, $link_types, $link_ids, $pubs); | |
370 | } | |
371 | } | |
372 | ||
373 | /** Iterator over a set of Phones | |
374 | * | |
375 | * @param $pid, $link_type, $link_id, $pub | |
376 | * | |
377 | * The iterator contains the phones that correspond to the value stored in the | |
378 | * parameters' arrays. | |
379 | */ | |
380 | class PhoneIterator implements PlIterator | |
381 | { | |
382 | private $dbiter; | |
383 | ||
384 | public function __construct(array $pids, array $link_types, array $link_ids, array $pubs) | |
385 | { | |
386 | $where = array(); | |
387 | if (count($pids) != 0) { | |
388 | $where[] = XDB::format('(pid IN {?})', $pids); | |
389 | } | |
390 | if (count($link_types) != 0) { | |
391 | $where[] = XDB::format('(link_type IN {?})', $link_types); | |
392 | } | |
393 | if (count($link_ids) != 0) { | |
394 | $where[] = XDB::format('(link_id IN {?})', $link_ids); | |
395 | } | |
396 | if (count($pubs) != 0) { | |
397 | $where[] = XDB::format('(pub IN {?})', $pubs); | |
398 | } | |
399 | $sql = 'SELECT search_tel AS search, display_tel AS display, comment, link_id, | |
400 | tel_type AS type, link_type, tel_id AS id, pid, pub | |
401 | FROM profile_phones | |
402 | WHERE ' . implode(' AND ', $where) . ' | |
403 | ORDER BY link_id, tel_id'; | |
404 | $this->dbiter = XDB::iterator($sql); | |
405 | } | |
406 | ||
407 | public function next() | |
408 | { | |
409 | if (is_null($this->dbiter)) { | |
410 | return null; | |
411 | } | |
412 | $data = $this->dbiter->next(); | |
413 | if (is_null($data)) { | |
414 | return null; | |
415 | } | |
416 | return new Phone($data); | |
417 | } | |
418 | ||
419 | public function total() | |
420 | { | |
421 | return $this->dbiter->total(); | |
422 | } | |
423 | ||
424 | public function first() | |
425 | { | |
426 | return $this->dbiter->first(); | |
427 | } | |
428 | ||
429 | public function last() | |
430 | { | |
431 | return $this->dbiter->last(); | |
432 | } | |
433 | ||
434 | public function value() | |
435 | { | |
436 | return $this->dbiter; | |
437 | } | |
438 | } | |
439 | ||
440 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
441 | ?> |