Add 'hidden' flag to text descriptions of the 'pub' flag
[platal.git] / classes / address.php
index 9783018..cffa45b 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -298,6 +298,7 @@ class Address
     // Geocoding fields.
     public $text = '';
     public $postalText = '';
+    public $postal_code_fr = null;
     public $types = '';
     public $formatted_address = '';
     public $components = array();
@@ -492,6 +493,7 @@ class Address
             --$count;
         }
 
+        $postal_code = null;
         // All the lines must have less than 38 characters but street and
         // locality lines whose limit is 32 characters.
         foreach ($arrayText as $lineNumber => $line) {
@@ -499,7 +501,8 @@ class Address
                 $formattedLine = $this->getStreetNumberFR($line) . ' ';
                 $limit = 32;
             } elseif ($this->isLocalityFR($line)) {
-                $formattedLine = $this->getPostalCodeFR($line) . ' ';
+                $postal_code = $this->getPostalCodeFR($line);
+                $formattedLine = $postal_code . ' ';
                 $limit = 32;
             } else {
                 $formattedLine = '';
@@ -536,6 +539,7 @@ class Address
             $arrayText[$lineNumber] = trim($formattedLine);
         }
 
+        $this->postal_code_fr = $postal_code;
         return implode("\n", $arrayText);
     }
 
@@ -646,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) {
@@ -693,12 +697,12 @@ class Address
             XDB::execute('INSERT IGNORE INTO  profile_addresses (pid, jobid, groupid, type, id, flags, text, postalText, pub, comment,
                                                                  types, formatted_address, location_type, partial_match, latitude, longitude,
                                                                  southwest_latitude, southwest_longitude, northeast_latitude, northeast_longitude,
-                                                                 geocoding_date, geocoding_calls)
+                                                                 geocoding_date, geocoding_calls, postal_code_fr)
                                       VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
-                                               {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, NOW(), {?})',
+                                               {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, NOW(), {?}, {?})',
                          $this->pid, $this->jobid, $this->groupid, $this->type, $this->id, $this->flags, $this->text, $this->postalText, $this->pub, $this->comment,
                          $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->southwest_latitude, $this->southwest_longitude, $this->northeast_latitude, $this->northeast_longitude, $this->geocoding_calls, $this->postal_code_fr);
 
             if ($this->componentsIds) {
                 foreach (explode(',', $this->componentsIds) as $component_id) {
@@ -722,7 +726,54 @@ class Address
                 $req = new AddressReq(S::user(), $this->toFormArray(), $this->pid, $this->jobid, $this->groupid, $this->type, $this->id);
                 $req->submit();
             }
+
+            if ($this->pid != 0) {
+                self::updateBestMail($this->pid);
+            }
+        }
+    }
+
+    /**
+     * 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)
+    {
+        if (!$fake) {
+            XDB::execute("UPDATE  profile_addresses
+                             SET  flags = REPLACE(flags, 'dn_best_mail', '')
+                           WHERE  pid = {?}",
+                         $pid);
+        }
+
+        /* 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  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, ',dn_best_mail')
+                           WHERE  pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?} AND id = {?}",
+                         $best_mail['pid'], $best_mail['jobid'], $best_mail['groupid'], $best_mail['type'], $best_mail['id']);
         }
+        return $best_mail;
     }
 
     public function updateGeocoding($text)
@@ -886,12 +937,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
 {
@@ -978,5 +1029,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:
 ?>