Convert uses of XDB::(..., $flags->flags()) to XDB::(..., $flags)
[platal.git] / modules / profile / addresses.inc.php
index 1ea8869..ec0f031 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   *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-class ProfileAddress extends ProfileNoSave
+class ProfileAddress extends ProfileGeoloc
 {
-    public function value(ProfilePage &$page, $field, $value, &$success)
+    private $bool;
+    private $pub;
+    private $tel;
+
+    public function __construct()
     {
+        $this->bool = new ProfileBool();
+        $this->pub  = new ProfilePub();
+        $this->tel  = new ProfileTel();
+    }
+
+    private function cleanAddress(ProfilePage &$page, array &$address, &$success)
+    {
+        if (@$address['changed']) {
+            $address['datemaj'] = time();
+        }
         $success = true;
+        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'], $s);
+                $tel['tel'] = $this->tel->value($page, 'tel', $tel['tel'], $s);
+                if (!$s) {
+                    $tel['error'] = true;
+                    $success = false;
+                }
+            }
+            unset($tel['removed']);
+        }
+        $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']);
+        unset($address['display']);
+    }
+
+    public function value(ProfilePage &$page, $field, $value, &$success)
+    {
+        $init = false;
         if (is_null($value)) {
-            return $page->values['addresses'];
+            $value = $page->values['addresses'];
+            $init = true;
+        }
+        foreach ($value as $key=>&$adr) {
+            if (@$adr['removed']) {
+                unset($value[$key]);
+            }
         }
+        $current = 0;
+        $success = true;
         foreach ($value as $key=>&$adr) {
-            if ($adr['removed']) {
+            if (@$adr['current']) {
+                $current++;
+            }
+        }
+        if ($current == 0 && count($value) > 0) {
+            foreach ($value as $key=>&$adr) {
+                $adr['current'] = true;
+                break;
+            }
+        } else if ($current > 1) {
+            $success = false;
+        }
+        foreach ($value as $key=>&$adr) {
+            $ls = true;
+            $this->geolocAddress($adr, $s);
+            $ls = ($ls && $s);
+            $this->cleanAddress($page, $adr, $s);
+            $ls = ($ls && $s);
+            if (!trim($adr['text'])) {
                 unset($value[$key]);
+            } else if (!$init) {
+                $success = ($success && $ls);
             }
         }
         return $value;
     }
+
+    private function saveTel($adrid, $telid, array &$tel)
+    {
+        XDB::execute("INSERT INTO  tels (uid, adrid, telid,
+                                         tel_type, tel_pub, tel)
+                           VALUES  ({?}, {?}, {?},
+                                    {?}, {?}, {?})",
+                    S::i('uid'), $adrid, $telid,
+                    $tel['type'], $tel['pub'], $tel['tel']);
+    }
+
+    private function saveAddress($adrid, array &$address)
+    {
+        $flags = new FlagSet();
+        if ($address['secondaire']) {
+            $flags->addFlag('res-secondaire');
+        }
+        if ($address['mail']) {
+            $flags->addFlag('courrier');
+        }
+        if ($address['temporary']) {
+            $flags->addFlag('temporaire');
+        }
+        if ($address['current']) {
+            $flags->addFlag('active');
+        }
+        if ($address['checked']) {
+            $flags->addFlag('coord-checked');
+        }
+        XDB::execute("INSERT INTO  adresses (adr1, adr2, adr3,
+                                              postcode, city, cityid,
+                                              country, region, regiontxt,
+                                              pub, datemaj, statut,
+                                              uid, adrid, glat, glng)
+                           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']);
+        foreach ($address['tel'] as $telid=>&$tel) {
+            $this->saveTel($adrid, $telid, $tel);
+        }
+    }
+
+    public function save(ProfilePage &$page, $field, $value)
+    {
+        XDB::execute("DELETE FROM  adresses
+                            WHERE  uid = {?}",
+                     S::i('uid'));
+        XDB::execute("DELETE FROM  tels
+                            WHERE  uid = {?}",
+                     S::i('uid'));
+        foreach ($value as $adrid=>&$address) {
+            $this->saveAddress($adrid, $address);
+        }
+    }
 }
 
 class ProfileAddresses extends ProfilePage
@@ -44,62 +175,63 @@ 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  adrid AS id, adr1, adr2, adr3,
-                                   postcode, city, cityid, region, regiontxt,
-                                   fax, glat, glng, datemaj, pub,
-                                   FIND_IN_SET('res-secondaire', statut) AS secondaire,
-                                   FIND_IN_SET('courrier', statut) AS mail,
-                                   FIND_IN_SET('temporary', statut) AS temporary,
-                                   FIND_IN_SET('active', statut) AS current,
-                                   FIND_IN_SET('coord-checked', statut) AS checked,
-                                   FIND_IN_SET('coord-valid', statut) AS valid
-                             FROM  adresses
-                            WHERE  uid = {?}
+        $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,
+                                   a.glat AS precise_lat, a.glng AS precise_lon
+                             FROM  adresses AS a
+                       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'));
-        $this->values['addresses'] = $res->fetchAllAssoc();
+        if ($res->numRows() == 0) {
+            $this->values['addresses'] = array();
+        } else {
+            $this->values['addresses'] = $res->fetchAllAssoc();
+        }
 
-        $res = XDB::iterRow("SELECT  adrid, telid, tel_type, tel_pub, tel
-                               FROM  tels
-                              WHERE  uid = {?}
-                           ORDER BY  adrid",
-                            S::i('uid'));
+        $res = XDB::iterator("SELECT  adrid, tel_type AS type, tel_pub AS pub, tel
+                                FROM  tels
+                               WHERE  uid = {?}
+                            ORDER BY  adrid",
+                             S::i('uid'));
         $i = 0;
-        while (list($adrid, $telid, $type, $pub, $tel) = $res->next()) {
-            while ($this->values['addresses'][$i]['id'] < $adrid) {
+        $adrNb = count($this->values['addresses']);
+        while ($tel = $res->next()) {
+            $adrid = $tel['adrid'];
+            unset($tel['adrid']);
+            while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $adrid) {
                 $i++;
             }
+            if ($i >= $adrNb) {
+                break;
+            }
             $address =& $this->values['addresses'][$i];
             if (!isset($address['tel'])) {
                 $address['tel'] = array();
             }
             if ($address['id'] == $adrid) {
-                $address['tel'][] = array('id'   => $telid,
-                                          'type' => $type,
-                                          'pub'  => $pub,
-                                          'tel'  => $tel);
+                $address['tel'][] = $tel;
             }
         }
-        parent::fetchData();
-    }
-
-    protected function saveData()
-    {
-        parent::saveData();
-    }
-
-    public function prepare(PlatalPage &$page)
-    {
-        parent::prepare($page);
+        foreach ($this->values['addresses'] as $id=>&$address) {
+            if (!isset($address['tel'])) {
+                $address['tel'] = array();
+            }
+            unset($address['id']);
+        }
     }
 }