Replace sectors by job terms in profile and search (job and mentoring).
[platal.git] / include / profilefields.inc.php
index dda573d..506b8ac 100644 (file)
  */
 abstract class ProfileField
 {
+    public static $fields = array(
+        Profile::FETCH_ADDRESSES      => 'ProfileAddresses',
+        Profile::FETCH_CORPS          => 'ProfileCorps',
+        Profile::FETCH_EDU            => 'ProfileEducation',
+        Profile::FETCH_JOBS           => 'ProfileJobs',
+        Profile::FETCH_MEDALS         => 'ProfileMedals',
+        Profile::FETCH_NETWORKING     => 'ProfileNetworking',
+        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
      */
     public $pid;
@@ -34,14 +48,15 @@ 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, $visibility)
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
         return PlIteratorUtils::emptyIterator();
     }
 
-    public static function buildForPID($cls, $pid, $visibility)
+    public static function buildForPID($cls, $pid, ProfileVisibility $visibility)
     {
         $res = self::buildFromPIDs($cls, array($pid), $visibility);
         return array_pop($res);
@@ -53,7 +68,7 @@ abstract class ProfileField
      * @param $visibility An array of allowed visibility contexts
      * @return An array of $pid => ProfileField
      */
-    public static function buildFromPIDs($cls, array $pids, $visibility)
+    public static function buildFromPIDs($cls, array $pids, ProfileVisibility $visibility)
     {
         $it = new ProfileFieldIterator($cls, $pids, $visibility);
         $res = array();
@@ -63,7 +78,7 @@ abstract class ProfileField
         return $res;
     }
 
-    public static function getForPID($cls, $pid, $visibility)
+    public static function getForPID($cls, $pid, ProfileVisibility $visibility)
     {
         $it = new ProfileFieldIterator($cls, array($pid), $visibility);
         return $it->next();
@@ -77,8 +92,11 @@ class ProfileFieldIterator implements PlIterator
     private $data;
     private $cls;
 
-    public function __construct($cls, array $pids, $visibility)
+    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;
     }
@@ -111,36 +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;
-        }
-    }
-}
-// }}}
 // {{{ class Company
 class Company
 {
@@ -163,7 +151,7 @@ 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;
         }
     }
@@ -184,8 +172,9 @@ class Job
     public $id;
 
     public $company = null;
-    private $phones = array();
-    private $address = null;
+    public $phones = array();
+    public $address = null;
+    public $terms = array();
 
     public $jobid;
 
@@ -206,6 +195,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()
@@ -213,19 +207,48 @@ class Job
         return $this->phones;
     }
 
+    public function address()
+    {
+        return $this->address;
+    }
+
     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->link_type == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) {
             $this->address = $address;
         }
     }
+
+    public function addTerm(JobTerm &$term)
+    {
+        $this->terms[$term->jtid] = $term;
+    }
+}
+// }}}
+// {{{ class JobTerm
+class JobTerm
+{
+    public $jtid;
+    public $full_name;
+    public $pid;
+    public $jid;
+
+    /** Fields are:
+     * pid, jid, jtid, full_name
+     */
+    public function __construct($data)
+    {
+        foreach ($data as $key => $val) {
+            $this->$key = $val;
+        }
+    }
 }
 // }}}
 // {{{ class Address
@@ -236,7 +259,8 @@ class Address
     const LINK_PROFILE = 'home';
 
     public $flags;
-    public $link_id;
+    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;
@@ -264,10 +288,20 @@ class Address
         $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->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && $phone->pid == $this->pid) {
-            $this->phones[] = $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;
         }
     }
 
@@ -278,6 +312,9 @@ class Address
 
     public function hasFlag($flag)
     {
+        if (!$this->flags instanceof PlFlagSet) {
+            $this->flags = new PlFlagSet($this->flags);
+        }
         return $this->flags->hasFlag($flag);
     }
 }
@@ -302,6 +339,8 @@ class Education
     public $degree_short;
     public $degree_level;
 
