Fixes profil_job_enum.
[platal.git] / modules / profile / jobs.inc.php
CommitLineData
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
12bcf04b 22class ProfileSettingJob extends ProfileSettingGeocoding
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
afaa2cc7
SJ
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_phone' => '',
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 );
114 }
115
b814a8b8 116 private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success)
37d44b3b
FB
117 {
118 $success = true;
541e8d03
SJ
119 if ($job['w_email'] == "new@example.org") {
120 $job['w_email'] = $job['w_email_new'];
121 }
37d44b3b
FB
122 foreach ($this->checks as $obj=>&$fields) {
123 $chk =& $this->$obj;
124 foreach ($fields as $field) {
125 $job[$field] = $chk->value($page, $field, $job[$field], $s);
126 if (!$s) {
127 $success = false;
128 $job[$field . '_error'] = true;
129 }
130 }
131 }
541e8d03 132 if (!$job['subSubSectorName']) {
c7139c07
SJ
133 $res = XDB::query("SELECT name
134 FROM profile_job_subsubsector_enum
135 WHERE id = {?}",
541e8d03
SJ
136 $job['subSubSector']);
137 $job['subSubSectorName'] = $res->fetchOneCell();
c7139c07
SJ
138 } else {
139 $res = XDB::query("SELECT sectorid, subsectorid, id
140 FROM profile_job_subsubsector_enum
141 WHERE name = {?}",
541e8d03 142 $job['subSubSectorName']);
c7139c07
SJ
143 if ($res->numRows() != 1) {
144 $success = false;
145 $job['sector_error'] = true;
146 } else {
541e8d03 147 list($job['sector'], $job['subSector'], $job['subSubSector']) = $res->fetchOneRow();
c7139c07
SJ
148 }
149 }
b814a8b8 150 if ($job['name']) {
c7139c07
SJ
151 $res = XDB::query("SELECT id
152 FROM profile_job_enum
153 WHERE name = {?}",
154 $job['name']);
155 if ($res->numRows() != 1) {
4d7d27fc
SJ
156 $this->geocodeAddress($job['hq_address'], $s);
157 if (!$s) {
158 $gmapsGeocoder = new GMapsGeocoder();
159 $job['hq_address'] = $gmapsGeocoder->stripGeocodingFromAddress($job['hq_address']);
160 }
024ec1e5 161 $req = new EntrReq(S::user(), $page->profile, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
4d7d27fc 162 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
b814a8b8
SJ
163 $req->submit();
164 $job['jobid'] = null;
858a5b42 165 sleep(1);
c7139c07
SJ
166 } else {
167 $job['jobid'] = $res->fetchOneCell();
168 }
169 }
541e8d03
SJ
170 $job['w_address']['pub'] = $this->pub->value($page, 'address_pub', $job['w_address']['pub'], $s);
171 if (!isset($job['w_phone'])) {
172 $job['w_phone'] = array();
bde2be3b 173 }
12bcf04b 174 $profiletel = new ProfileSettingPhones('pro', $jobid);
541e8d03 175 $job['w_phone'] = $profiletel->value($page, 'tel', $job['w_phone'], $s);
afaa2cc7 176
37d44b3b
FB
177 unset($job['removed']);
178 unset($job['new']);
37d44b3b
FB
179 }
180
181 public function value(ProfilePage &$page, $field, $value, &$success)
182 {
a6483c12 183 require_once 'validations.inc.php';
024ec1e5 184 $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise');
b814a8b8
SJ
185 $entr_val = 0;
186
37d44b3b
FB
187 $init = false;
188 if (is_null($value)) {
189 $value = $page->values['jobs'];
190 $init = true;
191 }
192 $success = true;
a6483c12 193 foreach ($value as $key => &$job) {
b814a8b8 194 $job['name'] = trim($job['name']);
a6483c12 195 if ($job['name'] == '' && $entreprise) {
b814a8b8 196 $job['tmp_name'] = $entreprise[$entr_val]->name;
a6483c12
SJ
197 ++$entr_val;
198 } else if ($job['name'] == '') {
ba6ee875
SJ
199 if ($job['subSubSectorName'] == '' && $job['description'] == '' && $job['w_url'] == ''
200 && $job['w_address']['text'] == '' && $job['w_email'] == ''
201 && count($job['w_phone']) == 1 && $job['w_phone']['tel'] == '') {
202 array_splice($value, $key, 1);
203 continue;
204 }
205
a6483c12
SJ
206 $job['name_error'] = true;
207 $success = false;
b814a8b8 208 }
b94719c1 209
541e8d03 210 if (isset($job['removed']) && $job['removed']) {
b94719c1
SJ
211 if ($job['name'] == '' && $entreprise) {
212 $entreprise[$entr_val - 1]->clean();
213 }
ed01acac 214 array_splice($value, $key, 1);
37d44b3b
FB
215 }
216 }
a6483c12 217 foreach ($value as $key => &$job) {
37d44b3b 218 $ls = true;
541e8d03 219 $this->geocodeAddress($job['w_address'], $s);
37d44b3b 220 $ls = ($ls && $s);
bde2be3b 221 $this->cleanJob($page, $key, $job, $s);
37d44b3b
FB
222 $ls = ($ls && $s);
223 if (!$init) {
224 $success = ($success && $ls);
225 }
226 }
227 return $value;
228 }
229
230 public function save(ProfilePage &$page, $field, $value)
231 {
ecad8a08
SJ
232 // TODO: use address and phone classes to update profile_job_enum and profile_phones once they are done.
233
a6483c12 234 require_once 'profil.func.inc.php';
b814a8b8 235
c7139c07 236 XDB::execute("DELETE FROM profile_job
ce0b2c6f 237 WHERE pid = {?}",
a6483c12 238 $page->pid());
541e8d03
SJ
239 XDB::execute("DELETE FROM profile_addresses
240 WHERE pid = {?} AND type = 'job'",
a6483c12 241 $page->pid());
b235d980 242 XDB::execute("DELETE FROM profile_phones
ce0b2c6f 243 WHERE pid = {?} AND link_type = 'pro'",
a6483c12
SJ
244 $page->pid());
245 foreach ($value as $id => &$job) {
afaa2cc7
SJ
246 if (isset($job['name']) && $job['name']) {
247 if (isset($job['jobid']) && $job['jobid']) {
ce0b2c6f 248 XDB::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
afaa2cc7
SJ
249 subsubsectorid, email, url, pub, email_pub, jobid)
250 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
a6483c12 251 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
afaa2cc7
SJ
252 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
253 } else {
ce0b2c6f 254 XDB::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
afaa2cc7
SJ
255 subsubsectorid, email, url, pub, email_pub)
256 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
a6483c12 257 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
afaa2cc7
SJ
258 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
259 }
12bcf04b 260 $address = new ProfileSettingAddress();
a6483c12 261 $address->saveAddress($page->pid(), $id, $job['w_address'], 'job');
12bcf04b 262 $profiletel = new ProfileSettingPhones('pro', $id);
e3164bb1 263 $profiletel->saveTels($page->pid(), 'tel', $job['w_phone']);
b814a8b8 264 }
37d44b3b
FB
265 }
266 }
a0fce0c6
SJ
267
268 public function getText($value) {
269 $jobs = array();
270 foreach ($value as $id => $job) {
271 $address = new ProfileSettingAddress();
272 $phones = new ProfileSettingPhones('pro', $id);
273 $jobs[] = 'Entreprise : ' . $job['name'] . ', secteur : ' . $job['subSubSectorName']
274 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
275 . ', email : ' . $job['w_email']
276 . ', ' . $phones->getText($job['w_phone']) . ', ' . $address->getText($job['w_address']);
277 }
278 return implode(' ; ' , $jobs);
279 }
37d44b3b
FB
280}
281
603aeb6c
SJ
282class ProfileSettingCorps implements ProfileSetting
283{
284 public function value(ProfilePage &$page, $field, $value, &$success)
285 {
286 $success = true;
287 if (is_null($value)) {
288 $res = XDB::query("SELECT original_corpsid AS original, current_corpsid AS current,
289 rankid AS rank, corps_pub AS pub
290 FROM profile_corps
291 WHERE pid = {?}",
292 $page->pid());
293 return $res->fetchOneAssoc();
294 }
295 return $value;
296 }
297
298 public function save(ProfilePage &$page, $field, $value)
299 {
300 XDB::execute('REPLACE INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
301 VALUES ({?}, {?}, {?}, {?}, {?})',
302 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
303 }
304
305 public function getText($value)
306 {
307 $corpsList = DirEnum::getOptions(DirEnum::CORPS);
308 $rankList = DirEnum::getOptions(DirEnum::CORPSRANKS);
309 return 'Corps actuel : ' . $corpsList[$value['current']] . ' , rang : ' . $corpsList[$value['rank']]
310 . ' , corps d\'origine : ' . $corpsList[$value['original']] . ' , affichage : ' . $value['pub'];
311 }
312}
313
12bcf04b 314class ProfileSettingJobs extends ProfilePage
3950bc21
FB
315{
316 protected $pg_template = 'profile/jobs.tpl';
317
318 public function __construct(PlWizard &$wiz)
319 {
320 parent::__construct($wiz);
37d44b3b 321 $this->settings['cv'] = null;
603aeb6c 322 $this->settings['corps'] = new ProfileSettingCorps();
12bcf04b 323 $this->settings['jobs'] = new ProfileSettingJob();
72e96bc0 324 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
37d44b3b
FB
325 }
326
7c2e0f0d 327 protected function _fetchData()
37d44b3b 328 {
37d44b3b
FB
329 // Checkout the CV
330 $res = XDB::query("SELECT cv
e5bcd851
FB
331 FROM profiles
332 WHERE pid = {?}",
333 $this->pid());
37d44b3b
FB
334 $this->values['cv'] = $res->fetchOneCell();
335
336 // Build the jobs tree
563f86f5 337 $res = XDB::iterRow("SELECT j.id, j.jobid, je.name, j.sectorid, j.subsectorid, j.subsubsectorid,
541e8d03
SJ
338 s.name, j.description, j.email, j.email_pub, j.url, j.pub,
339 je.acronym, je.url, je.email,
340 aw.accuracy, aw.text, aw.postalText, aw.postalCode, aw.localityId,
341 aw.subAdministrativeAreaId, aw.administrativeAreaId, aw.countryId,
342 aw.latitude, aw.longitude, aw.pub, aw.updateTime,
343 aw.north, aw.south, aw.east, aw.west,
344 ah.accuracy, ah.text, ah.postalText, ah.postalCode, ah.localityId,
345 ah.subAdministrativeAreaId, ah.administrativeAreaId, ah.countryId,
346 ah.latitude, ah.longitude, ah.pub, ah.updateTime,
347 ah.north, ah.south, ah.east, ah.west
c7139c07
SJ
348 FROM profile_job AS j
349 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
c7139c07 350 LEFT JOIN profile_job_subsubsector_enum AS s ON (s.id = j.subsubsectorid)
ce0b2c6f 351 LEFT JOIN profile_addresses AS aw ON (aw.pid = j.pid AND aw.type = 'job'
4d7d27fc 352 AND aw.id = j.id)
541e8d03 353 LEFT JOIN profile_addresses AS ah ON (ah.jobid = j.jobid AND ah.type = 'hq')
ce0b2c6f 354 WHERE j.pid = {?}
541e8d03 355 ORDER BY j.id",
a6391000 356 $this->pid());
37d44b3b 357 $this->values['jobs'] = array();
bde2be3b 358
afaa2cc7
SJ
359 if ($res->numRows() > 0) {
360 while (list($id, $jobid, $name, $sector, $subSector, $subSubSector,
361 $subSubSectorName, $description, $w_email, $w_emailPub, $w_url, $pub,
362 $hq_acronym, $hq_url, $hq_email,
363 $w_accuracy, $w_text, $w_postalText, $w_postalCode, $w_localityId,
364 $w_subAdministrativeAreaId, $w_administrativeAreaId, $w_countryId,
365 $w_latitude, $w_longitude, $w_pub, $w_updateTime,
366 $w_north, $w_south, $w_east, $w_west,
367 $hq_accuracy, $hq_text, $hq_postalText, $hq_postalCode, $hq_localityId,
368 $hq_subAdministrativeAreaId, $hq_administrativeAreaId, $hq_countryId,
369 $hq_latitude, $hq_longitude, $hq_pub, $hq_updateTime,
370 $hq_north, $hq_south, $hq_east, $hq_west,
371 ) = $res->next()) {
372 $this->values['jobs'][] = array(
373 'id' => $id,
374 'jobid' => $jobid,
375 'name' => $name,
376 'sector' => $sector,
377 'subSector' => $subSector,
378 'subSubSector' => $subSubSector,
379 'subSubSectorName' => $subSubSectorName,
380 'description' => $description,
381 'pub' => $pub,
382 'w_email' => $w_email,
383 'w_email_pub' => $w_emailPub,
384 'w_url' => $w_url,
385 'hq_acronym' => $hq_acronym,
386 'hq_url' => $hq_url,
387 'hq_email' => $hq_email,
388 'w_address' => array(
389 'accuracy' => $w_accuracy,
390 'text' => $w_text,
391 'postalText' => $w_postalText,
392 'postalCode' => $w_postalCode,
393 'localityId' => $w_localityId,
394 'subAdministrativeAreaId' => $w_subAdministrativeAreaId,
395 'administrativeAreaId' => $w_administrativeAreaId,
396 'countryId' => $w_countryId,
397 'latitude' => $w_latitude,
398 'longitude' => $w_longitude,
399 'pub' => $w_pub,
400 'updateTime' => $w_updateTime,
401 'north' => $w_north,
402 'south' => $w_south,
403 'east' => $w_east,
404 'west' => $w_west,
405 ),
406 'hq_address' => array(
407 'accuracy' => $hq_accuracy,
408 'text' => $hq_text,
409 'postalText' => $hq_postalText,
410 'postalCode' => $hq_postalCode,
411 'localityId' => $hq_localityId,
412 'subAdministrativeAreaId' => $hq_subAdministrativeAreaId,
413 'administrativeAreaId' => $hq_administrativeAreaId,
414 'countryId' => $hq_countryId,
415 'latitude' => $hq_latitude,
416 'longitude' => $hq_longitude,
417 'pub' => $hq_pub,
418 'updateTime' => $hq_updateTime,
419 'north' => $hq_north,
420 'south' => $hq_south,
421 'east' => $hq_east,
422 'west' => $hq_west,
423 ),
424 );
bde2be3b 425 }
afaa2cc7
SJ
426
427 $res = XDB::iterator("SELECT link_id AS jobid, tel_type AS type, pub, display_tel AS tel, comment
428 FROM profile_phones
ce0b2c6f 429 WHERE pid = {?} AND link_type = 'pro'
afaa2cc7 430 ORDER BY link_id",
a6483c12 431 $this->pid());
afaa2cc7
SJ
432 $i = 0;
433 $jobNb = count($this->values['jobs']);
434 while ($phone = $res->next()) {
435 $jobid = $phone['jobid'];
436 while ($i < $jobNb && $this->values['jobs'][$i]['id'] < $jobid) {
437 $i++;
438 }
439 if ($i >= $jobNb) {
440 break;
441 }
442 $job =& $this->values['jobs'][$i];
443 if (!isset($job['w_phone'])) {
444 $job['w_phone'] = array();
445 }
446 if ($job['id'] == $jobid) {
447 $job['w_phone'][] = $phone;
448 }
bde2be3b 449 }
afaa2cc7
SJ
450 foreach ($this->values['jobs'] as $id => &$job) {
451 if (!isset($job['w_phone'])) {
452 $job['w_phone'] = array(
453 0 => array(
454 'type' => 'fixed',
455 'tel' => '',
456 'pub' => 'private',
457 'comment' => '',
458 )
459 );
460 }
bde2be3b 461 }
aab2ffdd 462
afaa2cc7
SJ
463 $job['w_email_new'] = '';
464 if (!isset($job['hq_phone'])) {
465 $job['hq_phone'] = '';
bde2be3b 466 }
afaa2cc7
SJ
467 if (!isset($job['hq_fax'])) {
468 $job['hq_fax'] = '';
bde2be3b 469 }
afaa2cc7
SJ
470 if (!isset($job['w_email_pub'])) {
471 $job['w_email_pub'] = 'private';
bde2be3b 472 }
afaa2cc7
SJ
473 if (!$job['hq_address']['text']) {
474 $job['hq_address'] = array(
475 'text' => '',
476 'accuracy' => '',
477 'postalText' => '',
478 'postalCode' => '',
479 'administrativeAreaId' => '',
480 'subAdministrativeAreaId' => '',
481 'localityId' => '',
482 'countryId' => '',
483 'latitude' => '',
484 'longitude' => '',
485 'north' => '',
486 'south' => '',
487 'east' => '',
488 'west' => '',
489 'cedex' => '',
490 'updateTime' => '',
491 'changed' => '0',
492 'removed' => '0',
493 );
494 }
495 $job['w_address']['cedex'] = '';
496 $job['w_address']['changed'] = '0';
497 $job['w_address']['removed'] = '0';
498 } else {
499 $this->values['jobs'][] = $this->settings['jobs']->emptyJob();
bde2be3b 500 }
37d44b3b
FB
501 }
502
7c2e0f0d 503 protected function _saveData()
37d44b3b
FB
504 {
505 if ($this->changed['cv']) {
e5bcd851 506 XDB::execute("UPDATE profiles
37d44b3b 507 SET cv = {?}
e5bcd851
FB
508 WHERE pid = {?}",
509 $this->values['cv'], $this->pid());
37d44b3b 510 }
3950bc21 511 }
2dcac0f5 512
04334c61 513 public function _prepare(PlPage &$page, $id)
2dcac0f5 514 {
a6483c12 515 require_once 'emails.combobox.inc.php';
4e698dc9 516 fill_email_combobox($page, $this->owner);
b715c1e1 517
33b6f5a0
SJ
518 $res = XDB::query("SELECT id, name AS label
519 FROM profile_job_sector_enum");
541e8d03 520 $page->assign('sectors', $res->fetchAllAssoc());
72e96bc0
SJ
521
522 $res = XDB::iterator("SELECT id, name
523 FROM profile_corps_enum
524 ORDER BY id = 1 DESC, name");
525 $page->assign('original_corps', $res->fetchAllAssoc());
526
527 $res = XDB::iterator("SELECT id, name
528 FROM profile_corps_enum
529 WHERE still_exists = 1
530 ORDER BY id = 1 DESC, name");
531 $page->assign('current_corps', $res->fetchAllAssoc());
532
533 $res = XDB::iterator("SELECT id, name
e489faf7
SJ
534 FROM profile_corps_rank_enum
535 ORDER BY id = 1 DESC, name");
72e96bc0 536 $page->assign('corps_rank', $res->fetchAllAssoc());
2dcac0f5 537 }
3950bc21
FB
538}
539
540// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
541?>