Add 'hidden' flag to text descriptions of the 'pub' flag
[platal.git] / modules / profile / jobs.inc.php
CommitLineData
3950bc21
FB
1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 Polytechnique.org *
3950bc21
FB
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
eb54852e 22class ProfileSettingJob implements ProfileSetting
37d44b3b
FB
23{
24 private $pub;
541e8d03
SJ
25 private $email_new;
26 private $email;
27 private $url;
f93fb300 28 private $bool;
37d44b3b
FB
29 private $checks;
30
31 public function __construct()
32 {
12bcf04b 33 $this->pub = new ProfileSettingPub();
541e8d03
SJ
34 $this->email
35 = $this->email_new
12bcf04b
RB
36 = new ProfileSettingEmail();
37 $this->url = new ProfileSettingWeb();
38 $this->bool = new ProfileSettingBool();
541e8d03
SJ
39 $this->checks = array('url' => array('w_url'),
40 'email' => array('w_email'),
41 'pub' => array('pub', 'w_email_pub'),
42 );
37d44b3b
FB
43 }
44
237f662f 45 private function emptyJob()
afaa2cc7 46 {
eb54852e 47 $address = new Address();
972fd3c5 48 $phone = new Phone();
afaa2cc7
SJ
49 return array(
50 'id' => '0',
51 'jobid' => '',
39ee5e75 52 'pub' => 'ax',
afaa2cc7 53 'name' => '',
afaa2cc7
SJ
54 'description' => '',
55 'w_url' => '',
eb54852e 56 'w_address' => $address->toFormArray(),
afaa2cc7 57 'w_email' => '',
39ee5e75 58 'w_email_pub' => 'ax',
afaa2cc7 59 'w_email_new' => '',
972fd3c5
SJ
60 'w_phone' => array(0 => $phone->toFormArray()),
61 'terms' => array()
afaa2cc7
SJ
62 );
63 }
64
237f662f
FB
65 private function fetchJobs(ProfilePage $page)
66 {
67 // Build the jobs tree
68 $jobs = XDB::fetchAllAssoc('SELECT j.id, j.jobid, je.name,
69 j.description, j.email AS w_email,
70 j.email_pub AS w_email_pub,
0863d308 71 j.url AS w_url, j.pub, j.entry_year AS w_entry_year
237f662f
FB
72 FROM profile_job AS j
73 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
74 WHERE j.pid = {?}
75 ORDER BY j.id',
76 $page->pid());
77
78 if (empty($jobs)) {
79 return array($this->emptyJob());
80 }
81
82 $compagnies = array();
83 $backtrack = array();
84 foreach ($jobs as $key=>$job) {
85 $compagnies[] = $job['jobid'];
1a0064e8 86 $backtrack[$job['id']] = $key;
237f662f
FB
87 }
88
07e4a9a0 89 $it = Address::iterate(array($page->pid()), array(Address::LINK_JOB), array(), Visibility::get(Visibility::VIEW_ADMIN));
237f662f 90 while ($address = $it->next()) {
10a574ea 91 $jobs[$address->id]['w_address'] = $address->toFormArray();
237f662f 92 }
07e4a9a0 93 $it = Phone::iterate(array($page->pid()), array(Phone::LINK_JOB), array(), Visibility::get(Visibility::VIEW_ADMIN));
237f662f 94 while ($phone = $it->next()) {
0b53817f 95 $jobs[$phone->link_id]['w_phone'][$phone->id] = $phone->toFormArray();
237f662f 96 }
1a0064e8 97 $res = XDB::iterator("SELECT e.jtid, e.full_name, j.jid
237f662f
FB
98 FROM profile_job_term_enum AS e
99 INNER JOIN profile_job_term AS j USING(jtid)
100 WHERE pid = {?}
101 ORDER BY j.jid",
102 $page->pid());
103 while ($term = $res->next()) {
1a0064e8
RB
104 // $jid is the ID of the job among this user's jobs
105 $jid = $term['jid'];
106 if (!isset($backtrack[$jid])) {
237f662f
FB
107 continue;
108 }
1a0064e8 109 $job =& $jobs[$backtrack[$jid]];
237f662f
FB
110 if (!isset($job['terms'])) {
111 $job['terms'] = array();
112 }
113 $job['terms'][] = $term;
114 }
115
116 $phone = new Phone();
117 $address = new Address();
118 foreach ($jobs as $id => &$job) {
119 if (!isset($job['w_phone'])) {
120 $job['w_phone'] = array(0 => $phone->toFormArray());
121 }
122 if (!isset($job['w_address'])) {
123 $job['w_address'] = $address->toFormArray();
124 }
125
126 $job['w_email_new'] = '';
127 if (!isset($job['w_email_pub'])) {
128 $job['w_email_pub'] = 'private';
129 }
130 }
131 return $jobs;
132 }
133
22771578 134 private function cleanJob(ProfilePage $page, $jobid, array &$job, &$success, $job_level)
37d44b3b 135 {
541e8d03
SJ
136 if ($job['w_email'] == "new@example.org") {
137 $job['w_email'] = $job['w_email_new'];
138 }
37d44b3b
FB
139 foreach ($this->checks as $obj=>&$fields) {
140 $chk =& $this->$obj;
141 foreach ($fields as $field) {
142 $job[$field] = $chk->value($page, $field, $job[$field], $s);
143 if (!$s) {
144 $success = false;
145 $job[$field . '_error'] = true;
146 }
147 }
148 }
3ac45f10
PC
149 if (count($job['terms'])) {
150 $termsid = array();
151 foreach ($job['terms'] as $term) {
e156780a 152 if (!isset($term['full_name'])) {
3ac45f10
PC
153 $termsid[] = $term['jtid'];
154 }
155 }
156 if (count($termsid)) {
157 $res = XDB::query("SELECT jtid, full_name
158 FROM profile_job_term_enum
159 WHERE jtid IN {?}",
160 $termsid);
161 $term_id_to_name = $res->fetchAllAssoc('jtid', false);
162 foreach ($job['terms'] as &$term) {
e156780a 163 if (!isset($term['full_name'])) {
3ac45f10
PC
164 $term['full_name'] = $term_id_to_name[$term['jtid']];
165 }
166 }
167 }
168 }
b814a8b8 169 if ($job['name']) {
c7139c07
SJ
170 $res = XDB::query("SELECT id
171 FROM profile_job_enum
172 WHERE name = {?}",
173 $job['name']);
174 if ($res->numRows() != 1) {
b814a8b8 175 $job['jobid'] = null;
c7139c07
SJ
176 } else {
177 $job['jobid'] = $res->fetchOneCell();
178 }
179 }
93a45366 180
3a649523 181 if (Visibility::isLessRestrictive($job_level, $job['w_email_pub'])) {
22771578 182 $job['w_email_pub'] = $job_level;
93a45366 183 }
22771578 184 $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s, $job_level);
afaa2cc7 185
0863d308
SJ
186 if ($job['w_entry_year'] && strlen($job['w_entry_year']) != 4) {
187 $job['w_entry_year_error'] = true;
188 $success = false;
189 }
190
37d44b3b
FB
191 unset($job['removed']);
192 unset($job['new']);
37d44b3b
FB
193 }
194
237f662f
FB
195
196
26ba053e 197 public function value(ProfilePage $page, $field, $value, &$success)
37d44b3b 198 {
024ec1e5 199 $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise');
b814a8b8
SJ
200 $entr_val = 0;
201
37d44b3b
FB
202 $init = false;
203 if (is_null($value)) {
237f662f 204 $value = $this->fetchJobs($page);
37d44b3b
FB
205 $init = true;
206 }
207 $success = true;
237f662f 208 foreach ($value as $key => $job) {
b814a8b8 209 $job['name'] = trim($job['name']);
328bd54a 210 if ($job['name'] == '' && $entreprise[$entr_val]->id == $key) {
b814a8b8 211 $job['tmp_name'] = $entreprise[$entr_val]->name;
a6483c12
SJ
212 ++$entr_val;
213 } else if ($job['name'] == '') {
ea9cfc3e 214 if ($job['description'] == '' && $job['w_url'] == ''
ba6ee875 215 && $job['w_address']['text'] == '' && $job['w_email'] == ''
cbbf5ac9 216 && count($job['w_phone']) >= 1 && $job['w_phone'][0]['display'] == '') {
237f662f 217 unset($value[$key]);
ba6ee875
SJ
218 continue;
219 }
220
d1d01361
SJ
221 if (!$init) {
222 $job['name_error'] = true;
223 $success = false;
224 }
b814a8b8 225 }
b94719c1 226
541e8d03 227 if (isset($job['removed']) && $job['removed']) {
0409b3c2 228 if (!S::user()->checkPerms('directory_private')
0c1a29ba 229 && (Phone::hasPrivate($job['w_phone']) || Address::hasPrivate($job['w_address']) || $job['w_email_pub'] == 'private')) {
5271d768 230 Platal::page()->trigWarning("L'entreprise ne peut être supprimée car elle contient des informations pour lesquelles vous n'avez pas le droit d'édition.");
0c1a29ba
SJ
231 } else {
232 if ($job['name'] == '' && $entreprise && isset($entreprise[$entr_val - 1])) {
233 $entreprise[$entr_val - 1]->clean();
234 }
235 unset($value[$key]);
236 continue;
b94719c1 237 }
37d44b3b 238 }
0fc3d4a7
FB
239 if (!isset($job['pub']) || !$job['pub']) {
240 $job['pub'] = 'private';
241 }
237f662f 242 $value[$key] = $job;
37d44b3b 243 }
a6483c12 244 foreach ($value as $key => &$job) {
eb54852e
SJ
245 $address = new Address($job['w_address']);
246 $s = $address->format();
22771578
RB
247
248 // Force the address publicity to be at least as restricted as
249 // the job publicity.
250 $job_level = $job['pub'];
3a649523 251 if (Visibility::isLessRestrictive($job_level, $address->pub)) {
22771578 252 $address->pub = $job_level;
93a45366 253 }
eb54852e 254 $job['w_address'] = $address->toFormArray();
22771578 255 $this->cleanJob($page, $key, $job, $s, $job_level);
37d44b3b 256 if (!$init) {
eb54852e 257 $success = ($success && $s);
37d44b3b
FB
258 }
259 }
22771578 260 usort($value, 'Visibility::comparePublicity');
37d44b3b
FB
261 return $value;
262 }
263
26ba053e 264 public function save(ProfilePage $page, $field, $value)
37d44b3b 265 {
c3951bbd 266
ca17d55d 267 $deletePrivate = S::user()->isMe($page->owner) || S::admin();
6592a264
SJ
268 XDB::execute('DELETE FROM pj, pjt
269 USING profile_job AS pj
270 LEFT JOIN profile_job_term AS pjt ON (pj.pid = pjt.pid AND pj.id = pjt.jid)
271 WHERE pj.pid = {?}' . (($deletePrivate) ? '' : ' AND pj.pub IN (\'public\', \'ax\')'),
a6483c12 272 $page->pid());
baab50c7 273 Address::deleteAddresses($page->pid(), Address::LINK_JOB, null, null, $deletePrivate);
6592a264 274 Phone::deletePhones($page->pid(), Phone::LINK_JOB, null, $deletePrivate);
c3951bbd
SJ
275 $previous_requests = EntrReq::get_typed_requests($page->pid(), 'entreprise');
276 foreach ($previous_requests as $request) {
277 $request->clean();
278 }
3ac45f10 279 $terms_values = array();
a6483c12 280 foreach ($value as $id => &$job) {
d3ce1117 281 if (($job['pub'] != 'private' || $deletePrivate) && (isset($job['name']) && $job['name'])) {
afaa2cc7 282 if (isset($job['jobid']) && $job['jobid']) {
0863d308 283 XDB::execute('INSERT INTO profile_job (pid, id, description, email, entry_year,
237f662f 284 url, pub, email_pub, jobid)
0863d308
SJ
285 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
286 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_entry_year'],
237f662f 287 $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
afaa2cc7 288 } else {
0863d308 289 XDB::execute('INSERT INTO profile_job (pid, id, description, email, entry_year,
237f662f 290 url, pub, email_pub)
0863d308
SJ
291 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
292 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_entry_year'],
237f662f 293 $job['w_url'], $job['pub'], $job['w_email_pub']);
c3951bbd
SJ
294 $request = new EntrReq(S::user(), $page->profile, $id, $job['name'], $job['hq_acronym'], $job['hq_url'],
295 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
296 $request->submit();
297 sleep(1);
afaa2cc7 298 }
237f662f
FB
299 $address = new Address(array_merge($job['w_address'],
300 array('pid' => $page->pid(),
301 'id' => $id,
302 'type' => Address::LINK_JOB)));
eb54852e 303 $address->save();
0b6c8b36 304 Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id);
3ac45f10
PC
305 if (isset($job['terms'])) {
306 foreach ($job['terms'] as $term) {
237f662f
FB
307 $terms_values[] = XDB::format('({?}, {?}, {?}, {?})',
308 $page->pid(), $id, $term['jtid'], "original");
3ac45f10
PC
309 }
310 }
b814a8b8 311 }
37d44b3b 312 }
3ac45f10 313 if (count($terms_values) > 0) {
00ba8a74
SJ
314 XDB::rawExecute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
315 VALUES ' . implode(', ', $terms_values) . '
316 ON DUPLICATE KEY UPDATE computed = VALUES(computed)');
3ac45f10 317 }
313e8575
SJ
318 if (S::user()->isMe($page->owner) && count($value) > 1) {
319 Platal::page()->trigWarning('Attention, tu as plusieurs emplois sur ton profil. Pense à supprimer ceux qui sont obsolètes.');
320 }
37d44b3b 321 }
a0fce0c6 322
4dbd6184
FB
323 public function getText($value)
324 {
b49e98ca 325 static $pubs = array('public' => 'publique', 'ax' => 'annuaire papier', 'private' => 'privé', 'hidden' => 'administrateurs');
a0fce0c6
SJ
326 $jobs = array();
327 foreach ($value as $id => $job) {
4dbd6184 328 $address = Address::formArrayToString(array($job['w_address']));
0b6c8b36 329 $phones = Phone::formArrayToString($job['w_phone']);
14aba233
SJ
330 $jobs[$id] = $job['name'];
331 $jobs[$id] .= ($job['description'] ? (', ' . $job['description']) : '');
332 $jobs[$id] .= ' (affichage ' . $pubs[$job['pub']];
333 if (count($job['terms'])) {
334 $terms = array();
335 foreach ($job['terms'] as $term) {
336 $terms[] = $term['full_name'];
337 }
338 $jobs[$id] .= ', mots-clefs : ' . implode(', ', $terms);
339 }
340 if ($job['w_url']) {
341 $jobs[$id] .= ', page perso : ' . $job['w_url'];
342 }
343 if ($address) {
344 $jobs[$id] .= ', adresse : ' . $address;
345 }
346 if ($job['w_email']) {
347 $jobs[$id] .= ', email : ' . $job['w_email'];
348 }
349 if ($phones) {
350 $jobs[$id] .= ', téléphones : ' . $phones;
351 }
352 $jobs[$id] .= ')';
a0fce0c6
SJ
353 }
354 return implode(' ; ' , $jobs);
355 }
37d44b3b
FB
356}
357
603aeb6c
SJ
358class ProfileSettingCorps implements ProfileSetting
359{
26ba053e 360 public function value(ProfilePage $page, $field, $value, &$success)
603aeb6c
SJ
361 {
362 $success = true;
363 if (is_null($value)) {
8c5f91c4
SJ
364 $res = XDB::query('SELECT c.original_corpsid AS original, e.name AS originalText,
365 c.current_corpsid AS current, c.rankid AS rank, c.corps_pub AS pub
366 FROM profile_corps AS c
367 INNER JOIN profile_corps_enum AS e ON (c.original_corpsid = e.id)
368 WHERE c.pid = {?}',
603aeb6c
SJ
369 $page->pid());
370 return $res->fetchOneAssoc();
371 }
372 return $value;
373 }
374
26ba053e 375 public function save(ProfilePage $page, $field, $value)
603aeb6c 376 {
8c5f91c4
SJ
377 if (!S::user()->isMe($page->owner)) {
378 XDB::execute('INSERT INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
379 VALUES ({?}, {?}, {?}, {?}, {?})
380 ON DUPLICATE KEY UPDATE original_corpsid = VALUES(original_corpsid), current_corpsid = VALUES(current_corpsid),
381 rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
382 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
383 } else {
384 XDB::execute('INSERT INTO profile_corps (current_corpsid, rankid, corps_pub, pid)
385 VALUES ({?}, {?}, {?}, {?})
386 ON DUPLICATE KEY UPDATE current_corpsid = VALUES(current_corpsid),
387 rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
388 $value['current'], $value['rank'], $value['pub'], $page->pid());
389 }
603aeb6c
SJ
390 }
391
392 public function getText($value)
393 {
b49e98ca 394 static $pubs = array('public' => 'publique', 'ax' => 'annuaire papier', 'private' => 'privé', 'hidden' => 'administrateurs');
7c026a22 395 $corpsList = DirEnum::getOptions(DirEnum::ORIGINCORPS);
603aeb6c 396 $rankList = DirEnum::getOptions(DirEnum::CORPSRANKS);
14aba233
SJ
397 return $corpsList[$value['current']] . ', ' . $corpsList[$value['rank']] . ' ('
398 . 'corps d\'origine : ' . $corpsList[$value['original']] . ', affichage ' . $pubs[$value['pub']] . ')';
603aeb6c
SJ
399 }
400}
401
66c4bdaf 402class ProfilePageJobs extends ProfilePage
3950bc21
FB
403{
404 protected $pg_template = 'profile/jobs.tpl';
405
26ba053e 406 public function __construct(PlWizard $wiz)
3950bc21
FB
407 {
408 parent::__construct($wiz);
b539d596
FB
409 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
410 $this->settings['cv'] = null;
411 }
603aeb6c 412 $this->settings['corps'] = new ProfileSettingCorps();
12bcf04b 413 $this->settings['jobs'] = new ProfileSettingJob();
72e96bc0 414 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
26a19295 415 Platal::page()->assign('geocoding_removal', true);
37d44b3b
FB
416 }
417
7c2e0f0d 418 protected function _fetchData()
37d44b3b 419 {
b539d596
FB
420 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
421 // Checkout the CV
422 $res = XDB::query("SELECT cv
423 FROM profiles
424 WHERE pid = {?}",
425 $this->pid());
426 $this->values['cv'] = $res->fetchOneCell();
427 }
37d44b3b
FB
428 }
429
7c2e0f0d 430 protected function _saveData()
37d44b3b 431 {
b539d596
FB
432 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
433 if ($this->changed['cv']) {
434 XDB::execute("UPDATE profiles
435 SET cv = {?}
436 WHERE pid = {?}",
437 $this->values['cv'], $this->pid());
438 }
37d44b3b 439 }
3950bc21 440 }
2dcac0f5 441
26ba053e 442 public function _prepare(PlPage $page, $id)
2dcac0f5 443 {
a6483c12 444 require_once 'emails.combobox.inc.php';
17c6e7bb 445 fill_email_combobox($page, array('redirect', 'job', 'stripped_directory'), $this->owner);
b715c1e1 446
ca17d55d 447 if (!S::user()->isMe($this->owner)) {
8c5f91c4
SJ
448 $res = XDB::iterator('SELECT id, name
449 FROM profile_corps_enum
450 ORDER BY id = 1 DESC, name');
451 $page->assign('original_corps', $res->fetchAllAssoc());
452 }
72e96bc0
SJ
453
454 $res = XDB::iterator("SELECT id, name
455 FROM profile_corps_enum
456 WHERE still_exists = 1
457 ORDER BY id = 1 DESC, name");
458 $page->assign('current_corps', $res->fetchAllAssoc());
459
460 $res = XDB::iterator("SELECT id, name
e489faf7
SJ
461 FROM profile_corps_rank_enum
462 ORDER BY id = 1 DESC, name");
72e96bc0 463 $page->assign('corps_rank', $res->fetchAllAssoc());
2dcac0f5 464 }
3950bc21
FB
465}
466
448c8cdc 467// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
3950bc21 468?>