Removes warnings in profile edition.
[platal.git] / modules / profile / jobs.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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
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->linkId()]['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)
135 {
136 static $publicity = array('private' => 0, 'ax' => 1, 'public' => 2);
137
138 if ($job['w_email'] == "new@example.org") {
139 $job['w_email'] = $job['w_email_new'];
140 }
141 foreach ($this->checks as $obj=>&$fields) {
142 $chk =& $this->$obj;
143 foreach ($fields as $field) {
144 $job[$field] = $chk->value($page, $field, $job[$field], $s);
145 if (!$s) {
146 $success = false;
147 $job[$field . '_error'] = true;
148 }
149 }
150 }
151 if (count($job['terms'])) {
152 $termsid = array();
153 foreach ($job['terms'] as $term) {
154 if (!isset($term['full_name'])) {
155 $termsid[] = $term['jtid'];
156 }
157 }
158 if (count($termsid)) {
159 $res = XDB::query("SELECT jtid, full_name
160 FROM profile_job_term_enum
161 WHERE jtid IN {?}",
162 $termsid);
163 $term_id_to_name = $res->fetchAllAssoc('jtid', false);
164 foreach ($job['terms'] as &$term) {
165 if (!isset($term['full_name'])) {
166 $term['full_name'] = $term_id_to_name[$term['jtid']];
167 }
168 }
169 }
170 }
171 if ($job['name']) {
172 $res = XDB::query("SELECT id
173 FROM profile_job_enum
174 WHERE name = {?}",
175 $job['name']);
176 if ($res->numRows() != 1) {
177 $req = new EntrReq(S::user(), $page->profile, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
178 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
179 $req->submit();
180 $job['jobid'] = null;
181 sleep(1);
182 } else {
183 $job['jobid'] = $res->fetchOneCell();
184 }
185 }
186
187 if ($publicity[$job['w_email_pub']] > $publicity[$job['pub']]) {
188 $job['w_email_pub'] = $job['pub'];
189 }
190
191 $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s, $job['pub']);
192
193 unset($job['removed']);
194 unset($job['new']);
195 }
196
197
198
199 public function value(ProfilePage &$page, $field, $value, &$success)
200 {
201 static $publicity = array('private' => 0, 'ax' => 1, 'public' => 2);
202
203 $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise');
204 $entr_val = 0;
205
206 $init = false;
207 if (is_null($value)) {
208 $value = $this->fetchJobs($page);
209 $init = true;
210 }
211 $success = true;
212 foreach ($value as $key => $job) {
213 $job['name'] = trim($job['name']);
214 if ($job['name'] == '' && $entreprise[$entr_val]->id == $key) {
215 $job['tmp_name'] = $entreprise[$entr_val]->name;
216 ++$entr_val;
217 } else if ($job['name'] == '') {
218 if ($job['description'] == '' && $job['w_url'] == ''
219 && $job['w_address']['text'] == '' && $job['w_email'] == ''
220 && count($job['w_phone']) >= 1 && $job['w_phone'][0]['display'] == '') {
221 unset($value[$key]);
222 continue;
223 }
224
225 if (!$init) {
226 $job['name_error'] = true;
227 $success = false;
228 }
229 }
230
231 if (isset($job['removed']) && $job['removed']) {
232 if ($job['name'] == '' && $entreprise) {
233 $entreprise[$entr_val - 1]->clean();
234 }
235 unset($value[$key]);
236 continue;
237 }
238 if (!isset($job['pub']) || !$job['pub']) {
239 $job['pub'] = 'private';
240 }
241 $value[$key] = $job;
242 }
243 foreach ($value as $key => &$job) {
244 $address = new Address($job['w_address']);
245 $s = $address->format();
246 if ($publicity[$address->pub] > $publicity[$job['pub']]) {
247 $address->pub = $job['pub'];
248 }
249 $job['w_address'] = $address->toFormArray();
250 $this->cleanJob($page, $key, $job, $s);
251 if (!$init) {
252 $success = ($success && $s);
253 }
254 }
255 return $value;
256 }
257
258 public function save(ProfilePage &$page, $field, $value)
259 {
260 $deletePrivate = S::user()->isMe($page->owner) || S::admin();
261 XDB::execute('DELETE FROM pj, pjt
262 USING profile_job AS pj
263 LEFT JOIN profile_job_term AS pjt ON (pj.pid = pjt.pid AND pj.id = pjt.jid)
264 WHERE pj.pid = {?}' . (($deletePrivate) ? '' : ' AND pj.pub IN (\'public\', \'ax\')'),
265 $page->pid());
266 Address::deleteAddresses($page->pid(), Address::LINK_JOB, null, $deletePrivate);
267 Phone::deletePhones($page->pid(), Phone::LINK_JOB, null, $deletePrivate);
268 $terms_values = array();
269 foreach ($value as $id => &$job) {
270 if (isset($job['name']) && $job['name']) {
271 if (isset($job['jobid']) && $job['jobid']) {
272 XDB::execute('INSERT INTO profile_job (pid, id, description, email,
273 url, pub, email_pub, jobid)
274 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
275 $page->pid(), $id, $job['description'], $job['w_email'],
276 $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
277 } else {
278 XDB::execute('INSERT INTO profile_job (pid, id, description, email,
279 url, pub, email_pub)
280 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
281 $page->pid(), $id, $job['description'], $job['w_email'],
282 $job['w_url'], $job['pub'], $job['w_email_pub']);
283 }
284 $address = new Address(array_merge($job['w_address'],
285 array('pid' => $page->pid(),
286 'id' => $id,
287 'type' => Address::LINK_JOB)));
288 $address->save();
289 Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id);
290 if (isset($job['terms'])) {
291 foreach ($job['terms'] as $term) {
292 $terms_values[] = XDB::format('({?}, {?}, {?}, {?})',
293 $page->pid(), $id, $term['jtid'], "original");
294 }
295 }
296 }
297 }
298 if (count($terms_values) > 0) {
299 XDB::rawExecute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
300 VALUES ' . implode(', ', $terms_values) . '
301 ON DUPLICATE KEY UPDATE computed = VALUES(computed)');
302 }
303 }
304
305 public function getText($value)
306 {
307 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
308 $jobs = array();
309 foreach ($value as $id => $job) {
310 $address = Address::formArrayToString(array($job['w_address']));
311 $phones = Phone::formArrayToString($job['w_phone']);
312 $jobs[$id] = $job['name'];
313 $jobs[$id] .= ($job['description'] ? (', ' . $job['description']) : '');
314 $jobs[$id] .= ' (affichage ' . $pubs[$job['pub']];
315 if (count($job['terms'])) {
316 $terms = array();
317 foreach ($job['terms'] as $term) {
318 $terms[] = $term['full_name'];
319 }
320 $jobs[$id] .= ', mots-clefs : ' . implode(', ', $terms);
321 }
322 if ($job['w_url']) {
323 $jobs[$id] .= ', page perso : ' . $job['w_url'];
324 }
325 if ($address) {
326 $jobs[$id] .= ', adresse : ' . $address;
327 }
328 if ($job['w_email']) {
329 $jobs[$id] .= ', email : ' . $job['w_email'];
330 }
331 if ($phones) {
332 $jobs[$id] .= ', téléphones : ' . $phones;
333 }
334 $jobs[$id] .= ')';
335 }
336 return implode(' ; ' , $jobs);
337 }
338 }
339
340 class ProfileSettingCorps implements ProfileSetting
341 {
342 public function value(ProfilePage &$page, $field, $value, &$success)
343 {
344 $success = true;
345 if (is_null($value)) {
346 $res = XDB::query('SELECT c.original_corpsid AS original, e.name AS originalText,
347 c.current_corpsid AS current, c.rankid AS rank, c.corps_pub AS pub
348 FROM profile_corps AS c
349 INNER JOIN profile_corps_enum AS e ON (c.original_corpsid = e.id)
350 WHERE c.pid = {?}',
351 $page->pid());
352 return $res->fetchOneAssoc();
353 }
354 return $value;
355 }
356
357 public function save(ProfilePage &$page, $field, $value)
358 {
359 if (!S::user()->isMe($page->owner)) {
360 XDB::execute('INSERT INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
361 VALUES ({?}, {?}, {?}, {?}, {?})
362 ON DUPLICATE KEY UPDATE original_corpsid = VALUES(original_corpsid), current_corpsid = VALUES(current_corpsid),
363 rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
364 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
365 } else {
366 XDB::execute('INSERT INTO profile_corps (current_corpsid, rankid, corps_pub, pid)
367 VALUES ({?}, {?}, {?}, {?})
368 ON DUPLICATE KEY UPDATE current_corpsid = VALUES(current_corpsid),
369 rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
370 $value['current'], $value['rank'], $value['pub'], $page->pid());
371 }
372 }
373
374 public function getText($value)
375 {
376 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
377 $corpsList = DirEnum::getOptions(DirEnum::CORPS);
378 $rankList = DirEnum::getOptions(DirEnum::CORPSRANKS);
379 return $corpsList[$value['current']] . ', ' . $corpsList[$value['rank']] . ' ('
380 . 'corps d\'origine : ' . $corpsList[$value['original']] . ', affichage ' . $pubs[$value['pub']] . ')';
381 }
382 }
383
384 class ProfilePageJobs extends ProfilePage
385 {
386 protected $pg_template = 'profile/jobs.tpl';
387
388 public function __construct(PlWizard &$wiz)
389 {
390 parent::__construct($wiz);
391 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
392 $this->settings['cv'] = null;
393 }
394 $this->settings['corps'] = new ProfileSettingCorps();
395 $this->settings['jobs'] = new ProfileSettingJob();
396 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
397 }
398
399 protected function _fetchData()
400 {
401 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
402 // Checkout the CV
403 $res = XDB::query("SELECT cv
404 FROM profiles
405 WHERE pid = {?}",
406 $this->pid());
407 $this->values['cv'] = $res->fetchOneCell();
408 }
409 }
410
411 protected function _saveData()
412 {
413 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
414 if ($this->changed['cv']) {
415 XDB::execute("UPDATE profiles
416 SET cv = {?}
417 WHERE pid = {?}",
418 $this->values['cv'], $this->pid());
419 }
420 }
421 }
422
423 public function _prepare(PlPage &$page, $id)
424 {
425 require_once 'emails.combobox.inc.php';
426 fill_email_combobox($page, $this->owner);
427
428 if (!S::user()->isMe($this->owner)) {
429 $res = XDB::iterator('SELECT id, name
430 FROM profile_corps_enum
431 ORDER BY id = 1 DESC, name');
432 $page->assign('original_corps', $res->fetchAllAssoc());
433 }
434
435 $res = XDB::iterator("SELECT id, name
436 FROM profile_corps_enum
437 WHERE still_exists = 1
438 ORDER BY id = 1 DESC, name");
439 $page->assign('current_corps', $res->fetchAllAssoc());
440
441 $res = XDB::iterator("SELECT id, name
442 FROM profile_corps_rank_enum
443 ORDER BY id = 1 DESC, name");
444 $page->assign('corps_rank', $res->fetchAllAssoc());
445 }
446 }
447
448 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
449 ?>