2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 Address is meant to perform most of the access to the table profile_addresses.
24 * profile_addresses describes an Address, which can be related to either a
25 * Profile, a Job or a Company:
27 * - `type` is set to 'home'
28 * - `pid` is set to the related profile pid (in profiles)
29 * - `id` is the id of the address in the list of those related to that profile
30 * - `jobid` is set to 0
33 * - `type` is set to 'hq'
35 * - `jobid` is set to the id of the company (in profile_job_enum)
36 * - `id` is set to 0 (only one address per Company)
39 * - `type` is set to 'job'
40 * - `pid` is set to the pid of the Profile of the related Job (in both profiles and profile_job)
41 * - `id` is the id of the job to which we refer (in profile_job)
42 * - `jobid` is set to 0
44 * Thus an Address can be linked to a Company, a Profile, or a Job.
48 const LINK_JOB
= 'job';
49 const LINK_COMPANY
= 'hq';
50 const LINK_PROFILE
= 'home';
52 // List of all available postal formattings.
53 private static $formattings = array('FRANCE' => 'FR');
55 // Abbreviations to be used to format French postal addresses.
56 private static $streetAbbreviations = array(
61 'CENTRE COMMERCIAL' => 'CCAL',
66 'LOTISSEMENT' => 'LOT',
70 'ROND-POINT' => 'RPT',
74 'ZONE D\'ACTIVITE' => 'ZA',
75 'ZONE D\'AMENAGEMENT CONCERTE' => 'ZAC',
76 'ZONE D\'AMENAGEMENT DIFFERE' => 'ZAD',
77 'ZONE INDUSTRIELLE' => 'ZI'
79 private static $otherAbbreviations = array(
81 'AERODROME' => 'AERD',
83 'AERONAUTIQUE' => 'AERN',
86 'AGRICOLE' => 'AGRIC',
88 'ANCIENNEMENT' => 'ANC',
89 'APPARTEMENT' => 'APP',
90 'APPARTEMENTS' => 'APP',
92 'ARRONDISSEMENT' => 'ARR',
94 'ASSOCIATION' => 'ASSOC',
95 'ASSURANCE' => 'ASSUR',
97 'BARAQUEMENT' => 'BRQ',
101 'BATAILLON' => 'BTN',
102 'BATAILLONS' => 'BTN',
104 'BATIMENTS' => 'BAT',
106 'BOITE POSTALE' => 'BP',
110 'CASE POSTALE' => 'CP',
112 'CITADELLE' => 'CTD',
117 'COMMANDANT' => 'CDT',
118 'COMMERCIAL' => 'CIAL',
121 'COMMUNAUX' => 'COM',
122 'COMPAGNIE' => 'CIE',
123 'COMPAGNON' => 'COMP',
124 'COMPAGNONS' => 'COMP',
125 'COOPERATIVE' => 'COOP',
126 'COURSE SPECIALE' => 'CS',
128 'DELEGATION' => 'DELEG',
129 'DEPARTEMENTAL' => 'DEP',
130 'DEPARTEMENTAUX' => 'DEP',
131 'DIRECTEUR' => 'DIR',
132 'DIRECTECTION' => 'DIR',
136 'ECONOMIQUE' => 'ECO',
137 'ECRIVAIN' => 'ECRIV',
138 'ECRIVAINS' => 'ECRIV',
139 'ENSEIGNEMENT' => 'ENST',
143 'ENTREPRISE' => 'ENTR',
146 'ETABLISSEMENT' => 'ETS',
148 'ETAT MAJOR' => 'EM',
152 'FORESTIER' => 'FOR',
156 'GENDARMERIE' => 'GEND',
158 'GOUVERNEMENTAL' => 'GOUV',
159 'GOUVERNEUR' => 'GOU',
171 'HOSPITALIER' => 'HOSP',
173 'INFANTERIE' => 'INFANT',
174 'INFERIEUR' => 'INF',
175 'INFERIEUR' => 'INF',
176 'INGENIEUR' => 'ING',
177 'INSPECTEUR' => 'INSP',
178 'INSTITUT' => 'INST',
179 'INTERNATIONAL' => 'INTERN',
180 'INTERNATIONALE' => 'INTERN',
181 'LABORATOIRE' => 'LABO',
182 'LIEUTENANT' => 'LT',
183 'LIEUTENANT DE VAISSEAU' => 'LTDV',
185 'MADEMOISELLE' => 'MLLE',
193 'MESDAMES' => 'MMES',
194 'MESDEMOISELLES' => 'MLLES',
196 'MILITAIRE' => 'MIL',
197 'MINISTERE' => 'MIN',
198 'MONSEIGNEUR' => 'MGR',
200 'MUNICIPAL' => 'MUN',
203 'NOTRE DAME' => 'ND',
206 'NOUVELLE' => 'NOUV',
207 'OBSERVATOIRE' => 'OBS',
215 'PREFECTURE' => 'PREF',
216 'PRESIDENT' => 'PDT',
217 'PROFESSEUR' => 'PR',
218 'PROFESSIONNEL' => 'PROF',
219 'PROFESSIONNELE' => 'PROF',
220 'PROLONGE' => 'PROL',
221 'PROLONGEE' => 'PROL',
222 'PROPRIETE' => 'PROP',
229 'REGIONALE' => 'REG',
230 'REPUBLIQUE' => 'REP',
231 'RESTAURANT' => 'REST',
236 'SANATORIUM' => 'SANA',
240 'SOUS COUVERT' => 'SC',
241 'SOUS-PREFET' => 'SPREF',
242 'SUPERIEUR' => 'SUP',
243 'SUPERIEURE' => 'SUP',
244 'SYNDICAT' => 'SYND',
245 'TECHNICIEN' => 'TECH',
246 'TECHNICIENNE' => 'TECH',
247 'TECHNICIQUE' => 'TECH',
249 'TRI SERVICE ARRIVEE' => 'TSA',
251 'UNIVERSITAIRE' => 'UNVT',
252 'UNIVERSITE' => 'UNIV',
253 'VELODROME' => 'VELOD',
256 'VIEILLES' => 'VIEL',
259 private static $entrepriseAbbreviations = array(
260 'COOPERATIVE D\'UTILISATION DE MATERIEL AGRICOLE EN COMMUN' => 'CUMA',
261 'ETABLISSEMENT PUBLIC A CARACTERE INDUSTRIEL ET COMMERCIAL' => 'EPIC',
262 'ETABLISSEMENT PUBLIC ADMINISTRATIF' => 'EPA',
263 'GROUPEMENT AGRICOLE D\'EXPLOITATION EN COMMUN' => 'GAEC',
264 'GROUPEMENT D\'INTERET ECONOMIQUE' => 'GIE',
265 'GROUPEMENT D\'INTERET PUBLIC' => 'GIP',
266 'GROUPEMENT EUROPEEN D\'INTERET ECONOMIQUE' => 'GEIE',
267 'OFFICE PUBLIC D\'HABITATION A LOYER MODERE' => 'OPHLM',
268 'SOCIETE A RESPONSABILITE LIMITEE' => 'SARL',
269 'SOCIETE ANONYME' => 'SA',
270 'SOCIETE CIVILE DE PLACEMENT COLLECTIF IMMOBILIER' => 'SCPI',
271 'SOCIETE CIVILE PROFESSIONNELLE' => 'SCP',
272 'SOCIETE COOPERATIVE OUVRIERE DE PRODUCTION ET DE CREDIT' => 'SCOP',
273 'SOCIETE D\'AMENAGEMENT FONCIER ET D\'EQUIPEMENT RURAL' => 'SAFER',
274 'SOCIETE D\'ECONOMIE MIXTE' => 'SEM',
275 'SOCIETE D\'INTERET COLLECTIF AGRICOLE' => 'SICA',
276 'SOCIETE D\'INVESTISSEMENT A CAPITAL VARIABLE' => 'SICAV',
277 'SOCIETE EN NOM COLLECTIF' => 'SNC',
278 'SOCIETE IMMOBILIERE POUR LE COMMERCE ET L\'INDUSTRIE' => 'SICOMI',
279 'SOCIETE MIXTE D\'INTERET AGRICOLE' => 'SMIA',
280 'SYNDICAT INTERCOMMUNAL A VOCATION MULTIPLE' => 'SIVOM',
281 'SYNDICAT INTERCOMMUNAL A VOCATION UNIQUE' => 'SIVU'
284 // Primary key fields: the quadruplet ($pid, $jobid, $type, $id) defines a unique address.
287 public $type = Address
::LINK_PROFILE
;
291 public $accuracy = 0;
293 public $postalText = '';
294 public $postalCode = null
;
295 public $localityId = null
;
296 public $subAdministrativeAreaId = null
;
297 public $administrativeAreaId = null
;
298 public $localityName = null
;
299 public $subAdministrativeAreaName = null
;
300 public $administrativeAreaName = null
;
301 public $countryId = null
;
302 public $latitude = null
;
303 public $longitude = null
;
304 public $north = null
;
305 public $south = null
;
308 public $geocodedText = null
;
309 public $geocodeChosen = null
;
311 // Database's field required for both 'home' and 'job' addresses.
314 // Database's fields required for 'home' addresses.
315 public $flags = null
; // 'current', 'temporary', 'secondary', 'mail', 'cedex', 'deliveryIssue'
316 public $comment = null
;
317 public $current = null
;
318 public $temporary = null
;
319 public $secondary = null
;
321 public $deliveryIssue = null
;
323 // Remaining fields that do not belong to profile_addresses.
324 public $phones = array();
325 public $error = false
;
329 public function __construct(array $data = array())
331 if (count($data) > 0) {
332 foreach ($data as $key => $val) {
337 if (!is_null($this->flags
)) {
338 $this->flags
= new PlFlagSet($this->flags
);
340 static $flags = array('current', 'temporary', 'secondary', 'mail', 'deliveryIssue');
342 $this->flags
= new PlFlagSet();
343 foreach ($flags as $flag) {
344 if (!is_null($this->$flag) && ($this->$flag == 1 ||
$this->$flag == 'on')) {
345 $this->flags
->addFlag($flag, 1);
348 $this->flags
->addFlag('cedex', (strpos(strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
349 array('', "\n"), $this->text
)), 'CEDEX')) !== false
);
354 public function setId($id)
359 public function phones()
361 return $this->phones
;
364 public function addPhone(Phone
&$phone)
366 if ($phone->linkType() == Phone
::LINK_ADDRESS
&& $phone->pid() == $this->pid
) {
367 $this->phones
[$phone->uniqueId()] = $phone;
371 public function hasFlag($flag)
373 return ($this->flags
!= null
&& $this->flags
->hasFlag($flag));
376 public function addFlag($flag)
378 $this->flags
->addFlag($flag);
381 /** Auxilary function for formatting postal addresses.
382 * If the needle is found in the haystack, it notifies the substitution's
383 * success, modifies the length accordingly and returns either the matching
384 * substitution or the needle.
386 private function substitute($needle, $haystack, &$length, &$success, $trim = false
)
388 if (array_key_exists($needle, $haystack)) {
390 $length -= (strlen($needle) - strlen($haystack[$needle]));
391 return $haystack[$needle];
394 if (strlen($needle) > 4) {
395 $length -= (strlen($needle) - 4);
396 $needle = $needle{4};
402 /** Checks if the line corresponds to a French street line.
403 * A line is considered a French street line if it starts by between 1 and 4 numbers.
405 private function isStreetFR($line)
407 return preg_match('/^\d{1,4}\D/', $line);
410 /** Retrieves a French street number and slit the rest of the line into an array.
411 * @param $words: array containing the rest of the line (a word per cell).
412 * @param $line: line to consider.
413 * Returns the street number.
415 private function getStreetNumberFR(&$line)
417 // First we define numbers and separators.
418 $numberReq = '(\d{1,4})\s*(BIS|TER|QUATER|[A-Z])?';
419 $separatorReq = '\s*(?:\\|-|&|A|ET)?\s*';
421 // Then we retrieve the number(s) and the rest of the line.
422 // $matches contains:
423 // -0: the full patern, here the given line,
425 // -2: its optionnal quantifier,
426 // -3: an optionnal second number,
427 // -4: the second number's optionnal quantifier,
428 // -5: the rest of the line.
429 preg_match('/^' . $numberReq . '(?:' . $separatorReq . $numberReq . ')?\s+(.*)/', $line, $matches);
430 $number = $matches[1];
433 // If there is a precision on the address, we concatenate it to the number.
434 if ($matches[2] != '') {
435 $number .= $matches[2]{0};
436 } elseif ($matches[4] != '') {
437 $number .= $matches[4]{0};
443 /** Checks if the line corresponds to a French locality line.
444 * A line is considered a French locality line if it starts by exactly a
445 * postal code of exactly 5 numbers.
447 private function isLocalityFR($line)
449 return preg_match('/^\d{5}\D/', $line);
452 /** Retrieves a French postal code and slit the rest of the line into an array.
453 * @param $words: array containing the rest of the line (a word per cell).
454 * @param $line: line to consider.
455 * Returns the postal code, and cuts it out from the line.
457 private function getPostalCodeFR(&$line)
459 $number = substr($line, 0, 5);
460 $line = trim(substr($line, 5));
464 /** Returns the address formated for French postal use (cf AFNOR XPZ 10-011).
465 * A postal addresse containts at most 6 lines of at most 38 characters each:
466 * - addressee's identification ("MONSIEUR JEAN DURAND", "DURAND SA"…),
467 * - delivery point identification ("CHEZ TOTO APPARTEMENT 2", "SERVICE ACHAT"…),
468 * - building localisation complement ("ENTREE A BATIMENT DES JONQUILLES", "ZONE INDUSTRIELLE OUEST"…),
469 * - N° and street name ("25 RUE DES FLEURS", "LES VIGNES"…),
470 * - delivery service, street localisation complement ("BP 40122", "BP 40112 AREYRES"…),
471 * - postal code and locality or cedex code and cedex ("33500 LIBOURNE", "33506 LIBOURNE CEDEX"…).
472 * Punctuation must be removed, all leters must be uppercased.
473 * Both locality and street name must not take more than 32 characters.
475 * @param $arrayText: array containing the address to be formated, one
476 * address line per array line.
477 * @param $count: array size.
479 private function formatPostalAddressFR($arrayText)
481 // First removes country if any.
482 $count = count($arrayText);
483 if ($arrayText[$count - 1] == 'FRANCE') {
484 unset($arrayText[$count - 1]);
488 // All the lines must have less than 38 characters but street and
489 // locality lines whose limit is 32 characters.
490 foreach ($arrayText as $lineNumber => $line) {
491 if ($isStreetLine = $this->isStreetFR($line)) {
492 $formattedLine = $this->getStreetNumberFR($line) . ' ';
494 } elseif ($this->isLocalityFR($line)) {
495 $formattedLine = $this->getPostalCodeFR($line) . ' ';
502 $words = explode(' ', $line);
503 $count = count($words);
504 $length = $count - 1;
505 foreach ($words as $word) {
506 $length +
= strlen($word);
509 // Checks is length is ok. Otherwise, we try to shorten words and
510 // update the length of the current line accordingly.
511 for ($i = 0; $i < $count && $length > $limit; ++
$i) {
514 $sub = $this->substitute($words[$i], Address
::$streetAbbreviations, $length, $success, ($i == 0));
516 // Entreprises' substitution are only suitable for the first two lines.
517 if ($lineNumber <= 2 && !$success) {
518 $sub = $this->substitute($words[$i], Address
::$entrepriseAbbreviations, $length, $success);
521 $sub = $this->substitute($words[$i], Address
::$otherAbbreviations, $length, $success);
524 $formattedLine .= $sub . ' ';
526 for (; $i < $count; ++
$i) {
527 $formattedLine .= $words[$i] . ' ';
529 $arrayText[$lineNumber] = trim($formattedLine);
532 return implode("\n", $arrayText);
535 // Formats postal addresses.
536 // First erases punctuation, accents… Then uppercase the address and finally
537 // calls the country's dedicated formatting function.
538 public function formatPostalAddress()
540 // Performs rough formatting.
541 $text = mb_strtoupper(replace_accent($this->text
));
542 $text = str_replace(array(',', ';', '.', ':', '!', '?', '"', '«', '»'), '', $text);
543 $text = preg_replace('/( |\t)+/', ' ', $text);
544 $arrayText = explode("\n", $text);
545 $arrayText = array_map('trim', $arrayText);
547 // Search for country.
548 $countries = DirEnum
::getOptions(DirEnum
::COUNTRIES
);
549 $countries = array_map('replace_accent', $countries);
550 $countries = array_map('strtoupper', $countries);
551 $count = count($arrayText);
552 if (in_array(strtoupper($this->countryId
), Address
::$formattings)) {
553 $text = call_user_func(array($this, 'formatPostalAddress' . strtoupper($this->countryId
)), $arrayText);
554 } elseif (array_key_exists($arrayText[$count - 1], Address
::$formattings)) {
555 $text = call_user_func(array($this, 'formatPostalAddress' . Address
::$formattings[$arrayText[$count - 1]]), $arrayText);
556 } elseif (!in_array($arrayText[$count - 1], $countries)) {
557 $text = $this->formatPostalAddressFR($arrayText);
559 $text = implode("\n", $arrayText);
562 $this->postalText
= $text;
565 public function format(array $format = array())
567 if (empty($format)) {
568 $format['requireGeocoding'] = false
;
569 $format['stripGeocoding'] = false
;
570 $format['postalText'] = false
;
572 foreach (array('requireGeocoding', 'stripGeocoding', 'postalText') as $type) {
573 $format[$type] = (isset($format[$type])) ?
$format[$type] : false
;
576 $this->text
= trim($this->text
);
577 if ($this->removed
== 1) {
582 if ($format['requireGeocoding'] ||
$this->changed
== 1) {
583 $gmapsGeocoder = new GMapsGeocoder();
584 $gmapsGeocoder->getGeocodedAddress($this);
586 $this->error
= !empty($this->geocodedText
);
588 if ($format['stripGeocoding'] ||
($this->type
== self
::LINK_COMPANY
&& $this->error
) ||
$this->geocodeChosen
=== '0') {
589 $gmapsGeocoder = new GMapsGeocoder();
590 $gmapsGeocoder->stripGeocodingFromAddress($this);
591 if ($this->geocodeChosen
=== '0') {
592 $mailer = new PlMailer('profile/geocoding.mail.tpl');
593 $mailer->assign('text', $this->text
);
594 $mailer->assign('geoloc', $this->geocodedText
);
598 if ($this->countryId
== '') {
599 $this->countryId
= null
;
601 $this->geocodeChosen
= null
;
602 $this->phones
= Phone
::formatFormArray($this->phones
, $this->error
, new ProfileVisibility($this->pub
));
603 if ($format['postalText']) {
604 $this->formatPostalAddress();
606 return !$this->error
;
609 public function toFormArray()
612 'accuracy' => $this->accuracy
,
613 'text' => $this->text
,
614 'postalText' => $this->postalText
,
615 'postalCode' => $this->postalCode
,
616 'localityId' => $this->localityId
,
617 'subAdministrativeAreaId' => $this->subAdministrativeAreaId
,
618 'administrativeAreaId' => $this->administrativeAreaId
,
619 'countryId' => $this->countryId
,
620 'localityName' => $this->localityName
,
621 'subAdministrativeAreaName' => $this->subAdministrativeAreaName
,
622 'administrativeAreaName' => $this->administrativeAreaName
,
623 'latitude' => $this->latitude
,
624 'longitude' => $this->longitude
,
625 'north' => $this->north
,
626 'south' => $this->south
,
627 'east' => $this->east
,
628 'west' => $this->west
,
629 'error' => $this->error
,
630 'changed' => $this->changed
,
631 'removed' => $this->removed
,
633 if (!is_null($this->geocodedText
)) {
634 $address['geocodedText'] = $this->geocodedText
;
635 $address['geocodeChosen'] = $this->geocodeChosen
;
638 if ($this->type
== self
::LINK_PROFILE ||
$this->type
== self
::LINK_JOB
) {
639 $address['pub'] = $this->pub
;
641 if ($this->type
== self
::LINK_PROFILE
) {
642 static $flags = array('current', 'temporary', 'secondary', 'mail', 'cedex', 'deliveryIssue');
644 foreach ($flags as $flag) {
645 $address[$flag] = $this->flags
->hasFlag($flag);
647 $address['comment'] = $this->comment
;
648 $address['phones'] = Phone
::formatFormArray($this->phones
);
654 private function toString()
656 $address = $this->text
;
657 if ($this->type
== self
::LINK_PROFILE ||
$this->type
== self
::LINK_JOB
) {
658 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
659 $address .= ' (affichage ' . $pubs[$this->pub
];
661 if ($this->type
== self
::LINK_PROFILE
) {
662 static $flags = array(
663 'current' => 'actuelle',
664 'temporary' => 'temporaire',
665 'secondary' => 'secondaire',
666 'mail' => 'conctactable par courier',
667 'deliveryIssue' => 'n\'habite pas à l\'adresse indiquée',
668 'cedex' => 'type cédex',
671 if (!$this->flags
->hasFlag('temporary')) {
672 $address .= ', permanente';
674 if (!$this->flags
->hasFlag('secondary')) {
675 $address .= ', principale';
677 foreach ($flags as $flag => $flagName) {
678 if ($this->flags
->hasFlag($flag)) {
679 $address .= ', ' . $flagName;
682 if ($this->comment
) {
683 $address .= ', commentaire : ' . $this->comment
;
685 if ($phones = Phone
::formArrayToString($this->phones
)) {
686 $address .= ', ' . $phones;
688 } elseif ($this->type
== self
::LINK_JOB
) {
694 private function isEmpty()
696 return (!$this->text ||
$this->text
== '');
699 public function save()
701 static $areas = array('administrativeArea', 'subAdministrativeArea', 'locality');
703 $this->format(array('postalText' => true
));
704 if (!$this->isEmpty()) {
705 foreach ($areas as $area) {
706 Geocoder
::getAreaId($this, $area);
709 XDB
::execute('INSERT INTO profile_addresses (pid, jobid, type, id, flags, accuracy,
710 text, postalText, postalCode, localityId,
711 subAdministrativeAreaId, administrativeAreaId,
712 countryId, latitude, longitude, pub, comment,
713 north, south, east, west)
714 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
715 {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
716 $this->pid
, $this->jobid
, $this->type
, $this->id
, $this->flags
, $this->accuracy
,
717 $this->text
, $this->postalText
, $this->postalCode
, $this->localityId
,
718 $this->subAdministrativeAreaId
, $this->administrativeAreaId
,
719 $this->countryId
, $this->latitude
, $this->longitude
,
720 $this->pub
, $this->comment
,
721 $this->north
, $this->south
, $this->east
, $this->west
);
723 if ($this->type
== self
::LINK_PROFILE
) {
724 Phone
::savePhones($this->phones
, $this->pid
, Phone
::LINK_ADDRESS
, $this->id
);
729 public function delete()
731 XDB
::execute('DELETE FROM profile_addresses
732 WHERE pid = {?} AND jobid = {?} AND type = {?} AND id = {?}',
733 $this->pid
, $this->jobid
, $this->type
, $this->id
);
736 static public function deleteAddresses($pid, $type, $jobid = null
, $deletePrivate = true
)
739 if (!is_null($pid)) {
740 $where = XDB
::format(' AND pid = {?}', $pid);
742 if (!is_null($jobid)) {
743 $where = XDB
::format(' AND jobid = {?}', $jobid);
745 XDB
::execute('DELETE FROM profile_addresses
746 WHERE type = {?}' . $where . (($deletePrivate) ?
'' : ' AND pub IN (\'public\', \'ax\')'),
748 if ($type == self
::LINK_PROFILE
) {
749 Phone
::deletePhones($pid, Phone
::LINK_ADDRESS
, null
, $deletePrivate);
753 /** Saves addresses into the database.
754 * @param $data: an array of form formatted addresses.
755 * @param $pid, $type, $linkid: pid, type and id concerned by the update.
757 static public function saveFromArray(array $data, $pid, $type = self
::LINK_PROFILE
, $linkid = null
)
759 foreach ($data as $id => $value) {
760 if (!is_null($linkid)) {
761 $value['id'] = $linkid;
765 if (!is_null($pid)) {
766 $value['pid'] = $pid;
768 if (!is_null($type)) {
769 $value['type'] = $type;
771 $address = new Address($value);
776 static private function formArrayWalk(array $data, $function, &$success = true
, $requiresEmptyAddress = false
)
778 $addresses = array();
779 foreach ($data as $item) {
780 $address = new Address($item);
781 $success = ($address->format() && $success);
782 if (!$address->isEmpty()) {
783 $addresses[] = call_user_func(array($address, $function));
786 if (count($address) == 0 && $requiresEmptyAddress) {
787 $address = new Address();
788 $addresses[] = call_user_func(array($address, $function));
793 // Formats an array of form addresses into an array of form formatted addresses.
794 static public function formatFormArray(array $data, &$success = true
)
796 // Only a single address can be the profile's current address and she must have one.
798 foreach ($data as $key => &$address) {
799 if (isset($address['current']) && $address['current']) {
801 $address['current'] = false
;
807 if (!$hasCurrent && count($value) > 0) {
808 foreach ($value as &$address) {
809 $address['current'] = true
;
814 return self
::formArrayWalk($data, 'toFormArray', $success, true
);
817 static public function formArrayToString(array $data)
819 return implode(', ', self
::formArrayWalk($data, 'toString'));
822 static public function iterate(array $pids = array(), array $types = array(),
823 array $jobids = array(), array $pubs = array())
825 return new AddressIterator($pids, $types, $jobids, $pubs);
829 /** Iterator over a set of Phones
831 * @param $pid, $type, $jobid, $pub
833 * The iterator contains the phones that correspond to the value stored in the
834 * parameters' arrays.
836 class AddressIterator
implements PlIterator
840 public function __construct(array $pids, array $types, array $jobids, array $pubs)
843 if (count($pids) != 0) {
844 $where[] = XDB
::format('(pa.pid IN {?})', $pids);
846 if (count($types) != 0) {
847 $where[] = XDB
::format('(pa.type IN {?})', $types);
849 if (count($jobids) != 0) {
850 $where[] = XDB
::format('(pa.jobid IN {?})', $jobids);
852 if (count($pubs) != 0) {
853 $where[] = XDB
::format('(pa.pub IN {?})', $pubs);
855 $sql = 'SELECT pa.pid, pa.jobid, pa.type, pa.id, pa.flags,
856 pa.accuracy, pa.text, pa.postalText, pa.postalCode,
857 pa.localityId, pa.subAdministrativeAreaId,
858 pa.administrativeAreaId, pa.countryId,
859 pa.latitude, pa.longitude, pa.north, pa.south, pa.east, pa.west,
861 gl.name AS locality, gs.name AS subAdministrativeArea,
862 ga.name AS administrativeArea, gc.countryFR AS country
863 FROM profile_addresses AS pa
864 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
865 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
866 LEFT JOIN geoloc_subadministrativeareas AS gs ON (gs.id = pa.subAdministrativeAreaId)
867 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
868 ' . ((count($where) > 0) ?
'WHERE ' . implode(' AND ', $where) : '') . '
869 ORDER BY pa.pid, pa.jobid, pa.id';
870 $this->dbiter
= XDB
::iterator($sql);
873 public function next()
875 if (is_null($this->dbiter
)) {
878 $data = $this->dbiter
->next();
879 if (is_null($data)) {
882 // Adds phones to addresses.
883 $it = Phone
::iterate(array($data['pid']), array(Phone
::LINK_ADDRESS
), array($data['id']));
884 while ($phone = $it->next()) {
885 $data['phones'][$phone->id()] = $phone->toFormArray();
887 return new Address($data);
890 public function total()
892 return $this->dbiter
->total();
895 public function first()
897 return $this->dbiter
->first();
900 public function last()
902 return $this->dbiter
->last();
905 public function value()
907 return $this->dbiter
;
911 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: