Merge commit 'origin/master' into fusionax
[platal.git] / modules / profile / addresses.inc.php
index e78a5a0..7317c5d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -19,7 +19,7 @@
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-class ProfileAddress
+class ProfileAddress extends ProfileGeoloc
 {
     private $bool;
     private $pub;
@@ -30,53 +30,23 @@ class ProfileAddress
         $this->pub  = new ProfilePub();
     }
 
-    private function geolocAddress(array &$address, &$success)
+    private function cleanAddress(ProfilePage &$page, $adrid, array &$address, &$success)
     {
-        require_once 'geoloc.inc.php';
-        $success = true;
-        unset($address['geoloc']);
-        unset($address['geoloc_cityid']);
-        if (@$address['parsevalid'] || (@$address['text'] && @$address['changed']) || !@$address['cityid']) {
-            $address = array_merge($address, empty_address());
-            $new = get_address_infos(@$address['text']);
-            if (compare_addresses_text(@$adress['text'], $geotxt = get_address_text($new))
-                || @$address['parsevalid']) {
-                $address = array_merge($address, $new);
-            } else {
-                $success = false;
-                $address = array_merge($address, cut_address(@$address['text']));
-                $address['geoloc'] = $geotxt;
-                $address['geoloc_cityid'] = $new['cityid'];
-            }
-        }
-        if (@$address['changed']) {
-            $address['datemaj'] = time();
-        }
-        $address['text'] = get_address_text($address);
-    }
-
-    private function cleanAddress(ProfilePage &$page, array &$address)
-    {
-        if (@$address['changed']) {
-            $address['datemaj'] = time();
-        }
-        foreach ($address['tel'] as $t=>&$tel) {
-            if (@$tel['removed'] || !trim($tel['tel'])) {
-                unset($address['tel'][$t]);
-            } else {
-                $tel['pub'] = $this->pub->value($page, 'pub', $tel['pub'], $success);
-            }
-            unset($tel['removed']);
-        }
         if (@$address['changed']) {
             $address['datemaj'] = time();
         }
         $success = true;
-        $address['secondaire'] = $this->bool->value($page, 'secondaire', $address['secondaire'], $success);
-        $address['mail'] = $this->bool->value($page, 'mail', $address['mail'], $success);
-        $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $success);
-        $address['current'] = $this->bool->value($page, 'current', @$address['current'], $success);
-        $address['pub'] = $this->pub->value($page, 'pub', $address['pub'], $success);
+        if (!isset($address['tel'])) {
+            $address['tel'] = array();
+        }
+        $profiletel  = new ProfilePhones('address', $adrid);
+        $address['tel'] = $profiletel->value($page, 'tel', $address['tel'], $s);
+        $address['checked'] = $this->bool->value($page, 'checked', $address['checked'], $s);
+        $address['secondaire'] = $this->bool->value($page, 'secondaire', $address['secondaire'], $s);
+        $address['mail'] = $this->bool->value($page, 'mail', $address['mail'], $s);
+        $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $s);
+        $address['current'] = $this->bool->value($page, 'current', @$address['current'], $s);
+        $address['pub'] = $this->pub->value($page, 'pub', $address['pub'], $s);
         unset($address['parsevalid']);
         unset($address['changed']);
         unset($address['removed']);
@@ -96,24 +66,30 @@ class ProfileAddress
             }
         }
         $current = 0;
+        $success = true;
         foreach ($value as $key=>&$adr) {
             if (@$adr['current']) {
                 $current++;
             }
         }
-        if (!$init && $current != 1 && count($value) > 0) {
+        if ($current == 0 && count($value) > 0) {
+            foreach ($value as $key=>&$adr) {
+                $adr['current'] = true;
+                break;
+            }
+        } else if ($current > 1) {
             $success = false;
-        } else {
-            $success = true;
         }
         foreach ($value as $key=>&$adr) {
+            $ls = true;
             $this->geolocAddress($adr, $s);
-            $this->cleanAddress($page, $adr);
-            if (!$init) {
-                $success = $success && $s;
-            }
+            $ls = ($ls && $s);
+            $this->cleanAddress($page, $key, $adr, $s);
+            $ls = ($ls && $s);
             if (!trim($adr['text'])) {
                 unset($value[$key]);
+            } else if (!$init) {
+                $success = ($success && $ls);
             }
         }
         return $value;
@@ -121,48 +97,47 @@ class ProfileAddress
 
     private function saveTel($adrid, $telid, array &$tel)
     {
-        XDB::execute("INSERT INTO  tels (uid, adrid, telid,
-                                         tel_type, tel_pub, tel)
-                           VALUES  ({?}, {?}, {?},
+        XDB::execute("INSERT INTO  profile_phones (uid, link_type, link_id, tel_id, tel_type,
+                                              search_tel, display_tel, pub)
+                           VALUES  ({?}, 'address', {?}, {?}, {?},
                                     {?}, {?}, {?})",
-                    S::i('uid'), $adrid, $telid,
-                    $tel['type'], $tel['pub'], $tel['tel']);
+                    S::i('uid'), $adrid, $telid, $tel['type'],
+                    format_phone_number($tel['tel']), $tel['tel'], $tel['pub']);
     }
 
     private function saveAddress($adrid, array &$address)
     {
-        $flags = array();
+        $flags = new PlFlagSet();
         if ($address['secondaire']) {
-            $flags[] = 'res-secondaire';
+            $flags->addFlag('res-secondaire');
         }
         if ($address['mail']) {
-            $flags[] = 'courrier';
+            $flags->addFlag('courrier');
         }
         if ($address['temporary']) {
-            $flags[] = 'temporaire';
+            $flags->addFlag('temporaire');
         }
         if ($address['current']) {
-            $flags[] = 'active';
+            $flags->addFlag('active');
+        }
+        if ($address['checked']) {
+            $flags->addFlag('coord-checked');
         }
-        $flags = implode(',', $flags);
         XDB::execute("INSERT INTO  adresses (adr1, adr2, adr3,
                                               postcode, city, cityid,
                                               country, region, regiontxt,
                                               pub, datemaj, statut,
-                                              uid, adrid)
+                                              uid, adrid, glat, glng, comment)
                            VALUES  ({?}, {?}, {?},
                                     {?}, {?}, {?},
                                     {?}, {?}, {?},
                                     {?}, FROM_UNIXTIME({?}), {?},
-                                    {?}, {?})",
+                                    {?}, {?}, {?}, {?}, {?})",
                      $address['adr1'], $address['adr2'], $address['adr3'],
                      $address['postcode'], $address['city'], $address['cityid'],
                      $address['country'], $address['region'], $address['regiontxt'],
                      $address['pub'], $address['datemaj'], $flags,
-                     S::i('uid'), $adrid);
-        foreach ($address['tel'] as $telid=>&$tel) {
-            $this->saveTel($adrid, $telid, $tel);
-        }
+                     S::i('uid'), $adrid, $address['precise_lat'], $address['precise_lon'], $address['comment']);
     }
 
     public function save(ProfilePage &$page, $field, $value)
@@ -170,11 +145,13 @@ class ProfileAddress
         XDB::execute("DELETE FROM  adresses
                             WHERE  uid = {?}",
                      S::i('uid'));
-        XDB::execute("DELETE FROM  tels
-                            WHERE  uid = {?}",
+        XDB::execute("DELETE FROM  profile_phones
+                            WHERE  uid = {?} AND link_type = 'address'",
                      S::i('uid'));
         foreach ($value as $adrid=>&$address) {
             $this->saveAddress($adrid, $address);
+            $profiletel = new ProfilePhones('address', $adrid);
+            $profiletel->saveTels('tel', $address['tel']);
         }
     }
 }
@@ -187,25 +164,25 @@ class ProfileAddresses extends ProfilePage
     {
         parent::__construct($wiz);
         $this->settings['addresses'] = new ProfileAddress();
+        $this->watched['addresses'] = true;
     }
 
-    protected function fetchData()
+    protected function _fetchData()
     {
-        if (count($this->orig) > 0) {
-            $this->values = $this->orig;
-            return;
-        }
         // Build the addresses tree
         $res = XDB::query("SELECT  a.adrid AS id, a.adr1, a.adr2, a.adr3,
                                    UNIX_TIMESTAMP(a.datemaj) AS datemaj,
                                    a.postcode, a.city, a.cityid, a.region, a.regiontxt,
                                    a.pub, a.country, gp.pays AS countrytxt, gp.display,
+                                   FIND_IN_SET('coord-checked', a.statut) AS checked,
                                    FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
                                    FIND_IN_SET('courrier', a.statut) AS mail,
                                    FIND_IN_SET('temporaire', a.statut) AS temporary,
-                                   FIND_IN_SET('active', a.statut) AS current
+                                   FIND_IN_SET('active', a.statut) AS current,
+                                   a.glat AS precise_lat, a.glng AS precise_lon,
+                                   a.comment
                              FROM  adresses AS a
-                       INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country)
+                       INNER JOIN  geoloc_pays AS gp ON(gp.a2 = a.country)
                             WHERE  uid = {?} AND NOT FIND_IN_SET('pro', statut)
                          ORDER BY  adrid",
                            S::i('uid'));
@@ -215,10 +192,10 @@ class ProfileAddresses extends ProfilePage
             $this->values['addresses'] = $res->fetchAllAssoc();
         }
 
-        $res = XDB::iterator("SELECT  adrid, tel_type AS type, tel_pub AS pub, tel
-                                FROM  tels
-                               WHERE  uid = {?}
-                            ORDER BY  adrid",
+        $res = XDB::iterator("SELECT  link_id AS adrid, tel_type AS type, pub, display_tel AS tel, comment
+                                FROM  profile_phones
+                               WHERE  uid = {?} AND link_type = 'address'
+                            ORDER BY  link_id",
                              S::i('uid'));
         $i = 0;
         $adrNb = count($this->values['addresses']);
@@ -245,7 +222,6 @@ class ProfileAddresses extends ProfilePage
             }
             unset($address['id']);
         }
-        parent::fetchData();
     }
 }