+    public $field;
+
     public function __construct(array $data)
     {
         foreach ($data as $key => $val) {
@@ -320,7 +359,6 @@ class ProfileEducation extends ProfileField
     public function __construct(PlInnerSubIterator $it)
     {
         $this->pid = $it->value();
-        $this->visibility = Profile::VISIBILITY_PUBLIC;
         while ($edu = $it->next()) {
             $this->educations[$edu['id']] = new Education($edu);
         }
@@ -354,7 +392,7 @@ class ProfileEducation extends ProfileField
         return $educations;
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
         $data = XDB::iterator('SELECT  pe.id, pe.pid,
                                        pe.entry_year, pe.grad_year, pe.program, pe.flags,
@@ -389,15 +427,16 @@ class ProfileMedals extends ProfileField
         }
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pm.pid, pm.mid, pm.gid, pme.text, pme.img
+        $data = XDB::iterator('SELECT  pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
                                  FROM  profile_medals AS pm
                             LEFT JOIN  profiles AS p ON (pm.pid = p.pid)
                             LEFT JOIN  profile_medal_enum AS pme ON (pme.id = pm.mid)
+                            LEFT JOIN  profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
                                 WHERE  pm.pid IN {?} AND p.medals_pub IN {?}
                              ORDER BY  ' . XDB::formatCustomOrder('pm.pid', $pids),
-                                $pids, $visibility);
+                                $pids, $visibility->levels());
 
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }
@@ -408,21 +447,24 @@ class ProfileNetworking extends ProfileField
 {
     private $networks = array();
 
-    public function __construct(PlIterator $it)
+    public function __construct(PlInnerSubIterator $it)
     {
+        $this->pid = $it->value();
         while ($network = $it->next()) {
-            $this->networks[$network['nwid']] = $network['address'];
+            $network['network_type'] = new PlFlagSet($network['network_type']);
+            $this->networks[$network['id']] = $network;
         }
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pid, nwid, address, network_type
-                                 FROM  profile_networking
+        $data = XDB::iterator('SELECT  pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
+                                 FROM  profile_networking AS pn
+                            LEFT JOIN  profile_networking_enum AS pne USING(nwid)
                                 WHERE  pid IN {?} AND pub IN {?}
                              ORDER BY  ' . XDB::formatCustomOrder('pid', $pids) . ',
-                                       network_type, nwid',
-                               $pids, $visibility);
+                                       pn.nwid, id',
+                               $pids, $visibility->levels());
 
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }
@@ -432,53 +474,21 @@ class ProfileNetworking extends ProfileField
         $nws = array();
         $nb = 0;
         foreach ($this->networks as $id => $nw) {
-            // XXX hardcoded reference to web site index
-            if (
-                (($flags & self::NETWORKING_WEB) && $nw['network_type'] == 0)
-                ||
-                (! ($flags & self::NETWORKING_WEB))
-            ) {
+            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)) {
                 $nws[$id] = $nw;
                 ++$nb;
-            }
-            if ($nb >= $limit) {
-                break;
+                if (isset($limit) && $nb >= $limit) {
+                    break;
+                }
             }
         }
         return $nws;
     }
 }
 // }}}
-// {{{ class ProfilePhoto                             [ Field ]
-class ProfilePhoto extends ProfileField
-{
-    public $pic;
-
-    public function __construct(array $data)
-    {
-        if ($data == null || count($data) == 0) {
-            $this->pic = null;
-        } else {
-            $this->pid = $data['pid'];
-            $this->pic = PlImage::fromDATA($data['attach'],
-                                           $data['attachmime'],
-                                           $data['x'],
-                                           $data['y']);
-        }
-    }
-
-    public static function fetchData(array $pids, $visibility)
-    {
-        $data = XDB::iterator('SELECT  *
-                                 FROM  profile_photos
-                                WHERE  pid IN {?} AND attachmime IN (\'jpeg\', \'png\') AND pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                               $pids, $visibility);
-
-        return $data;
-    }
-}
-// }}}
 // {{{ class ProfileCorps                             [ Field ]
 class ProfileCorps extends ProfileField
 {
@@ -502,7 +512,7 @@ class ProfileCorps extends ProfileField
         }
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
         $data = XDB::iterator('SELECT  pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
                                        pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
@@ -515,14 +525,67 @@ class ProfileCorps extends ProfileField
                             LEFT JOIN  profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
                             LEFT JOIN  profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
                             LEFT JOIN  profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
-                                WHERE  pc.pid IN {?} AND pc.corps_pub IN {?}
+                                WHERE  pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
                              ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                                $pids, $visibility);
+                                $pids, $visibility->levels());
 
         return $data;
     }
 }
 // }}}
