2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class ProfileSettingJob
extends ProfileSettingGeocoding
31 public function __construct()
33 $this->pub
= new ProfileSettingPub();
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'),
45 public function emptyJob()
55 'hq_address' => array(
60 'administrativeAreaId' => '',
61 'subAdministrativeAreaId' => '',
77 'subSubSectorName' => null
,
80 'subSubSector' => '0',
89 'administrativeAreaId' => '',
90 'subAdministrativeAreaId' => '',
105 'w_email_pub' => 'private',
107 'w_phone' => array(0 => array(
117 private function cleanJob(ProfilePage
&$page, $jobid, array &$job, &$success)
120 if ($job['w_email'] == "new@example.org") {
121 $job['w_email'] = $job['w_email_new'];
123 foreach ($this->checks
as $obj=>&$fields) {
125 foreach ($fields as $field) {
126 $job[$field] = $chk->value($page, $field, $job[$field], $s);
129 $job[$field . '_error'] = true
;
133 if (!$job['subSubSectorName']) {
134 $res = XDB
::query("SELECT name
135 FROM profile_job_subsubsector_enum
137 $job['subSubSector']);
138 $job['subSubSectorName'] = $res->fetchOneCell();
140 $res = XDB
::query("SELECT sectorid, subsectorid, id
141 FROM profile_job_subsubsector_enum
143 $job['subSubSectorName']);
144 if ($res->numRows() != 1) {
146 $job['sector_error'] = true
;
148 list($job['sector'], $job['subSector'], $job['subSubSector']) = $res->fetchOneRow();
151 if (count($job['terms'])) {
153 foreach ($job['terms'] as $term) {
154 if (!$term['full_name']) {
155 $termsid[] = $term['jtid'];
158 if (count($termsid)) {
159 $res = XDB
::query("SELECT jtid, full_name
160 FROM profile_job_term_enum
163 $term_id_to_name = $res->fetchAllAssoc('jtid', false
);
164 foreach ($job['terms'] as &$term) {
165 if (!$term['full_name']) {
166 $term['full_name'] = $term_id_to_name[$term['jtid']];
172 $res = XDB
::query("SELECT id
173 FROM profile_job_enum
176 if ($res->numRows() != 1) {
177 $this->geocodeAddress($job['hq_address'], $s);
179 $gmapsGeocoder = new GMapsGeocoder();
180 $job['hq_address'] = $gmapsGeocoder->stripGeocodingFromAddress($job['hq_address']);
182 $req = new EntrReq(S
::user(), $page->profile
, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
183 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
185 $job['jobid'] = null
;
188 $job['jobid'] = $res->fetchOneCell();
191 $job['w_address']['pub'] = $this->pub
->value($page, 'address_pub', $job['w_address']['pub'], $s);
192 $job['w_phone'] = Phone
::formatFormArray($job['w_phone'], $s);
194 unset($job['removed']);
198 public function value(ProfilePage
&$page, $field, $value, &$success)
200 require_once 'validations.inc.php';
201 $entreprise = ProfileValidate
::get_typed_requests($page->pid(), 'entreprise');
205 if (is_null($value)) {
206 $value = $page->values
['jobs'];
210 foreach ($value as $key => &$job) {
211 $job['name'] = trim($job['name']);
212 if ($job['name'] == '' && $entreprise) {
213 $job['tmp_name'] = $entreprise[$entr_val]->name
;
215 } else if ($job['name'] == '') {
216 if ($job['subSubSectorName'] == '' && $job['description'] == '' && $job['w_url'] == ''
217 && $job['w_address']['text'] == '' && $job['w_email'] == ''
218 && count($job['w_phone']) == 1 && $job['w_phone']['tel'] == '') {
219 array_splice($value, $key, 1);
224 $job['name_error'] = true
;
229 if (isset($job['removed']) && $job['removed']) {
230 if ($job['name'] == '' && $entreprise) {
231 $entreprise[$entr_val - 1]->clean();
233 array_splice($value, $key, 1);
235 foreach (array('sectorid', 'subsectorid', 'subsubsectorid') as $key) {
236 if ($job[$key] == 0) {
241 foreach ($value as $key => &$job) {
243 $this->geocodeAddress($job['w_address'], $s);
245 $this->cleanJob($page, $key, $job, $s);
248 $success = ($success && $ls);
254 public function save(ProfilePage
&$page, $field, $value)
256 // TODO: use address class to update profile_job_enum once it is done.
257 XDB
::execute("DELETE FROM profile_job
260 XDB
::execute("DELETE FROM profile_addresses
261 WHERE pid = {?} AND type = 'job'",
263 Phone
::deletePhones($page->pid(), Phone
::LINK_JOB
);
264 $terms_values = array();
265 foreach ($value as $id => &$job) {
266 if (isset($job['name']) && $job['name']) {
267 if (isset($job['jobid']) && $job['jobid']) {
268 XDB
::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
269 subsubsectorid, email, url, pub, email_pub, jobid)
270 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
271 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
272 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
274 XDB
::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
275 subsubsectorid, email, url, pub, email_pub)
276 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
277 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
278 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
280 $address = new ProfileSettingAddress();
281 $address->saveAddress($page->pid(), $id, $job['w_address'], 'job');
282 Phone
::savePhones($job['w_phone'], $page->pid(), Phone
::LINK_JOB
, $id);
283 if (isset($job['terms'])) {
284 foreach ($job['terms'] as $term) {
285 $terms_values[] = '('.XDB
::escape($page->pid()).', '. XDB
::escape($id).', '.XDB
::escape($term['jtid']).', "original")';
290 if (count($terms_values) > 0) {
291 XDB
::execute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
292 VALUES '.implode(', ', $terms_values));
296 public function getText($value) {
298 foreach ($value as $id => $job) {
299 $address = new ProfileSettingAddress();
300 $phones = Phone
::formArrayToString($job['w_phone']);
301 $jobs[] = 'Entreprise : ' . $job['name'] . ', secteur : ' . $job['subSubSectorName']
302 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
303 . ', email : ' . $job['w_email']
304 . ($phones ?
', ' . $phones : '') . ', ' . $address->getText($job['w_address']);
306 return implode(' ; ' , $jobs);
310 class ProfileSettingCorps
implements ProfileSetting
312 public function value(ProfilePage
&$page, $field, $value, &$success)
315 if (is_null($value)) {
316 $res = XDB
::query("SELECT original_corpsid AS original, current_corpsid AS current,
317 rankid AS rank, corps_pub AS pub
321 return $res->fetchOneAssoc();
326 public function save(ProfilePage
&$page, $field, $value)
328 XDB
::execute('REPLACE INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
329 VALUES ({?}, {?}, {?}, {?}, {?})',
330 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
333 public function getText($value)
335 $corpsList = DirEnum
::getOptions(DirEnum
::CORPS
);
336 $rankList = DirEnum
::getOptions(DirEnum
::CORPSRANKS
);
337 return 'Corps actuel : ' . $corpsList[$value['current']] . ' , rang : ' . $corpsList[$value['rank']]
338 . ' , corps d\'origine : ' . $corpsList[$value['original']] . ' , affichage : ' . $value['pub'];
342 class ProfileSettingJobs
extends ProfilePage
344 protected $pg_template = 'profile/jobs.tpl';
346 public function __construct(PlWizard
&$wiz)
348 parent
::__construct($wiz);
349 $this->settings
['cv'] = null
;
350 $this->settings
['corps'] = new ProfileSettingCorps();
351 $this->settings
['jobs'] = new ProfileSettingJob();
352 $this->watched
= array('cv' => true
, 'jobs' => true
, 'corps' => true
);
355 protected function _fetchData()
358 $res = XDB
::query("SELECT cv
362 $this->values
['cv'] = $res->fetchOneCell();
364 // Build the jobs tree
365 $res = XDB
::iterRow("SELECT j.id, j.jobid, je.name, j.sectorid, j.subsectorid, j.subsubsectorid,
366 s.name, j.description, j.email, j.email_pub, j.url, j.pub,
367 je.acronym, je.url, je.email,
368 aw.accuracy, aw.text, aw.postalText, aw.postalCode, aw.localityId,
369 aw.subAdministrativeAreaId, aw.administrativeAreaId, aw.countryId,
370 aw.latitude, aw.longitude, aw.pub, aw.updateTime,
371 aw.north, aw.south, aw.east, aw.west,
372 ah.accuracy, ah.text, ah.postalText, ah.postalCode, ah.localityId,
373 ah.subAdministrativeAreaId, ah.administrativeAreaId, ah.countryId,
374 ah.latitude, ah.longitude, ah.pub, ah.updateTime,
375 ah.north, ah.south, ah.east, ah.west
376 FROM profile_job AS j
377 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
378 LEFT JOIN profile_job_subsubsector_enum AS s ON (s.id = j.subsubsectorid)
379 LEFT JOIN profile_addresses AS aw ON (aw.pid = j.pid AND aw.type = 'job'
381 LEFT JOIN profile_addresses AS ah ON (ah.jobid = j.jobid AND ah.type = 'hq')
385 $this->values
['jobs'] = array();
387 if ($res->numRows() > 0) {
388 while (list($id, $jobid, $name, $sector, $subSector, $subSubSector,
389 $subSubSectorName, $description, $w_email, $w_emailPub, $w_url, $pub,
390 $hq_acronym, $hq_url, $hq_email,
391 $w_accuracy, $w_text, $w_postalText, $w_postalCode, $w_localityId,
392 $w_subAdministrativeAreaId, $w_administrativeAreaId, $w_countryId,
393 $w_latitude, $w_longitude, $w_pub, $w_updateTime,
394 $w_north, $w_south, $w_east, $w_west,
395 $hq_accuracy, $hq_text, $hq_postalText, $hq_postalCode, $hq_localityId,
396 $hq_subAdministrativeAreaId, $hq_administrativeAreaId, $hq_countryId,
397 $hq_latitude, $hq_longitude, $hq_pub, $hq_updateTime,
398 $hq_north, $hq_south, $hq_east, $hq_west,
400 $this->values
['jobs'][] = array(
405 'subSector' => $subSector,
406 'subSubSector' => $subSubSector,
407 'subSubSectorName' => $subSubSectorName,
408 'description' => $description,
410 'w_email' => $w_email,
411 'w_email_pub' => $w_emailPub,
413 'hq_acronym' => $hq_acronym,
415 'hq_email' => $hq_email,
416 'w_address' => array(
417 'accuracy' => $w_accuracy,
419 'postalText' => $w_postalText,
420 'postalCode' => $w_postalCode,
421 'localityId' => $w_localityId,
422 'subAdministrativeAreaId' => $w_subAdministrativeAreaId,
423 'administrativeAreaId' => $w_administrativeAreaId,
424 'countryId' => $w_countryId,
425 'latitude' => $w_latitude,
426 'longitude' => $w_longitude,
428 'updateTime' => $w_updateTime,
434 'hq_address' => array(
435 'accuracy' => $hq_accuracy,
437 'postalText' => $hq_postalText,
438 'postalCode' => $hq_postalCode,
439 'localityId' => $hq_localityId,
440 'subAdministrativeAreaId' => $hq_subAdministrativeAreaId,
441 'administrativeAreaId' => $hq_administrativeAreaId,
442 'countryId' => $hq_countryId,
443 'latitude' => $hq_latitude,
444 'longitude' => $hq_longitude,
446 'updateTime' => $hq_updateTime,
447 'north' => $hq_north,
448 'south' => $hq_south,
455 $it = Phone
::iterate(array($this->pid()), array(Phone
::LINK_JOB
));
456 while ($phone = $it->next()) {
457 $this->values
['jobs'][$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray();
459 $res = XDB
::iterator("SELECT e.jtid, e.full_name, j.jid AS jobid
460 FROM profile_job_term_enum AS e
461 INNER JOIN profile_job_term AS j USING(jtid)
466 $jobNb = count($this->values
['jobs']);
467 while ($term = $res->next()) {
468 $jobid = $term['jobid'];
469 while ($i < $jobNb && $this->values
['jobs'][$i]['id'] < $jobid) {
475 $job =& $this->values
['jobs'][$i];
476 if ($job['id'] != $jobid) {
479 if (!isset($job['terms'])) {
480 $job['terms'] = array();
482 $job['terms'][] = $term;
485 foreach ($this->values
['jobs'] as $id => &$job) {
486 $phone = new Phone();
487 if (!isset($job['w_phone'])) {
488 $job['w_phone'] = array(0 => $phone->toFormArray());
492 $job['w_email_new'] = '';
493 if (!isset($job['hq_fixed'])) {
494 $job['hq_fixed'] = '';
496 if (!isset($job['hq_fax'])) {
499 if (!isset($job['w_email_pub'])) {
500 $job['w_email_pub'] = 'private';
502 if (!$job['hq_address']['text']) {
503 $job['hq_address'] = array(
508 'administrativeAreaId' => '',
509 'subAdministrativeAreaId' => '',
524 $job['w_address']['cedex'] = '';
525 $job['w_address']['changed'] = '0';
526 $job['w_address']['removed'] = '0';
528 $this->values
['jobs'][] = $this->settings
['jobs']->emptyJob();
532 protected function _saveData()
534 if ($this->changed
['cv']) {
535 XDB
::execute("UPDATE profiles
538 $this->values
['cv'], $this->pid());
542 public function _prepare(PlPage
&$page, $id)
544 require_once 'emails.combobox.inc.php';
545 fill_email_combobox($page, $this->owner
);
547 $res = XDB
::query("SELECT id, name AS label
548 FROM profile_job_sector_enum");
549 $page->assign('sectors', $res->fetchAllAssoc());
551 $res = XDB
::iterator("SELECT id, name
552 FROM profile_corps_enum
553 ORDER BY id = 1 DESC, name");
554 $page->assign('original_corps', $res->fetchAllAssoc());
556 $res = XDB
::iterator("SELECT id, name
557 FROM profile_corps_enum
558 WHERE still_exists = 1
559 ORDER BY id = 1 DESC, name");
560 $page->assign('current_corps', $res->fetchAllAssoc());
562 $res = XDB
::iterator("SELECT id, name
563 FROM profile_corps_rank_enum
564 ORDER BY id = 1 DESC, name");
565 $page->assign('corps_rank', $res->fetchAllAssoc());
569 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: