2 /***************************************************************************
3 * Copyright (C) 2003-2013 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';
58 const LINK_GROUP
= 'group';
60 /** The following fields, but $error, all correspond to the fields of the
61 * database table profile_phones.
66 public $link_type = 'user';
68 // The following fields are the fields of the form in the profile edition.
69 public $type = 'fixed';
73 public $error = false
;
75 public function __construct(array $data = array())
77 if (count($data) > 0) {
78 foreach ($data as $key => $val) {
84 public function setId($id)
89 /** Returns the unique ID of a phone.
90 * This ID will allow to link it to an address, a user or a job.
91 * The format is address_addressId_phoneId (where phoneId is the id
92 * of the phone in the list of those associated with the address).
94 public function uniqueId() {
95 return $this->link_type
. '_' . $this->link_id
. '_' . $this->id
;
98 public function hasFlags($flags) {
99 return $this->hasType($flags) && $this->hasLink($flags);
102 /** Returns true if this phone's type matches the flags.
104 public function hasType($flags) {
105 $flags = $flags & Profile
::PHONE_TYPE_ANY
;
107 ($flags == Profile
::PHONE_TYPE_ANY
)
109 (($flags & Profile
::PHONE_TYPE_FAX
) && $this->type
== self
::TYPE_FAX
)
111 (($flags & Profile
::PHONE_TYPE_FIXED
) && $this->type
== self
::TYPE_FIXED
)
113 (($flags & Profile
::PHONE_TYPE_MOBILE
) && $this->type
== self
::TYPE_MOBILE
)
117 /** User-friendly accessible version of the type.
119 public function displayType($short = false
)
121 switch ($this->type
) {
122 case Phone
::TYPE_FIXED
:
123 return $short ?
'Tél' : 'Fixe';
124 case Phone
::TYPE_FAX
:
126 case Phone
::TYPE_MOBILE
:
127 return $short ?
'Mob' : 'Mobile';
133 /** Returns true if this phone's link matches the flags.
135 public function hasLink($flags)
137 $flags = $flags & Profile
::PHONE_LINK_ANY
;
139 ($flags == Profile
::PHONE_LINK_ANY
)
141 (($flags & Profile
::PHONE_LINK_COMPANY
) && $this->link_type
== self
::LINK_COMPANY
)
143 (($flags & Profile
::PHONE_LINK_JOB
) && $this->link_type
== self
::LINK_JOB
)
145 (($flags & Profile
::PHONE_LINK_ADDRESS
) && $this->link_type
== self
::LINK_ADDRESS
)
147 (($flags & Profile
::PHONE_LINK_PROFILE
) && $this->link_type
== self
::LINK_PROFILE
)
151 /* Properly formats the search phone, based on actual display phone.
153 * Computes a base form of the phone number with the international prefix.
154 * This number only contains digits, thus does not begin with the '+' sign.
155 * Numbers starting with 0 (or '(0)') are considered as French.
156 * This assumes that non-French numbers have their international prefix.
158 private function formatSearch()
160 $tel = trim($this->display
);
161 // Number starting with "(0)" is a French number.
162 if (substr($tel, 0, 3) === '(0)') {
165 // Removes all "(0)" often used as local prefix.
166 $tel = str_replace('(0)', '', $tel);
167 // Removes all non-digit chars.
168 $tel = preg_replace('/[^0-9]/', '', $tel);
170 if (substr($tel, 0, 2) === '00') {
171 // Removes prefix for international calls.
172 $tel = substr($tel, 2);
173 } else if (substr($tel, 0, 1) === '0') {
174 // Number starting with 0 is a French number.
175 $tel = '33' . substr($tel, 1);
177 $this->search
= $tel;
180 // Properly formats the display phone, it requires the search phone to be already formatted.
181 private function formatDisplay($format = array())
183 $tel = $this->search
;
185 $telLength = strlen($tel);
186 // Try to find the country by trying to find a matching prefix of 1, 2 or 3 digits.
187 if ((!isset($format['phoneprf'])) ||
($format['phoneprf'] == '')) {
188 $res = XDB
::query('SELECT phonePrefix AS phoneprf, phoneFormat AS format
189 FROM geoloc_countries
190 WHERE phonePrefix = SUBSTRING({?}, 1, LENGTH(phonePrefix))
191 ORDER BY LENGTH(phonePrefix) DESC
194 if ($res->numRows() == 0) {
195 // No country found, does not format more than prepending a '+' sign.
197 $this->display
= '+' . $tel;
200 $format = $res->fetchOneAssoc();
202 if ($format['format'] == '') {
203 // If the country does not have a phone number format, the number will be displayed
204 // as "+prefix ## ## ## ##...".
205 $format['format'] = '(+p)';
208 /* Formats the phone number according t the template with these rules:
209 * - p is replaced by the international prefix,
210 * - # is replaced by one digit,
211 * - other chars are left intact.
212 * If the number is longer than the format, remaining digits are
213 * appended by blocks of two digits separated by spaces.
214 * The last block can have 3 digits to avoid a final single-digit block.
217 $i = strlen($format['phoneprf']);
218 $lengthFormat = strlen($format['format']);
219 while (($i < $telLength) && ($j < $lengthFormat)) {
220 if ($format['format'][$j] == '#') {
223 } else if ($format['format'][$j] == 'p') {
224 $ret .= $format['phoneprf'];
226 $ret .= $format['format'][$j];
230 for (; $i < $telLength - 1; $i +
= 2) {
231 $ret .= ' ' . substr($tel, $i, 2);
233 // Appends last left alone numbers to the last block.
234 if ($i < $telLength) {
235 $ret .= substr($tel, $i);
237 $this->display
= $ret;
241 public function format($format = array())
243 if (!($this->type
== Phone
::TYPE_FIXED
244 ||
$this->type
== Phone
::TYPE_MOBILE
245 ||
$this->type
== Phone
::TYPE_FAX
)) {
246 $this->type
= Phone
::TYPE_FIXED
;
248 $this->formatSearch();
249 $this->formatDisplay($format);
250 return !$this->error
;
253 public function toFormArray()
256 'type' => $this->type
,
257 'display' => $this->display
,
259 'comment' => $this->comment
,
260 'error' => $this->error
264 private function toString()
266 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
267 static $types = array('fax' => 'fax', 'fixed' => 'fixe', 'mobile' => 'mobile');
268 return $this->display
. ' (' . $types[$this->type
] . (($this->comment
) ?
', commentaire : « ' . $this->comment
. ' »' : '')
269 . ', affichage ' . $pubs[$this->pub
] . ')';
272 private function isEmpty()
274 return (!$this->search ||
$this->search
== '');
277 public function save()
280 if (!$this->isEmpty()) {
281 XDB
::execute('INSERT IGNORE INTO profile_phones (pid, link_type, link_id, tel_id, tel_type,
282 search_tel, display_tel, pub, comment)
283 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
284 $this->pid
, $this->link_type
, $this->link_id
, $this->id
, $this->type
,
285 $this->search
, $this->display
, $this->pub
, $this->comment
);
289 public function delete()
291 XDB
::execute('DELETE FROM profile_phones
292 WHERE pid = {?} AND link_type = {?} AND link_id = {?} AND tel_id = {?}',
293 $this->pid
, $this->link_type
, $this->link_id
, $this->id
);
296 static public function deletePhones($pid, $link_type, $link_id = null
, $deletePrivate = true
)
299 if (!is_null($link_id)) {
300 $where = XDB
::format(' AND link_id = {?}', $link_id);
302 XDB
::execute('DELETE FROM profile_phones
303 WHERE pid = {?} AND link_type = {?}' . $where . (($deletePrivate) ?
'' : ' AND pub IN (\'public\', \'ax\')'),
307 /** Saves phones into the database.
308 * @param $data: an array of form formatted phones.
309 * @param $pid, $link_type, $link_id: pid, link_type and link_id concerned by the update.
311 static public function savePhones(array $data, $pid, $link_type, $link_id = null
)
313 foreach ($data as $id => $value) {
315 if (!is_null($pid)) {
316 $value['pid'] = $pid ;
318 if (!is_null($link_type)) {
319 $value['link_type'] = $link_type ;
321 if (!is_null($link_id)) {
322 $value['link_id'] = $link_id ;
324 $phone = new Phone($value);
329 static private function formArrayWalk(array $data, $function, &$success = true
, $requiresEmptyPhone = false
, $maxPublicity = null
)
332 if (!is_null($data)) {
333 foreach ($data as $item) {
334 $phone = new Phone($item);
335 $success = (!$phone->error
&& ($phone->format() ||
$phone->isEmpty()) && $success);
336 if (!$phone->isEmpty()) {
337 // Restrict phone visibility to $maxPublicity
338 if (!is_null($maxPublicity) && Visibility
::isLessRestrictive($maxPublicity, $phone->pub
)) {
339 $phone->pub
= $maxPublicity;
341 $phones[] = call_user_func(array($phone, $function));
345 if (count($phones) == 0 && $requiresEmptyPhone) {
346 $phone = new Phone();
347 if (!is_null($maxPublicity) && Visibility
::isLessRestrictive($maxPublicity, $phone->pub
)) {
348 // Restrict phone visibility to $maxPublicity
349 $phone->pub
= $maxPublicity;
351 $phones[] = call_user_func(array($phone, $function));
356 // Formats an array of form phones into an array of form formatted phones.
357 static public function formatFormArray(array $data, &$success = true
, $maxPublicity = null
)
359 $phones = self
::formArrayWalk($data, 'toFormArray', $success, true
, $maxPublicity);
360 usort($phones, 'Visibility::comparePublicity');
364 static public function formArrayToString(array $data)
366 return implode(', ', self
::formArrayWalk($data, 'toString'));
369 static public function hasPrivate(array $phones)
371 foreach ($phones as $phone) {
372 if ($phone['pub'] == 'private') {
379 static public function iterate(array $pids = array(), array $link_types = array(),
380 array $link_ids = array(), $visibility = null
)
382 return new PhoneIterator($pids, $link_types, $link_ids, $visibility);
386 /** Iterator over a set of Phones
388 * @param $pid, $link_type, $link_id, $pub
390 * The iterator contains the phones that correspond to the value stored in the
391 * parameters' arrays.
393 class PhoneIterator
implements PlIterator
397 public function __construct(array $pids, array $link_types, array $link_ids, $visibility)
400 if (count($pids) != 0) {
401 $where[] = XDB
::format('(pid IN {?})', $pids);
403 if (count($link_types) != 0) {
404 $where[] = XDB
::format('(link_type IN {?})', $link_types);
406 if (count($link_ids) != 0) {
407 $where[] = XDB
::format('(link_id IN {?})', $link_ids);
409 if ($visibility == null ||
!($visibility instanceof Visibility
)) {
410 $visibility = Visibility
::defaultForRead();
412 $where[] = 'pve.best_display_level+0 <= pub+0';
414 $sql = 'SELECT search_tel AS search, display_tel AS display, comment, link_id,
415 tel_type AS type, link_type, tel_id AS id, pid, pub
417 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
418 WHERE ' . implode(' AND ', $where) . '
419 ORDER BY pid, link_id, tel_id';
420 $this->dbiter
= XDB
::iterator($sql, $visibility->level());
423 public function next()
425 if (is_null($this->dbiter
)) {
428 $data = $this->dbiter
->next();
429 if (is_null($data)) {
432 return new Phone($data);
435 public function total()
437 return $this->dbiter
->total();
440 public function first()
442 return $this->dbiter
->first();
445 public function last()
447 return $this->dbiter
->last();
450 public function value()
452 return $this->dbiter
;
456 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: