Fixes erroneous queries.
[platal.git] / modules / profile / jobs.inc.php
index f0d30d7..723370b 100644 (file)
@@ -42,7 +42,7 @@ class ProfileSettingJob implements ProfileSetting
                              );
     }
 
-    public function emptyJob()
+    private function emptyJob()
     {
         $address = new Address();
         $phone = new Phone();
@@ -62,6 +62,75 @@ class ProfileSettingJob implements ProfileSetting
         );
     }
 
+    private function fetchJobs(ProfilePage $page)
+    {
+        // Build the jobs tree
+        $jobs  = XDB::fetchAllAssoc('SELECT  j.id, j.jobid, je.name,
+                                             j.description, j.email AS w_email,
+                                             j.email_pub AS w_email_pub,
+                                             j.url AS w_url, j.pub
+                                       FROM  profile_job      AS j
+                                  LEFT JOIN  profile_job_enum AS je ON (j.jobid = je.id)
+                                      WHERE  j.pid = {?}
+                                   ORDER BY  j.id',
+                                    $page->pid());
+
+        if (empty($jobs)) {
+            return array($this->emptyJob());
+        }
+
+        $compagnies = array();
+        $backtrack = array();
+        foreach ($jobs as $key=>$job) {
+            $compagnies[] = $job['jobid'];
+            $backtrack[$job['id']] = $key;
+        }
+
+        $it = Address::iterate(array($page->pid()), array(Address::LINK_JOB));
+        while ($address = $it->next()) {
+            $jobs[$address->id]['w_address'] = $address->toFormArray();
+        }
+        $it = Phone::iterate(array($page->pid()), array(Phone::LINK_JOB));
+        while ($phone = $it->next()) {
+            $jobs[$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray();
+        }
+        $res = XDB::iterator("SELECT  e.jtid, e.full_name, j.jid
+                                FROM  profile_job_term_enum AS e
+                          INNER JOIN  profile_job_term AS j USING(jtid)
+                               WHERE  pid = {?}
+                            ORDER BY  j.jid",
+                             $page->pid());
+        while ($term = $res->next()) {
+            // $jid is the ID of the job among this user's jobs
+            $jid = $term['jid'];
+            if (!isset($backtrack[$jid])) {
+                continue;
+            }
+            $job =& $jobs[$backtrack[$jid]];
+            if (!isset($job['terms'])) {
+                $job['terms'] = array();
+            }
+            $job['terms'][] = $term;
+        }
+
+        $phone = new Phone();
+        $address = new Address();
+        foreach ($jobs as $id => &$job) {
+            if (!isset($job['w_phone'])) {
+                $job['w_phone'] = array(0 => $phone->toFormArray());
+            }
+            if (!isset($job['w_address'])) {
+                $job['w_address'] = $address->toFormArray();
+            }
+
+            $job['w_email_new'] = '';
+            if (!isset($job['w_email_pub'])) {
+                $job['w_email_pub'] = 'private';
+            }
+        }
+        return $jobs;
+    }
+
     private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success)
     {
         if ($job['w_email'] == "new@example.org") {
@@ -118,19 +187,20 @@ class ProfileSettingJob implements ProfileSetting
         unset($job['new']);
     }
 
+
+
     public function value(ProfilePage &$page, $field, $value, &$success)
     {
-        require_once 'validations.inc.php';
         $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise');
         $entr_val = 0;
 
         $init = false;
         if (is_null($value)) {
-            $value = $page->values['jobs'];
+            $value = $this->fetchJobs($page);
             $init = true;
         }
         $success = true;
-        foreach ($value as $key => &$job) {
+        foreach ($value as $key => $job) {
             $job['name'] = trim($job['name']);
             if ($job['name'] == '' && $entreprise) {
                 $job['tmp_name'] = $entreprise[$entr_val]->name;
@@ -139,7 +209,7 @@ class ProfileSettingJob implements ProfileSetting
                 if ($job['description'] == '' && $job['w_url'] == ''
                     && $job['w_address']['text'] == '' && $job['w_email'] == ''
                     && count($job['w_phone']) >= 1 && $job['w_phone'][0]['display'] == '') {
-                    array_splice($value, $key, 1);
+                    unset($value[$key]);
                     continue;
                 }
 
@@ -153,8 +223,13 @@ class ProfileSettingJob implements ProfileSetting
                 if ($job['name'] == '' && $entreprise) {
                     $entreprise[$entr_val - 1]->clean();
                 }
-                array_splice($value, $key, 1);
+                unset($value[$key]);
+                continue;
             }
+            if (!isset($job['pub']) || !$job['pub']) {
+                $job['pub'] = 'private';
+            }
+            $value[$key] = $job;
         }
         foreach ($value as $key => &$job) {
             $address = new Address($job['w_address']);
@@ -173,40 +248,53 @@ class ProfileSettingJob implements ProfileSetting
         XDB::execute("DELETE FROM  profile_job
                             WHERE  pid = {?}",
                      $page->pid());
-        Address::delete($page->pid(), Address::LINK_JOB);
+        XDB::execute("DELETE FROM  profile_job_term
+                            WHERE  pid = {?}",
+                     $page->pid());
+        Address::deleteAddresses($page->pid(), Address::LINK_JOB);
         Phone::deletePhones($page->pid(), Phone::LINK_JOB);
         $terms_values = array();
         foreach ($value as $id => &$job) {
             if (isset($job['name']) && $job['name']) {
                 if (isset($job['jobid']) && $job['jobid']) {
-                    XDB::execute('INSERT INTO  profile_job (pid, id, description, email, url, pub, email_pub, jobid)
+                    XDB::execute('INSERT INTO  profile_job (pid, id, description, email,
+                                                            url, pub, email_pub, jobid)
                                        VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
-                                 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
+                                 $page->pid(), $id, $job['description'], $job['w_email'],
+                                 $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
                 } else {
-                    XDB::execute('INSERT INTO  profile_job (pid, id, description, email, url, pub, email_pub)
+                    XDB::execute('INSERT INTO  profile_job (pid, id, description, email,
+                                                            url, pub, email_pub)
                                        VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
-                                 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
+                                 $page->pid(), $id, $job['description'], $job['w_email'],
+                                 $job['w_url'], $job['pub'], $job['w_email_pub']);
                 }
-                $address = new Address(array_merge($job['w_address'], array('pid' => $page->pid(), 'id' => $id, 'type' => Address::LINK_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) {
-                        $terms_values[] = '('.XDB::escape($page->pid()).', '. XDB::escape($id).', '.XDB::escape($term['jtid']).', "original")';
+                        $terms_values[] = XDB::format('({?}, {?}, {?}, {?})',
+                                                      $page->pid(), $id, $term['jtid'], "original");
                     }
                 }
             }
         }
         if (count($terms_values) > 0) {
-            XDB::execute('INSERT INTO  profile_job_term (pid, jid, jtid, computed)
-                               VALUES  '.implode(', ', $terms_values));
+            XDB::rawExecute('INSERT INTO  profile_job_term (pid, jid, jtid, computed)
+                                  VALUES  ' . implode(', ', $terms_values) . '
+                 ON DUPLICATE KEY UPDATE  computed = VALUES(computed)');
         }
     }
 
-    public function getText($value) {
+    public function getText($value)
+    {
         $jobs = array();
         foreach ($value as $id => $job) {
-            $address = Address::formArrayToString($job['w_address']);
+            $address = Address::formArrayToString(array($job['w_address']));
             $phones = Phone::formArrayToString($job['w_phone']);
             // TODO: add jobterms here.
             $jobs[] = 'Entreprise : ' . $job['name']
@@ -236,8 +324,10 @@ class ProfileSettingCorps implements ProfileSetting
 
     public function save(ProfilePage &$page, $field, $value)
     {
-        XDB::execute('REPLACE INTO  profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
-                            VALUES  ({?}, {?}, {?}, {?}, {?})',
+        XDB::execute('INSERT INTO  profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
+                           VALUES  ({?}, {?}, {?}, {?}, {?})
+          ON DUPLICATE KEY UPDATE  original_corpsid = VALUES(original_corpsid), current_corpsid = VALUES(current_corpsid),
+                                   rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
                       $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
     }
 
@@ -275,85 +365,6 @@ class ProfilePageJobs extends ProfilePage
                               $this->pid());
             $this->values['cv'] = $res->fetchOneCell();
         }
-
-        // Build the jobs tree
-        $res = XDB::iterRow('SELECT  j.id, j.jobid, je.name, j.description, j.email, j.email_pub,
-                                     j.url, j.pub
-                               FROM  profile_job      AS j
-                          LEFT JOIN  profile_job_enum AS je ON (j.jobid = je.id)
-                              WHERE  j.pid = {?}
-                           ORDER BY  j.id',
-                            $this->pid());
-        $this->values['jobs'] = array();
-
-        $compagnies = array();
-        if ($res->numRows() > 0) {
-            while (list($id, $jobid, $name, $description, $w_email, $w_emailPub, $w_url, $pub) = $res->next()) {
-                $compagnies[] = $jobid;
-                $this->values['jobs'][] = array(
-                    'id'               => $id,
-                    'jobid'            => $jobid,
-                    'name'             => $name,
-                    'description'      => $description,
-                    'pub'              => $pub,
-                    'w_email'          => $w_email,
-                    'w_email_pub'      => $w_emailPub,
-                    'w_url'            => $w_url,
-                );
-            }
-
-            $it = Address::iterate(array($this->pid()), array(Address::LINK_JOB));
-            while ($address = $it->next()) {
-                $this->values['jobs'][$address->jobid]['w_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();
-            }
-            $res = XDB::iterator("SELECT  e.jtid, e.full_name, j.jid AS jobid
-                                    FROM  profile_job_term_enum AS e
-                              INNER JOIN  profile_job_term AS j USING(jtid)
-                                   WHERE  pid = {?}
-                                ORDER BY  j.jid",
-                                 $this->pid());
-            $i = 0;
-            $jobNb = count($this->values['jobs']);
-            while ($term = $res->next()) {
-                $jobid = $term['jobid'];
-                while ($i < $jobNb && $this->values['jobs'][$i]['id'] < $jobid) {
-                    $i++;
-                }
-                if ($i >= $jobNb) {
-                    break;
-                }
-                $job =& $this->values['jobs'][$i];
-                if ($job['id'] != $jobid) {
-                    continue;
-                }
-                if (!isset($job['terms'])) {
-                    $job['terms'] = array();
-                }
-                $job['terms'][] = $term;
-            }
-
-            $phone = new Phone();
-            $address = new Address();
-            foreach ($this->values['jobs'] as $id => &$job) {
-                if (!isset($job['w_phone'])) {
-                    $job['w_phone'] = array(0 => $phone->toFormArray());
-                }
-                if (!isset($job['w_address'])) {
-                    $job['w_address'] = $address->toFormArray();
-                }
-            }
-
-            $job['w_email_new'] = '';
-            if (!isset($job['w_email_pub'])) {
-                $job['w_email_pub'] = 'private';
-            }
-        } else {
-            $this->values['jobs'][] = $this->settings['jobs']->emptyJob();
-        }
     }
 
     protected function _saveData()