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