Merge branch 'xorg/maint' into xorg/master
[platal.git] / modules / profile / jobs.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
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
22 class ProfileSettingJob implements ProfileSetting
23 {
24 private $pub;
25 private $email_new;
26 private $email;
27 private $url;
28 private $bool;
29 private $checks;
30
31 public function __construct()
32 {
33 $this->pub = new ProfileSettingPub();
34 $this->email
35 = $this->email_new
36 = new ProfileSettingEmail();
37 $this->url = new ProfileSettingWeb();
38 $this->bool = new ProfileSettingBool();
39 $this->checks = array('url' => array('w_url'),
40 'email' => array('w_email'),
41 'pub' => array('pub', 'w_email_pub'),
42 );
43 }
44
45 private function emptyJob()
46 {
47 $address = new Address();
48 $phone = new Phone();
49 return array(
50 'id' => '0',
51 'jobid' => '',
52 'pub' => 'ax',
53 'name' => '',
54 'description' => '',
55 'w_url' => '',
56 'w_address' => $address->toFormArray(),
57 'w_email' => '',
58 'w_email_pub' => 'ax',
59 'w_email_new' => '',
60 'w_phone' => array(0 => $phone->toFormArray()),
61 'terms' => array()
62 );
63 }
64
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, j.entry_year AS w_entry_year
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'];
86 $backtrack[$job['id']] = $key;
87 }
88
89 $it = Address::iterate(array($page->pid()), array(Address::LINK_JOB));
90 while ($address = $it->next()) {
91 $jobs[$address->id]['w_address'] = $address->toFormArray();
92 }
93 $it = Phone::iterate(array($page->pid()), array(Phone::LINK_JOB));
94 while ($phone = $it->next()) {
95 $jobs[$phone->link_id]['w_phone'][$phone->id] = $phone->toFormArray();
96 }
97 $res = XDB::iterator("SELECT e.jtid, e.full_name, j.jid
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()) {
104 // $jid is the ID of the job among this user's jobs
105 $jid = $term['jid'];
106 if (!isset($backtrack[$jid])) {
107 continue;
108 }
109 $job =& $jobs[$backtrack[$jid]];
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
134 private function cleanJob(ProfilePage $page, $jobid, array &$job, &$success, $maxPublicity)
135 {
136 if ($job['w_email'] == "new@example.org") {
137 $job['w_email'] = $job['w_email_new'];
138 }
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 }
149 if (count($job['terms'])) {
150 $termsid = array();
151 foreach ($job['terms'] as $term) {
152 if (!isset($term['full_name'])) {
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) {
163 if (!isset($term['full_name'])) {
164 $term['full_name'] = $term_id_to_name[$term['jtid']];
165 }
166 }
167 }
168 }
169 if ($job['name']) {
170 $res = XDB::query("SELECT id
171 FROM profile_job_enum
172 WHERE name = {?}",
173 $job['name']);
174 if ($res->numRows() != 1) {
175 $job['jobid'] = null;
176 } else {
177 $job['jobid'] = $res->fetchOneCell();
178 }
179 }
180
181 if ($maxPublicity->isVisible($job['w_email_pub'])) {
182 $job['w_email_pub'] = $maxPublicity->level();
183 }
184 $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s, $maxPublicity);
185
186 if ($job['w_entry_year'] && strlen($job['w_entry_year']) != 4) {
187 $job['w_entry_year_error'] = true;
188 $success = false;
189 }
190
191 unset($job['removed']);
192 unset($job['new']);
193 }
194
195
196
197 public function value(ProfilePage $page, $field, $value, &$success)
198 {
199 $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise');
200 $entr_val = 0;
201
202 $init = false;
203 if (is_null($value)) {
204 $value = $this->fetchJobs($page);
205 $init = true;
206 }
207 $success = true;
208 foreach ($value as $key => $job) {
209 $job['name'] = trim($job['name']);
210 if ($job['name'] == '' && $entreprise[$entr_val]->id == $key) {
211 $job['tmp_name'] = $entreprise[$entr_val]->name;
212 ++$entr_val;
213 } else if ($job['name'] == '') {
214 if ($job['description'] == '' && $job['w_url'] == ''
215 && $job['w_address']['text'] == '' && $job['w_email'] == ''
216 && count($job['w_phone']) >= 1 && $job['w_phone'][0]['display'] == '') {
217 unset($value[$key]);
218 continue;
219 }
220
221 if (!$init) {
222 $job['name_error'] = true;
223 $success = false;
224 }
225 }
226
227 if (isset($job['removed']) && $job['removed']) {
228 if (!S::user()->checkPerms('directory_private')
229 && (Phone::hasPrivate($job['w_phone']) || Address::hasPrivate($job['w_address']) || $job['w_email_pub'] == 'private')) {
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.");
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;
237 }
238 }
239 if (!isset($job['pub']) || !$job['pub']) {
240 $job['pub'] = 'private';
241 }
242 $value[$key] = $job;
243 }
244 foreach ($value as $key => &$job) {
245 $address = new Address($job['w_address']);
246 $s = $address->format();
247 $maxPublicity = new ProfileVisibility($job['pub']);
248 if ($maxPublicity->isVisible($address->pub)) {
249 $address->pub = $maxPublicity->level();
250 }
251 $job['w_address'] = $address->toFormArray();
252 $this->cleanJob($page, $key, $job, $s, $maxPublicity);
253 if (!$init) {
254 $success = ($success && $s);
255 }
256 }
257 usort($value, 'ProfileVisibility::comparePublicity');
258 return $value;
259 }
260
261 public function save(ProfilePage $page, $field, $value)
262 {
263
264 $deletePrivate = S::user()->isMe($page->owner) || S::admin();
265 XDB::execute('DELETE FROM pj, pjt
266 USING profile_job AS pj
267 LEFT JOIN profile_job_term AS pjt ON (pj.pid = pjt.pid AND pj.id = pjt.jid)
268 WHERE pj.pid = {?}' . (($deletePrivate) ? '' : ' AND pj.pub IN (\'public\', \'ax\')'),
269 $page->pid());
270 Address::deleteAddresses($page->pid(), Address::LINK_JOB, null, null, $deletePrivate);
271 Phone::deletePhones($page->pid(), Phone::LINK_JOB, null, $deletePrivate);
272 $previous_requests = EntrReq::get_typed_requests($page->pid(), 'entreprise');
273 foreach ($previous_requests as $request) {
274 $request->clean();
275 }
276 $terms_values = array();
277 foreach ($value as $id => &$job) {
278 if (($job['pub'] != 'private' || $deletePrivate) && (isset($job['name']) && $job['name'])) {
279 if (isset($job['jobid']) && $job['jobid']) {
280 XDB::execute('INSERT INTO profile_job (pid, id, description, email, entry_year,
281 url, pub, email_pub, jobid)
282 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
283 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_entry_year'],
284 $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
285 } else {
286 XDB::execute('INSERT INTO profile_job (pid, id, description, email, entry_year,
287 url, pub, email_pub)
288 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
289 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_entry_year'],
290 $job['w_url'], $job['pub'], $job['w_email_pub']);
291 $request = new EntrReq(S::user(), $page->profile, $id, $job['name'], $job['hq_acronym'], $job['hq_url'],
292 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
293 $request->submit();
294 sleep(1);
295 }
296 $address = new Address(array_merge($job['w_address'],
297 array('pid' => $page->pid(),
298 'id' => $id,
299 'type' => Address::LINK_JOB)));
300 $address->save();
301 Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id);
302 if (isset($job['terms'])) {
303 foreach ($job['terms'] as $term) {
304 $terms_values[] = XDB::format('({?}, {?}, {?}, {?})',
305 $page->pid(), $id, $term['jtid'], "original");
306 }
307 }
308 }
309 }
310 if (count($terms_values) > 0) {
311 XDB::rawExecute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
312 VALUES ' . implode(', ', $terms_values) . '
313 ON DUPLICATE KEY UPDATE computed = VALUES(computed)');
314 }
315 if (S::user()->isMe($page->owner) && count($value) > 1) {
316 Platal::page()->trigWarning('Attention, tu as plusieurs emplois sur ton profil. Pense à supprimer ceux qui sont obsolètes.');
317 }
318 }
319
320 public function getText($value)
321 {
322 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
323 $jobs = array();
324 foreach ($value as $id => $job) {
325 $address = Address::formArrayToString(array($job['w_address']));
326 $phones = Phone::formArrayToString($job['w_phone']);
327 $jobs[$id] = $job['name'];
328 $jobs[$id] .= ($job['description'] ? (', ' . $job['description']) : '');
329 $jobs[$id] .= ' (affichage ' . $pubs[$job['pub']];
330 if (count($job['terms'])) {
331 $terms = array();
332 foreach ($job['terms'] as $term) {
333 $terms[] = $term['full_name'];
334 }
335 $jobs[$id] .= ', mots-clefs : ' . implode(', ', $terms);
336 }
337 if ($job['w_url']) {
338 $jobs[$id] .= ', page perso : ' . $job['w_url'];
339 }
340 if ($address) {
341 $jobs[$id] .= ', adresse : ' . $address;
342 }
343 if ($job['w_email']) {
344 $jobs[$id] .= ', email : ' . $job['w_email'];
345 }
346 if ($phones) {
347 $jobs[$id] .= ', téléphones : ' . $phones;
348 }
349 $jobs[$id] .= ')';
350 }
351 return implode(' ; ' , $jobs);
352 }
353 }
354
355 class ProfileSettingCorps implements ProfileSetting
356 {
357 public function value(ProfilePage $page, $field, $value, &$success)
358 {
359 $success = true;
360 if (is_null($value)) {
361 $res = XDB::query('SELECT c.original_corpsid AS original, e.name AS originalText,
362 c.current_corpsid AS current, c.rankid AS rank, c.corps_pub AS pub
363 FROM profile_corps AS c
364 INNER JOIN profile_corps_enum AS e ON (c.original_corpsid = e.id)
365 WHERE c.pid = {?}',
366 $page->pid());
367 return $res->fetchOneAssoc();
368 }
369 return $value;
370 }
371
372 public function save(ProfilePage $page, $field, $value)
373 {
374 if (!S::user()->isMe($page->owner)) {
375 XDB::execute('INSERT INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
376 VALUES ({?}, {?}, {?}, {?}, {?})
377 ON DUPLICATE KEY UPDATE original_corpsid = VALUES(original_corpsid), current_corpsid = VALUES(current_corpsid),
378 rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
379 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
380 } else {
381 XDB::execute('INSERT INTO profile_corps (current_corpsid, rankid, corps_pub, pid)
382 VALUES ({?}, {?}, {?}, {?})
383 ON DUPLICATE KEY UPDATE current_corpsid = VALUES(current_corpsid),
384 rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
385 $value['current'], $value['rank'], $value['pub'], $page->pid());
386 }
387 }
388
389 public function getText($value)
390 {
391 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
392 $corpsList = DirEnum::getOptions(DirEnum::CORPS);
393 $rankList = DirEnum::getOptions(DirEnum::CORPSRANKS);
394 return $corpsList[$value['current']] . ', ' . $corpsList[$value['rank']] . ' ('
395 . 'corps d\'origine : ' . $corpsList[$value['original']] . ', affichage ' . $pubs[$value['pub']] . ')';
396 }
397 }
398
399 class ProfilePageJobs extends ProfilePage
400 {
401 protected $pg_template = 'profile/jobs.tpl';
402
403 public function __construct(PlWizard $wiz)
404 {
405 parent::__construct($wiz);
406 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
407 $this->settings['cv'] = null;
408 }
409 $this->settings['corps'] = new ProfileSettingCorps();
410 $this->settings['jobs'] = new ProfileSettingJob();
411 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
412 }
413
414 protected function _fetchData()
415 {
416 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
417 // Checkout the CV
418 $res = XDB::query("SELECT cv
419 FROM profiles
420 WHERE pid = {?}",
421 $this->pid());
422 $this->values['cv'] = $res->fetchOneCell();
423 }
424 }
425
426 protected function _saveData()
427 {
428 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
429 if ($this->changed['cv']) {
430 XDB::execute("UPDATE profiles
431 SET cv = {?}
432 WHERE pid = {?}",
433 $this->values['cv'], $this->pid());
434 }
435 }
436 }
437
438 public function _prepare(PlPage $page, $id)
439 {
440 require_once 'emails.combobox.inc.php';
441 fill_email_combobox($page, array('redirect', 'job', 'stripped_directory'), $this->owner);
442
443 if (!S::user()->isMe($this->owner)) {
444 $res = XDB::iterator('SELECT id, name
445 FROM profile_corps_enum
446 ORDER BY id = 1 DESC, name');
447 $page->assign('original_corps', $res->fetchAllAssoc());
448 }
449
450 $res = XDB::iterator("SELECT id, name
451 FROM profile_corps_enum
452 WHERE still_exists = 1
453 ORDER BY id = 1 DESC, name");
454 $page->assign('current_corps', $res->fetchAllAssoc());
455
456 $res = XDB::iterator("SELECT id, name
457 FROM profile_corps_rank_enum
458 ORDER BY id = 1 DESC, name");
459 $page->assign('corps_rank', $res->fetchAllAssoc());
460 }
461 }
462
463 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
464 ?>