Prevents annoying warning.
[platal.git] / modules / profile / jobs.inc.php
index ba594b9..83dcade 100644 (file)
@@ -83,29 +83,30 @@ class ProfileSettingJob implements ProfileSetting
         $backtrack = array();
         foreach ($jobs as $key=>$job) {
             $compagnies[] = $job['jobid'];
-            $backtrack[$job['jobid']] = $key;
+            $backtrack[$job['id']] = $key;
         }
 
         $it = Address::iterate(array($page->pid()), array(Address::LINK_JOB));
         while ($address = $it->next()) {
-            $jobs[$address->jobid]['w_address'] = $address->toFormArray();
+            $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 AS jobid
+        $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()) {
-            $jobid = $term['jobid'];
-            if (!isset($backtrack[$jobid])) {
+            // $jid is the ID of the job among this user's jobs
+            $jid = $term['jid'];
+            if (!isset($backtrack[$jid])) {
                 continue;
             }
-            $job =& $jobs[$backtrack[$jobid]];
+            $job =& $jobs[$backtrack[$jid]];
             if (!isset($job['terms'])) {
                 $job['terms'] = array();
             }
@@ -225,6 +226,9 @@ class ProfileSettingJob implements ProfileSetting
                 unset($value[$key]);
                 continue;
             }
+            if (!isset($job['pub']) || !$job['pub']) {
+                $job['pub'] = 'private';
+            }
             $value[$key] = $job;
         }
         foreach ($value as $key => &$job) {
@@ -247,7 +251,7 @@ class ProfileSettingJob implements ProfileSetting
         XDB::execute("DELETE FROM  profile_job_term
                             WHERE  pid = {?}",
                      $page->pid());
-        Address::delete($page->pid(), Address::LINK_JOB);
+        Address::deleteAddresses($page->pid(), Address::LINK_JOB);
         Phone::deletePhones($page->pid(), Phone::LINK_JOB);
         $terms_values = array();
         foreach ($value as $id => &$job) {
@@ -280,15 +284,17 @@ class ProfileSettingJob implements ProfileSetting
             }
         }
         if (count($terms_values) > 0) {
-            XDB::execute('REPLACE 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']
@@ -306,10 +312,11 @@ class ProfileSettingCorps implements ProfileSetting
     {
         $success = true;
         if (is_null($value)) {
-            $res = XDB::query("SELECT  original_corpsid AS original, current_corpsid AS current,
-                                       rankid AS rank, corps_pub AS pub
-                                 FROM  profile_corps
-                                WHERE  pid = {?}",
+            $res = XDB::query('SELECT  c.original_corpsid AS original, e.name AS originalText,
+                                       c.current_corpsid AS current, c.rankid AS rank, c.corps_pub AS pub
+                                 FROM  profile_corps      AS c
+                           INNER JOIN  profile_corps_enum AS e ON (c.original_corpsid = e.id)
+                                WHERE  c.pid = {?}',
                             $page->pid());
             return $res->fetchOneAssoc();
         }
@@ -318,9 +325,19 @@ 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  ({?}, {?}, {?}, {?}, {?})',
-                      $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
+        if (!S::user()->isMe($page->owner)) {
+            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());
+        } else {
+            XDB::execute('INSERT INTO  profile_corps (current_corpsid, rankid, corps_pub, pid)
+                               VALUES  ({?}, {?}, {?}, {?})
+              ON DUPLICATE KEY UPDATE  current_corpsid = VALUES(current_corpsid),
+                                       rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
+                          $value['current'], $value['rank'], $value['pub'], $page->pid());
+        }
     }
 
     public function getText($value)
@@ -376,10 +393,12 @@ class ProfilePageJobs extends ProfilePage
         require_once 'emails.combobox.inc.php';
         fill_email_combobox($page, $this->owner);
 
-        $res = XDB::iterator("SELECT  id, name
-                                FROM  profile_corps_enum
-                            ORDER BY  id = 1 DESC, name");
-        $page->assign('original_corps', $res->fetchAllAssoc());
+        if (!S::user()->isMe($page->owner)) {
+            $res = XDB::iterator('SELECT  id, name
+                                    FROM  profile_corps_enum
+                                ORDER BY  id = 1 DESC, name');
+            $page->assign('original_corps', $res->fetchAllAssoc());
+        }
 
         $res = XDB::iterator("SELECT  id, name
                                 FROM  profile_corps_enum