From: Stéphane Jacob Date: Mon, 9 Aug 2010 16:23:18 +0000 (+0200) Subject: Phone class: new class to access profile_phones. X-Git-Tag: xorg/1.0.1~264 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=0b6c8b36e62b950d1b24088e98c4026a54efe9d0;p=platal.git Phone class: new class to access profile_phones. Signed-off-by: Stéphane Jacob --- diff --git a/bin/cron/phones.check.php b/bin/cron/phones.check.php index ffea56d..c4fbd77 100755 --- a/bin/cron/phones.check.php +++ b/bin/cron/phones.check.php @@ -2,10 +2,9 @@ debug = 0; // do not store backtraces - +// TODO: Improve this using Phone::iterate. function do_update_by_block($values) { @@ -76,14 +75,15 @@ foreach ($prefixes as $i => $prefix) { $values = ''; $i = 0; while ($phone = $res->next()) { - $disp = format_display_number($phone['search_tel'], $error, array('format' => $format, 'phoneprf' => $prefix)); + $phone = new Phone('display' => $phone['display_tel']); + $phone->format(array('format' => $format, 'phoneprf' => $prefix)); if ($values != '') { $values .= ",\n"; } $values .= "('" . addslashes($phone['pid']) . "', '" . addslashes($phone['link_type']) . "', '" . addslashes($phone['link_id']) . "', '" . addslashes($phone['tel_id']) . "', '" . addslashes($phone['tel_type']) - . "', '" . addslashes($phone['search_tel']) . "', '" . addslashes($disp) + . "', '" . addslashes($phone['search_tel']) . "', '" . addslashes($phone->display) . "', '" . addslashes($phone['pub']) . "', '" . addslashes($phone['comment']) . "')"; $i++; if ($i == 1000) { diff --git a/classes/phone.php b/classes/phone.php new file mode 100644 index 0000000..29f06df --- /dev/null +++ b/classes/phone.php @@ -0,0 +1,441 @@ + 0) { + foreach ($data as $key => $val) { + $this->$key = $val; + } + } + } + + public function linkType() + { + return $this->link_type; + } + + public function linkId() + { + return $this->link_id; + } + + public function id() + { + return $this->id; + } + + public function pid() + { + return $this->pid; + } + + public function search() + { + return $this->search; + } + + /** Returns the unique ID of a phone. + * This ID will allow to link it to an address, a user or a job. + * The format is address_addressId_phoneId (where phoneId is the id + * of the phone in the list of those associated with the address). + */ + public function uniqueId() { + return $this->link_type . '_' . $this->link_id . '_' . $this->id; + } + + public function hasFlags($flags) { + return $this->hasType($flags) && $this->hasLink($flags); + } + + /** Returns true if this phone's type matches the flags. + */ + public function hasType($flags) { + $flags = $flags & Profile::PHONE_TYPE_ANY; + return ( + ($flags == Profile::PHONE_TYPE_ANY) + || + (($flags & Profile::PHONE_TYPE_FAX) && $this->type == self::TYPE_FAX) + || + (($flags & Profile::PHONE_TYPE_FIXED) && $this->type == self::TYPE_FIXED) + || + (($flags & Profile::PHONE_TYPE_MOBILE) && $this->type == self::TYPE_MOBILE) + ); + } + + /** User-friendly accessible version of the type. + */ + public function displayType($short = false) + { + switch ($this->type) { + case Phone::TYPE_FIXED: + return $short ? 'Tél' : 'Fixe'; + case Phone::TYPE_FAX: + return 'Fax'; + case Phone::TYPE_MOBILE: + return $short ? 'Mob' : 'Mobile'; + default: + return $this->type; + } + } + + /** Returns true if this phone's link matches the flags. + */ + public function hasLink($flags) + { + $flags = $flags & Profile::PHONE_LINK_ANY; + return ( + ($flags == Profile::PHONE_LINK_ANY) + || + (($flags & Profile::PHONE_LINK_COMPANY) && $this->link_type == self::LINK_COMPANY) + || + (($flags & Profile::PHONE_LINK_JOB) && $this->link_type == self::LINK_JOB) + || + (($flags & Profile::PHONE_LINK_ADDRESS) && $this->link_type == self::LINK_ADDRESS) + || + (($flags & Profile::PHONE_LINK_PROFILE) && $this->link_type == self::LINK_PROFILE) + ); + } + + /* Properly formats the search phone, based on actual display phone. + * + * Computes a base form of the phone number with the international prefix. + * This number only contains digits, thus does not begin with the '+' sign. + * Numbers starting with 0 (or '(0)') are considered as French. + * This assumes that non-French numbers have their international prefix. + */ + private function formatSearch() + { + $tel = trim($this->display); + // Number starting with "(0)" is a French number. + if (substr($tel, 0, 3) === '(0)') { + $tel = '33' . $tel; + } + // Removes all "(0)" often used as local prefix. + $tel = str_replace('(0)', '', $tel); + // Removes all non-digit chars. + $tel = preg_replace('/[^0-9]/', '', $tel); + + if (substr($tel, 0, 2) === '00') { + // Removes prefix for international calls. + $tel = substr($tel, 2); + } else if (substr($tel, 0, 1) === '0') { + // Number starting with 0 is a French number. + $tel = '33' . substr($tel, 1); + } + $this->search = $tel; + } + + // Properly formats the display phone, it requires the search phone to be already formatted. + private function formatDisplay($format = array()) + { + $tel = $this->search; + $ret = ''; + $telLength = strlen($tel); + // Try to find the country by trying to find a matching prefix of 1, 2 or 3 digits. + if ((!isset($format['phoneprf'])) || ($format['phoneprf'] == '')) { + $res = XDB::query('SELECT phonePrefix AS phoneprf, phoneFormat AS format + FROM geoloc_countries + WHERE phonePrefix = {?} OR phonePrefix = {?} OR phonePrefix = {?} + LIMIT 1', + substr($tel, 0, 1), substr($tel, 0, 2), substr($tel, 0, 3)); + if ($res->numRows() == 0) { + // No country found, does not format more than prepending a '+' sign. + $this->error = true; + $this->display = '+' . $tel; + return; + } + $format = $res->fetchOneAssoc(); + } + if ($format['format'] == '') { + // If the country does not have a phone number format, the number will be displayed + // as "+prefix ## ## ## ##...". + $format['format'] = '+p'; + } + + /* Formats the phone number according t the template with these rules: + * - p is replaced by the international prefix, + * - # is replaced by one digit, + * - other chars are left intact. + * If the number is longer than the format, remaining digits are + * appended by blocks of two digits separated by spaces. + * The last block can have 3 digits to avoid a final single-digit block. + */ + $j = 0; + $i = strlen($format['phoneprf']); + $lengthFormat = strlen($format['format']); + while (($i < $telLength) && ($j < $lengthFormat)) { + if ($format['format'][$j] == '#') { + $ret .= $tel[$i]; + ++$i; + } else if ($format['format'][$j] == 'p') { + $ret .= $format['phoneprf']; + } else { + $ret .= $format['format'][$j]; + } + ++$j; + } + for (; $i < $telLength - 1; $i += 2) { + $ret .= ' ' . substr($tel, $i, 2); + } + // Appends last left alone numbers to the last block. + if ($i < $telLength) { + $ret .= substr($tel, $i); + } + $this->display = $ret; + } + + + public function format($format = array()) + { + if (!($this->type == Phone::TYPE_FIXED + || $this->type == Phone::TYPE_MOBILE + || $this->type == Phone::TYPE_FAX)) { + $this->type = Phone::TYPE_FIXED; + } + $this->formatSearch(); + $this->formatDisplay($format); + return !$this->error; + } + + public function toFormArray() + { + return array( + 'type' => $this->type, + 'display' => $this->display, + 'pub' => $this->pub, + 'comment' => $this->comment, + 'error' => $this->error + ); + } + + private function toString() + { + return 'type : ' . $this->type .', numéro : ' . $this->display + . ', commentaire : « ' . $this->comment . ' », affichage : ' . $this->pub; + } + + private function isEmpty() + { + return (!$this->search || $this->search == ''); + } + + public function save() + { + $this->format(); + if (!$this->isEmpty()) { + XDB::execute('INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type, + search_tel, display_tel, pub, comment) + VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})', + $this->pid, $this->link_type, $this->link_id, $this->id, $this->type, + $this->search, $this->display, $this->pub, $this->comment); + } + } + + static public function deletePhones($pid, $link_type, $link_id = null) + { + $where = ''; + if (!is_null($link_id)) { + $where = XDB::format(' AND link_id = {?}', $link_id); + } + XDB::execute('DELETE FROM profile_phones + WHERE pid = {?} AND link_type = {?}' . $where, + $pid, $link_type); + } + + /** Saves phones into the database. + * @param $data: an array of form formatted phones. + * @param $pid, $link_type, $link_id: pid, link_type and link_id concerned by the update. + */ + static public function savePhones(array $data, $pid, $link_type, $link_id = null) + { + foreach ($data as $id => $value) { + $value['id'] = $id; + if (!is_null($pid)) { + $value['pid'] = $pid ; + } + if (!is_null($link_type)) { + $value['link_type'] = $link_type ; + } + if (!is_null($link_id)) { + $value['link_id'] = $link_id ; + } + $phone = new Phone($value); + $phone->save(); + } + } + + static private function formArrayWalk(array $data, $function, &$success = true, $requiresEmptyPhone = false) + { + $phones = array(); + foreach ($data as $item) { + $phone = new Phone($item); + $success = ($phone->format() && $success); + if (!$phone->isEmpty()) { + $phones[] = call_user_func(array($phone, $function)); + } + } + if (count($phones) == 0 && $requiresEmptyPhone) { + $phone = new Phone(); + $phones[] = call_user_func(array($phone, $function)); + } + return $phones; + } + + // Formats an array of form phones into an array of form formatted phones. + static public function formatFormArray(array $data, &$success = true) + { + return self::formArrayWalk($data, 'toFormArray', $success, true); + } + + static public function formArrayToString(array $data) + { + return implode(' ; ', self::formArrayWalk($data, 'toString')); + } + + static public function iterate(array $pids = array(), array $link_types = array(), + array $link_ids = array(), array $pubs = array()) + { + return new PhoneIterator($pids, $link_types, $link_ids, $pubs); + } +} + +/** Iterator over a set of Phones + * + * @param $pid, $link_type, $link_id, $pub + * + * The iterator contains the phones that correspond to the value stored in the + * parameters' arrays. + */ +class PhoneIterator implements PlIterator +{ + private $dbiter; + + public function __construct(array $pids, array $link_types, array $link_ids, array $pubs) + { + $where = array(); + if (count($pids) != 0) { + $where[] = XDB::format('(pid IN {?})', $pids); + } + if (count($link_types) != 0) { + $where[] = XDB::format('(link_type IN {?})', $link_types); + } + if (count($link_ids) != 0) { + $where[] = XDB::format('(link_id IN {?})', $link_ids); + } + if (count($pubs) != 0) { + $where[] = XDB::format('(pub IN {?})', $pubs); + } + $sql = 'SELECT search_tel AS search, display_tel AS display, comment, link_id, + tel_type AS type, link_type, tel_id AS id, pid, pub + FROM profile_phones + WHERE ' . implode(' AND ', $where) . ' + ORDER BY link_id, tel_id'; + $this->dbiter = XDB::iterator($sql); + } + + public function next() + { + if (is_null($this->dbiter)) { + return null; + } + $data = $this->dbiter->next(); + if (is_null($data)) { + return null; + } + return new Phone($data); + } + + public function total() + { + return $this->dbiter->total(); + } + + public function first() + { + return $this->dbiter->first(); + } + + public function last() + { + return $this->dbiter->last(); + } + + public function value() + { + return $this->dbiter; + } +} + +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +?> diff --git a/classes/userfilter.php b/classes/userfilter.php index becf26b..e779261 100644 --- a/classes/userfilter.php +++ b/classes/userfilter.php @@ -1080,10 +1080,11 @@ class UFC_Phone implements UserFilterCondition public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY) { - require_once('profil.func.inc.php'); - $this->number = $number; + $phone = new Phone('display' => $number); + $phone->format(); + $this->number = $phone->search(); $this->num_type = $num_type; - $this->phone_type = format_phone_number($phone_type); + $this->phone_type = $phone_type; } public function buildCondition(PlFilter &$uf) diff --git a/htdocs/javascript/profile.js b/htdocs/javascript/profile.js index 217658b..cafaba9 100644 --- a/htdocs/javascript/profile.js +++ b/htdocs/javascript/profile.js @@ -381,7 +381,7 @@ function renumberPhone(prefname, prefid, i) $('#' + id).find('div.titre').html('N°' + i); $('#' + id).find('a.removeTel').attr('href', 'javascript:removeTel(\'' + prefname + '\',\'' + prefid + '\',' + telid + ')'); $('#' + id).find('select').attr('name', telpref + '[type]'); - $('#' + id).find("[name='" + telprefOld + "[tel]']").attr('name', telpref + '[tel]'); + $('#' + id).find("[name='" + telprefOld + "[display]']").attr('name', telpref + '[display]'); $('#' + id).find("[name='" + telprefOld + "[comment]']").attr('name', telpref + '[comment]'); $('#' + id).find('a.removePhoneComment').attr('href', 'javascript:removePhoneComment(' + id + ',' + telpref + ')'); $('#' + id).find('#' + idOld + '_addComment').attr('id', id + '_addComment'); diff --git a/include/profil.func.inc.php b/include/profil.func.inc.php deleted file mode 100644 index 465108b..0000000 --- a/include/profil.func.inc.php +++ /dev/null @@ -1,83 +0,0 @@ -'','phoneprf'=>'')) -{ - $error = false; - $ret = ''; - $tel_length = strlen($tel); - if((!isset($format['phoneprf'])) || ($format['phoneprf'] == '')) { - $res = XDB::query("SELECT phonePrefix AS phoneprf, phoneFormat AS format - FROM geoloc_countries - WHERE phonePrefix = {?} OR phonePrefix = {?} OR phonePrefix = {?} - LIMIT 1", - substr($tel, 0, 1), substr($tel, 0, 2), substr($tel, 0, 3)); - if ($res->numRows() == 0) { - $error = true; - return '+' . $tel; - } - $format = $res->fetchOneAssoc(); - } - if ($format['format'] == '') { - $format['format'] = '+p'; - } - $j = 0; - $i = strlen($format['phoneprf']); - $length_format = strlen($format['format']); - while (($i < $tel_length) && ($j < $length_format)){ - if ($format['format'][$j] == '#'){ - $ret .= $tel[$i]; - $i++; - } else if ($format['format'][$j] == 'p') { - $ret .= $format['phoneprf']; - } else { - $ret .= $format['format'][$j]; - } - $j++; - } - for (; $i < $tel_length - 1; $i += 2) { - $ret .= ' ' . substr($tel, $i, 2); - } - //appends last alone number to the last block - if ($i < $tel_length) { - $ret .= substr($tel, $i); - } - return $ret; -} - -// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: -?> diff --git a/include/profilefields.inc.php b/include/profilefields.inc.php index 16b8e1e..594e327 100644 --- a/include/profilefields.inc.php +++ b/include/profilefields.inc.php @@ -124,99 +124,6 @@ class ProfileFieldIterator implements PlIterator } // }}} -// {{{ class Phone -class Phone -{ - const TYPE_FAX = 'fax'; - const TYPE_FIXED = 'fixed'; - const TYPE_MOBILE = 'mobile'; - public $type; - - public $search; - public $display; - public $comment = ''; - - const LINK_JOB = 'pro'; - const LINK_ADDRESS = 'address'; - const LINK_PROFILE = 'user'; - const LINK_COMPANY = 'hq'; - public $link_type; - public $link_id; - - public $id; - - /** Fields are : - * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id - */ - public function __construct($data) - { - foreach ($data as $key => $val) { - $this->$key = $val; - } - } - - /** Returns the unique ID of a phone - * This ID will allow to link it to an address, a user or a job - * The format is address_addressId_phoneId (where phoneId is the id - * of the phone in the list of those associated with the address) - */ - public function uid() { - return $this->link_type . '_' . $this->link_id . '_' . $this->id; - } - - public function hasFlags($flags) { - return $this->hasType($flags) && $this->hasLink($flags); - } - - /** Returns true if this phone's type matches the flags - */ - public function hasType($flags) { - $flags = $flags & Profile::PHONE_TYPE_ANY; - return ( - ($flags == Profile::PHONE_TYPE_ANY) - || - (($flags & Profile::PHONE_TYPE_FAX) && $this->type == self::TYPE_FAX) - || - (($flags & Profile::PHONE_TYPE_FIXED) && $this->type == self::TYPE_FIXED) - || - (($flags & Profile::PHONE_TYPE_MOBILE) && $this->type == self::TYPE_MOBILE) - ); - } - - /** User accessible version of the type - */ - public function displayType($short = false) - { - switch ($this->type) { - case Phone::TYPE_FIXED: - return $short ? 'Tél' : 'Fixe'; - case Phone::TYPE_FAX: - return 'Fax'; - case Phone::TYPE_MOBILE: - return $short ? 'Mob' : 'Mobile'; - default: - return $this->type; - } - } - - /** Returns true if this phone's link matches the flags - */ - public function hasLink($flags) { - $flags = $flags & Profile::PHONE_LINK_ANY; - return ( - ($flags == Profile::PHONE_LINK_ANY) - || - (($flags & Profile::PHONE_LINK_COMPANY) && $this->link_type == self::LINK_COMPANY) - || - (($flags & Profile::PHONE_LINK_JOB) && $this->link_type == self::LINK_JOB) - || - (($flags & Profile::PHONE_LINK_ADDRESS) && $this->link_type == self::LINK_ADDRESS) - || - (($flags & Profile::PHONE_LINK_PROFILE) && $this->link_type == self::LINK_PROFILE) - ); - } -} -// }}} // {{{ class Company class Company { @@ -239,7 +146,7 @@ class Company public function setPhone(Phone &$phone) { - if ($phone->link_type == Phone::LINK_COMPANY && $phone->link_id == $this->id) { + if ($phone->linkType() == Phone::LINK_COMPANY && $phone->linkId() == $this->id) { $this->phone = $phone; } } @@ -301,8 +208,8 @@ class Job public function addPhone(Phone &$phone) { - if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) { - $this->phones[$phone->uid()] = $phone; + if ($phone->linkType() == Phone::LINK_JOB && $phone->linkId() == $this->id && $phone->pid() == $this->pid) { + $this->phones[$phone->uniqueId()] = $phone; } } @@ -362,9 +269,9 @@ class Address public function addPhone(Phone &$phone) { if ( - $phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && - ($this->link_type == self::LINK_COMPANY || $phone->pid == $this->pid) ) { - $this->phones[$phone->uid()] = $phone; + $phone->linkType() == Phone::LINK_ADDRESS && $phone->linkId() == $this->id && + ($this->link_type == self::LINK_COMPANY || $phone->pid() == $this->pid) ) { + $this->phones[$phone->uniqueId()] = $phone; } } @@ -656,7 +563,7 @@ class ProfileMentoringCountries extends ProfileField * 3) attach addresses to jobs and profiles */ -// {{{ Database schema (profile_address, profile_phones, profile_jobs) +// {{{ Database schema (profile_address, profile_jobs) /** The database for this is very unclear, so here is a little schema : * profile_job describes a Job, links to: * - a Profile, through `pid` @@ -684,33 +591,11 @@ class ProfileMentoringCountries extends ProfileField * with that of the row of profile_job for the related job) * - `id` is the id of the job to which we refer (i.e `profile_job.id`) * - * profile_phone describes a Phone, which can be related to an Address, - * a Job, a Profile or a Company: - * - for a Profile: - * - `link_type` is set to 'user' - * - `link_id` is set to 0 - * - `pid` is set to the id of the related Profile - * - * - for a Company: - * - `link_type` is set to 'hq' - * - `link_id` is set to the id of the related Company - * - `pid` is set to 0 - * - * - for an Address (this is only possible for a *personal* address) - * - `link_type` is set to 'address' - * - `link_id` is set to the related Address `id` - * - `pid` is set to the related Address `pid` - * - * - for a Job: - * - `link_type` is set to 'pro' - * - `link_id` is set to the related Job `id` (not `jobid`) - * - `pid` is set to the related Job `pid` - * + * For the documentation of the phone table, please see classes/phone.php. * * The possible relations are as follow: * An Address can be linked to a Company, a Profile, a Job * A Job is linked to a Company and a Profile - * A Phone can be linked to a Company, a Profile, a Job, or a Profile-related Address */ // }}} @@ -777,8 +662,8 @@ class ProfileAddresses extends ProfileField { $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY); foreach ($p as $phone) { - if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) { - $this->addresses[$phone->link_id]->addPhone($phone); + if ($phone->linkType() == Phone::LINK_ADDRESS && array_key_exists($phone->linkId(), $this->addresses)) { + $this->addresses[$phone->linkId()]->addPhone($phone); } } } @@ -815,12 +700,8 @@ class ProfilePhones extends ProfileField public static function fetchData(array $pids, ProfileVisibility $visibility) { - $data = XDB::iterator('SELECT tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment, pid, link_id, tel_id AS id - FROM profile_phones - WHERE pid IN {?} AND pub IN {?} - ORDER BY ' . XDB::formatCustomOrder('pid', $pids), - $pids, $visibility->levels()); - return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); + $it = Phone::iterate($pids, array(), array(), $visibility->levels()); + return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid')); } } // }}} @@ -873,8 +754,8 @@ class ProfileJobs extends ProfileField { $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY); foreach ($p as $phone) { - if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) { - $this->jobs[$phone->link_id]->addPhone($phone); + if ($phone->linkType() == Phone::LINK_JOB && array_key_exists($phone->linkId(), $this->jobs)) { + $this->jobs[$phone->linkId()]->addPhone($phone); } } } @@ -938,13 +819,9 @@ class CompanyList // Add phones to hq if (count($newcompanies)) { - $it = XDB::iterator('SELECT search_tel AS search, display_tel AS display, comment, link_id, tel_type AS type, link_type, tel_id AS id - FROM profile_phones - WHERE link_id IN {?} AND link_type = \'hq\'', - $newcompanies); - while ($row = $it->next()) { - $p = new Phone($row); - self::$companies[$row['link_id']]->setPhone($p); + $it = Phone::iterate(array(), array(Phone::LINK_COMPANY), $newcompanies); + while ($phone = $it->next()) { + self::$companies[$phone->linkId()]->setPhone($phone); } } diff --git a/include/validations/entreprises.inc.php b/include/validations/entreprises.inc.php index 47e6a02..4571738 100644 --- a/include/validations/entreprises.inc.php +++ b/include/validations/entreprises.inc.php @@ -159,14 +159,13 @@ class EntrReq extends ProfileValidate public function commit() { - // TODO: use address and phone classes to update profile_job_enum and profile_phones once they are done. + // TODO: use address update profile_job_enum once it is done. $res = XDB::query('SELECT id FROM profile_job_enum WHERE name = {?}', $this->name); if ($res->numRows() != 1) { - require_once 'profil.func.inc.php'; require_once 'geocoding.inc.php'; XDB::execute('INSERT INTO profile_job_enum (name, acronym, url, email, holdingid, NAF_code, AX_code) @@ -175,14 +174,12 @@ class EntrReq extends ProfileValidate $this->holdingid, $this->NAF_code, $this->AX_code); $jobid = XDB::insertId(); - $display_tel = format_display_number($this->tel, $error_tel); - $display_fax = format_display_number($this->fax, $error_fax); - XDB::execute("INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type, - search_tel, display_tel, pub) - VALUES ({?}, 'hq', 0, 0, 'fixed', {?}, {?}, 'public'), - ({?}, 'hq', 0, 1, 'fax', {?}, {?}, 'public')", - $jobid, format_phone_number($this->tel), $display_tel, - $jobid, format_phone_number($this->fax), $display_fax); + $phone = new Phone(array('link_type' => 'hq', 'link_id' => $jobid, 'id' => 0, + 'type' => 'fixed', 'display' => $this->tel, 'pub' => 'public')); + $fax = new Phone(array('link_type' => 'hq', 'link_id' => $jobid, 'id' => 1, + 'type' => 'fax', 'display' => $this->fax, 'pub' => 'public')); + $phone->save(); + $fax->save(); $gmapsGeocoder = new GMapsGeocoder(); $address = $gmapsGeocoder->getGeocodedAddress($this->address); diff --git a/modules/admin.php b/modules/admin.php index caa412a..19f3ec5 100644 --- a/modules/admin.php +++ b/modules/admin.php @@ -1369,14 +1369,12 @@ class AdminModule extends PLModule } if (Env::has('edit')) { - // TODO: use address and phone classes to update profile_job_enum and profile_phones once they are done. + // TODO: use address class to update profile_job_enum once it is done. S::assert_xsrf_token(); $selectedJob = Env::has('selectedJob'); - XDB::execute("DELETE FROM profile_phones - WHERE pid = {?} AND link_type = 'hq'", - $id); + Phone::deletePhones(0, Phone::LINK_COMPANY, $id); XDB::execute("DELETE FROM profile_addresses WHERE jobid = {?} AND type = 'hq'", $id); @@ -1392,11 +1390,8 @@ class AdminModule extends PLModule $page->trigSuccess("L'entreprise a bien été remplacée."); } else { - require_once 'profil.func.inc.php'; require_once 'geocoding.inc.php'; - $display_tel = format_display_number(Env::v('tel'), $error_tel); - $display_fax = format_display_number(Env::v('fax'), $error_fax); $gmapsGeocoder = new GMapsGeocoder(); $address = array('text' => Env::t('address')); $address = $gmapsGeocoder->getGeocodedAddress($address); @@ -1411,12 +1406,12 @@ class AdminModule extends PLModule Env::t('name'), Env::t('acronym'), Env::t('url'), Env::t('email'), Env::t('NAF_code'), Env::i('AX_code'), Env::i('holdingId'), $id); - XDB::execute("INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type, - search_tel, display_tel, pub) - VALUES ({?}, 'hq', 0, 0, 'fixed', {?}, {?}, 'public'), - ({?}, 'hq', 0, 1, 'fax', {?}, {?}, 'public')", - $id, format_phone_number(Env::v('tel')), $display_tel, - $id, format_phone_number(Env::v('fax')), $display_fax); + $phone = new Phone(array('display' => Env::v('tel'), 'link_id' => $id, 'id' => 0, 'type' => 'fixed', + 'link_type' => Phone::LINK_COMPANY, 'pub' => 'public')); + $fax = new Phone(array('display' => Env::v('fax'), 'link_id' => $id, 'id' => 1, 'type' => 'fax', + 'link_type' => Phone::LINK_COMPANY, 'pub' => 'public')); + $phone->save(); + $fax->save(); XDB::execute("INSERT INTO profile_addresses (jobid, type, id, accuracy, text, postalText, postalCode, localityId, diff --git a/modules/payment.php b/modules/payment.php index 86e0e90..d24ebcf 100644 --- a/modules/payment.php +++ b/modules/payment.php @@ -108,7 +108,6 @@ class PaymentModule extends PLModule { global $globals; - require_once 'profil.func.inc.php' ; $this->load('money.inc.php'); if (!empty($GLOBALS['IS_XNET_SITE'])) { diff --git a/modules/profile/addresses.inc.php b/modules/profile/addresses.inc.php index 37deea8..9722476 100644 --- a/modules/profile/addresses.inc.php +++ b/modules/profile/addresses.inc.php @@ -32,11 +32,7 @@ class ProfileSettingAddress extends ProfileSettingGeocoding private function cleanAddress(ProfilePage &$page, $addrid, array &$address) { - if (!isset($address['tel'])) { - $address['tel'] = array(); - } - $profiletel = new ProfileSettingPhones('address', $addrid); - $address['tel'] = $profiletel->value($page, 'tel', $address['tel'], $s); + $address['tel'] = Phone::formatFormArray($address['tel'], $s); $address['current'] = $this->bool->value($page, 'current', $address['current'], $s); $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $s); $address['secondary'] = $this->bool->value($page, 'secondary', $address['secondary'], $s); @@ -118,25 +114,22 @@ class ProfileSettingAddress extends ProfileSettingGeocoding XDB::execute("DELETE FROM profile_addresses WHERE pid = {?} AND type = 'home'", $page->pid()); - XDB::execute("DELETE FROM profile_phones - WHERE pid = {?} AND link_type = 'address'", - $page->pid()); + Phone::deletePhones($page->pid(), Phone::LINK_ADDRESS); foreach ($value as $addrid => &$address) { $this->saveAddress($page->pid(), $addrid, $address, 'home'); - $profiletel = new ProfileSettingPhones('address', $addrid); - $profiletel->saveTels($page->pid(), 'tel', $address['tel']); + Phone::savePhones($address['tel'], $page->pid(), Phone::LINK_ADDRESS, $addrid); } } public function getText($value) { $addresses = array(); foreach ($value as $addrid => $address) { - $phones = new ProfileSettingPhones('address', $addrid); + $phones = Phone::formArrayToString($address['tel']); $addresses[] = 'Adresse : ' . $address['text'] . ', affichage : ' . $address['pub'] . ', commentaire : ' . $address['comment'] . ', actuelle : ' . ($address['current'] ? 'oui' : 'non') . ', temporaire : ' . ($address['temporary'] ? 'oui' : 'non') . ', secondaire : ' . ($address['secondary'] ? 'oui' : 'non') . ', conctactable par courier : ' - . ($address['mail'] ? 'oui' : 'non') . ', ' . $phones->getText($address['tel']); + . ($address['mail'] ? 'oui' : 'non') . ($phones ? ', ' . $phones : ''); } return implode(' ; ' , $addresses); } @@ -174,45 +167,23 @@ class ProfileSettingAddresses extends ProfilePage $this->values['addresses'] = $res->fetchAllAssoc(); } - $res = XDB::iterator("SELECT link_id AS addrid, tel_type AS type, pub, display_tel AS tel, comment - FROM profile_phones - WHERE pid = {?} AND link_type = 'address' - ORDER BY link_id", - $this->pid()); - $i = 0; - $adrNb = count($this->values['addresses']); - while ($tel = $res->next()) { - $addrid = $tel['addrid']; - unset($tel['addrid']); - while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $addrid) { - $i++; - } - if ($i >= $adrNb) { - break; - } - $address =& $this->values['addresses'][$i]; - if (!isset($address['tel'])) { - $address['tel'] = array(); - } - if ($address['id'] == $addrid) { - $address['tel'][] = $tel; - } + // Adds phones to addresses. + $it = Phone::iterate(array($this->pid()), array(Phone::LINK_ADDRESS)); + while ($phone = $it->next()) { + $this->values['addresses'][$phone->linkId()]['tel'][$phone->id()] = $phone->toFormArray(); } + + // Properly formats addresses. foreach ($this->values['addresses'] as $id => &$address) { + $phone = new Phone(); if (!isset($address['tel'])) { - $address['tel'] = array( - 0 => array( - 'type' => 'fixed', - 'tel' => '', - 'pub' => 'private', - 'comment' => '', - ) - ); + $address['tel'] = array(0 => $phone->toFormArray()); } unset($address['id']); $address['changed'] = 0; $address['removed'] = 0; } + //var_dump($this->values['addresses']['tel']); } } diff --git a/modules/profile/general.inc.php b/modules/profile/general.inc.php index 817b9a9..f89b8ac 100644 --- a/modules/profile/general.inc.php +++ b/modules/profile/general.inc.php @@ -537,7 +537,7 @@ class ProfileSettingGeneral extends ProfilePage $this->settings['email_directory_new'] = new ProfileSettingEmailDirectory(); $this->settings['networking'] = new ProfileSettingNetworking(); - $this->settings['tels'] = new ProfileSettingPhones('user', 0); + $this->settings['tels'] = new ProfileSettingPhones(); $this->settings['edus'] = new ProfileSettingEdu(); $this->settings['promo'] = new ProfileSettingPromo(); $this->watched= array('freetext' => true, 'tels' => true, @@ -550,12 +550,10 @@ class ProfileSettingGeneral extends ProfilePage { // Checkout all data... $res = XDB::query("SELECT p.nationality1, p.nationality2, p.nationality3, p.birthdate, - pp.display_tel as mobile, pp.pub as mobile_pub, p.email_directory as email_directory, pd.promo AS promo_display, p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself FROM profiles AS p INNER JOIN profile_display AS pd ON (pd.pid = p.pid) - LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND link_type = 'user') WHERE p.pid = {?}", $this->pid()); $this->values = $res->fetchOneAssoc(); diff --git a/modules/profile/jobs.inc.php b/modules/profile/jobs.inc.php index ddb628f..ccd7d0d 100644 --- a/modules/profile/jobs.inc.php +++ b/modules/profile/jobs.inc.php @@ -72,7 +72,7 @@ class ProfileSettingJob extends ProfileSettingGeocoding 'changed' => '0', 'removed' => '0', ), - 'hq_phone' => '', + 'hq_fixed' => '', 'hq_fax' => '', 'subSubSectorName' => null, 'sector' => '0', @@ -168,11 +168,7 @@ class ProfileSettingJob extends ProfileSettingGeocoding } } $job['w_address']['pub'] = $this->pub->value($page, 'address_pub', $job['w_address']['pub'], $s); - if (!isset($job['w_phone'])) { - $job['w_phone'] = array(); - } - $profiletel = new ProfileSettingPhones('pro', $jobid); - $job['w_phone'] = $profiletel->value($page, 'tel', $job['w_phone'], $s); + $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s); unset($job['removed']); unset($job['new']); @@ -229,19 +225,14 @@ class ProfileSettingJob extends ProfileSettingGeocoding public function save(ProfilePage &$page, $field, $value) { - // TODO: use address and phone classes to update profile_job_enum and profile_phones once they are done. - - require_once 'profil.func.inc.php'; - + // TODO: use address class to update profile_job_enum once it is done. XDB::execute("DELETE FROM profile_job WHERE pid = {?}", $page->pid()); XDB::execute("DELETE FROM profile_addresses WHERE pid = {?} AND type = 'job'", $page->pid()); - XDB::execute("DELETE FROM profile_phones - WHERE pid = {?} AND link_type = 'pro'", - $page->pid()); + Phone::deletePhones($page->pid(), Phone::LINK_JOB); foreach ($value as $id => &$job) { if (isset($job['name']) && $job['name']) { if (isset($job['jobid']) && $job['jobid']) { @@ -259,8 +250,7 @@ class ProfileSettingJob extends ProfileSettingGeocoding } $address = new ProfileSettingAddress(); $address->saveAddress($page->pid(), $id, $job['w_address'], 'job'); - $profiletel = new ProfileSettingPhones('pro', $id); - $profiletel->saveTels($page->pid(), 'tel', $job['w_phone']); + Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id); } } } @@ -269,11 +259,11 @@ class ProfileSettingJob extends ProfileSettingGeocoding $jobs = array(); foreach ($value as $id => $job) { $address = new ProfileSettingAddress(); - $phones = new ProfileSettingPhones('pro', $id); + $phones = Phone::formArrayToString($job['w_phone']); $jobs[] = 'Entreprise : ' . $job['name'] . ', secteur : ' . $job['subSubSectorName'] . ', description : ' . $job['description'] . ', web : ' . $job['w_url'] . ', email : ' . $job['w_email'] - . ', ' . $phones->getText($job['w_phone']) . ', ' . $address->getText($job['w_address']); + . ($phones ? ', ' . $phones : '') . ', ' . $address->getText($job['w_address']); } return implode(' ; ' , $jobs); } @@ -424,45 +414,20 @@ class ProfileSettingJobs extends ProfilePage ); } - $res = XDB::iterator("SELECT link_id AS jobid, tel_type AS type, pub, display_tel AS tel, comment - FROM profile_phones - WHERE pid = {?} AND link_type = 'pro' - ORDER BY link_id", - $this->pid()); - $i = 0; - $jobNb = count($this->values['jobs']); - while ($phone = $res->next()) { - $jobid = $phone['jobid']; - while ($i < $jobNb && $this->values['jobs'][$i]['id'] < $jobid) { - $i++; - } - if ($i >= $jobNb) { - break; - } - $job =& $this->values['jobs'][$i]; - if (!isset($job['w_phone'])) { - $job['w_phone'] = array(); - } - if ($job['id'] == $jobid) { - $job['w_phone'][] = $phone; - } + $it = Phone::iterate(array($this->pid()), array(Phone::LINK_JOB)); + while ($phone = $it->next()) { + $this->values['jobs'][$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray(); } foreach ($this->values['jobs'] as $id => &$job) { + $phone = new Phone(); if (!isset($job['w_phone'])) { - $job['w_phone'] = array( - 0 => array( - 'type' => 'fixed', - 'tel' => '', - 'pub' => 'private', - 'comment' => '', - ) - ); + $job['w_phone'] = array(0 => $phone->toFormArray()); } } $job['w_email_new'] = ''; - if (!isset($job['hq_phone'])) { - $job['hq_phone'] = ''; + if (!isset($job['hq_fixed'])) { + $job['hq_fixed'] = ''; } if (!isset($job['hq_fax'])) { $job['hq_fax'] = ''; diff --git a/modules/profile/page.inc.php b/modules/profile/page.inc.php index 8c17a05..70d0ab5 100644 --- a/modules/profile/page.inc.php +++ b/modules/profile/page.inc.php @@ -100,124 +100,37 @@ class ProfileSettingNumber extends ProfileNoSave } } - -class ProfileSettingTel extends ProfileNoSave -{ - public function value(ProfilePage &$page, $field, $value, &$success) - { - if (is_null($value)) { - return isset($page->values[$field]) ? $page->values[$field] : S::v($field); - } - require_once('profil.func.inc.php'); - $value = format_phone_number($value); - if($value == '') { - $success = true; - return $value; - } - $value = format_display_number($value,$error); - $success = !$error; - if (!$success) { - Platal::page()->trigError('Le préfixe international du numéro de téléphone est inconnu. '); - } - return $value; - } -} - class ProfileSettingPhones implements ProfileSetting { - private $tel; - private $pub; - protected $link_type; - protected $link_id; - - public function __construct($type, $link_id) - { - $this->tel = new ProfileSettingTel(); - $this->pub = new ProfileSettingPub(); - $this->link_type = $type; - $this->link_id = $link_id; - } - public function value(ProfilePage &$page, $field, $value, &$success) { $success = true; - if (is_null($value)) { - $value = array(); - $res = XDB::iterator('SELECT display_tel AS tel, tel_type AS type, pub, comment - FROM profile_phones - WHERE pid = {?} AND link_type = {?} - ORDER BY tel_id', - $page->pid(), $this->link_type); - if ($res->numRows() > 0) { - $value = $res->fetchAllAssoc(); - } else { - $value = array( - 0 => array( - 'type' => 'fixed', - 'tel' => '', - 'pub' => 'private', - 'comment' => '', - ) - ); - } - } + $phones = array(); - foreach ($value as $key=>&$phone) { - $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s); - $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s); - if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) { - $phone['type'] = 'fixed'; - $s = false; - } - if (!$s) { - $phone['error'] = true; - $success = false; + if (is_null($value)) { + $it = Phone::iterate(array($page->pid()), array(Phone::LINK_PROFILE), array(0)); + while ($phone = $it->next()) { + $success = ($phone->format() && $success); + $phones[] = $phone->toFormArray(); } - if (!isset($phone['comment'])) { - $phone['comment'] = ''; + if (count($phones) == 0) { + $phone = new Phone(); + $phones[] = $phone->toFormArray(); } + return $phones; } - return $value; - } - - private function saveTel($pid, $telid, array &$phone) - { - if ($phone['tel'] != '') { - XDB::execute("INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type, - search_tel, display_tel, pub, comment) - VALUES ({?}, {?}, {?}, {?}, {?}, - {?}, {?}, {?}, {?})", - $pid, $this->link_type, $this->link_id, $telid, $phone['type'], - format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']); - } + return Phone::formatFormArray($value, $success); } public function save(ProfilePage &$page, $field, $value) { - XDB::execute("DELETE FROM profile_phones - WHERE pid = {?} AND link_type = {?} AND link_id = {?}", - $page->pid(), $this->link_type, $this->link_id); - $this->saveTels($page->pid(), $field, $value); - } - - //Only saves phones without a delete operation - public function saveTels($pid, $field, $value) - { - foreach ($value as $telid=>&$phone) { - $this->saveTel($pid, $telid, $phone); - } + Phone::deletePhones($page->pid(), Phone::LINK_PROFILE); + Phone::savePhones($value, $page->pid(), Phone::LINK_PROFILE); } public function getText($value) { - $phones = array(); - foreach ($value as $phone) { - if ($phone['tel'] != '') { - $phones[] = 'type : ' . $phone['type'] .', numéro : ' . $phone['tel'] - . ', commentaire : « ' . $phone['comment'] . ' », affichage : ' . $phone['pub']; - } - } - return implode(' ; ' , $phones); + return Phone::formArrayToString($value); } } diff --git a/templates/core/vcard.tpl b/templates/core/vcard.tpl index 223ac4d..e4edd05 100644 --- a/templates/core/vcard.tpl +++ b/templates/core/vcard.tpl @@ -42,7 +42,7 @@ EMAIL;TYPE=internet:{$vcard.virtualalias} {/if} {if $vcard.tels} {foreach item=tel from=$vcard.tels} -{if $tel.tel_type eq 'mobile'}TEL;TYPE=cell{else}{if $tel.tel_type eq 'fax'}FAX{else}TEL{/if};TYPE=home{/if}:{$tel.tel|vcard_enc} +{if $tel.tel_type eq 'mobile'}TEL;TYPE=cell{else}{if $tel.tel_type eq 'fax'}FAX{else}TEL{/if};TYPE=home{/if}:{$tel.display|vcard_enc} {/foreach} {/if} {if $vcard.adr_pro} @@ -57,7 +57,7 @@ ROLE:{$vcard.adr_pro[0].fonction|vcard_enc} {/if} {if $vcard.adr_pro[0].tels} {foreach item=tel from=$vcard.adr_pro[0].tels} -{if $tel.tel_type eq 'mobile'}TEL;TYPE=cell,work{else}{if $tel.tel_type eq 'fax'}FAX{else}TEL{/if};TYPE=work{/if}:{$tel.tel|vcard_enc} +{if $tel.tel_type eq 'mobile'}TEL;TYPE=cell,work{else}{if $tel.tel_type eq 'fax'}FAX{else}TEL{/if};TYPE=work{/if}:{$tel.display|vcard_enc} {/foreach} {/if} ADR;TYPE=work:{format_adr adr=$vcard.adr_pro[0]} @@ -66,7 +66,7 @@ ADR;TYPE=work:{format_adr adr=$vcard.adr_pro[0]} ADR;TYPE=home{if $adr.courier},postal{/if}:{format_adr adr=$adr} {if $adr.tels} {foreach item=tel from=$adr.tels} -{if $tel.tel_type eq 'mobile'}TEL;TYPE=cell,home{else}{if $tel.tel_type eq 'fax'}FAX{else}TEL{/if};TYPE=home{/if}:{$tel.tel|vcard_enc} +{if $tel.tel_type eq 'mobile'}TEL;TYPE=cell,home{else}{if $tel.tel_type eq 'fax'}FAX{else}TEL{/if};TYPE=home{/if}:{$tel.display|vcard_enc} {/foreach} {/if} {/foreach} diff --git a/templates/profile/jobs.job.tpl b/templates/profile/jobs.job.tpl index ec4008f..118ad93 100644 --- a/templates/profile/jobs.job.tpl +++ b/templates/profile/jobs.job.tpl @@ -101,7 +101,7 @@ Téléphone + name="{$jobpref}[hq_fixed]" value="{$job.hq_fixed}" /> diff --git a/templates/profile/phone.tpl b/templates/profile/phone.tpl index 15a34e5..db5d919 100644 --- a/templates/profile/phone.tpl +++ b/templates/profile/phone.tpl @@ -29,7 +29,7 @@ - + {icon name=cross title="Supprimer ce numéro de téléphone"} diff --git a/upgrade/1.0.1/04_phones.sql b/upgrade/1.0.1/04_phones.sql new file mode 100644 index 0000000..9dc6fdf --- /dev/null +++ b/upgrade/1.0.1/04_phones.sql @@ -0,0 +1,4 @@ +ALTER TABLE profile_phones MODIFY COLUMN pid INT(6) NOT NULL DEFAULT 0; +ALTER TABLE profile_phones MODIFY COLUMN link_id INT(6) NOT NULL DEFAULT 0; + +-- vim:set syntax=mysql: