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