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