X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Faddress.php;h=fc438e508239982507aff914c75f1e8f77c44b6d;hb=977168f6915e290cf8d9351651b9d8a0c908ac70;hp=344d5f2438852a1d51ac3b9fd9a60c821e1a5389;hpb=f5f68e07124c5d508e9c1345696cc9cdcc795231;p=platal.git diff --git a/classes/address.php b/classes/address.php index 344d5f2..fc438e5 100644 --- a/classes/address.php +++ b/classes/address.php @@ -1,6 +1,6 @@ 'FR'); @@ -284,6 +291,7 @@ class Address // Primary key fields: the quadruplet ($pid, $jobid, $type, $id) defines a unique address. public $pid = 0; public $jobid = 0; + public $groupid = 0; public $type = Address::LINK_PROFILE; public $id = 0; @@ -298,6 +306,9 @@ class Address public $localityName = null; public $subAdministrativeAreaName = null; public $administrativeAreaName = null; + public $localityNameLocal = null; + public $subAdministrativeAreaNameLocal = null; + public $administrativeAreaNameLocal = null; public $countryId = null; public $latitude = null; public $longitude = null; @@ -361,9 +372,9 @@ class Address return $this->phones; } - public function addPhone(Phone &$phone) + public function addPhone(Phone $phone) { - if ($phone->linkType() == Phone::LINK_ADDRESS && $phone->pid() == $this->pid) { + if ($phone->link_type == Phone::LINK_ADDRESS && $phone->pid == $this->pid) { $this->phones[$phone->uniqueId()] = $phone; } } @@ -373,17 +384,28 @@ class Address return ($this->flags != null && $this->flags->hasFlag($flag)); } + public function addFlag($flag) + { + $this->flags->addFlag($flag); + } + /** Auxilary function for formatting postal addresses. * If the needle is found in the haystack, it notifies the substitution's * success, modifies the length accordingly and returns either the matching * substitution or the needle. */ - private function substitute($needle, $haystack, &$length, &$success) + private function substitute($needle, $haystack, &$length, &$success, $trim = false) { if (array_key_exists($needle, $haystack)) { $success = true; $length -= (strlen($needle) - strlen($haystack[$needle])); return $haystack[$needle]; + } elseif ($trim) { + $success = true; + if (strlen($needle) > 4) { + $length -= (strlen($needle) - 4); + $needle = $needle{4}; + } } return $needle; } @@ -405,18 +427,25 @@ class Address { // First we define numbers and separators. $numberReq = '(\d{1,4})\s*(BIS|TER|QUATER|[A-Z])?'; - $separatorReq = '\s*(?:\\|-|&|A|ET)\s*'; - - // Then we retrieve the number(s) and the reste of the line. - preg_match('/^' . $numberReq . '(?:' . $separatorReq . $numberReq . ')?\s*(.*)$', $matches); + $separatorReq = '\s*(?:\\|-|&|A|ET)?\s*'; + + // Then we retrieve the number(s) and the rest of the line. + // $matches contains: + // -0: the full patern, here the given line, + // -1: the number, + // -2: its optionnal quantifier, + // -3: an optionnal second number, + // -4: the second number's optionnal quantifier, + // -5: the rest of the line. + preg_match('/^' . $numberReq . '(?:' . $separatorReq . $numberReq . ')?\s+(.*)/', $line, $matches); $number = $matches[1]; $line = $matches[5]; // If there is a precision on the address, we concatenate it to the number. if ($matches[2] != '') { - $number .= $matches[2]{1}; + $number .= $matches[2]{0}; } elseif ($matches[4] != '') { - $number .= $matches[4]{1}; + $number .= $matches[4]{0}; } return $number; @@ -458,9 +487,10 @@ class Address * address line per array line. * @param $count: array size. */ - private function formatPostalAddressFR($arrayText, $count) + private function formatPostalAddressFR($arrayText) { // First removes country if any. + $count = count($arrayText); if ($arrayText[$count - 1] == 'FRANCE') { unset($arrayText[$count - 1]); --$count; @@ -492,7 +522,7 @@ class Address for ($i = 0; $i < $count && $length > $limit; ++$i) { $success = false; if ($isStreetLine) { - $sub = $this->substitute($words[$i], Address::$streetAbbreviations, $length, $success); + $sub = $this->substitute($words[$i], Address::$streetAbbreviations, $length, $success, ($i == 0)); } // Entreprises' substitution are only suitable for the first two lines. if ($lineNumber <= 2 && !$success) { @@ -525,19 +555,26 @@ class Address $arrayText = explode("\n", $text); $arrayText = array_map('trim', $arrayText); - // Search for country. - $countries = DirEnum::getOptions(DirEnum::COUNTRIES); - $countries = array_map('replace_accent', $countries); - $countries = array_map('strtoupper', $countries); + // Formats according to country rules. Thus we first identify the + // country, then apply corresponding formatting or translate country + // into default language. $count = count($arrayText); - if (in_array(strtoupper($address->country), Address::$formattings)) { - $text = call_user_func(array($this, 'formatPostalAddress' . Address::$formattings[strtoupper($address->country)]), $arrayText, $count); - } elseif (array_key_exists($arrayText[$count - 1], Address::$formattings)) { - $text = call_user_func(array($this, 'formatPostalAddress' . Address::$formattings[$arrayText[$count - 1]]), $arrayText, $count); - } elseif (!in_array($arrayText[$count - 1], $countries)) { - $text = $this->formatPostalAddressFR($arrayText, $count); + if (in_array(strtoupper($this->countryId), Address::$formattings)) { + $text = call_user_func(array($this, 'formatPostalAddress' . strtoupper($this->countryId)), $arrayText); } else { - $text = implode("\n", $arrayText); + list($countryId, $country) = XDB::fetchOneRow('SELECT gc.iso_3166_1_a2, gc.country + FROM geoloc_countries AS gc + INNER JOIN geoloc_languages AS gl ON (gc.iso_3166_1_a2 = gl.iso_3166_1_a2) + WHERE gc.iso_3166_1_a2 = {?} OR gl.countryPlain = {?} OR gc.countryPlain = {?}', + $this->countryId, $arrayText[$count - 1], $arrayText[$count - 1]); + if (is_null($countryId)) { + $text = $this->formatPostalAddressFR($arrayText); + } elseif (in_array(strtoupper($countryId), Address::$formattings)) { + $text = call_user_func(array($this, 'formatPostalAddress' . strtoupper($countryId)), $arrayText); + } else { + $arrayText[$count - 1] = mb_strtoupper(replace_accent($country)); + $text = implode("\n", $arrayText); + } } $this->postalText = $text; @@ -555,9 +592,14 @@ class Address } } $this->text = trim($this->text); + $this->phones = Phone::formatFormArray($this->phones, $this->error, new ProfileVisibility($this->pub)); if ($this->removed == 1) { - $this->text = ''; - return true; + if (!S::user()->checkPerms('directory_private') && Phone::hasPrivate($this->phones)) { + Platal::page()->trigWarning("L'adresse ne peut être supprimée car elle contient des informations pour lesquelles vous n'avez le droit d'édition."); + } else { + $this->text = ''; + return true; + } } if ($format['requireGeocoding'] || $this->changed == 1) { @@ -567,20 +609,19 @@ class Address $this->error = !empty($this->geocodedText); } if ($format['stripGeocoding'] || ($this->type == self::LINK_COMPANY && $this->error) || $this->geocodeChosen === '0') { - $gmapsGeocoder = new GMapsGeocoder(); - $gmapsGeocoder->stripGeocodingFromAddress($this); if ($this->geocodeChosen === '0') { $mailer = new PlMailer('profile/geocoding.mail.tpl'); $mailer->assign('text', $this->text); $mailer->assign('geoloc', $this->geocodedText); $mailer->send(); } + $gmapsGeocoder = new GMapsGeocoder(); + $gmapsGeocoder->stripGeocodingFromAddress($this); } if ($this->countryId == '') { $this->countryId = null; } $this->geocodeChosen = null; - $this->phones = Phone::formatFormArray($this->phones, $this->error); if ($format['postalText']) { $this->formatPostalAddress(); } @@ -590,26 +631,29 @@ class Address public function toFormArray() { $address = array( - 'accuracy' => $this->accuracy, - 'text' => $this->text, - 'postalText' => $this->postalText, - 'postalCode' => $this->postalCode, - 'localityId' => $this->localityId, - 'subAdministrativeAreaId' => $this->subAdministrativeAreaId, - 'administrativeAreaId' => $this->administrativeAreaId, - 'countryId' => $this->countryId, - 'localityName' => $this->localityName, - 'subAdministrativeAreaName' => $this->subAdministrativeAreaName, - 'administrativeAreaName' => $this->administrativeAreaName, - 'latitude' => $this->latitude, - 'longitude' => $this->longitude, - 'north' => $this->north, - 'south' => $this->south, - 'east' => $this->east, - 'west' => $this->west, - 'error' => $this->error, - 'changed' => $this->changed, - 'removed' => $this->removed, + 'accuracy' => $this->accuracy, + 'text' => $this->text, + 'postalText' => $this->postalText, + 'postalCode' => $this->postalCode, + 'localityId' => $this->localityId, + 'subAdministrativeAreaId' => $this->subAdministrativeAreaId, + 'administrativeAreaId' => $this->administrativeAreaId, + 'countryId' => $this->countryId, + 'localityName' => $this->localityName, + 'subAdministrativeAreaName' => $this->subAdministrativeAreaName, + 'administrativeAreaName' => $this->administrativeAreaName, + 'localityNameLocal' => $this->localityNameLocal, + 'subAdministrativeAreaNameLocal' => $this->subAdministrativeAreaNameLocal, + 'administrativeAreaNameLocal' => $this->administrativeAreaNameLocal, + 'latitude' => $this->latitude, + 'longitude' => $this->longitude, + 'north' => $this->north, + 'south' => $this->south, + 'east' => $this->east, + 'west' => $this->west, + 'error' => $this->error, + 'changed' => $this->changed, + 'removed' => $this->removed, ); if (!is_null($this->geocodedText)) { $address['geocodedText'] = $this->geocodedText; @@ -634,9 +678,10 @@ class Address private function toString() { - $address = 'Adresse : ' . $this->text; + $address = $this->text; if ($this->type == self::LINK_PROFILE || $this->type == self::LINK_JOB) { - $address .= ', affichage : ' . $this->pub; + static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé'); + $address .= ' (affichage ' . $pubs[$this->pub]; } if ($this->type == self::LINK_PROFILE) { static $flags = array( @@ -648,15 +693,25 @@ class Address 'cedex' => 'type cédex', ); - $address .= ', commentaire : ' . $this->comment; + if (!$this->flags->hasFlag('temporary')) { + $address .= ', permanente'; + } + if (!$this->flags->hasFlag('secondary')) { + $address .= ', principale'; + } foreach ($flags as $flag => $flagName) { if ($this->flags->hasFlag($flag)) { $address .= ', ' . $flagName; } } + if ($this->comment) { + $address .= ', commentaire : ' . $this->comment; + } if ($phones = Phone::formArrayToString($this->phones)) { $address .= ', ' . $phones; } + } elseif ($this->type == self::LINK_JOB) { + $address .= ')'; } return $address; } @@ -676,14 +731,14 @@ class Address Geocoder::getAreaId($this, $area); } - XDB::execute('INSERT INTO profile_addresses (pid, jobid, type, id, flags, accuracy, - text, postalText, postalCode, localityId, - subAdministrativeAreaId, administrativeAreaId, - countryId, latitude, longitude, pub, comment, - north, south, east, west) - VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, - {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})', - $this->pid, $this->jobid, $this->type, $this->id, $this->flags, $this->accuracy, + XDB::execute('INSERT IGNORE INTO profile_addresses (pid, jobid, groupid, type, id, flags, accuracy, + text, postalText, postalCode, localityId, + subAdministrativeAreaId, administrativeAreaId, + countryId, latitude, longitude, pub, comment, + north, south, east, west) + VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, + {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})', + $this->pid, $this->jobid, $this->groupid, $this->type, $this->id, $this->flags, $this->accuracy, $this->text, $this->postalText, $this->postalCode, $this->localityId, $this->subAdministrativeAreaId, $this->administrativeAreaId, $this->countryId, $this->latitude, $this->longitude, @@ -699,11 +754,11 @@ class Address public function delete() { XDB::execute('DELETE FROM profile_addresses - WHERE pid = {?} AND jobid = {?} AND type = {?} AND id = {?}', - $this->pid, $this->jobid, $this->type, $this->id); + WHERE pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?} AND id = {?}', + $this->pid, $this->jobid, $this->groupid, $this->type, $this->id); } - static public function deleteAddresses($pid, $type, $jobid = null) + static public function deleteAddresses($pid, $type, $jobid = null, $groupid = null, $deletePrivate = true) { $where = ''; if (!is_null($pid)) { @@ -712,11 +767,14 @@ class Address if (!is_null($jobid)) { $where = XDB::format(' AND jobid = {?}', $jobid); } + if (!is_null($groupid)) { + $where = XDB::format(' AND groupid = {?}', $groupid); + } XDB::execute('DELETE FROM profile_addresses - WHERE type = {?}' . $where, + WHERE type = {?}' . $where . (($deletePrivate) ? '' : ' AND pub IN (\'public\', \'ax\')'), $type); if ($type == self::LINK_PROFILE) { - Phone::deletePhones($pid, Phone::LINK_ADDRESS); + Phone::deletePhones($pid, Phone::LINK_ADDRESS, null, $deletePrivate); } } @@ -724,22 +782,24 @@ class Address * @param $data: an array of form formatted addresses. * @param $pid, $type, $linkid: pid, type and id concerned by the update. */ - static public function saveFromArray(array $data, $pid, $type = self::LINK_PROFILE, $linkid = null) + static public function saveFromArray(array $data, $pid, $type = self::LINK_PROFILE, $linkid = null, $savePrivate = true) { foreach ($data as $id => $value) { - if (!is_null($linkid)) { - $value['id'] = $linkid; - } else { - $value['id'] = $id; - } - if (!is_null($pid)) { - $value['pid'] = $pid; - } - if (!is_null($type)) { - $value['type'] = $type; + if ($value['pub'] != 'private' || $savePrivate) { + if (!is_null($linkid)) { + $value['id'] = $linkid; + } else { + $value['id'] = $id; + } + if (!is_null($pid)) { + $value['pid'] = $pid; + } + if (!is_null($type)) { + $value['type'] = $type; + } + $address = new Address($value); + $address->save(); } - $address = new Address($value); - $address->save(); } } @@ -760,12 +820,27 @@ class Address return $addresses; } + // Compares two addresses. First sort by publicity, then place primary + // addresses before secondary addresses. + static private function compare(array $a, array $b) + { + $value = ProfileVisibility::comparePublicity($a, $b); + if ($value == 0) { + if ($a['secondary'] != $b['secondary']) { + $value = $a['secondary'] ? 1 : -1; + } + } + return $value; + } + // Formats an array of form addresses into an array of form formatted addresses. static public function formatFormArray(array $data, &$success = true) { + $addresses = self::formArrayWalk($data, 'toFormArray', $success, true); + // Only a single address can be the profile's current address and she must have one. $hasCurrent = false; - foreach ($data as $key => &$address) { + foreach ($addresses as $key => &$address) { if (isset($address['current']) && $address['current']) { if ($hasCurrent) { $address['current'] = false; @@ -781,12 +856,23 @@ class Address } } - return self::formArrayWalk($data, 'toFormArray', $success, true); + usort($addresses, 'Address::compare'); + return $addresses; } static public function formArrayToString(array $data) { - return implode(' ; ', self::formArrayWalk($data, 'toString')); + return implode(', ', self::formArrayWalk($data, 'toString')); + } + + static public function hasPrivate(array $addresses) + { + foreach ($addresses as $address) { + if ($address['pub'] == 'private') { + return true; + } + } + return false; } static public function iterate(array $pids = array(), array $types = array(), @@ -828,8 +914,10 @@ class AddressIterator implements PlIterator pa.administrativeAreaId, pa.countryId, pa.latitude, pa.longitude, pa.north, pa.south, pa.east, pa.west, pa.pub, pa.comment, - gl.name AS locality, gs.name AS subAdministrativeArea, - ga.name AS administrativeArea, gc.countryFR AS country + gl.name AS locality, gl.nameLocal AS localityLocal, + gs.name AS subAdministrativeArea, gs.nameLocal AS subAdministrativeAreaLocal, + ga.name AS administrativeArea, ga.nameLocal AS administrativeAreaLocal, + gc.country FROM profile_addresses AS pa LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId) LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId) @@ -852,7 +940,7 @@ class AddressIterator implements PlIterator // Adds phones to addresses. $it = Phone::iterate(array($data['pid']), array(Phone::LINK_ADDRESS), array($data['id'])); while ($phone = $it->next()) { - $data['phones'][$phone->id()] = $phone->toFormArray(); + $data['phones'][$phone->id] = $phone->toFormArray(); } return new Address($data); }