Adapts geocoding classes to Address class.
[platal.git] / include / profilefields.inc.php
index d2de132..63c0a6e 100644 (file)
@@ -35,6 +35,8 @@ abstract class ProfileField
         Profile::FETCH_PHONES         => 'ProfilePhones',
         Profile::FETCH_MENTOR_SECTOR  => 'ProfileMentoringSectors',
         Profile::FETCH_MENTOR_COUNTRY => 'ProfileMentoringCountries',
+        Profile::FETCH_JOB_TERMS      => 'ProfileJobTerms',
+        Profile::FETCH_MENTOR_TERMS   => 'ProfileMentoringTerms',
     );
 
     /** The profile to which this field belongs
@@ -46,7 +48,8 @@ abstract class ProfileField
      * @param $pids An array of pids
      * @param $visibility The level of visibility fetched fields must have
      * @return a PlIterator yielding data suitable for a "new ProfileBlah($data)"
-     * XXX MUST be reimplemented for each kind of ProfileField
+     *
+     * MUST be reimplemented for each kind of ProfileField.
      */
     public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
@@ -91,6 +94,9 @@ class ProfileFieldIterator implements PlIterator
 
     public function __construct($cls, array $pids, ProfileVisibility $visibility)
     {
+        if (is_numeric($cls) && isset(ProfileField::$fields[$cls])) {
+            $cls = ProfileField::$fields[$cls];
+        }
         $this->data = call_user_func(array($cls, 'fetchData'), $pids, $visibility);
         $this->cls = $cls;
     }
@@ -123,72 +129,6 @@ class ProfileFieldIterator implements PlIterator
 }
 // }}}
 
-// {{{ class Phone
-class Phone
-{
-    const TYPE_FAX    = 'fax';
-    const TYPE_FIXED  = 'fixed';
-    const TYPE_MOBILE = 'mobile';
-    public $type;
-
-    public $search;
-    public $display;
-    public $comment = '';
-
-    const LINK_JOB     = 'job';
-    const LINK_ADDRESS = 'address';
-    const LINK_PROFILE = 'user';
-    const LINK_COMPANY = 'hq';
-    public $link_type;
-    public $link_id;
-
-    /** Fields are :
-     * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id
-     */
-    public function __construct($data)
-    {
-        foreach ($data as $key => $val) {
-            $this->$key = $val;
-        }
-    }
-
-    public function hasFlags($flags) {
-        return $this->hasType($flags) && $this->hasLink($flags);
-    }
-
-    /** Returns true if this phone's type matches the flags
-     */
-    public function hasType($flags) {
-        $flags = $flags & Profile::PHONE_TYPE_ANY;
-        return (
-            ($flags == Profile::PHONE_TYPE_ANY)
-            ||
-            (($flags & Profile::PHONE_TYPE_FAX) && $this->type == self::TYPE_FAX)
-            ||
-            (($flags & Profile::PHONE_TYPE_FIXED) && $this->type == self::TYPE_FIXED)
-            ||
-            (($flags & Profile::PHONE_TYPE_MOBILE) && $this->type == self::TYPE_MOBILE)
-        );
-    }
-
-    /** Returns true if this phone's link matches the flags
-     */
-    public function hasLink($flags) {
-        $flags = $flags & Profile::PHONE_LINK_ANY;
-        return (
-            ($flags == Profile::PHONE_LINK_ANY)
-            ||
-            (($flags & Profile::PHONE_LINK_COMPANY) && $this->link_type == self::LINK_COMPANY)
-            ||
-            (($flags & Profile::PHONE_LINK_JOB) && $this->link_type == self::LINK_JOB)
-            ||
-            (($flags & Profile::PHONE_LINK_ADDRESS) && $this->link_type == self::LINK_ADDRESS)
-            ||
-            (($flags & Profile::PHONE_LINK_PROFILE) && $this->link_type == self::LINK_PROFILE)
-        );
-    }
-}
-// }}}
 // {{{ class Company
 class Company
 {
@@ -211,14 +151,14 @@ class Company
 
     public function setPhone(Phone &$phone)
     {
-        if ($phone->link_type == Phone::LINK_COMPANY && $phone->link_id == $this->id) {
+        if ($phone->linkType() == Phone::LINK_COMPANY && $phone->linkId() == $this->id) {
             $this->phone = $phone;
         }
     }
 
     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;
         }
     }
