2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 /** Class Phone is meant to perform most of the access to the table profile_phones.
24 * profile_phone describes a Phone, which can be related to an Address,
25 * a Job, a Profile or a Company:
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)
32 * - `link_type` is set to 'hq'
33 * - `link_id` is set to the id of the related Company (in profile_job_enum)
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)
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)
46 * Thus a Phone can be linked to a Company, a Profile, a Job, or a Profile-related Address.
50 const TYPE_FAX
= 'fax';
51 const TYPE_FIXED
= 'fixed';
52 const TYPE_MOBILE
= 'mobile';
54 const LINK_JOB
= 'pro';
55 const LINK_ADDRESS
= 'address';
56 const LINK_PROFILE
= 'user';
57 const LINK_COMPANY
= 'hq';
59 /** The following fields, but $error, all correspond to the fields of the
60 * database table profile_phones.
65 public $link_type = 'user';
67 // The following fields are the fields of the form in the profile edition.
68 public $type = 'fixed';
72 public $error = false
;
74 public function __construct(array $data = array())
76 if (count($data) > 0) {
77 foreach ($data as $key => $val) {
83 public function setId($id)
88 /** Returns the unique ID of a phone.
89 * This ID will allow to link it to an address, a user or a job.
90 * The format is address_addressId_phoneId (where phoneId is the id
91 * of the phone in the list of those associated with the address).
93 public function uniqueId() {
94 return $this->link_type
. '_' . $this->link_id
. '_' . $this->id
;
97 public function hasFlags($flags) {
98 return $this->hasType($flags) && $this->hasLink($flags);
101 /** Returns true if this phone's type matches the flags.
103 public function hasType($flags) {
104 $flags = $flags & Profile
::PHONE_TYPE_ANY
;
106 ($flags == Profile
::PHONE_TYPE_ANY
)
108 (($flags & Profile
::PHONE_TYPE_FAX
) && $this->type
== self
::TYPE_FAX
)
110 (($flags & Profile
::PHONE_TYPE_FIXED
) && $this->type
== self
::TYPE_FIXED
)
112 (($flags & Profile
::PHONE_TYPE_MOBILE
) && $this->type
== self
::TYPE_MOBILE
)
116 /** User-friendly accessible version of the type.
118 public function displayType($short = false
)
120 switch ($this->type
) {
121 case Phone
::TYPE_FIXED
:
122 return $short ?
'Tél' : 'Fixe';
123 case Phone
::TYPE_FAX
:
125 case Phone
::TYPE_MOBILE
:
126 return $short ?
'Mob' : 'Mobile';
132 /** Returns true if this phone's link matches the flags.
134 public function hasLink($flags)
136 $flags = $flags & Profile
::PHONE_LINK_ANY
;
138 ($flags == Profile
::PHONE_LINK_ANY
)
140 (($flags & Profile
::PHONE_LINK_COMPANY
) && $this->link_type
== self
::LINK_COMPANY
)
142 (($flags & Profile
::PHONE_LINK_JOB
) && $this->link_type
== self
::LINK_JOB
)
144 (($flags & Profile
::PHONE_LINK_ADDRESS
) && $this->link_type
== self
::LINK_ADDRESS
)
146 (($flags & Profile
::PHONE_LINK_PROFILE
) && $this->link_type
== self
::LINK_PROFILE
)
150 /* Properly formats the search phone, based on actual display phone.
152 * Computes a base form of the phone number with the international prefix.
153 * This number only contains digits, thus does not begin with the '+' sign.
154 * Numbers starting with 0 (or '(0)') are considered as French.
155 * This assumes that non-French numbers have their international prefix.
157 private function formatSearch()
159 $tel = trim($this->display
);
160 // Number starting with "(0)" is a French number.
161 if (substr($tel, 0, 3) === '(0)') {
164 // Removes all "(0)" often used as local prefix.
165 $tel = str_replace('(0)', '', $tel);
166 // Removes all non-digit chars.
167 $tel = preg_replace('/[^0-9]/', '', $tel);
169 if (substr($tel, 0, 2) === '00') {
170 // Removes prefix for international calls.
171 $tel = substr($tel, 2);
172 } else if (substr($tel, 0, 1) === '0') {
173 // Number starting with 0 is a French number.
174 $tel = '33' . substr($tel, 1);
176 $this->search
= $tel;
179 // Properly formats the display phone, it requires the search phone to be already formatted.
180 private function formatDisplay($format = array())
182 $tel = $this->search
;
184 $telLength = strlen($tel);
185 // Try to find the country by trying to find a matching prefix of 1, 2 or 3 digits.
186 if ((!isset($format['phoneprf'])) ||
($format['phoneprf'] == '')) {
187 $res = XDB
::query('SELECT phonePrefix AS phoneprf, phoneFormat AS format
188 FROM geoloc_countries
189 WHERE phonePrefix = {?} OR phonePrefix = {?} OR phonePrefix = {?}
191 substr($tel, 0, 1), substr($tel, 0, 2), substr($tel, 0, 3));
192 if ($res->numRows() == 0) {
193 // No country found, does not format more than prepending a '+' sign.
195 $this->display
= '+' . $tel;
198 $format = $res->fetchOneAssoc();
200 if ($format['format'] == '') {
201 // If the country does not have a phone number format, the number will be displayed
202 // as "+prefix ## ## ## ##...".
203 $format['format'] = '(+p)';
206 /* Formats the phone number according t the template with these rules:
207 * - p is replaced by the international prefix,
208 * - # is replaced by one digit,
209 * - other chars are left intact.
210 * If the number is longer than the format, remaining digits are
211 * appended by blocks of two digits separated by spaces.
212 * The last block can have 3 digits to avoid a final single-digit block.
215 $i = strlen($format['phoneprf']);
216 $lengthFormat = strlen($format['format']);
217 while (($i < $telLength) && ($j < $lengthFormat)) {
218 if ($format['format'][$j] == '#') {
221 } else if ($format['format'][$j] == 'p') {
222 $ret .= $format['phoneprf'];
224 $ret .= $format['format'][$j];
228 for (; $i < $telLength - 1; $i +
= 2) {
229 $ret .= ' ' . substr($tel, $i, 2);
231 // Appends last left alone numbers to the last block.
232 if ($i < $telLength) {
233 $ret .= substr($tel, $i);
235 $this->display
= $ret;
239 public function format($format = array())
241 if (!($this->type
== Phone
::TYPE_FIXED
242 ||
$this->type
== Phone
::TYPE_MOBILE
243 ||
$this->type
== Phone
::TYPE_FAX
)) {
244 $this->type
= Phone
::TYPE_FIXED
;
246 $this->formatSearch();
247 $this->formatDisplay($format);
248 return !$this->error
;
251 public function toFormArray()
254 'type' => $this->type
,
255 'display' => $this->display
,
257 'comment' => $this->comment
,
258 'error' => $this->error
262 private function toString()
264 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
265 static $types = array('fax' => 'fax', 'fixed' => 'fixe', 'mobile' => 'mobile');
266 return $this->display
. ' (' . $types[$this->type
] . (($this->comment
) ?
', commentaire : « ' . $this->comment
. ' »' : '')
267 . ', affichage ' . $pubs[$this->pub
] . ')';
270 private function isEmpty()
272 return (!$this->search ||
$this->search
== '');
275 public function save()
278 if (!$this->isEmpty()) {
279 XDB
::execute('INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type,
280 search_tel, display_tel, pub, comment)
281 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
282 $this->pid
, $this->link_type
, $this->link_id
, $this->id
, $this->type
,
283 $this->search
, $this->display
, $this->pub
, $this->comment
);
287 public function delete()
289 XDB
::execute('DELETE FROM profile_phones
290 WHERE pid = {?} AND link_type = {?} AND link_id = {?} AND tel_id = {?}',
291 $this->pid
, $this->link_type
, $this->link_id
, $this->id
);
294 static public function deletePhones($pid, $link_type, $link_id = null
, $deletePrivate = true
)
297 if (!is_null($link_id)) {
298 $where = XDB
::format(' AND link_id = {?}', $link_id);
300 XDB
::execute('DELETE FROM profile_phones
301 WHERE pid = {?} AND link_type = {?}' . $where . (($deletePrivate) ?
'' : ' AND pub IN (\'public\', \'ax\')'),
305 /** Saves phones into the database.
306 * @param $data: an array of form formatted phones.
307 * @param $pid, $link_type, $link_id: pid, link_type and link_id concerned by the update.
309 static public function savePhones(array $data, $pid, $link_type, $link_id = null
)
311 foreach ($data as $id => $value) {
313 if (!is_null($pid)) {
314 $value['pid'] = $pid ;
316 if (!is_null($link_type)) {
317 $value['link_type'] = $link_type ;
319 if (!is_null($link_id)) {
320 $value['link_id'] = $link_id ;
322 $phone = new Phone($value);
327 static private function formArrayWalk(array $data, $function, &$success = true
, $requiresEmptyPhone = false
, $maxPublicity = null
)
330 foreach ($data as $item) {
331 $phone = new Phone($item);
332 $success = (!$phone->error
&& ($phone->format() ||
$phone->isEmpty()) && $success);
333 if (!$phone->isEmpty()) {
334 if (!is_null($maxPublicity) && $maxPublicity->isVisible($phone->pub
)) {
335 $phone->pub
= $maxPublicity->level();
337 $phones[] = call_user_func(array($phone, $function));
340 if (count($phones) == 0 && $requiresEmptyPhone) {
341 $phone = new Phone();
342 if (!is_null($maxPublicity) && $maxPublicity->isVisible($phone->pub
)) {
343 $phone->pub
= $maxPublicity->level();
345 $phones[] = call_user_func(array($phone, $function));
350 // Formats an array of form phones into an array of form formatted phones.
351 static public function formatFormArray(array $data, &$success = true
, $maxPublicity = null
)
353 return self
::formArrayWalk($data, 'toFormArray', $success, true
, $maxPublicity);
356 static public function formArrayToString(array $data)
358 return implode(', ', self
::formArrayWalk($data, 'toString'));
361 static public function iterate(array $pids = array(), array $link_types = array(),
362 array $link_ids = array(), array $pubs = array())
364 return new PhoneIterator($pids, $link_types, $link_ids, $pubs);
368 /** Iterator over a set of Phones
370 * @param $pid, $link_type, $link_id, $pub
372 * The iterator contains the phones that correspond to the value stored in the
373 * parameters' arrays.
375 class PhoneIterator
implements PlIterator
379 public function __construct(array $pids, array $link_types, array $link_ids, array $pubs)
382 if (count($pids) != 0) {
383 $where[] = XDB
::format('(pid IN {?})', $pids);
385 if (count($link_types) != 0) {
386 $where[] = XDB
::format('(link_type IN {?})', $link_types);
388 if (count($link_ids) != 0) {
389 $where[] = XDB
::format('(link_id IN {?})', $link_ids);
391 if (count($pubs) != 0) {
392 $where[] = XDB
::format('(pub IN {?})', $pubs);
394 $sql = 'SELECT search_tel AS search, display_tel AS display, comment, link_id,
395 tel_type AS type, link_type, tel_id AS id, pid, pub
397 ' . ((count($where) > 0) ?
'WHERE ' . implode(' AND ', $where) : '') . '
398 ORDER BY pid, link_id, tel_id';
399 $this->dbiter
= XDB
::iterator($sql);
402 public function next()
404 if (is_null($this->dbiter
)) {
407 $data = $this->dbiter
->next();
408 if (is_null($data)) {
411 return new Phone($data);
414 public function total()
416 return $this->dbiter
->total();
419 public function first()
421 return $this->dbiter
->first();
424 public function last()
426 return $this->dbiter
->last();
429 public function value()
431 return $this->dbiter
;
435 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: