Modifies database structure for phones
[platal.git] / modules / profile / addresses.inc.php
index 4c20bb2..6dfd2bb 100644 (file)
@@ -44,6 +44,10 @@ class ProfileAddress extends ProfileGeoloc
             } else {
                 $tel['pub'] = $this->pub->value($page, 'pub', $tel['pub'], $s);
                 $tel['tel'] = $this->tel->value($page, 'tel', $tel['tel'], $s);
+                if(!isset($tel['type']) || ($tel['type'] != 'fixed' && $tel['type'] != 'mobile' && $tel['type'] != 'fax')) {
+                    $tel['type'] = 'fixed';
+                    $s = false;
+                }
                 if (!$s) {
                     $tel['error'] = true;
                     $success = false;
@@ -76,15 +80,19 @@ class ProfileAddress extends ProfileGeoloc
             }
         }
         $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;
@@ -103,12 +111,12 @@ class ProfileAddress extends ProfileGeoloc
 
     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)
@@ -134,17 +142,17 @@ class ProfileAddress extends ProfileGeoloc
                                               postcode, city, cityid,
                                               country, region, regiontxt,
                                               pub, datemaj, statut,
-                                              uid, adrid, glat, glng)
+                                              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, $address['precise_lat'], $address['precise_lon']);
+                     S::i('uid'), $adrid, $address['precise_lat'], $address['precise_lon'], $address['comment']);
         foreach ($address['tel'] as $telid=>&$tel) {
             $this->saveTel($adrid, $telid, $tel);
         }
@@ -155,8 +163,8 @@ class ProfileAddress extends ProfileGeoloc
         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);
@@ -187,7 +195,8 @@ class ProfileAddresses extends ProfilePage
                                    FIND_IN_SET('courrier', a.statut) AS mail,
                                    FIND_IN_SET('temporaire', a.statut) AS temporary,
                                    FIND_IN_SET('active', a.statut) AS current,
-                                   a.glat AS precise_lat, a.glng AS precise_lon
+                                   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)
                             WHERE  uid = {?} AND NOT FIND_IN_SET('pro', statut)
@@ -199,10 +208,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
+                                FROM  profile_phones
+                               WHERE  uid = {?} AND link_type = 'address'
+                            ORDER BY  link_id",
                              S::i('uid'));
         $i = 0;
         $adrNb = count($this->values['addresses']);