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