Renumbering of phones now works in all cases (Closes #1158).
[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) {
a6483c12 156 $user = $page->profile->owner;
4d7d27fc
SJ
157 $this->geocodeAddress($job['hq_address'], $s);
158 if (!$s) {
159 $gmapsGeocoder = new GMapsGeocoder();
160 $job['hq_address'] = $gmapsGeocoder->stripGeocodingFromAddress($job['hq_address']);
161 }
162 $req = new EntrReq($user, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
163 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
b814a8b8
SJ
164 $req->submit();
165 $job['jobid'] = null;
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
SJ
183 require_once 'validations.inc.php';
184 $entreprise = Validate::get_typed_requests($page->profile->owner->id(), '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'] == '') {
199 $job['name_error'] = true;
200 $success = false;
b814a8b8 201 }
541e8d03 202 if (isset($job['removed']) && $job['removed']) {
ed01acac 203 array_splice($value, $key, 1);
37d44b3b
FB
204 }
205 }
a6483c12 206 foreach ($value as $key => &$job) {
37d44b3b 207 $ls = true;
541e8d03 208 $this->geocodeAddress($job['w_address'], $s);
37d44b3b 209 $ls = ($ls && $s);
bde2be3b 210 $this->cleanJob($page, $key, $job, $s);
37d44b3b
FB
211 $ls = ($ls && $s);
212 if (!$init) {
213 $success = ($success && $ls);
214 }
215 }
216 return $value;
217 }
218
219 public function save(ProfilePage &$page, $field, $value)
220 {
ecad8a08
SJ
221 // TODO: use address and phone classes to update profile_job_enum and profile_phones once they are done.
222
a6483c12
SJ
223 require_once 'profil.func.inc.php';
224 require_once 'validations.inc.php';
b814a8b8 225
c7139c07 226 XDB::execute("DELETE FROM profile_job
ce0b2c6f 227 WHERE pid = {?}",
a6483c12 228 $page->pid());
541e8d03
SJ
229 XDB::execute("DELETE FROM profile_addresses
230 WHERE pid = {?} AND type = 'job'",
a6483c12 231 $page->pid());
b235d980 232 XDB::execute("DELETE FROM profile_phones
ce0b2c6f 233 WHERE pid = {?} AND link_type = 'pro'",
a6483c12
SJ
234 $page->pid());
235 foreach ($value as $id => &$job) {
afaa2cc7
SJ
236 if (isset($job['name']) && $job['name']) {
237 if (isset($job['jobid']) && $job['jobid']) {
ce0b2c6f 238 XDB::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
afaa2cc7
SJ
239 subsubsectorid, email, url, pub, email_pub, jobid)
240 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
a6483c12 241 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
afaa2cc7
SJ
242 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
243 } else {
ce0b2c6f 244 XDB::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
afaa2cc7
SJ
245 subsubsectorid, email, url, pub, email_pub)
246 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
a6483c12 247 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
afaa2cc7
SJ
248 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
249 }
12bcf04b 250 $address = new ProfileSettingAddress();
a6483c12 251 $address->saveAddress($page->pid(), $id, $job['w_address'], 'job');
12bcf04b 252 $profiletel = new ProfileSettingPhones('pro', $id);
e3164bb1 253 $profiletel->saveTels($page->pid(), 'tel', $job['w_phone']);
b814a8b8 254 }
37d44b3b
FB
255 }
256 }
257}
258
12bcf04b 259class ProfileSettingJobs extends ProfilePage
3950bc21
FB
260{
261 protected $pg_template = 'profile/jobs.tpl';
262
263 public function __construct(PlWizard &$wiz)
264 {
265 parent::__construct($wiz);
37d44b3b 266 $this->settings['cv'] = null;
72e96bc0 267 $this->settings['corps'] = null;
12bcf04b 268 $this->settings['jobs'] = new ProfileSettingJob();
72e96bc0 269 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
37d44b3b
FB
270 }
271
7c2e0f0d 272 protected function _fetchData()
37d44b3b 273 {
37d44b3b
FB
274 // Checkout the CV
275 $res = XDB::query("SELECT cv
e5bcd851
FB
276 FROM profiles
277 WHERE pid = {?}",
278 $this->pid());
37d44b3b
FB
279 $this->values['cv'] = $res->fetchOneCell();
280
72e96bc0
SJ
281 // Checkout the corps
282 $res = XDB::query("SELECT original_corpsid AS original, current_corpsid AS current,
283 rankid AS rank, corps_pub AS pub
284 FROM profile_corps
ce0b2c6f 285 WHERE pid = {?}",
e5bcd851 286 $this->pid());
72e96bc0
SJ
287 $this->values['corps'] = $res->fetchOneAssoc();
288
37d44b3b 289 // Build the jobs tree
563f86f5 290 $res = XDB::iterRow("SELECT j.id, j.jobid, je.name, j.sectorid, j.subsectorid, j.subsubsectorid,
541e8d03
SJ
291 s.name, j.description, j.email, j.email_pub, j.url, j.pub,
292 je.acronym, je.url, je.email,
293 aw.accuracy, aw.text, aw.postalText, aw.postalCode, aw.localityId,
294 aw.subAdministrativeAreaId, aw.administrativeAreaId, aw.countryId,
295 aw.latitude, aw.longitude, aw.pub, aw.updateTime,
296 aw.north, aw.south, aw.east, aw.west,
297 ah.accuracy, ah.text, ah.postalText, ah.postalCode, ah.localityId,
298 ah.subAdministrativeAreaId, ah.administrativeAreaId, ah.countryId,
299 ah.latitude, ah.longitude, ah.pub, ah.updateTime,
300 ah.north, ah.south, ah.east, ah.west
c7139c07
SJ
301 FROM profile_job AS j
302 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
c7139c07 303 LEFT JOIN profile_job_subsubsector_enum AS s ON (s.id = j.subsubsectorid)
ce0b2c6f 304 LEFT JOIN profile_addresses AS aw ON (aw.pid = j.pid AND aw.type = 'job'
4d7d27fc 305 AND aw.id = j.id)
541e8d03 306 LEFT JOIN profile_addresses AS ah ON (ah.jobid = j.jobid AND ah.type = 'hq')
ce0b2c6f 307 WHERE j.pid = {?}
541e8d03 308 ORDER BY j.id",
a6391000 309 $this->pid());
37d44b3b 310 $this->values['jobs'] = array();
bde2be3b 311
afaa2cc7
SJ
312 if ($res->numRows() > 0) {
313 while (list($id, $jobid, $name, $sector, $subSector, $subSubSector,
314 $subSubSectorName, $description, $w_email, $w_emailPub, $w_url, $pub,
315 $hq_acronym, $hq_url, $hq_email,
316 $w_accuracy, $w_text, $w_postalText, $w_postalCode, $w_localityId,
317 $w_subAdministrativeAreaId, $w_administrativeAreaId, $w_countryId,
318 $w_latitude, $w_longitude, $w_pub, $w_updateTime,
319 $w_north, $w_south, $w_east, $w_west,
320 $hq_accuracy, $hq_text, $hq_postalText, $hq_postalCode, $hq_localityId,
321 $hq_subAdministrativeAreaId, $hq_administrativeAreaId, $hq_countryId,
322 $hq_latitude, $hq_longitude, $hq_pub, $hq_updateTime,
323 $hq_north, $hq_south, $hq_east, $hq_west,
324 ) = $res->next()) {
325 $this->values['jobs'][] = array(
326 'id' => $id,
327 'jobid' => $jobid,
328 'name' => $name,
329 'sector' => $sector,
330 'subSector' => $subSector,
331 'subSubSector' => $subSubSector,
332 'subSubSectorName' => $subSubSectorName,
333 'description' => $description,
334 'pub' => $pub,
335 'w_email' => $w_email,
336 'w_email_pub' => $w_emailPub,
337 'w_url' => $w_url,
338 'hq_acronym' => $hq_acronym,
339 'hq_url' => $hq_url,
340 'hq_email' => $hq_email,
341 'w_address' => array(
342 'accuracy' => $w_accuracy,
343 'text' => $w_text,
344 'postalText' => $w_postalText,
345 'postalCode' => $w_postalCode,
346 'localityId' => $w_localityId,
347 'subAdministrativeAreaId' => $w_subAdministrativeAreaId,
348 'administrativeAreaId' => $w_administrativeAreaId,
349 'countryId' => $w_countryId,
350 'latitude' => $w_latitude,
351 'longitude' => $w_longitude,
352 'pub' => $w_pub,
353 'updateTime' => $w_updateTime,
354 'north' => $w_north,
355 'south' => $w_south,
356 'east' => $w_east,
357 'west' => $w_west,
358 ),
359 'hq_address' => array(
360 'accuracy' => $hq_accuracy,
361 'text' => $hq_text,
362 'postalText' => $hq_postalText,
363 'postalCode' => $hq_postalCode,
364 'localityId' => $hq_localityId,
365 'subAdministrativeAreaId' => $hq_subAdministrativeAreaId,
366 'administrativeAreaId' => $hq_administrativeAreaId,
367 'countryId' => $hq_countryId,
368 'latitude' => $hq_latitude,
369 'longitude' => $hq_longitude,
370 'pub' => $hq_pub,
371 'updateTime' => $hq_updateTime,
372 'north' => $hq_north,
373 'south' => $hq_south,
374 'east' => $hq_east,
375 'west' => $hq_west,
376 ),
377 );
bde2be3b 378 }
afaa2cc7
SJ
379
380 $res = XDB::iterator("SELECT link_id AS jobid, tel_type AS type, pub, display_tel AS tel, comment
381 FROM profile_phones
ce0b2c6f 382 WHERE pid = {?} AND link_type = 'pro'
afaa2cc7 383 ORDER BY link_id",
a6483c12 384 $this->pid());
afaa2cc7
SJ
385 $i = 0;
386 $jobNb = count($this->values['jobs']);
387 while ($phone = $res->next()) {
388 $jobid = $phone['jobid'];
389 while ($i < $jobNb && $this->values['jobs'][$i]['id'] < $jobid) {
390 $i++;
391 }
392 if ($i >= $jobNb) {
393 break;
394 }
395 $job =& $this->values['jobs'][$i];
396 if (!isset($job['w_phone'])) {
397 $job['w_phone'] = array();
398 }
399 if ($job['id'] == $jobid) {
400 $job['w_phone'][] = $phone;
401 }
bde2be3b 402 }
afaa2cc7
SJ
403 foreach ($this->values['jobs'] as $id => &$job) {
404 if (!isset($job['w_phone'])) {
405 $job['w_phone'] = array(
406 0 => array(
407 'type' => 'fixed',
408 'tel' => '',
409 'pub' => 'private',
410 'comment' => '',
411 )
412 );
413 }
bde2be3b 414 }
afaa2cc7
SJ
415
416 $job['w_email_new'] = '';
417 if (!isset($job['hq_phone'])) {
418 $job['hq_phone'] = '';
bde2be3b 419 }
afaa2cc7
SJ
420 if (!isset($job['hq_fax'])) {
421 $job['hq_fax'] = '';
bde2be3b 422 }
afaa2cc7
SJ
423 if (!isset($job['w_email_pub'])) {
424 $job['w_email_pub'] = 'private';
bde2be3b 425 }
afaa2cc7
SJ
426 if (!$job['hq_address']['text']) {
427 $job['hq_address'] = array(
428 'text' => '',
429 'accuracy' => '',
430 'postalText' => '',
431 'postalCode' => '',
432 'administrativeAreaId' => '',
433 'subAdministrativeAreaId' => '',
434 'localityId' => '',
435 'countryId' => '',
436 'latitude' => '',
437 'longitude' => '',
438 'north' => '',
439 'south' => '',
440 'east' => '',
441 'west' => '',
442 'cedex' => '',
443 'updateTime' => '',
444 'changed' => '0',
445 'removed' => '0',
446 );
447 }
448 $job['w_address']['cedex'] = '';
449 $job['w_address']['changed'] = '0';
450 $job['w_address']['removed'] = '0';
451 } else {
452 $this->values['jobs'][] = $this->settings['jobs']->emptyJob();
bde2be3b 453 }
37d44b3b
FB
454 }
455
7c2e0f0d 456 protected function _saveData()
37d44b3b
FB
457 {
458 if ($this->changed['cv']) {
e5bcd851 459 XDB::execute("UPDATE profiles
37d44b3b 460 SET cv = {?}
e5bcd851
FB
461 WHERE pid = {?}",
462 $this->values['cv'], $this->pid());
37d44b3b 463 }
72e96bc0
SJ
464
465 if ($this->changed['corps']) {
34d09b54
SJ
466 XDB::execute('REPLACE INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
467 VALUES ({?}, {?}, {?}, {?}, {?})',
72e96bc0 468 $this->values['corps']['original'], $this->values['corps']['current'],
e5bcd851 469 $this->values['corps']['rank'], $this->values['corps']['pub'], $this->pid());
72e96bc0 470 }
3950bc21 471 }
2dcac0f5 472
04334c61 473 public function _prepare(PlPage &$page, $id)
2dcac0f5 474 {
a6483c12 475 require_once 'emails.combobox.inc.php';
e5bcd851 476 fill_email_combobox($page, $this->owner, $this->profile);
b715c1e1 477
33b6f5a0
SJ
478 $res = XDB::query("SELECT id, name AS label
479 FROM profile_job_sector_enum");
541e8d03 480 $page->assign('sectors', $res->fetchAllAssoc());
72e96bc0
SJ
481
482 $res = XDB::iterator("SELECT id, name
483 FROM profile_corps_enum
484 ORDER BY id = 1 DESC, name");
485 $page->assign('original_corps', $res->fetchAllAssoc());
486
487 $res = XDB::iterator("SELECT id, name
488 FROM profile_corps_enum
489 WHERE still_exists = 1
490 ORDER BY id = 1 DESC, name");
491 $page->assign('current_corps', $res->fetchAllAssoc());
492
493 $res = XDB::iterator("SELECT id, name
e489faf7
SJ
494 FROM profile_corps_rank_enum
495 ORDER BY id = 1 DESC, name");
72e96bc0 496 $page->assign('corps_rank', $res->fetchAllAssoc());
2dcac0f5 497 }
3950bc21
FB
498}
499
500// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
501?>