X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Faddress.php;h=62e7350e3bdc803e411243c2df2675dd2e721bcd;hb=1aff3b59c0f46dafe3396c1123a252645805e233;hp=ba4f9bd1d6fc6314c85998786693fbfbd4b181bb;hpb=6e1128e02f7568a692d548d8bc43cad6bde38efe;p=platal.git diff --git a/classes/address.php b/classes/address.php index ba4f9bd..62e7350 100644 --- a/classes/address.php +++ b/classes/address.php @@ -650,7 +650,7 @@ class Address { $address = $this->text; if ($this->type == self::LINK_PROFILE || $this->type == self::LINK_JOB) { - static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé'); + static $pubs = array('public' => 'publique', 'ax' => 'annuaire papier', 'private' => 'privé', 'hidden' => 'administrateurs'); $address .= ' (affichage ' . $pubs[$this->pub]; } if ($this->type == self::LINK_PROFILE) { @@ -704,6 +704,11 @@ class Address $this->types, $this->formatted_address, $this->location_type, $this->partial_match, $this->latitude, $this->longitude, $this->southwest_latitude, $this->southwest_longitude, $this->northeast_latitude, $this->northeast_longitude, $this->geocoding_calls, $this->postal_code_fr); + // In an ideal world there would not be any components to clean up before insertion. + // In real world, it happens that some addresses are badly removed and this query removes the leaked trash. + XDB::execute('DELETE FROM profile_addresses_components + WHERE pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?} AND id = {?}', + $this->pid, $this->jobid, $this->groupid, $this->type, $this->id); if ($this->componentsIds) { foreach (explode(',', $this->componentsIds) as $component_id) { XDB::execute('INSERT IGNORE INTO profile_addresses_components (pid, jobid, groupid, type, id, component_id) @@ -728,34 +733,55 @@ class Address } if ($this->pid != 0) { - $this->updateAxMail(); + self::updateBestMail($this->pid); } } } - private function updateAxMail() + /** + * Upate the denormalized flag which is used to mark the best mail to use + * when sending postal mail + * + * Call with $fake to true to only get which address would be selected, + * without updating anything in the database. + * Returns an array describing the selected profile address + */ + static public function updateBestMail($pid, $fake=false) { - XDB::execute("UPDATE profile_addresses - SET flags = REPLACE(flags, 'ax_mail', '') - WHERE pid = {?}", - $this->pid); + if (!$fake) { + XDB::execute("UPDATE profile_addresses + SET flags = REPLACE(flags, 'dn_best_mail', '') + WHERE pid = {?}", + $pid); + } - $ax_mail = XDB::fetchOneAssoc("SELECT pid, jobid, groupid, type, id + /* Following order is selected to find the best mail: + * * Use addresses without the deliveryIssue flag if possible. + * * Among these, use addresses flagged as "current". + * * If there is no such addresses or several ones, prefer those + * without "secondary" flag. + * * If there are still several addresses in the selection, try not + * to select the ones with "job" type. + */ + $best_mail = XDB::fetchOneAssoc("SELECT pid, jobid, groupid, type, id, flags FROM profile_addresses - WHERE pub IN ('public', 'ax') AND FIND_IN_SET('mail', flags) AND pid = {?} - ORDER BY NOT FIND_IN_SET('current', flags), - FIND_IN_SET('secondary', flags), type = 'job'", - $this->pid); - - if ($ax_mail) { + WHERE FIND_IN_SET('mail', flags) AND pid = {?} + ORDER BY FIND_IN_SET('deliveryIssue', flags), + NOT FIND_IN_SET('current', flags), + FIND_IN_SET('secondary', flags), type = 'job' + LIMIT 1", + $pid); + + if (!$fake && $best_mail) { XDB::execute("UPDATE profile_addresses - SET flags = CONCAT(flags, ',ax_mail') + SET flags = CONCAT(flags, ',dn_best_mail') WHERE pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?} AND id = {?}", - $ax_mail['pid'], $ax_mail['jobid'], $ax_mail['groupid'], $ax_mail['type'], $ax_mail['id']); + $best_mail['pid'], $best_mail['jobid'], $best_mail['groupid'], $best_mail['type'], $best_mail['id']); } + return $best_mail; } - public function updateGeocoding($text) + public function updateGeocoding() { XDB::execute('UPDATE profile_addresses SET text = {?}, postalText = {?}, types = {?}, formatted_address = {?}, @@ -796,16 +822,38 @@ class Address { $where = ''; if (!is_null($pid)) { - $where = XDB::format(' AND pid = {?}', $pid); + $where .= XDB::format(' AND pid = {?}', $pid); } if (!is_null($jobid)) { - $where = XDB::format(' AND jobid = {?}', $jobid); + $where .= XDB::format(' AND jobid = {?}', $jobid); } if (!is_null($groupid)) { - $where = XDB::format(' AND groupid = {?}', $groupid); + $where .= XDB::format(' AND groupid = {?}', $groupid); + } + // Prevent buggy callers from accidentally dropping profile_addresses + if (!$where) { + throw new Exception("Unable to delete all addresses of the given type, the request was too generic"); + } + $where_pub = $where . (($deletePrivate) ? '' : ' AND pub IN (\'public\', \'ax\')'); + + if ($deletePrivate) { + XDB::execute('DELETE FROM profile_addresses_components + WHERE type = {?}' . $where, + $type); + } else { + // Delete address components one by one if $deletePrivate is not requested + $res = XDB::iterator('SELECT pid, jobid, groupid, type, id + FROM profile_addresses + WHERE type = {?}' . $where_pub, + $type); + while (($values = $res->next())) { + XDB::execute('DELETE FROM profile_addresses_components + WHERE pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?} AND id = {?}', + $values['pid'], $values['jobid'], $values['groupid'], $values['type'], $values['id']); + } } XDB::execute('DELETE FROM profile_addresses - WHERE type = {?}' . $where . (($deletePrivate) ? '' : ' AND pub IN (\'public\', \'ax\')'), + WHERE type = {?}' . $where_pub, $type); if ($type == self::LINK_PROFILE) { Phone::deletePhones($pid, Phone::LINK_ADDRESS, null, $deletePrivate); @@ -916,12 +964,12 @@ class Address } } -/** Iterator over a set of Phones +/** Iterator over a set of Addresses * * @param $pid, $type, $jobid, $pub * - * The iterator contains the phones that correspond to the value stored in the - * parameters' arrays. + * The iterator contains the addresses that correspond to the value stored in + * the parameters' arrays. */ class AddressIterator implements PlIterator { @@ -1008,5 +1056,5 @@ class AddressIterator implements PlIterator } } -// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: ?>