Class Address.
authorStéphane Jacob <sj@m4x.org>
Mon, 13 Sep 2010 08:27:09 +0000 (10:27 +0200)
committerStéphane Jacob <sj@m4x.org>
Sat, 18 Sep 2010 13:01:43 +0000 (15:01 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
classes/address.php [new file with mode: 0644]
htdocs/javascript/profile.js
include/profilefields.inc.php
include/validations/entreprises.inc.php
modules/profile/addresses.inc.php
modules/profile/jobs.inc.php
modules/profile/page.inc.php
templates/geoloc/form.address.tpl
templates/profile/adresses.address.tpl
templates/profile/geocoding.mail.tpl [moved from templates/geoloc/geoloc.mail.tpl with 100% similarity]

diff --git a/classes/address.php b/classes/address.php
new file mode 100644 (file)
index 0000000..aad9fd0
--- /dev/null
@@ -0,0 +1,461 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+/** Class Address is meant to perform most of the access to the table profile_addresses.
+ *
+ * profile_addresses describes an Address, which can be related to either a
+ * Profile, a Job or a Company:
+ * - for a Profile:
+ *   - `type` is set to 'home'
+ *   - `pid` is set to the related profile pid (in profiles)
+ *   - `id` is the id of the address in the list of those related to that profile
+ *   - `jobid` is set to 0
+ *
+ * - for a Company:
+ *   - `type` is set to 'hq'
+ *   - `pid` is set to 0
+ *   - `jobid` is set to the id of the company (in profile_job_enum)
+ *   - `id` is set to 0 (only one address per Company)
+ *
+ * - for a Job:
+ *   - `type` is set to 'job'
+ *   - `pid` is set to the pid of the Profile of the related Job (in both profiles and profile_job)
+ *   - `id` is the id of the job to which we refer (in profile_job)
+ *   - `jobid` is set to 0
+ *
+ * Thus an Address can be linked to a Company, a Profile, or a Job.
+ */
+class Address
+{
+    const LINK_JOB     = 'job';
+    const LINK_COMPANY = 'hq';
+    const LINK_PROFILE = 'home';
+
+    // Primary key fields: the quadruplet ($pid, $jobid, $type, $id) defines a unique address.
+    public $pid = 0;
+    public $jobid = 0;
+    public $type = Address::LINK_PROFILE;
+    public $id = 0;
+
+    // Geocoding fields.
+    public $accuracy = 0;
+    public $text = '';
+    public $postalText = '';
+    public $postalCode = null;
+    public $localityId = null;
+    public $subAdministrativeAreaId = null;
+    public $administrativeAreaId = null;
+    public $localityName = null;
+    public $subAdministrativeAreaName = null;
+    public $administrativeAreaName = null;
+    public $countryId = null;
+    public $latitude = null;
+    public $longitude = null;
+    public $north = null;
+    public $south = null;
+    public $east = null;
+    public $west = null;
+    public $geocodedText = null;
+    public $geocodedPostalText = null;
+    public $geocodeChosen = null;
+
+    // Database's field required for both 'home' and 'job' addresses.
+    public $pub = 'private';
+
+    // Database's fields required for 'home' addresses.
+    public $flags = null; // 'current', 'temporary', 'secondary', 'mail', 'cedex'
+    public $comment = null;
+    public $current = null;
+    public $temporary = null;
+    public $secondary = null;
+    public $mail = null;
+
+    // Remaining fields that do not belong to profile_addresses.
+    public $phones = array();
+    public $error = false;
+    public $changed = 0;
+    public $removed = 0;
+
+    public function __construct(array $data = array())
+    {
+        if (count($data) > 0) {
+            foreach ($data as $key => $val) {
+                $this->$key = $val;
+            }
+        }
+
+        if ($this->type == self::LINK_PROFILE) {
+            if (!is_null($this->flags)) {
+                $this->flags = new PlFlagSet($this->flags);
+            } else {
+                static $flags = array('current', 'temporary', 'secondary', 'mail');
+
+                $this->flags = new PlFlagSet();
+                foreach ($flags as $flag) {
+                    if (!is_null($this->$flag) && ($this->$flag == 1 || $this->$flag == 'on')) {
+                        $this->flags->addFlag($flag, 1);
+                        $this->$flag = null;
+                    }
+                    $this->flags->addFlag('cedex', (strpos(strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
+                                                                                   array('', "\n"), $this->text)), 'CEDEX')) !== false);
+                }
+            }
+        }
+    }
+
+    public function phones()
+    {
+        return $this->phones;
+    }
+
+    public function addPhone(Phone &$phone)
+    {
+        if ($phone->linkType() == Phone::LINK_ADDRESS && $phone->pid() == $this->pid) {
+            $this->phones[$phone->uniqueId()] = $phone;
+        }
+    }
+
+    public function hasFlag($flag)
+    {
+        return $this->flags->hasFlag($flag);
+    }
+
+    public function format(array $format = array())
+    {
+        if (empty($format)) {
+            $format['requireGeocoding'] = false;
+            $format['stripGeocoding'] = false;
+        }
+        $this->text = trim($this->text);
+        if ($this->removed == 1) {
+            $this->text = '';
+            return true;
+        }
+
+        require_once 'geocoding.inc.php';
+        if ($format['requireGeocoding'] || $this->changed == 1) {
+            $gmapsGeocoder = new GMapsGeocoder();
+            $gmapsGeocoder->getGeocodedAddress($this);
+            $this->changed = 0;
+            $this->error = !empty($this->geocodedText);
+        }
+        if ($format['stripGeocoding'] || ($this->type == self::LINK_COMPANY && $this->error) || $this->geocodeChosen === '0') {
+            $gmapsGeocoder = new GMapsGeocoder();
+            $gmapsGeocoder->stripGeocodingFromAddress($this);
+            if ($this->geocodeChosen === '0') {
+                $mailer = new PlMailer('profile/geocoding.mail.tpl');
+                $mailer->assign('text', $this->text);
+                $mailer->assign('geoloc', $this->geocodedText);
+                $mailer->send();
+            }
+        }
+        $this->geocodeChosen = null;
+        $this->phones = Phone::formatFormArray($this->phones, $this->error);
+        return !$this->error;
+    }
+
+    public function toFormArray()
+    {
+        $address = array(
+            'accuracy'                  => $this->accuracy,
+            'text'                      => $this->text,
+            'postalText'                => $this->postalText,
+            'postalCode'                => $this->postalCode,
+            'localityId'                => $this->localityId,
+            'subAdministrativeAreaId'   => $this->subAdministrativeAreaId,
+            'administrativeAreaId'      => $this->administrativeAreaId,
+            'countryId'                 => $this->countryId,
+            'localityName'              => $this->localityName,
+            'subAdministrativeAreaName' => $this->subAdministrativeAreaName,
+            'administrativeAreaName'    => $this->administrativeAreaName,
+            'latitude'                  => $this->latitude,
+            'longitude'                 => $this->longitude,
+            'north'                     => $this->north,
+            'south'                     => $this->south,
+            'east'                      => $this->east,
+            'west'                      => $this->west,
+            'error'                     => $this->error,
+            'changed'                   => $this->changed,
+            'removed'                   => $this->removed,
+        );
+        if (!is_null($this->geocodedText)) {
+            $address['geocodedText'] = $this->geocodedText;
+            $address['geocodedPostalText'] = $this->geocodedPostalText;
+            $address['geocodeChosen'] = $this->geocodeChosen;
+        }
+
+        if ($this->type == self::LINK_PROFILE || $this->type == self::LINK_JOB) {
+            $address['pub'] = $this->pub;
+        }
+        if ($this->type == self::LINK_PROFILE) {
+            static $flags = array('current', 'temporary', 'secondary', 'mail', 'cedex');
+
+            foreach ($flags as $flag) {
+                $address[$flag] = $this->flags->hasFlag($flag);
+            }
+            $address['comment'] = $this->comment;
+            $address['phones']  = Phone::formatFormArray($this->phones);
+        }
+
+        return $address;
+    }
+
+    private function toString()
+    {
+        $address = 'Adresse : ' . $this->text;
+        if ($this->type == self::LINK_PROFILE || $this->type == self::LINK_JOB) {
+            $address .= ', affichage : ' . $this->pub;
+        }
+        if ($this->type == self::LINK_PROFILE) {
+            static $flags = array(
+                'current'   => 'actuelle',
+                'temporary' => 'temporaire',
+                'secondary' => 'secondaire',
+                'mail'      => 'conctactable par courier',
+                'cedex'     => 'type cédex',
+            );
+
+            $address .= ', commentaire : ' . $this->comment;
+            foreach ($flags as $flag => $flagName) {
+                if ($this->flags->hasFlag($flag)) {
+                    $address .= ', ' . $flagName;
+                }
+            }
+            if ($phones = Phone::formArrayToString($this->phones)) {
+                $address .= ', ' . $phones;
+            }
+        }
+        return $address;
+    }
+
+    private function isEmpty()
+    {
+        return (!$this->text || $this->text == '');
+    }
+
+    public function save()
+    {
+        static $areas = array('administrativeArea', 'subAdministrativeArea', 'locality');
+
+        $this->format();
+        if (!$this->isEmpty()) {
+            require_once 'geocoding.inc.php';
+            foreach ($areas as $area) {
+                Geocoder::getAreaId($this, $area);
+            }
+
+            XDB::execute('INSERT INTO  profile_addresses (pid, jobid, type, id, flags, accuracy,
+                                                          text, postalText, postalCode, localityId,
+                                                          subAdministrativeAreaId, administrativeAreaId,
+                                                          countryId, latitude, longitude, updateTime, pub, comment,
+                                                          north, south, east, west)
+                               VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
+                                        {?}, {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})',
+                         $this->pid, $this->jobid, $this->type, $this->id, $this->flags, $this->accuracy,
+                         $this->text, $this->postalText, $this->postalCode, $this->localityId,
+                         $this->subAdministrativeAreaId, $this->administrativeAreaId,
+                         $this->countryId, $this->latitude, $this->longitude,
+                         time(), $this->pub, $this->comment,
+                         $this->north, $this->south, $this->east, $this->west);
+
+            if ($this->type == self::LINK_PROFILE) {
+                Phone::savePhones($this->phones, $this->pid, Phone::LINK_ADDRESS, $this->id);
+            }
+        }
+    }
+
+    static public function delete($pid, $type, $jobid = null)
+    {
+        $where = '';
+        if (!is_null($pid)) {
+            $where = XDB::format(' AND pid = {?}', $pid);
+        }
+        if (!is_null($jobid)) {
+            $where = XDB::format(' AND jobid = {?}', $jobid);
+        }
+        XDB::execute('DELETE FROM  profile_addresses
+                            WHERE  type = {?}' . $where,
+                     $type);
+        if ($type == self::LINK_PROFILE) {
+            Phone::deletePhones($pid, Phone::LINK_ADDRESS);
+        }
+    }
+
+    /** Saves addresses into the database.
+     * @param $data: an array of form formatted addresses.
+     * @param $pid, $type, $linkid: pid, type and id concerned by the update.
+     */
+    static public function saveFromArray(array $data, $pid, $type = self::LINK_PROFILE, $linkid = null)
+    {
+        foreach ($data as $id => $value) {
+            if (!is_null($linkid)) {
+                $value['id'] = $linkid;
+            } else {
+                $value['id'] = $id;
+            }
+            if (!is_null($pid)) {
+                $value['pid'] = $pid;
+            }
+            if (!is_null($type)) {
+                $value['type'] = $type;
+            }
+            $address = new Address($value);
+            $address->save();
+        }
+    }
+
+    static private function formArrayWalk(array $data, $function, &$success = true, $requiresEmptyAddress = false)
+    {
+        $addresses = array();
+        foreach ($data as $item) {
+            $address = new Address($item);
+            $success = ($address->format() && $success);
+            if (!$address->isEmpty()) {
+                $addresses[] = call_user_func(array($address, $function));
+            }
+        }
+        if (count($address) == 0 && $requiresEmptyAddress) {
+            $address = new Address();
+            $addresses[] = call_user_func(array($address, $function));
+        }
+        return $addresses;
+    }
+
+    // Formats an array of form addresses into an array of form formatted addresses.
+    static public function formatFormArray(array $data, &$success = true)
+    {
+        // Only a single address can be the profile's current address and she must have one.
+        $hasCurrent = false;
+        foreach ($data as $key => &$address) {
+            if (isset($address['current']) && $address['current']) {
+                if ($hasCurrent) {
+                    $address['current'] = false;
+                } else {
+                    $hasCurrent = true;
+                }
+            }
+        }
+        if (!$hasCurrent && count($value) > 0) {
+            foreach ($value as &$address) {
+                $address['current'] = true;
+                break;
+            }
+        }
+
+        return self::formArrayWalk($data, 'toFormArray', $success, true);
+    }
+
+    static public function formArrayToString(array $data)
+    {
+        return implode(' ; ', self::formArrayWalk($data, 'toString'));
+    }
+
+    static public function iterate(array $pids = array(), array $types = array(),
+                                   array $jobids = array(), array $pubs = array())
+    {
+        return new AddressIterator($pids, $types, $jobids, $pubs);
+    }
+}
+
+/** Iterator over a set of Phones
+ *
+ * @param $pid, $type, $jobid, $pub
+ *
+ * The iterator contains the phones that correspond to the value stored in the
+ * parameters' arrays.
+ */
+class AddressIterator implements PlIterator
+{
+    private $dbiter;
+
+    public function __construct(array $pids, array $types, array $jobids, array $pubs)
+    {
+        $where = array();
+        if (count($pids) != 0) {
+            $where[] = XDB::format('(pa.pid IN {?})', $pids);
+        }
+        if (count($types) != 0) {
+            $where[] = XDB::format('(pa.type IN {?})', $types);
+        }
+        if (count($jobids) != 0) {
+            $where[] = XDB::format('(pa.jobid IN {?})', $jobids);
+        }
+        if (count($pubs) != 0) {
+            $where[] = XDB::format('(pa.pub IN {?})', $pubs);
+        }
+        $sql = 'SELECT  pa.pid, pa.jobid, pa.type, pa.id, pa.flags,
+                        pa.accuracy, pa.text, pa.postalText, pa.postalCode,
+                        pa.localityId, pa.subAdministrativeAreaId,
+                        pa.administrativeAreaId, pa.countryId,
+                        pa.latitude, pa.longitude, pa.north, pa.south, pa.east, pa.west,
+                        pa.pub, pa.comment,
+                        gl.name AS locality, gs.name AS subAdministrativeArea,
+                        ga.name AS administrativeArea, gc.countryFR AS country
+                  FROM  profile_addresses             AS pa
+             LEFT JOIN  geoloc_localities             AS gl ON (gl.id = pa.localityId)
+             LEFT JOIN  geoloc_administrativeareas    AS ga ON (ga.id = pa.administrativeAreaId)
+             LEFT JOIN  geoloc_subadministrativeareas AS gs ON (gs.id = pa.subAdministrativeAreaId)
+             LEFT JOIN  geoloc_countries              AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
+                 WHERE  ' . implode(' AND ', $where) . '
+              ORDER BY  pa.pid, pa.jobid, pa.id';
+        $this->dbiter = XDB::iterator($sql);
+    }
+
+    public function next()
+    {
+        if (is_null($this->dbiter)) {
+            return null;
+        }
+        $data = $this->dbiter->next();
+        if (is_null($data)) {
+            return null;
+        }
+        // Adds phones to addresses.
+        $it = Phone::iterate(array($data['pid']), array(Phone::LINK_ADDRESS), array($data['id']));
+        while ($phone = $it->next()) {
+            $data['phones'][$phone->id()] = $phone->toFormArray();
+        }
+        return new Address($data);
+    }
+
+    public function total()
+    {
+        return $this->dbiter->total();
+    }
+
+    public function first()
+    {
+        return $this->dbiter->first();
+    }
+
+    public function last()
+    {
+        return $this->dbiter->last();
+    }
+
+    public function value()
+    {
+        return $this->dbiter;
+    }
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
index a24a83b..75b5b8d 100644 (file)
@@ -323,14 +323,15 @@ function addressChanged(prefid)
 function validGeoloc(prefid, id, geoloc)
 {
     if (geoloc == 1) {
-        $('#' + prefid + '_cont').find('[name*=text]').val($('#' + prefid + '_cont').find('[name*=geoloc]').val());
+        $('#' + prefid + '_cont').find('[name*=text]').val($('#' + prefid + '_cont').find('[name*=geocodedText]').val());
         $('#' + prefid + '_cont').find('[name*=postalText]').val($('#' + prefid + '_cont').find('[name*=geocodedPostalText]').val());
     }
     if (geoloc > 0) {
-        $('#' + prefid + '_cont').find("[name*='[geoloc]']").remove();
+        $('#' + prefid + '_cont').find("[name*='[geocodedText]']").remove();
+        $('#' + prefid + '_cont').find("[name*='[geocodedPostalText]']").remove();
     }
     $('#' + prefid + '_cont').find('[name*=text]').removeClass('error');
-    $('#' + prefid + '_cont').find('[name*=geoloc_choice]').val(geoloc);
+    $('#' + prefid + '_cont').find('[name*=geocodeChosen]').val(geoloc);
     $('.' + prefid + '_geoloc').remove();
 }
 
index 506b8ac..63c0a6e 100644 (file)
@@ -158,7 +158,7 @@ class Company
 
     public function setAddress(Address &$address)
     {
-        if ($address->link_type == Address::LINK_COMPANY && $address->link_id == $this->id) {
+        if ($address->type == Address::LINK_COMPANY && $address->jobid == $this->id) {
             $this->address = $address;
         }
     }
@@ -166,6 +166,17 @@ class Company
 }
 // }}}
 // {{{ class Job
+/** profile_job describes a Job, links to:
+ * - a Profile, through `pid`
+ * - a Company, through `jobid`
+ * The `id` field is the id of this job in the list of the jobs of its profile
+ *
+ * For the documentation of the phone table, please see classes/phone.php.
+ * For the documentation of the address table, please see classes/address.php.
+ *
+ * The possible relations are as follow:
+ * A Job is linked to a Company and a Profile
+ */
 class Job
 {
     public $pid;
@@ -221,7 +232,7 @@ class Job
 
     public function setAddress(Address $address)
     {
-        if ($address->link_type == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) {
+        if ($address->type == Address::LINK_JOB && $address->jobid == $this->id && $address->pid == $this->pid) {
             $this->address = $address;
         }
     }
@@ -251,74 +262,6 @@ class JobTerm
     }
 }
 // }}}
-// {{{ class Address
-class Address
-{
-    const LINK_JOB     = 'job';
-    const LINK_COMPANY = 'hq';
-    const LINK_PROFILE = 'home';
-
-    public $flags;
-    public $id; // The ID of the address among those associated with its link
-    public $link_id; // The ID of the object to which the address is linked (profile, job, company)
-    public $link_type;
-
-    public $text;
-    public $postalCode;
-    public $latitude;
-    public $longitude;
-
-    public $locality;
-    public $subAdministrativeArea;
-    public $administrativeArea;
-    public $country;
-
-    public $comment;
-
-    private $phones = array();
-
-    /** Fields are:
-     * pîd, id, link_id, link_type, flags, text, postcode, country
-     */
-    public function __construct($data)
-    {
-        foreach ($data as $key => $val) {
-            $this->$key = $val;
-        }
-        $this->flags = new PlFlagSet($this->flags);
-    }
-
-    public function uid() {
-        $uid = $this->link_type . '_';
-        if ($this->link_type != self::LINK_COMPANY) {
-            $uid .= $this->pid . '_';
-        }
-        $uid .= $this->link_id . '_' . $this->id;
-    }
-
-    public function addPhone(Phone &$phone)
-    {
-        if (
-            $phone->linkType() == Phone::LINK_ADDRESS && $phone->linkId() == $this->id &&
-            ($this->link_type == self::LINK_COMPANY || $phone->pid() == $this->pid) ) {
-            $this->phones[$phone->uniqueId()] = $phone;
-        }
-    }
-
-    public function phones()
-    {
-        return $this->phones;
-    }
-
-    public function hasFlag($flag)
-    {
-        if (!$this->flags instanceof PlFlagSet) {
-            $this->flags = new PlFlagSet($this->flags);
-        }
-        return $this->flags->hasFlag($flag);
-    }
-}
-// }}}
 // {{{ class Education
 class Education
 {
@@ -586,106 +529,43 @@ class ProfileMentoringCountries extends ProfileField
     }
 }
 // }}}