+// {{{ class ProfileMentoringSectors                  [ Field ]
+class ProfileMentoringSectors extends ProfileField
+{
+    public $sectors = array();
+
+    public function __construct(PlInnerSubIterator $it)
+    {
+        $this->pid = $it->value();
+        while ($sector = $it->next()) {
+            $this->sectors[] = $sector;
+        }
+    }
+
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  pms.pid, pjse.name AS sector, pjsse.name AS subsector
+                                 FROM  profile_mentor_sector AS pms
+                            LEFT JOIN  profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
+                            LEFT JOIN  profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
+                                WHERE  pms.pid IN {?}
+                             ORDER BY  ' . XDB::formatCustomOrder('pms.pid', $pids),
+                                $pids);
+
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+    }
+}
+// }}}
+// {{{ class ProfileMentoringCountries                [ Field ]
+class ProfileMentoringCountries extends ProfileField
+{
+    public $countries = array();
+
+    public function __construct(PlInnerSubIterator $it)
+    {
+        $this->pid = $it->value();
+        while ($country = $it->next()) {
+            $this->countries[$country['id']] = $country['name'];
+        }
+    }
+
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  pmc.pid, pmc.country AS id, gc.countryFR AS name
+                                 FROM  profile_mentor_country AS pmc
+                            LEFT JOIN  geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
+                                WHERE  pmc.pid IN {?}
+                             ORDER BY  ' . XDB::formatCustomOrder('pmc.pid', $pids),
+                                $pids);
+
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+    }
+}
+// }}}
 
 /** Loading of data for a Profile :
  * 1) load jobs, addresses, phones
@@ -530,6 +593,42 @@ class ProfileCorps extends ProfileField
  * 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
 {
@@ -552,15 +651,13 @@ class ProfileAddresses extends ProfileField
         $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_PERSO) && $addr->link_type == Address::LINK_PROFILE)
                 ||
-                (($flags & Profile::ADDRESS_PRO) && $addr->link_type == 'job')
+                (($flags & Profile::ADDRESS_PRO) && $addr->link_type == Address::LINK_JOB)
             ) {
                 $res[] = $addr;
                 $nb++;
@@ -572,10 +669,10 @@ class ProfileAddresses extends ProfileField
         return $res;
     }
 
-    public static function fetchData(array $pids, $visibility)
+    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,
+                                       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
@@ -586,17 +683,17 @@ class ProfileAddresses extends ProfileField
                             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);
+                               $pids, $visibility->levels());
 
         return PlIteratorUtils::subIterator($data, 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);
             }
         }
     }
@@ -620,23 +717,21 @@ class ProfilePhones extends ProfileField
         $phones = array();
         $nb = 0;
         foreach ($this->phones as $id => $phone) {
-            $phones[$id] = $phone;
-            ++$nb;
-            if ($limit != null && $nb == $limit) {
-                break;
+            if ($phone->hasFlags($flags)) {
+                $phones[$id] = $phone;
+                ++$nb;
+                if ($limit != null && $nb == $limit) {
+                    break;
+                }
             }
         }
         return $phones;
     }
 
-    public static function fetchData(array $pids, $visibility)
+    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
-                                 FROM  profile_phones
-                                WHERE  pid IN {?} AND pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                                 $pids, $visibility);
-        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+        $it = Phone::iterate($pids, array(), array(), $visibility->levels());
+        return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
     }
 }
 // }}}
@@ -653,7 +748,7 @@ class ProfileJobs extends ProfileField
         }
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
         CompanyList::preload($pids);
         $data = XDB::iterator('SELECT  pj.id, pj.pid, pj.description, pj.url as user_site,
@@ -667,7 +762,7 @@ class ProfileJobs extends ProfileField
                                 WHERE  pj.pid IN {?} AND pj.pub IN {?}
                              ORDER BY  ' . XDB::formatCustomOrder('pid', $pids) . ',
                                        pj.id',
-                                 $visibility, $pids, $visibility);
+                                 $visibility->levels(), $pids, $visibility->levels());
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }
 
@@ -687,15 +782,15 @@ 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);
             }
         }
     }
 
-    public static function addAddresses(ProfileAddresses $addresses)
+    public function addAddresses(ProfileAddresses $addresses)
     {
         $a = $addresses->get(Profile::ADDRESS_PRO);
         foreach ($a as $address) {
@@ -705,16 +800,70 @@ class ProfileJobs extends ProfileField
         }
     }
 
-    public static function addCompanies(array $companies)
+    public function addCompanies(array $companies)
     {
-        foreach ($this->jobs as $job)
-        {
-            $job->company = $companies[$job->jobid];
+        foreach ($this->jobs as $job) {
+            $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
 {
@@ -742,14 +891,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;
         }