Fixes payment transactions addition
[platal.git] / classes / address.php
index 665d91d..8a2c44f 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2013 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();
@@ -423,7 +424,7 @@ class Address
     {
         // First we define numbers and separators.
         $numberReq = '(\d{1,4})\s*(BIS|TER|QUATER|[A-Z])?';
-        $separatorReq = '\s*(?:\\|-|&|A|ET)?\s*';
+        $separatorReq = '\s*(?:\\\\|\/|-|&|A|ET)?\s*';
 
         // Then we retrieve the number(s) and the rest of the line.
         // $matches contains:
@@ -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);
     }
 
@@ -625,14 +629,16 @@ class Address
             'request'             => $this->request
         );
 
-        if ($this->type == self::LINK_PROFILE || $this->type == self::LINK_JOB) {
-            $address['pub'] = $this->pub;
+        if ($this->type == self::LINK_JOB) {
+            $address['pub']  = $this->pub;
+            $address['mail'] = $this->flags->hasFlag('mail');
         }
         if ($this->type == self::LINK_PROFILE) {
             static $flags = array('current', 'temporary', 'secondary', 'mail', 'cedex', 'deliveryIssue');
             foreach ($flags as $flag) {
                 $address[$flag] = $this->flags->hasFlag($flag);
             }
+            $address['pub']     = $this->pub;
             $address['comment'] = $this->comment;
             $address['phones']  = Phone::formatFormArray($this->phones);
         }
@@ -685,18 +691,18 @@ class Address
         return (!$this->text || $this->text == '');
     }
 
-    public function save()
+    public function save($notify_ungeocoded = true)
     {
         if (!$this->isEmpty()) {
             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) {
@@ -704,7 +710,7 @@ class Address
                                               VALUES  ({?}, {?}, {?}, {?}, {?}, {?})',
                                  $this->pid, $this->jobid, $this->groupid, $this->type, $this->id, $component_id);
                 }
-            } else {
+            } elseif ($notify_ungeocoded) {
                 // If the address was not geocoded, notifies it to the appropriate ML.
                 $mailer = new PlMailer('profile/no_geocoding.mail.tpl');
                 $mailer->assign('text', $this->text);
@@ -720,6 +726,32 @@ 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) {
+                $this->updateAxMail();
+            }
+        }
+    }
+
+    private function updateAxMail()
+    {
+        XDB::execute("UPDATE  profile_addresses
+                         SET  flags = REPLACE(flags, 'ax_mail', '')
+                       WHERE  pid = {?}",
+                     $this->pid);
+
+        $ax_mail = XDB::fetchOneAssoc("SELECT  pid, jobid, groupid, type, id
+                                         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) {
+            XDB::execute("UPDATE  profile_addresses
+                             SET  flags = CONCAT(flags, ',ax_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']);
         }
     }
 
@@ -878,9 +910,9 @@ class Address
     }
 
     static public function iterate(array $pids = array(), array $types = array(),
-                                   array $jobids = array(), Visibility $visibility)
+                                   array $jobids = array(), $visibility = null, $where = null)
     {
-        return new AddressIterator($pids, $types, $jobids, $visibility);
+        return new AddressIterator($pids, $types, $jobids, $visibility, $where);
     }
 }
 
@@ -894,10 +926,14 @@ class Address
 class AddressIterator implements PlIterator
 {
     private $dbiter;
+    private $visibility;
 
-    public function __construct(array $pids, array $types, array $jobids, Visibility $visibility)
+    public function __construct(array $pids, array $types, array $jobids, $visibility, $_where)
     {
         $where = array();
+        if (!is_null($_where)) {
+            $where[] = $_where;
+        }
         if (count($pids) != 0) {
             $where[] = XDB::format('(pa.pid IN {?})', $pids);
         }
@@ -907,7 +943,7 @@ class AddressIterator implements PlIterator
         if (count($jobids) != 0) {
             $where[] = XDB::format('(pa.jobid IN {?})', $jobids);
         }
-        if ($visibility == null) {
+        if ($visibility == null || !($visibility instanceof Visibility)) {
             $visibility = Visibility::defaultForRead();
         }
         $where[] = 'pve.best_display_level+0 <= pa.pub+0';
@@ -931,6 +967,7 @@ class AddressIterator implements PlIterator
               GROUP BY  pa.pid, pa.jobid, pa.groupid, pa.type, pa.id
               ORDER BY  pa.pid, pa.jobid, pa.id';
         $this->dbiter = XDB::iterator($sql, $visibility->level());
+        $this->visibility = $visibility;
     }
 
     public function next()
@@ -943,7 +980,7 @@ class AddressIterator implements PlIterator
             return null;
         }
         // Adds phones to addresses.
-        $it = Phone::iterate(array($data['pid']), array(Phone::LINK_ADDRESS), array($data['id']));
+        $it = Phone::iterate(array($data['pid']), array(Phone::LINK_ADDRESS), array($data['id']), $this->visibility);
         while ($phone = $it->next()) {
             $data['phones'][$phone->id] = $phone->toFormArray();
         }