-
-/** Loading of data for a Profile :
- * 1) load jobs, addresses, phones
- * 2) attach phones to addresses, jobs and profiles
- * 3) attach addresses to jobs and profiles
- */
-
-// {{{ Database schema (profile_address, profile_jobs)
-/** The database for this is very unclear, so here is a little schema :
- * profile_job describes a Job, links to:
- * - a Profile, through `pid`
- * - a Company, through `jobid`
- * The `id` field is the id of this job in the list of the jobs of its profile
- *
- * profile_addresses describes an Address, which
- * related to either a Profile, a Job or a Company:
- * - for a Profile:
- *   - `type` is set to 'home'
- *   - `pid` is set to the related profile pid
- *   - `id` is the id of the address in the list of those related to that profile
- *   - `jobid` is empty
- *
- * - for a Company:
- *   - `type` is set to 'hq'
- *   - `pid` is set to 0
- *   - `jobid` is set to the id of the company
- *   - `id` is set to 0 (only one address per Company)
- *
- * - for a Job:
- *   - `type` is set to 'job'
- *   - `pid` is set to the pid of the Profile of the related Job
- *   - `jobid` is set to the Company of the job (this information is redundant
- *              with that of the row of profile_job for the related job)
- *   - `id` is the id of the job to which we refer (i.e `profile_job.id`)
- *
- * For the documentation of the phone table, please see classes/phone.php.
- *
- * The possible relations are as follow:
- * An Address can be linked to a Company, a Profile, a Job
- * A Job is linked to a Company and a Profile
- */
-// }}}
-
 // {{{ class ProfileAddresses                         [ Field ]
 class ProfileAddresses extends ProfileField
 {
     private $addresses = array();
 
-    public function __construct(PlIterator $it)
+    public function __construct(PlInnerSubIterator $it)
     {
-        if ($it instanceof PlInnerSubIterator) {
-            $this->pid = $it->value();
-        }
-
-        while ($addr = $it->next()) {
-            $this->addresses[] = new Address($addr);
+        $this->pid = $it->value();
+        while ($address = $it->next()) {
+            $this->addresses[] = new Address($address);
         }
     }
 
     public function get($flags, $limit = null)
     {
-        $res = array();
+        $addresses = array();
         $nb = 0;
-        foreach ($this->addresses as $addr) {
-            if (
-                (($flags & Profile::ADDRESS_MAIN) && $addr->hasFlag('current'))
-                ||
-                (($flags & Profile::ADDRESS_POSTAL) && $addr->hasFlag('mail'))
-                ||
-                (($flags & Profile::ADDRESS_PERSO) && $addr->link_type == Address::LINK_PROFILE)
-                ||
-                (($flags & Profile::ADDRESS_PRO) && $addr->link_type == Address::LINK_JOB)
+        foreach ($this->addresses as $address) {
+            if ((($flags & Profile::ADDRESS_MAIN) && $address->hasFlag('current'))
+                || (($flags & Profile::ADDRESS_POSTAL) && $address->hasFlag('mail'))
+                || (($flags & Profile::ADDRESS_PERSO) && $address->type == Address::LINK_PROFILE)
+                || (($flags & Profile::ADDRESS_PRO) && $address->type == Address::LINK_JOB)
             ) {
-                $res[] = $addr;
-                $nb++;
+                $addresses[] = $address;
+                ++$nb;
             }
             if ($limit != null && $nb == $limit) {
                 break;
             }
         }
-        return $res;
+        return $addresses;
     }
 
     public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pa.id, pa.pid, pa.flags, pa.type AS link_type,
-                                       IF(pa.type = \'home\', pid, IF(pa.type = \'job\', pa.id, jobid)) AS link_id,
-                                       pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
-                                       gl.name AS locality, gas.name AS subAdministrativeArea,
-                                       ga.name AS administrativeArea, gc.countryFR AS country
-                                 FROM  profile_addresses AS pa
-                            LEFT JOIN  geoloc_localities AS gl ON (gl.id = pa.localityId)
-                            LEFT JOIN  geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
-                            LEFT JOIN  geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
-                            LEFT JOIN  geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
-                                WHERE  pa.pid in {?} AND pa.pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                               $pids, $visibility->levels());
-
-        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+        $it = Address::iterate($pids, array(), array(), $visibility->levels());
+        return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
     }
 
     public function addPhones(ProfilePhones $phones)
@@ -794,8 +674,8 @@ class ProfileJobs extends ProfileField
     {
         $a = $addresses->get(Profile::ADDRESS_PRO);
         foreach ($a as $address) {
-            if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) {
-                $this->jobs[$address->link_id]->setAddress($address);
+            if ($address->type == Address::LINK_JOB && array_key_exists($address->jobid, $this->jobs)) {
+                $this->jobs[$address->jobid]->setAddress($address);
             }
         }
     }
index 13ec9ce..409b1a4 100644 (file)
@@ -160,15 +160,11 @@ class EntrReq extends ProfileValidate
 
     public function commit()
     {
-        // TODO: use address update profile_job_enum once it is done.
-
         $res = XDB::query('SELECT  id
                              FROM  profile_job_enum
                             WHERE  name = {?}',
                           $this->name);
         if ($res->numRows() != 1) {
-            require_once 'geocoding.inc.php';
-
             XDB::execute('INSERT INTO  profile_job_enum (name, acronym, url, email, holdingid, NAF_code, AX_code)
                                VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
                          $this->name, $this->acronym, $this->url, $this->email,
@@ -179,30 +175,14 @@ class EntrReq extends ProfileValidate
                                      'type' => 'fixed', 'display' => $this->tel, 'pub' => 'public'));
             $fax   = new Phone(array('link_type' => 'hq', 'link_id' => $jobid, 'id' => 1,
                                      'type' => 'fax', 'display' => $this->fax, 'pub' => 'public'));
+            $address = new Address(array('jobid' => $jobid, 'type' => Address::LINK_COMPANY, 'text' => $this->address));
             $phone->save();
             $fax->save();
-
-            $gmapsGeocoder = new GMapsGeocoder();
-            $address = $gmapsGeocoder->getGeocodedAddress($this->address);
-            Geocoder::getAreaId($address, 'administrativeArea');
-            Geocoder::getAreaId($address, 'subAdministrativeArea');
-            Geocoder::getAreaId($address, 'locality');
-            XDB::execute("INSERT INTO  profile_addresses (jobid, type, id, accuracy,
-                                                          text, postalText, postalCode, localityId,
-                                                          subAdministrativeAreaId, administrativeAreaId,
-                                                          countryId, latitude, longitude, updateTime,
-                                                          north, south, east, west)
-                               VALUES  ({?}, 'hq', 0, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
-                                        {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?})",
-                         $jobid, $this->address['accuracy'], $this->address['text'], $this->address['postalText'],
-                         $this->address['postalCode'], $this->address['localityId'],
-                         $this->address['subAdministrativeAreaId'], $this->address['administrativeAreaId'],
-                         $this->address['countryId'], $this->address['latitude'], $this->address['longitude'],
-                         $this->address['updateTime'], $this->address['north'], $this->address['south'],
-                         $this->address['east'], $this->address['west']);
+            $address->save();
         } else {
             $jobid = $res->fetchOneCell();
         }
+
         XDB::execute('UPDATE  profile_job
                          SET  jobid = {?}
                        WHERE  pid = {?} AND id = {?}',
index 0c6629a..95c1a89 100644 (file)
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-class ProfileSettingAddress extends ProfileSettingGeocoding
+class ProfileSettingAddresses implements ProfileSetting
 {
-    private $bool;
-    private $pub;
-
-    public function __construct()
-    {
-        $this->bool = new ProfileSettingBool();
-        $this->pub  = new ProfileSettingPub();
-    }
-
-    private function cleanAddress(ProfilePage &$page, $addrid, array &$address)
-    {
-        $address['tel']       = Phone::formatFormArray($address['tel'], $s);
-        $address['current']   = $this->bool->value($page, 'current',   $address['current'],   $s);
-        $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $s);
-        $address['secondary'] = $this->bool->value($page, 'secondary', $address['secondary'], $s);
-        $address['mail']      = $this->bool->value($page, 'mail',      $address['mail'],      $s);
-        $address['pub']       = $this->pub->value($page,  'pub',       $address['pub'],       $s);
-    }
-
     public function value(ProfilePage &$page, $field, $value, &$success)
     {
-        $init = false;
-        if (is_null($value)) {
-            $value = $page->values['addresses'];
-            $init  = true;
-        }
-        foreach ($value as $key => &$address) {
-            if (isset($address['removed']) && $address['removed']) {
-                array_splice($value, $key, 1);
-            }
-        }
-        $current = 0;
         $success = true;
-        foreach ($value as $key => &$address) {
-            if (isset($address['current']) && $address['current']) {
-                $current++;
-            }
-        }
-        if ($current == 0 && count($value) > 0) {
-            foreach ($value as &$address) {
-                $address['current'] = true;
-                break;
+        $addresses = array();
+
+        if (is_null($value)) {
+            $it = Address::iterate(array($page->pid()), array(Address::LINK_PROFILE), array(0));
+            while ($address = $it->next()) {
+                $addresses[] = $address->toFormArray();
             }
-        } elseif ($current > 1) {
-            $success = false;
-        }
-        foreach ($value as $key => &$address) {
-            if (!trim($address['text'])) {
-                unset($value[$key]);
-            } elseif (!$init) {
-                $this->geocodeAddress($address, $s);
-                $success = $success && $s;
+            if (count($addresses) == 0) {
+                $address = new Address();
+                $addresses[] = $address->toFormArray();
             }
-            $this->cleanAddress($page, $key, $address);
+            return $addresses;
         }
-        return $value;
-    }
-
-    public function saveAddress($pid, $addrid, array &$address, $type)
-    {
-        require_once 'geocoding.inc.php';
 
-        $flags = new PlFlagSet();
-        $flags->addFlag('current', $address['current']);
-        $flags->addFlag('temporary', $address['temporary']);
-        $flags->addFlag('secondary', $address['secondary']);
-        $flags->addFlag('mail', $address['mail']);
-        $flags->addFlag('cedex', $address['cedex'] =
-            (strpos(strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
-                                            array("", "\n"), $address['text'])), 'CEDEX')) !== false);
-        Geocoder::getAreaId($address, "administrativeArea");
-        Geocoder::getAreaId($address, "subAdministrativeArea");
-        Geocoder::getAreaId($address, "locality");
-
-        XDB::execute("INSERT INTO  profile_addresses (pid, type, id, flags, accuracy,
-                                                      text, postalText, postalCode, localityId,
-                                                      subAdministrativeAreaId, administrativeAreaId,
-                                                      countryId, latitude, longitude, updateTime, pub, comment,
-                                                      north, south, east, west)
-                           VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
-                                    {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})",
-                     $pid, $type, $addrid, $flags, $address['accuracy'],
-                     $address['text'], $address['postalText'], $address['postalCode'], $address['localityId'],
-                     $address['subAdministrativeAreaId'], $address['administrativeAreaId'],
-                     $address['countryId'], $address['latitude'], $address['longitude'],
-                     $address['updateTime'], $address['pub'], $address['comment'],
-                     $address['north'], $address['south'], $address['east'], $address['west']);
+        return Address::formatFormArray($value, $success);
     }
 
     public function save(ProfilePage &$page, $field, $value)
     {
-        XDB::execute("DELETE FROM  profile_addresses
-                            WHERE  pid = {?} AND type = 'home'",
-                     $page->pid());
         Phone::deletePhones($page->pid(), Phone::LINK_ADDRESS);
-        foreach ($value as $addrid => &$address) {
-            $this->saveAddress($page->pid(), $addrid, $address, 'home');
-            Phone::savePhones($address['tel'], $page->pid(), Phone::LINK_ADDRESS, $addrid);
-        }
+        Address::delete($page->pid(), Address::LINK_PROFILE);
+        Address::saveFromArray($value, $page->pid(), Address::LINK_PROFILE);
     }
 
-    public function getText($value) {
-        $addresses = array();
-        foreach ($value as $addrid => $address) {
-            $phones = Phone::formArrayToString($address['tel']);
-            $addresses[] = 'Adresse : ' . $address['text'] . ', affichage : ' . $address['pub']
-                         . ', commentaire : ' . $address['comment'] . ', actuelle : ' . ($address['current'] ? 'oui' : 'non')
-                         . ', temporaire : ' . ($address['temporary'] ? 'oui' : 'non') . ', secondaire : '
-                         . ($address['secondary'] ? 'oui' : 'non') . ', conctactable par courier : '
-                         . ($address['mail'] ? 'oui' : 'non') . ($phones ? ', ' . $phones : '');
-        }
-        return implode(' ; ' , $addresses);
+    public function getText($value)
+    {
+        return Address::formArrayToString($value);
     }
 }
 
@@ -143,49 +61,9 @@ class ProfileSettingAddresses extends ProfilePage
     public function __construct(PlWizard &$wiz)
     {
         parent::__construct($wiz);
-        $this->settings['addresses'] = new ProfileSettingAddress();
+        $this->settings['addresses'] = new ProfileSettingAddresses();
         $this->watched['addresses']  = true;
     }
-
-    protected function _fetchData()
-    {
-        $res = XDB::query("SELECT  id, accuracy, text, postalText,
-                                   postalCode, localityId, subAdministrativeAreaId, administrativeAreaId,
-                                   countryId, latitude, longitude, pub, comment, UNIX_TIMESTAMP(updateTime) AS updateTime,
-                                   north, south, east, west,
-                                   FIND_IN_SET('current', flags) AS current,
-                                   FIND_IN_SET('temporary', flags) AS temporary,
-                                   FIND_IN_SET('secondary', flags) AS secondary,
-                                   FIND_IN_SET('mail', flags) AS mail,
-                                   FIND_IN_SET('cedex', flags) AS cedex
-                             FROM  profile_addresses
-                            WHERE  pid = {?} AND type = 'home'
-                         ORDER BY  id",
-                           $this->pid());
-        if ($res->numRows() == 0) {
-            $this->values['addresses'] = array();
-        } else {
-            $this->values['addresses'] = $res->fetchAllAssoc();
-        }
-
-        // Adds phones to addresses.
-        $it = Phone::iterate(array($this->pid()), array(Phone::LINK_ADDRESS));
-        while ($phone = $it->next()) {
-            $this->values['addresses'][$phone->linkId()]['tel'][$phone->id()] = $phone->toFormArray();
-        }
-
-        // Properly formats addresses.
-        foreach ($this->values['addresses'] as $id => &$address) {
-            $phone = new Phone();
-            if (!isset($address['tel'])) {
-                $address['tel'] = array(0 => $phone->toFormArray());
-            }
-            unset($address['id']);
-            $address['changed'] = 0;
-            $address['removed'] = 0;
-        }
-        //var_dump($this->values['addresses']['tel']);
-    }
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
index 79b8f41..73aabe5 100644 (file)
@@ -19,7 +19,7 @@
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-class ProfileSettingJob extends ProfileSettingGeocoding
+class ProfileSettingJob implements ProfileSetting
 {
     private $pub;
     private $email_new;
@@ -44,6 +44,7 @@ class ProfileSettingJob extends ProfileSettingGeocoding
 
     public function emptyJob()
     {
+        $address = new Address();
         return array(
             'id'               => '0',
             'jobid'            => '',
@@ -52,26 +53,7 @@ class ProfileSettingJob extends ProfileSettingGeocoding
             'hq_acronym'       => '',
             'hq_url'           => '',
             'hq_email'         => '',
-            'hq_address'       => array(
-                'text'                    => '',
-                'accuracy'                => '',
-                'postalText'              => '',
-                'postalCode'              => '',
-                'administrativeAreaId'    => '',
-                'subAdministrativeAreaId' => '',
-                'localityId'              => '',
-                'countryId'               => '',
-                'latitude'                => '',
-                'longitude'               => '',
-                'north'                   => '',
-                'south'                   => '',
-                'east'                    => '',
-                'west'                    => '',
-                'cedex'                   => '',
-                'updateTime'              => '',
-                'changed'                 => '0',
-                'removed'                 => '0',
-            ),
+            'hq_address'       => $address->toFormArray(),
             'hq_fixed'         => '',
             'hq_fax'           => '',
             'subSubSectorName' => null,
@@ -80,27 +62,7 @@ class ProfileSettingJob extends ProfileSettingGeocoding
             'subSubSector'     => '0',
             'description'      => '',
             'w_url'            => '',
-            'w_address'        => array(
-                'pub'                     => 'private',
-                'text'                    => '',
-                'accuracy'                => '',
-                'postalText'              => '',
-                'postalCode'              => '',
-                'administrativeAreaId'    => '',
-                'subAdministrativeAreaId' => '',
-                'localityId'              => '',
-                'countryId'               => '',
-                'latitude'                => '',
-                'longitude'               => '',
-                'north'                   => '',
-                'south'                   => '',
-                'east'                    => '',
-                'west'                    => '',
-                'cedex'                   => '',
-                'updateTime'              => '',
-                'changed'                 => '0',
-                'removed'                 => '0',
-            ),
+            'w_address'        => $address->toFormArray(),
             'w_email'          => '',
             'w_email_pub'      => 'private',
             'w_email_new'      => '',
@@ -116,7 +78,6 @@ class ProfileSettingJob extends ProfileSettingGeocoding
 
     private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success)
     {
-        $success = true;
         if ($job['w_email'] == "new@example.org") {
             $job['w_email'] = $job['w_email_new'];
         }
@@ -174,11 +135,6 @@ class ProfileSettingJob extends ProfileSettingGeocoding
                                 WHERE  name = {?}",
                               $job['name']);
             if ($res->numRows() != 1) {
-                $this->geocodeAddress($job['hq_address'], $s);
-                if (!$s) {
-                    $gmapsGeocoder = new GMapsGeocoder();
-                    $job['hq_address'] = $gmapsGeocoder->stripGeocodingFromAddress($job['hq_address']);
-                }
                 $req = new EntrReq(S::user(), $page->profile, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
                                    $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
                 $req->submit();
@@ -188,7 +144,6 @@ class ProfileSettingJob extends ProfileSettingGeocoding
                 $job['jobid'] = $res->fetchOneCell();
             }
         }
-        $job['w_address']['pub'] = $this->pub->value($page, 'address_pub', $job['w_address']['pub'], $s);
         $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s);
 
         unset($job['removed']);
@@ -239,13 +194,12 @@ class ProfileSettingJob extends ProfileSettingGeocoding
             }
         }
         foreach ($value as $key => &$job) {
-            $ls = true;
-            $this->geocodeAddress($job['w_address'], $s);
-            $ls = ($ls && $s);
+            $address = new Address($job['w_address']);
+            $s = $address->format();
+            $job['w_address'] = $address->toFormArray();
             $this->cleanJob($page, $key, $job, $s);
-            $ls = ($ls && $s);
             if (!$init) {
-                $success = ($success && $ls);
+                $success = ($success && $s);
             }
         }
         return $value;
@@ -253,13 +207,10 @@ class ProfileSettingJob extends ProfileSettingGeocoding
 
     public function save(ProfilePage &$page, $field, $value)
     {
-        // TODO: use address class to update profile_job_enum once it is done.
         XDB::execute("DELETE FROM  profile_job
                             WHERE  pid = {?}",
                      $page->pid());
-        XDB::execute("DELETE FROM  profile_addresses
-                            WHERE  pid = {?} AND type = 'job'",
-                     $page->pid());
+        Address::delete($page->pid(), Address::LINK_JOB);
         Phone::deletePhones($page->pid(), Phone::LINK_JOB);
         $terms_values = array();
         foreach ($value as $id => &$job) {
@@ -277,8 +228,8 @@ class ProfileSettingJob extends ProfileSettingGeocoding
                                  $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
                                  $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
                 }
-                $address = new ProfileSettingAddress();
-                $address->saveAddress($page->pid(), $id, $job['w_address'], 'job');
+                $address = new Address(array_merge($job['w_address'], array('pid' => $page->pid(), 'id' => $id, 'type' => Address::LINK_JOB)));
+                $address->save();
                 Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id);
                 if (isset($job['terms'])) {
                     foreach ($job['terms'] as $term) {
@@ -296,12 +247,12 @@ class ProfileSettingJob extends ProfileSettingGeocoding
     public function getText($value) {
         $jobs = array();
         foreach ($value as $id => $job) {
-            $address = new ProfileSettingAddress();
+            $address = Address::formArrayToString($job['w_address']);
             $phones = Phone::formArrayToString($job['w_phone']);
             $jobs[] = 'Entreprise : ' . $job['name'] . ', secteur : ' . $job['subSubSectorName']
                     . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
                     . ', email : ' . $job['w_email']
-                    . ($phones ? ', ' . $phones : '') . ', ' .  $address->getText($job['w_address']);
+                    . ($phones ? ', ' . $phones : '') . ($address ? ', ' . $address : '');
         }
         return implode(' ; ' , $jobs);
     }
@@ -368,39 +319,23 @@ class ProfileSettingJobs extends ProfilePage
         // Build the jobs tree
         $res = XDB::iterRow("SELECT  j.id, j.jobid, je.name, j.sectorid, j.subsectorid, j.subsubsectorid,
                                      s.name, j.description, j.email, j.email_pub, j.url, j.pub,
-                                     je.acronym, je.url, je.email,
-                                     aw.accuracy, aw.text, aw.postalText, aw.postalCode, aw.localityId,
-                                     aw.subAdministrativeAreaId, aw.administrativeAreaId, aw.countryId,
-                                     aw.latitude, aw.longitude, aw.pub, aw.updateTime,
-                                     aw.north, aw.south, aw.east, aw.west,
-                                     ah.accuracy, ah.text, ah.postalText, ah.postalCode, ah.localityId,
-                                     ah.subAdministrativeAreaId, ah.administrativeAreaId, ah.countryId,
-                                     ah.latitude, ah.longitude, ah.pub, ah.updateTime,
-                                     ah.north, ah.south, ah.east, ah.west
+                                     je.acronym, je.url, je.email
                                FROM  profile_job                   AS j
                           LEFT JOIN  profile_job_enum              AS je ON (j.jobid = je.id)
                           LEFT JOIN  profile_job_subsubsector_enum AS s  ON (s.id = j.subsubsectorid)
-                          LEFT JOIN  profile_addresses             AS aw ON (aw.pid = j.pid AND aw.type = 'job'
-                                                                             AND aw.id = j.id)
-                          LEFT JOIN  profile_addresses             AS ah ON (ah.jobid = j.jobid AND ah.type = 'hq')
                               WHERE  j.pid = {?}
                            ORDER BY  j.id",
                             $this->pid());
         $this->values['jobs'] = array();
 
+        $compagnies = array();
         if ($res->numRows() > 0) {
             while (list($id, $jobid, $name, $sector, $subSector, $subSubSector,
                         $subSubSectorName, $description, $w_email, $w_emailPub, $w_url, $pub,
                         $hq_acronym, $hq_url, $hq_email,
-                        $w_accuracy, $w_text, $w_postalText, $w_postalCode, $w_localityId,
                         $w_subAdministrativeAreaId, $w_administrativeAreaId, $w_countryId,
-                        $w_latitude, $w_longitude, $w_pub, $w_updateTime,
-                        $w_north, $w_south, $w_east, $w_west,
-                        $hq_accuracy, $hq_text, $hq_postalText, $hq_postalCode, $hq_localityId,
-                        $hq_subAdministrativeAreaId, $hq_administrativeAreaId, $hq_countryId,
-                        $hq_latitude, $hq_longitude, $hq_pub, $hq_updateTime,
-                        $hq_north, $hq_south, $hq_east, $hq_west,
                        ) = $res->next()) {
+                $compagnies[] = $jobid;
                 $this->values['jobs'][] = array(
                     'id'               => $id,
                     'jobid'            => $jobid,
@@ -417,45 +352,17 @@ class ProfileSettingJobs extends ProfilePage
                     'hq_acronym'       => $hq_acronym,
                     'hq_url'           => $hq_url,
                     'hq_email'         => $hq_email,
-                    'w_address'        => array(
-                        'accuracy'                => $w_accuracy,
-                        'text'                    => $w_text,
-                        'postalText'              => $w_postalText,
-                        'postalCode'              => $w_postalCode,
-                        'localityId'              => $w_localityId,
-                        'subAdministrativeAreaId' => $w_subAdministrativeAreaId,
-                        'administrativeAreaId'    => $w_administrativeAreaId,
-                        'countryId'               => $w_countryId,
-                        'latitude'                => $w_latitude,
-                        'longitude'               => $w_longitude,
-                        'pub'                     => $w_pub,
-                        'updateTime'              => $w_updateTime,
-                        'north'                   => $w_north,
-                        'south'                   => $w_south,
-                        'east'                    => $w_east,
-                        'west'                    => $w_west,
-                    ),
-                    'hq_address'       => array(
-                        'accuracy'                => $hq_accuracy,
-                        'text'                    => $hq_text,
-                        'postalText'              => $hq_postalText,
-                        'postalCode'              => $hq_postalCode,
-                        'localityId'              => $hq_localityId,
-                        'subAdministrativeAreaId' => $hq_subAdministrativeAreaId,
-                        'administrativeAreaId'    => $hq_administrativeAreaId,
-                        'countryId'               => $hq_countryId,
-                        'latitude'                => $hq_latitude,
-                        'longitude'               => $hq_longitude,
-                        'pub'                     => $hq_pub,
-                        'updateTime'              => $hq_updateTime,
-                        'north'                   => $hq_north,
-                        'south'                   => $hq_south,
-                        'east'                    => $hq_east,
-                        'west'                    => $hq_west,
-                    ),
                 );
             }
 
+            $it = Address::iterate(array($this->pid()), array(Address::LINK_JOB));
+            while ($address = $it->next()) {
+                $this->values['jobs'][$address->jobid]['w_address'] = $address->toFormArray();
+            }
+            $it = Address::iterate(array(), array(Address::LINK_COMPANY), $companies);
+            while ($address = $it->next()) {
+                $this->values['jobs'][$address->jobId()]['h_address'] = $address->toFormArray();
+            }
             $it = Phone::iterate(array($this->pid()), array(Phone::LINK_JOB));
             while ($phone = $it->next()) {
                 $this->values['jobs'][$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray();
@@ -488,9 +395,16 @@ class ProfileSettingJobs extends ProfilePage
 
             foreach ($this->values['jobs'] as $id => &$job) {
                 $phone = new Phone();
+                $address = new Address();
                 if (!isset($job['w_phone'])) {
                     $job['w_phone'] = array(0 => $phone->toFormArray());
                 }
+                if (!isset($job['w_address'])) {
+                    $job['w_address'] = $address->toFormArray();
+                }
+                if (!isset($job['h_address'])) {
+                    $job['h_address'] = $address->toFormArray();
+                }
             }
 
             $job['w_email_new'] = '';
@@ -503,31 +417,6 @@ class ProfileSettingJobs extends ProfilePage
             if (!isset($job['w_email_pub'])) {
                 $job['w_email_pub'] = 'private';
             }
-            if (!$job['hq_address']['text']) {
-                $job['hq_address'] = array(
-                    'text'                    => '',
-                    'accuracy'                => '',
-                    'postalText'              => '',
-                    'postalCode'              => '',
-                    'administrativeAreaId'    => '',
-                    'subAdministrativeAreaId' => '',
-                    'localityId'              => '',
-                    'countryId'               => '',
-                    'latitude'                => '',
-                    'longitude'               => '',
-                    'north'                   => '',
-                    'south'                   => '',
-                    'east'                    => '',
-                    'west'                    => '',
-                    'cedex'                   => '',
-                    'updateTime'              => '',
-                    'changed'                 => '0',
-                    'removed'                 => '0',
-                );
-            }
-            $job['w_address']['cedex'] = '';
-            $job['w_address']['changed'] = '0';
-            $job['w_address']['removed'] = '0';
         } else {
             $this->values['jobs'][] = $this->settings['jobs']->emptyJob();
         }
index 70d0ab5..3117b29 100644 (file)
@@ -192,38 +192,6 @@ class ProfileSettingDate extends ProfileNoSave
     }
 }
 
-abstract class ProfileSettingGeocoding implements ProfileSetting
-{
-    protected function geocodeAddress(array &$address, &$success)
-    {
-        require_once 'geocoding.inc.php';
-        $success = true;
-        if (isset($address['changed']) && $address['changed'] == 1) {
-            $gmapsGeocoder = new GMapsGeocoder();
-            $address = $gmapsGeocoder->getGeocodedAddress($address);
-            if (isset($address['geoloc'])) {
-                $success = false;
-            }
-        } elseif (@$address['changed'] && !@$address['text']) {
-            $address = empty_address();
-            $address['pub'] = 'private';
-        }
-        if (isset($address['geoloc_choice']) && ($address['geoloc_choice'] == 0)) {
-            $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
-            $mailer->assign('text', $address['text']);
-            $mailer->assign('geoloc', $address['geoloc']);
-            $mailer->send();
-            $gmapsGeocoder = new GMapsGeocoder();
-            $address = $gmapsGeocoder->stripGeocodingFromAddress($address);
-        }
-    }
-
-    public function getText($value) {
-        return $value;
-    }
-}
-
-
 abstract class ProfilePage implements PlWizardPage
 {
     protected $wizard;
index 6539391..2ef9f3c 100644 (file)
 {*                                                                        *}
 {**************************************************************************}
 
-{if $address.geoloc}
+{if t($address.geocodedText)}
 <div class="erreur center {$prefid}_geoloc">
   Le géocodage n'a pas donné un résultat certain&nbsp;! Tu as le choix entre&nbsp;:
 </div>
 <div class="{$prefid}_geoloc">
   <ul>
-    <li><a href="javascript:validGeoloc('{$prefid}','{$id}',0)" style="color: red">ton adresse (à gauche)</a>&nbsp;;</li>
-    <li><a href="javascript:validGeoloc('{$prefid}','{$id}',1)" style="color: green">notre suggestion (à droite)</a>&nbsp;;</li>
     <li><a href="javascript:validGeoloc('{$prefid}','{$id}',2)"
            title="Garder le texte de l'adresse que tu as renseignée tout en utilisant les informations trouvées par le géocodage pour te localiser sur le planisphère et dans lors d'une recherche dans l'annuaire.">
       le texte de ton adresse localisé à l'endroit que nous te suggérons</a>.</li>
+    <li><a href="javascript:validGeoloc('{$prefid}','{$id}',0)" style="color: red">ton adresse (à gauche)</a>&nbsp;;</li>
+    <li><a href="javascript:validGeoloc('{$prefid}','{$id}',1)" style="color: green">notre suggestion (à droite)</a>&nbsp;;</li>
   </ul>
 </div>
 {/if}
 
 <div>
   <textarea name="{$prefname}[text]" cols="30" rows="4" onkeyup="addressChanged('{$prefid}')"
-            {if $address.geoloc}class="error"{/if}>{$address.text}</textarea>
-{if $address.geoloc}
-  <textarea cols="30" rows="4" class="valid {$prefid}_geoloc"
-            name="{$prefname}[geoloc]">{$address.geoloc}</textarea>
+            {if t($address.geocodedText)}class="error"{/if}>{$address.text}</textarea>
+{if t($address.geocodedText)}
+  <textarea cols="30" rows="4" class="valid {$prefid}_geoloc">{$address.geocodedText}</textarea>
 {/if}
 </div>
-{if $address.geoloc}
-<input type="hidden" name="{$prefname}[geoloc_choice]" value="1" />
-<input type="hidden" name="{$prefname}[geoloc]" value="{$address.geoloc}" />
+{if t($address.geocodedText)}
+<input type="hidden" name="{$prefname}[geocodeChosen]" value="1" />
+<input type="hidden" name="{$prefname}[geocodedText]" value="{$address.geocodedText}" />
 <input type="hidden" name="{$prefname}[geocodedPostalText]" value="{$address.geocodedPostalText}" />
-<input type="hidden" name="{$prefname}[updateTime]" value="{$address.updateTime}" />
 {/if}
 <input type="hidden" name="{$prefname}[accuracy]" value="{$address.accuracy}" />
 <input type="hidden" name="{$prefname}[postalText]" value="{$address.postalText}" />
@@ -62,8 +60,6 @@
 <input type="hidden" name="{$prefname}[south]" value="{$address.south}" />
 <input type="hidden" name="{$prefname}[east]" value="{$address.east}" />
 <input type="hidden" name="{$prefname}[west]" value="{$address.west}" />
-<input type="hidden" name="{$prefname}[cedex]" value="{$address.cedex}" />
-<input type="hidden" name="{$prefname}[updateTime]" value="{$address.updateTime}" />
 <input type="hidden" name="{$prefname}[changed]" value="0" />
 <input type="hidden" name="{$prefname}[removed]" value="0" />
 
index 48319b6..ecd7128 100644 (file)
   </tr>
   <tr class="pair">
     <td>
-      {foreach from=$address.tel key=t item=tel}
+      {foreach from=$address.phones key=t item=tel}
         <div id="{"`$prefid`_tel_`$t`"}" style="clear: both">
-          {include file="profile/phone.tpl" prefname="`$prefname`[tel]"
+          {include file="profile/phone.tpl" prefname="`$prefname`[phones]"
                    prefid="`$prefid`_tel" telid=$t tel=$tel}
         </div>
       {/foreach}
-      {if $address.tel|@count eq 0}
+      {if $address.phones|@count eq 0}
         <div id="{"`$prefid`_tel_0"}" style="clear: both">
-          {include file="profile/phone.tpl" prefname="`$prefname`[tel]" prefid="`$prefid`_tel" telid=0 tel=0}
+          {include file="profile/phone.tpl" prefname="`$prefname`[phones]" prefid="`$prefid`_tel" telid=0 tel=0}
         </div>
       {/if}
       <div id="{$prefid}_tel_add" class="center" style="clear: both; padding-top: 4px">
-        <a href="javascript:addTel('{$prefid}_tel','{$prefname}[tel]')">
+        <a href="javascript:addTel('{$prefid}_tel','{$prefname}[phones]')">
           {icon name=add title="Ajouter un numéro de téléphone"} Ajouter un numéro de téléphone
         </a>
       </div>