@@ -226,14 +166,26 @@ 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;
     public $id;
 
     public $company = null;
-    private $phones = array();
-    private $address = null;
+    public $phones = array();
+    public $address = null;
+    public $terms = array();
 
     public $jobid;
 
@@ -254,6 +206,11 @@ class Job
             $this->$key = $val;
         }
         $this->company = CompanyList::get($this->jobid);
+        if (is_null($this->company)) {
+            require_once 'validations.inc.php';
+            $entreprise = ProfileValidate::get_typed_requests($this->pid, 'entreprise');
+            $this->company = new Company(array('name' =>  $entreprise[$this->id]->name));
+        }
     }
 
     public function phones()
@@ -268,73 +225,40 @@ class Job
 
     public function addPhone(Phone &$phone)
     {
-        if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) {
-            $this->phones[] = $phone;
+        if ($phone->linkType() == Phone::LINK_JOB && $phone->linkId() == $this->id && $phone->pid() == $this->pid) {
+            $this->phones[$phone->uniqueId()] = $phone;
         }
     }
 
     public function setAddress(Address $address)
     {
-        if ($address->link_id == 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;
         }
     }
+
+    public function addTerm(JobTerm &$term)
+    {
+        $this->terms[$term->jtid] = $term;
+    }
 }
 // }}}
-// {{{ class Address
-class Address
+// {{{ class JobTerm
+class JobTerm
 {
-    const LINK_JOB     = 'job';
-    const LINK_COMPANY = 'hq';
-    const LINK_PROFILE = 'home';
-
-    public $flags;
-    public $link_id;
-    public $link_type;
-
-    public $text;
-    public $postalCode;
-    public $latitude;
-    public $longitude;
-
-    public $locality;
-    public $subAdministrativeArea;
-    public $administrativeArea;
-    public $country;
-
-    public $comment;
-
-    private $phones = array();
+    public $jtid;
+    public $full_name;
+    public $pid;
+    public $jid;
 
     /** Fields are:
-     * pîd, id, link_id, link_type, flags, text, postcode, country
+     * pid, jid, jtid, full_name
      */
     public function __construct($data)
     {
         foreach ($data as $key => $val) {
             $this->$key = $val;
         }
-        $this->flags = new PlFlagSet($this->flags);
-    }
-
-    public function addPhone(Phone &$phone)
-    {
-        if ($phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && $phone->pid == $this->pid) {
-            $this->phones[] = $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);
     }
 }
 // }}}
@@ -490,16 +414,13 @@ class ProfileNetworking extends ProfileField
 
     public function get($flags, $limit = null)
     {
-        if (!$flags) {
-            $flags = Profile::NETWORKING_ALL;
-        }
         $nws = array();
         $nb = 0;
         foreach ($this->networks as $id => $nw) {
             if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') ||
                 ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') ||
                 ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') ||
-                ($flags & Profile::NETWORKING_ALL)) {
+                ($flags == Profile::NETWORKING_ALL)) {
                 $nws[$id] = $nw;
                 ++$nb;
                 if (isset($limit) && $nb >= $limit) {
@@ -608,80 +529,51 @@ 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
- */
-
 // {{{ 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_ALL)
-                ||
-                (($flags & Profile::ADDRESS_MAIN) && $addr->hasFlag('current'))
-                ||
-                (($flags & Profile::ADDRESS_POSTAL) && $addr->hasFlag('mail'))
-                ||
-                (($flags & Profile::ADDRESS_PERSO) && $addr->link_type == 'home')
-                ||
-                (($flags & Profile::ADDRESS_PRO) && $addr->link_type == '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, 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)
     {
-        $p = $phones->get(0);
+        $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY);
         foreach ($p as $phone) {
-            if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) {
-                $this->addresses[$phone->link_id]->addPhone($phone);
+            if ($phone->linkType() == Phone::LINK_ADDRESS && array_key_exists($phone->linkId(), $this->addresses)) {
+                $this->addresses[$phone->linkId()]->addPhone($phone);
             }
         }
     }
@@ -718,12 +610,8 @@ class ProfilePhones extends ProfileField
 
     public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
-        $data = XDB::iterator('SELECT  tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment, pid
-                                 FROM  profile_phones
-                                WHERE  pid IN {?} AND pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                                 $pids, $visibility->levels());
-        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+        $it = Phone::iterate($pids, array(), array(), $visibility->levels());
+        return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
     }
 }
 // }}}
@@ -774,10 +662,10 @@ class ProfileJobs extends ProfileField
 
     public function addPhones(ProfilePhones $phones)
     {
-        $p = $phones->get(0);
+        $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY);
         foreach ($p as $phone) {
-            if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
-                $this->jobs[$phone->link_id]->addPhones($phone);
+            if ($phone->linkType() == Phone::LINK_JOB && array_key_exists($phone->linkId(), $this->jobs)) {
+                $this->jobs[$phone->linkId()]->addPhone($phone);
             }
         }
     }
