Hide 'always-private' fields when profile is edited by an AX secretary.
[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 extends ProfileSettingGeocoding
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 public function emptyJob()
46 {
47 return array(
48 'id' => '0',
49 'jobid' => '',
50 'pub' => 'private',
51 'name' => '',
52 'hq_acronym' => '',
53 'hq_url' => '',
54 'hq_email' => '',
55 'hq_address' => array(
56 'text' => '',
57 'accuracy' => '',
58 'postalText' => '',
59 'postalCode' => '',
60 'administrativeAreaId' => '',
61 'subAdministrativeAreaId' => '',
62 'localityId' => '',
63 'countryId' => '',
64 'latitude' => '',
65 'longitude' => '',
66 'north' => '',
67 'south' => '',
68 'east' => '',
69 'west' => '',
70 'cedex' => '',
71 'updateTime' => '',
72 'changed' => '0',
73 'removed' => '0',
74 ),
75 'hq_fixed' => '',
76 'hq_fax' => '',
77 'subSubSectorName' => null,
78 'sector' => '0',
79 'subSector' => '0',
80 'subSubSector' => '0',
81 'description' => '',
82 'w_url' => '',
83 'w_address' => array(
84 'pub' => 'private',
85 'text' => '',
86 'accuracy' => '',
87 'postalText' => '',
88 'postalCode' => '',
89 'administrativeAreaId' => '',
90 'subAdministrativeAreaId' => '',
91 'localityId' => '',
92 'countryId' => '',
93 'latitude' => '',
94 'longitude' => '',
95 'north' => '',
96 'south' => '',
97 'east' => '',
98 'west' => '',
99 'cedex' => '',
100 'updateTime' => '',
101 'changed' => '0',
102 'removed' => '0',
103 ),
104 'w_email' => '',
105 'w_email_pub' => 'private',
106 'w_email_new' => '',
107 'w_phone' => array(0 => array(
108 'type' => 'fixed',
109 'tel' => '',
110 'pub' => 'private',
111 'comment' => '',
112 ),
113 'terms' => array()),
114 );
115 }
116
117 private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success)
118 {
119 $success = true;
120 if ($job['w_email'] == "new@example.org") {
121 $job['w_email'] = $job['w_email_new'];
122 }
123 foreach ($this->checks as $obj=>&$fields) {
124 $chk =& $this->$obj;
125 foreach ($fields as $field) {
126 $job[$field] = $chk->value($page, $field, $job[$field], $s);
127 if (!$s) {
128 $success = false;
129 $job[$field . '_error'] = true;
130 }
131 }
132 }
133 if (!$job['subSubSectorName']) {
134 $res = XDB::query("SELECT name
135 FROM profile_job_subsubsector_enum
136 WHERE id = {?}",
137 $job['subSubSector']);
138 $job['subSubSectorName'] = $res->fetchOneCell();
139 } else {
140 $res = XDB::query("SELECT sectorid, subsectorid, id
141 FROM profile_job_subsubsector_enum
142 WHERE name = {?}",
143 $job['subSubSectorName']);
144 if ($res->numRows() != 1) {
145 $success = false;
146 $job['sector_error'] = true;
147 } else {
148 list($job['sector'], $job['subSector'], $job['subSubSector']) = $res->fetchOneRow();
149 }
150 }
151 if (count($job['terms'])) {
152 $termsid = array();
153 foreach ($job['terms'] as $term) {
154 if (!$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 (!$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 $this->geocodeAddress($job['hq_address'], $s);
178 if (!$s) {
179 $gmapsGeocoder = new GMapsGeocoder();
180 $job['hq_address'] = $gmapsGeocoder->stripGeocodingFromAddress($job['hq_address']);
181 }
182 $req = new EntrReq(S::user(), $page->profile, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
183 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
184 $req->submit();
185 $job['jobid'] = null;
186 sleep(1);
187 } else {
188 $job['jobid'] = $res->fetchOneCell();
189 }
190 }
191 $job['w_address']['pub'] = $this->pub->value($page, 'address_pub', $job['w_address']['pub'], $s);
192 $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s);
193
194 unset($job['removed']);
195 unset($job['new']);
196 }
197
198 public function value(ProfilePage &$page, $field, $value, &$success)
199 {
200 require_once 'validations.inc.php';
201 $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise');
202 $entr_val = 0;
203
204 $init = false;
205 if (is_null($value)) {
206 $value = $page->values['jobs'];
207 $init = true;
208 }
209 $success = true;
210 foreach ($value as $key => &$job) {
211 $job['name'] = trim($job['name']);
212 if ($job['name'] == '' && $entreprise) {
213 $job['tmp_name'] = $entreprise[$entr_val]->name;
214 ++$entr_val;
215 } else if ($job['name'] == '') {
216 if ($job['subSubSectorName'] == '' && $job['description'] == '' && $job['w_url'] == ''
217 && $job['w_address']['text'] == '' && $job['w_email'] == ''
218 && count($job['w_phone']) == 1 && $job['w_phone']['tel'] == '') {
219 array_splice($value, $key, 1);
220 continue;
221 }
222
223 if (!$init) {
224 $job['name_error'] = true;
225 $success = false;
226 }
227 }
228
229 if (isset($job['removed']) && $job['removed']) {
230 if ($job['name'] == '' && $entreprise) {
231 $entreprise[$entr_val - 1]->clean();
232 }
233 array_splice($value, $key, 1);
234 }
235 foreach (array('sectorid', 'subsectorid', 'subsubsectorid') as $key) {
236 if ($job[$key] == 0) {
237 $job[$key] = null;
238 }
239 }
240 }
241 foreach ($value as $key => &$job) {
242 $ls = true;
243 $this->geocodeAddress($job['w_address'], $s);
244 $ls = ($ls && $s);
245 $this->cleanJob($page, $key, $job, $s);
246 $ls = ($ls && $s);
247 if (!$init) {
248 $success = ($success && $ls);
249 }
250 }
251 return $value;
252 }
253
254 public function save(ProfilePage &$page, $field, $value)
255 {
256 // TODO: use address class to update profile_job_enum once it is done.
257 XDB::execute("DELETE FROM profile_job
258 WHERE pid = {?}",
259 $page->pid());
260 XDB::execute("DELETE FROM profile_addresses
261 WHERE pid = {?} AND type = 'job'",
262 $page->pid());
263 Phone::deletePhones($page->pid(), Phone::LINK_JOB);
264 $terms_values = array();
265 foreach ($value as $id => &$job) {
266 if (isset($job['name']) && $job['name']) {
267 if (isset($job['jobid']) && $job['jobid']) {
268 XDB::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
269 subsubsectorid, email, url, pub, email_pub, jobid)
270 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
271 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
272 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
273 } else {
274 XDB::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
275 subsubsectorid, email, url, pub, email_pub)
276 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
277 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
278 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
279 }
280 $address = new ProfileSettingAddress();
281 $address->saveAddress($page->pid(), $id, $job['w_address'], 'job');
282 Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id);
283 if (isset($job['terms'])) {
284 foreach ($job['terms'] as $term) {
285 $terms_values[] = '('.XDB::escape($page->pid()).', '. XDB::escape($id).', '.XDB::escape($term['jtid']).', "original")';
286 }
287 }
288 }
289 }
290 if (count($terms_values) > 0) {
291 XDB::execute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
292 VALUES '.implode(', ', $terms_values));
293 }
294 }
295
296 public function getText($value) {
297 $jobs = array();
298 foreach ($value as $id => $job) {
299 $address = new ProfileSettingAddress();
300 $phones = Phone::formArrayToString($job['w_phone']);
301 $jobs[] = 'Entreprise : ' . $job['name'] . ', secteur : ' . $job['subSubSectorName']
302 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
303 . ', email : ' . $job['w_email']
304 . ($phones ? ', ' . $phones : '') . ', ' . $address->getText($job['w_address']);
305 }
306 return implode(' ; ' , $jobs);
307 }
308 }
309
310 class ProfileSettingCorps implements ProfileSetting
311 {
312 public function value(ProfilePage &$page, $field, $value, &$success)
313 {
314 $success = true;
315 if (is_null($value)) {
316 $res = XDB::query("SELECT original_corpsid AS original, current_corpsid AS current,
317 rankid AS rank, corps_pub AS pub
318 FROM profile_corps
319 WHERE pid = {?}",
320 $page->pid());
321 return $res->fetchOneAssoc();
322 }
323 return $value;
324 }
325
326 public function save(ProfilePage &$page, $field, $value)
327 {
328 XDB::execute('REPLACE INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
329 VALUES ({?}, {?}, {?}, {?}, {?})',
330 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
331 }
332
333 public function getText($value)
334 {
335 $corpsList = DirEnum::getOptions(DirEnum::CORPS);
336 $rankList = DirEnum::getOptions(DirEnum::CORPSRANKS);
337 return 'Corps actuel : ' . $corpsList[$value['current']] . ' , rang : ' . $corpsList[$value['rank']]
338 . ' , corps d\'origine : ' . $corpsList[$value['original']] . ' , affichage : ' . $value['pub'];
339 }
340 }
341
342 class ProfileSettingJobs extends ProfilePage
343 {
344 protected $pg_template = 'profile/jobs.tpl';
345
346 public function __construct(PlWizard &$wiz)
347 {
348 parent::__construct($wiz);
349 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
350 $this->settings['cv'] = null;
351 }
352 $this->settings['corps'] = new ProfileSettingCorps();
353 $this->settings['jobs'] = new ProfileSettingJob();
354 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
355 }
356
357 protected function _fetchData()
358 {
359 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
360 // Checkout the CV
361 $res = XDB::query("SELECT cv
362 FROM profiles
363 WHERE pid = {?}",
364 $this->pid());
365 $this->values['cv'] = $res->fetchOneCell();
366 }
367
368 // Build the jobs tree
369 $res = XDB::iterRow("SELECT j.id, j.jobid, je.name, j.sectorid, j.subsectorid, j.subsubsectorid,
370 s.name, j.description, j.email, j.email_pub, j.url, j.pub,
371 je.acronym, je.url, je.email,
372 aw.accuracy, aw.text, aw.postalText, aw.postalCode, aw.localityId,
373 aw.subAdministrativeAreaId, aw.administrativeAreaId, aw.countryId,
374 aw.latitude, aw.longitude, aw.pub, aw.updateTime,
375 aw.north, aw.south, aw.east, aw.west,
376 ah.accuracy, ah.text, ah.postalText, ah.postalCode, ah.localityId,
377 ah.subAdministrativeAreaId, ah.administrativeAreaId, ah.countryId,
378 ah.latitude, ah.longitude, ah.pub, ah.updateTime,
379 ah.north, ah.south, ah.east, ah.west
380 FROM profile_job AS j
381 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
382 LEFT JOIN profile_job_subsubsector_enum AS s ON (s.id = j.subsubsectorid)
383 LEFT JOIN profile_addresses AS aw ON (aw.pid = j.pid AND aw.type = 'job'
384 AND aw.id = j.id)
385 LEFT JOIN profile_addresses AS ah ON (ah.jobid = j.jobid AND ah.type = 'hq')
386 WHERE j.pid = {?}
387 ORDER BY j.id",
388 $this->pid());
389 $this->values['jobs'] = array();
390
391 if ($res->numRows() > 0) {
392 while (list($id, $jobid, $name, $sector, $subSector, $subSubSector,
393 $subSubSectorName, $description, $w_email, $w_emailPub, $w_url, $pub,
394 $hq_acronym, $hq_url, $hq_email,
395 $w_accuracy, $w_text, $w_postalText, $w_postalCode, $w_localityId,
396 $w_subAdministrativeAreaId, $w_administrativeAreaId, $w_countryId,
397 $w_latitude, $w_longitude, $w_pub, $w_updateTime,
398 $w_north, $w_south, $w_east, $w_west,
399 $hq_accuracy, $hq_text, $hq_postalText, $hq_postalCode, $hq_localityId,
400 $hq_subAdministrativeAreaId, $hq_administrativeAreaId, $hq_countryId,
401 $hq_latitude, $hq_longitude, $hq_pub, $hq_updateTime,
402 $hq_north, $hq_south, $hq_east, $hq_west,
403 ) = $res->next()) {
404 $this->values['jobs'][] = array(
405 'id' => $id,
406 'jobid' => $jobid,
407 'name' => $name,
408 'sector' => $sector,
409 'subSector' => $subSector,
410 'subSubSector' => $subSubSector,
411 'subSubSectorName' => $subSubSectorName,
412 'description' => $description,
413 'pub' => $pub,
414 'w_email' => $w_email,
415 'w_email_pub' => $w_emailPub,
416 'w_url' => $w_url,
417 'hq_acronym' => $hq_acronym,
418 'hq_url' => $hq_url,
419 'hq_email' => $hq_email,
420 'w_address' => array(
421 'accuracy' => $w_accuracy,
422 'text' => $w_text,
423 'postalText' => $w_postalText,
424 'postalCode' => $w_postalCode,
425 'localityId' => $w_localityId,
426 'subAdministrativeAreaId' => $w_subAdministrativeAreaId,
427 'administrativeAreaId' => $w_administrativeAreaId,
428 'countryId' => $w_countryId,
429 'latitude' => $w_latitude,
430 'longitude' => $w_longitude,
431 'pub' => $w_pub,
432 'updateTime' => $w_updateTime,
433 'north' => $w_north,
434 'south' => $w_south,
435 'east' => $w_east,
436 'west' => $w_west,
437 ),
438 'hq_address' => array(
439 'accuracy' => $hq_accuracy,
440 'text' => $hq_text,
441 'postalText' => $hq_postalText,
442 'postalCode' => $hq_postalCode,
443 'localityId' => $hq_localityId,
444 'subAdministrativeAreaId' => $hq_subAdministrativeAreaId,
445 'administrativeAreaId' => $hq_administrativeAreaId,
446 'countryId' => $hq_countryId,
447 'latitude' => $hq_latitude,
448 'longitude' => $hq_longitude,
449 'pub' => $hq_pub,
450 'updateTime' => $hq_updateTime,
451 'north' => $hq_north,
452 'south' => $hq_south,
453 'east' => $hq_east,
454 'west' => $hq_west,
455 ),
456 );
457 }
458
459 $it = Phone::iterate(array($this->pid()), array(Phone::LINK_JOB));
460 while ($phone = $it->next()) {
461 $this->values['jobs'][$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray();
462 }
463 $res = XDB::iterator("SELECT e.jtid, e.full_name, j.jid AS jobid
464 FROM profile_job_term_enum AS e
465 INNER JOIN profile_job_term AS j USING(jtid)
466 WHERE pid = {?}
467 ORDER BY j.jid",
468 $this->pid());
469 $i = 0;
470 $jobNb = count($this->values['jobs']);
471 while ($term = $res->next()) {
472 $jobid = $term['jobid'];
473 while ($i < $jobNb && $this->values['jobs'][$i]['id'] < $jobid) {
474 $i++;
475 }
476 if ($i >= $jobNb) {
477 break;
478 }
479 $job =& $this->values['jobs'][$i];
480 if ($job['id'] != $jobid) {
481 continue;
482 }
483 if (!isset($job['terms'])) {
484 $job['terms'] = array();
485 }
486 $job['terms'][] = $term;
487 }
488
489 foreach ($this->values['jobs'] as $id => &$job) {
490 $phone = new Phone();
491 if (!isset($job['w_phone'])) {
492 $job['w_phone'] = array(0 => $phone->toFormArray());
493 }
494 }
495
496 $job['w_email_new'] = '';
497 if (!isset($job['hq_fixed'])) {
498 $job['hq_fixed'] = '';
499 }
500 if (!isset($job['hq_fax'])) {
501 $job['hq_fax'] = '';
502 }
503 if (!isset($job['w_email_pub'])) {
504 $job['w_email_pub'] = 'private';
505 }
506 if (!$job['hq_address']['text']) {
507 $job['hq_address'] = array(
508 'text' => '',
509 'accuracy' => '',
510 'postalText' => '',
511 'postalCode' => '',
512 'administrativeAreaId' => '',
513 'subAdministrativeAreaId' => '',
514 'localityId' => '',
515 'countryId' => '',
516 'latitude' => '',
517 'longitude' => '',
518 'north' => '',
519 'south' => '',
520 'east' => '',
521 'west' => '',
522 'cedex' => '',
523 'updateTime' => '',
524 'changed' => '0',
525 'removed' => '0',
526 );
527 }
528 $job['w_address']['cedex'] = '';
529 $job['w_address']['changed'] = '0';
530 $job['w_address']['removed'] = '0';
531 } else {
532 $this->values['jobs'][] = $this->settings['jobs']->emptyJob();
533 }
534 }
535
536 protected function _saveData()
537 {
538 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
539 if ($this->changed['cv']) {
540 XDB::execute("UPDATE profiles
541 SET cv = {?}
542 WHERE pid = {?}",
543 $this->values['cv'], $this->pid());
544 }
545 }
546 }
547
548 public function _prepare(PlPage &$page, $id)
549 {
550 require_once 'emails.combobox.inc.php';
551 fill_email_combobox($page, $this->owner);
552
553 $res = XDB::query("SELECT id, name AS label
554 FROM profile_job_sector_enum");
555 $page->assign('sectors', $res->fetchAllAssoc());
556
557 $res = XDB::iterator("SELECT id, name
558 FROM profile_corps_enum
559 ORDER BY id = 1 DESC, name");
560 $page->assign('original_corps', $res->fetchAllAssoc());
561
562 $res = XDB::iterator("SELECT id, name
563 FROM profile_corps_enum
564 WHERE still_exists = 1
565 ORDER BY id = 1 DESC, name");
566 $page->assign('current_corps', $res->fetchAllAssoc());
567
568 $res = XDB::iterator("SELECT id, name
569 FROM profile_corps_rank_enum
570 ORDER BY id = 1 DESC, name");
571 $page->assign('corps_rank', $res->fetchAllAssoc());
572 }
573 }
574
575 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
576 ?>