Commit | Line | Data |
---|---|---|
3950bc21 FB |
1 | <?php |
2 | /*************************************************************************** | |
9f5bd98e | 3 | * Copyright (C) 2003-2010 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 | 22 | class 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, | |
71 | j.url AS w_url, j.pub | |
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 | ||
89 | $it = Address::iterate(array($page->pid()), array(Address::LINK_JOB)); | |
90 | while ($address = $it->next()) { | |
10a574ea | 91 | $jobs[$address->id]['w_address'] = $address->toFormArray(); |
237f662f FB |
92 | } |
93 | $it = Phone::iterate(array($page->pid()), array(Phone::LINK_JOB)); | |
94 | while ($phone = $it->next()) { | |
95 | $jobs[$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray(); | |
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 | ||
e7027060 | 134 | private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success, $maxPublicity) |
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) { | |
024ec1e5 | 175 | $req = new EntrReq(S::user(), $page->profile, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'], |
4d7d27fc | 176 | $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']); |
b814a8b8 SJ |
177 | $req->submit(); |
178 | $job['jobid'] = null; | |
858a5b42 | 179 | sleep(1); |
c7139c07 SJ |
180 | } else { |
181 | $job['jobid'] = $res->fetchOneCell(); | |
182 | } | |
183 | } | |
93a45366 | 184 | |
e7027060 SJ |
185 | if ($maxPublicity->isVisible($job['w_email_pub'])) { |
186 | $job['w_email_pub'] = $maxPublicity->level(); | |
93a45366 | 187 | } |
e7027060 | 188 | $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s, $maxPublicity); |
afaa2cc7 | 189 | |
37d44b3b FB |
190 | unset($job['removed']); |
191 | unset($job['new']); | |
37d44b3b FB |
192 | } |
193 | ||
237f662f FB |
194 | |
195 | ||
37d44b3b FB |
196 | public function value(ProfilePage &$page, $field, $value, &$success) |
197 | { | |
024ec1e5 | 198 | $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise'); |
b814a8b8 SJ |
199 | $entr_val = 0; |
200 | ||
37d44b3b FB |
201 | $init = false; |
202 | if (is_null($value)) { | |
237f662f | 203 | $value = $this->fetchJobs($page); |
37d44b3b FB |
204 | $init = true; |
205 | } | |
206 | $success = true; | |
237f662f | 207 | foreach ($value as $key => $job) { |
b814a8b8 | 208 | $job['name'] = trim($job['name']); |
328bd54a | 209 | if ($job['name'] == '' && $entreprise[$entr_val]->id == $key) { |
b814a8b8 | 210 | $job['tmp_name'] = $entreprise[$entr_val]->name; |
a6483c12 SJ |
211 | ++$entr_val; |
212 | } else if ($job['name'] == '') { | |
ea9cfc3e | 213 | if ($job['description'] == '' && $job['w_url'] == '' |
ba6ee875 | 214 | && $job['w_address']['text'] == '' && $job['w_email'] == '' |
cbbf5ac9 | 215 | && count($job['w_phone']) >= 1 && $job['w_phone'][0]['display'] == '') { |
237f662f | 216 | unset($value[$key]); |
ba6ee875 SJ |
217 | continue; |
218 | } | |
219 | ||
d1d01361 SJ |
220 | if (!$init) { |
221 | $job['name_error'] = true; | |
222 | $success = false; | |
223 | } | |
b814a8b8 | 224 | } |
b94719c1 | 225 | |
541e8d03 | 226 | if (isset($job['removed']) && $job['removed']) { |
1d36c125 | 227 | if ($job['name'] == '' && $entreprise && isset($entreprise[$entr_val - 1])) { |
b94719c1 SJ |
228 | $entreprise[$entr_val - 1]->clean(); |
229 | } | |
237f662f FB |
230 | unset($value[$key]); |
231 | continue; | |
37d44b3b | 232 | } |
0fc3d4a7 FB |
233 | if (!isset($job['pub']) || !$job['pub']) { |
234 | $job['pub'] = 'private'; | |
235 | } | |
237f662f | 236 | $value[$key] = $job; |
37d44b3b | 237 | } |
a6483c12 | 238 | foreach ($value as $key => &$job) { |
eb54852e SJ |
239 | $address = new Address($job['w_address']); |
240 | $s = $address->format(); | |
e7027060 SJ |
241 | $maxPublicity = new ProfileVisibility($job['pub']); |
242 | if ($maxPublicity->isVisible($address->pub)) { | |
243 | $address->pub = $maxPublicity->level(); | |
93a45366 | 244 | } |
eb54852e | 245 | $job['w_address'] = $address->toFormArray(); |
e7027060 | 246 | $this->cleanJob($page, $key, $job, $s, $maxPublicity); |
37d44b3b | 247 | if (!$init) { |
eb54852e | 248 | $success = ($success && $s); |
37d44b3b FB |
249 | } |
250 | } | |
251 | return $value; | |
252 | } | |
253 | ||
254 | public function save(ProfilePage &$page, $field, $value) | |
255 | { | |
ca17d55d | 256 | $deletePrivate = S::user()->isMe($page->owner) || S::admin(); |
6592a264 SJ |
257 | XDB::execute('DELETE FROM pj, pjt |
258 | USING profile_job AS pj | |
259 | LEFT JOIN profile_job_term AS pjt ON (pj.pid = pjt.pid AND pj.id = pjt.jid) | |
260 | WHERE pj.pid = {?}' . (($deletePrivate) ? '' : ' AND pj.pub IN (\'public\', \'ax\')'), | |
a6483c12 | 261 | $page->pid()); |
6592a264 SJ |
262 | Address::deleteAddresses($page->pid(), Address::LINK_JOB, null, $deletePrivate); |
263 | Phone::deletePhones($page->pid(), Phone::LINK_JOB, null, $deletePrivate); | |
3ac45f10 | 264 | $terms_values = array(); |
a6483c12 | 265 | foreach ($value as $id => &$job) { |
afaa2cc7 SJ |
266 | if (isset($job['name']) && $job['name']) { |
267 | if (isset($job['jobid']) && $job['jobid']) { | |
237f662f FB |
268 | XDB::execute('INSERT INTO profile_job (pid, id, description, email, |
269 | url, pub, email_pub, jobid) | |
ea9cfc3e | 270 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})', |
237f662f FB |
271 | $page->pid(), $id, $job['description'], $job['w_email'], |
272 | $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']); | |
afaa2cc7 | 273 | } else { |
237f662f FB |
274 | XDB::execute('INSERT INTO profile_job (pid, id, description, email, |
275 | url, pub, email_pub) | |
ea9cfc3e | 276 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})', |
237f662f FB |
277 | $page->pid(), $id, $job['description'], $job['w_email'], |
278 | $job['w_url'], $job['pub'], $job['w_email_pub']); | |
afaa2cc7 | 279 | } |
237f662f FB |
280 | $address = new Address(array_merge($job['w_address'], |
281 | array('pid' => $page->pid(), | |
282 | 'id' => $id, | |
283 | 'type' => Address::LINK_JOB))); | |
eb54852e | 284 | $address->save(); |
0b6c8b36 | 285 | Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id); |
3ac45f10 PC |
286 | if (isset($job['terms'])) { |
287 | foreach ($job['terms'] as $term) { | |
237f662f FB |
288 | $terms_values[] = XDB::format('({?}, {?}, {?}, {?})', |
289 | $page->pid(), $id, $term['jtid'], "original"); | |
3ac45f10 PC |
290 | } |
291 | } | |
b814a8b8 | 292 | } |
37d44b3b | 293 | } |
3ac45f10 | 294 | if (count($terms_values) > 0) { |
00ba8a74 SJ |
295 | XDB::rawExecute('INSERT INTO profile_job_term (pid, jid, jtid, computed) |
296 | VALUES ' . implode(', ', $terms_values) . ' | |
297 | ON DUPLICATE KEY UPDATE computed = VALUES(computed)'); | |
3ac45f10 | 298 | } |
37d44b3b | 299 | } |
a0fce0c6 | 300 | |
4dbd6184 FB |
301 | public function getText($value) |
302 | { | |
14aba233 | 303 | static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé'); |
a0fce0c6 SJ |
304 | $jobs = array(); |
305 | foreach ($value as $id => $job) { | |
4dbd6184 | 306 | $address = Address::formArrayToString(array($job['w_address'])); |
0b6c8b36 | 307 | $phones = Phone::formArrayToString($job['w_phone']); |
14aba233 SJ |
308 | $jobs[$id] = $job['name']; |
309 | $jobs[$id] .= ($job['description'] ? (', ' . $job['description']) : ''); | |
310 | $jobs[$id] .= ' (affichage ' . $pubs[$job['pub']]; | |
311 | if (count($job['terms'])) { | |
312 | $terms = array(); | |
313 | foreach ($job['terms'] as $term) { | |
314 | $terms[] = $term['full_name']; | |
315 | } | |
316 | $jobs[$id] .= ', mots-clefs : ' . implode(', ', $terms); | |
317 | } | |
318 | if ($job['w_url']) { | |
319 | $jobs[$id] .= ', page perso : ' . $job['w_url']; | |
320 | } | |
321 | if ($address) { | |
322 | $jobs[$id] .= ', adresse : ' . $address; | |
323 | } | |
324 | if ($job['w_email']) { | |
325 | $jobs[$id] .= ', email : ' . $job['w_email']; | |
326 | } | |
327 | if ($phones) { | |
328 | $jobs[$id] .= ', téléphones : ' . $phones; | |
329 | } | |
330 | $jobs[$id] .= ')'; | |
a0fce0c6 SJ |
331 | } |
332 | return implode(' ; ' , $jobs); | |
333 | } | |
37d44b3b FB |
334 | } |
335 | ||
603aeb6c SJ |
336 | class ProfileSettingCorps implements ProfileSetting |
337 | { | |
338 | public function value(ProfilePage &$page, $field, $value, &$success) | |
339 | { | |
340 | $success = true; | |
341 | if (is_null($value)) { | |
8c5f91c4 SJ |
342 | $res = XDB::query('SELECT c.original_corpsid AS original, e.name AS originalText, |
343 | c.current_corpsid AS current, c.rankid AS rank, c.corps_pub AS pub | |
344 | FROM profile_corps AS c | |
345 | INNER JOIN profile_corps_enum AS e ON (c.original_corpsid = e.id) | |
346 | WHERE c.pid = {?}', | |
603aeb6c SJ |
347 | $page->pid()); |
348 | return $res->fetchOneAssoc(); | |
349 | } | |
350 | return $value; | |
351 | } | |
352 | ||
353 | public function save(ProfilePage &$page, $field, $value) | |
354 | { | |
8c5f91c4 SJ |
355 | if (!S::user()->isMe($page->owner)) { |
356 | XDB::execute('INSERT INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid) | |
357 | VALUES ({?}, {?}, {?}, {?}, {?}) | |
358 | ON DUPLICATE KEY UPDATE original_corpsid = VALUES(original_corpsid), current_corpsid = VALUES(current_corpsid), | |
359 | rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)', | |
360 | $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid()); | |
361 | } else { | |
362 | XDB::execute('INSERT INTO profile_corps (current_corpsid, rankid, corps_pub, pid) | |
363 | VALUES ({?}, {?}, {?}, {?}) | |
364 | ON DUPLICATE KEY UPDATE current_corpsid = VALUES(current_corpsid), | |
365 | rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)', | |
366 | $value['current'], $value['rank'], $value['pub'], $page->pid()); | |
367 | } | |
603aeb6c SJ |
368 | } |
369 | ||
370 | public function getText($value) | |
371 | { | |
14aba233 | 372 | static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé'); |
603aeb6c SJ |
373 | $corpsList = DirEnum::getOptions(DirEnum::CORPS); |
374 | $rankList = DirEnum::getOptions(DirEnum::CORPSRANKS); | |
14aba233 SJ |
375 | return $corpsList[$value['current']] . ', ' . $corpsList[$value['rank']] . ' (' |
376 | . 'corps d\'origine : ' . $corpsList[$value['original']] . ', affichage ' . $pubs[$value['pub']] . ')'; | |
603aeb6c SJ |
377 | } |
378 | } | |
379 | ||
66c4bdaf | 380 | class ProfilePageJobs extends ProfilePage |
3950bc21 FB |
381 | { |
382 | protected $pg_template = 'profile/jobs.tpl'; | |
383 | ||
384 | public function __construct(PlWizard &$wiz) | |
385 | { | |
386 | parent::__construct($wiz); | |
b539d596 FB |
387 | if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) { |
388 | $this->settings['cv'] = null; | |
389 | } | |
603aeb6c | 390 | $this->settings['corps'] = new ProfileSettingCorps(); |
12bcf04b | 391 | $this->settings['jobs'] = new ProfileSettingJob(); |
72e96bc0 | 392 | $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true); |
37d44b3b FB |
393 | } |
394 | ||
7c2e0f0d | 395 | protected function _fetchData() |
37d44b3b | 396 | { |
b539d596 FB |
397 | if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) { |
398 | // Checkout the CV | |
399 | $res = XDB::query("SELECT cv | |
400 | FROM profiles | |
401 | WHERE pid = {?}", | |
402 | $this->pid()); | |
403 | $this->values['cv'] = $res->fetchOneCell(); | |
404 | } | |
37d44b3b FB |
405 | } |
406 | ||
7c2e0f0d | 407 | protected function _saveData() |
37d44b3b | 408 | { |
b539d596 FB |
409 | if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) { |
410 | if ($this->changed['cv']) { | |
411 | XDB::execute("UPDATE profiles | |
412 | SET cv = {?} | |
413 | WHERE pid = {?}", | |
414 | $this->values['cv'], $this->pid()); | |
415 | } | |
37d44b3b | 416 | } |
3950bc21 | 417 | } |
2dcac0f5 | 418 | |
04334c61 | 419 | public function _prepare(PlPage &$page, $id) |
2dcac0f5 | 420 | { |
a6483c12 | 421 | require_once 'emails.combobox.inc.php'; |
4e698dc9 | 422 | fill_email_combobox($page, $this->owner); |
b715c1e1 | 423 | |
ca17d55d | 424 | if (!S::user()->isMe($this->owner)) { |
8c5f91c4 SJ |
425 | $res = XDB::iterator('SELECT id, name |
426 | FROM profile_corps_enum | |
427 | ORDER BY id = 1 DESC, name'); | |
428 | $page->assign('original_corps', $res->fetchAllAssoc()); | |
429 | } | |
72e96bc0 SJ |
430 | |
431 | $res = XDB::iterator("SELECT id, name | |
432 | FROM profile_corps_enum | |
433 | WHERE still_exists = 1 | |
434 | ORDER BY id = 1 DESC, name"); | |
435 | $page->assign('current_corps', $res->fetchAllAssoc()); | |
436 | ||
437 | $res = XDB::iterator("SELECT id, name | |
e489faf7 SJ |
438 | FROM profile_corps_rank_enum |
439 | ORDER BY id = 1 DESC, name"); | |
72e96bc0 | 440 | $page->assign('corps_rank', $res->fetchAllAssoc()); |
2dcac0f5 | 441 | } |
3950bc21 FB |
442 | } |
443 | ||
444 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
445 | ?> |