@@ -786,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);
             }
         }
     }
@@ -798,9 +686,64 @@ class ProfileJobs extends ProfileField
             $this->company = $companies[$job->jobid];
         }
     }
+
+    public function addJobTerms(ProfileJobTerms $jobterms)
+    {
+        $terms = $jobterms->get();
+        foreach ($terms as $term) {
+            if ($this->pid == $term->pid && array_key_exists($term->jid, $this->jobs)) {
+                $this->jobs[$term->jid]->addTerm(&$term);
+            }
+        }
+    }
 }
 // }}}
+// {{{ class ProfileJobTerms                          [ Field ]
+class ProfileJobTerms extends ProfileField
+{
+    private $jobterms = array();
+
+    public function __construct(PlInnerSubIterator $it)
+    {
+        $this->pid = $it->value();
+        while ($term = $it->next()) {
+            $this->jobterms[] = new JobTerm($term);
+        }
+    }
+
+    public function get()
+    {
+        return $this->jobterms;
+    }
 
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  jt.jtid, jte.full_name, jt.pid, jt.jid
+                                 FROM  profile_job_term AS jt
+                           INNER JOIN  profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id)
+                            LEFT JOIN  profile_job_term_enum AS jte USING(jtid)
+                                WHERE  jt.pid IN {?} AND j.pub IN {?}
+                             ORDER BY  ' . XDB::formatCustomOrder('jt.pid', $pids),
+                                 $pids, $visibility->levels());
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+    }
+}
+// }}}
+// {{{ class ProfileMentoringTerms                    [ Field ]
+class ProfileMentoringTerms extends ProfileJobTerms
+{
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  mt.jtid, jte.full_name, mt.pid
+                                 FROM  profile_mentor_term AS mt
+                            LEFT JOIN  profile_job_term_enum AS jte USING(jtid)
+                                WHERE  mt.pid IN {?}
+                             ORDER BY  ' . XDB::formatCustomOrder('mt.pid', $pids),
+                                $pids);
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+    }
+}
+// }}}
 // {{{ class CompanyList
 class CompanyList
 {
@@ -828,14 +771,25 @@ class CompanyList
                           LEFT JOIN  profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
                                   ' . $join . '
                                   ' . $where);
+        $newcompanies = array();
         while ($row = $it->next()) {
             $cp = new Company($row);
             $addr = new Address($row);
             $cp->setAddress($addr);
+            if (!array_key_exists($row['id'], self::$companies)) {
+                $newcompanies[] = $row['id'];
+            }
             self::$companies[$row['id']] = $cp;
         }
 
-        // TODO: add phones to addresses
+        // Add phones to hq
+        if (count($newcompanies)) {
+            $it = Phone::iterate(array(), array(Phone::LINK_COMPANY), $newcompanies);
+            while ($phone = $it->next()) {
+                self::$companies[$phone->linkId()]->setPhone($phone);
+            }
+        }
+
         if (count($pids) == 0) {
             self::$fullload = true;
         }