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
implements ProfileSetting
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()
47 $address = new Address();
56 'hq_address' => $address->toFormArray(),
59 'subSubSectorName' => null
,
62 'subSubSector' => '0',
65 'w_address' => $address->toFormArray(),
67 'w_email_pub' => 'private',
69 'w_phone' => array(0 => array(
79 private function cleanJob(ProfilePage
&$page, $jobid, array &$job, &$success)
81 if ($job['w_email'] == "new@example.org") {
82 $job['w_email'] = $job['w_email_new'];
84 foreach ($this->checks
as $obj=>&$fields) {
86 foreach ($fields as $field) {
87 $job[$field] = $chk->value($page, $field, $job[$field], $s);
90 $job[$field . '_error'] = true
;
94 if (!$job['subSubSectorName']) {
95 $res = XDB
::query("SELECT name
96 FROM profile_job_subsubsector_enum
98 $job['subSubSector']);
99 $job['subSubSectorName'] = $res->fetchOneCell();
101 $res = XDB
::query("SELECT sectorid, subsectorid, id
102 FROM profile_job_subsubsector_enum
104 $job['subSubSectorName']);
105 if ($res->numRows() != 1) {
107 $job['sector_error'] = true
;
109 list($job['sector'], $job['subSector'], $job['subSubSector']) = $res->fetchOneRow();
112 if (count($job['terms'])) {
114 foreach ($job['terms'] as $term) {
115 if (!$term['full_name']) {
116 $termsid[] = $term['jtid'];
119 if (count($termsid)) {
120 $res = XDB
::query("SELECT jtid, full_name
121 FROM profile_job_term_enum
124 $term_id_to_name = $res->fetchAllAssoc('jtid', false
);
125 foreach ($job['terms'] as &$term) {
126 if (!$term['full_name']) {
127 $term['full_name'] = $term_id_to_name[$term['jtid']];
133 $res = XDB
::query("SELECT id
134 FROM profile_job_enum
137 if ($res->numRows() != 1) {
138 $req = new EntrReq(S
::user(), $page->profile
, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
139 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
141 $job['jobid'] = null
;
144 $job['jobid'] = $res->fetchOneCell();
147 $job['w_phone'] = Phone
::formatFormArray($job['w_phone'], $s);
149 unset($job['removed']);
153 public function value(ProfilePage
&$page, $field, $value, &$success)
155 require_once 'validations.inc.php';
156 $entreprise = ProfileValidate
::get_typed_requests($page->pid(), 'entreprise');
160 if (is_null($value)) {
161 $value = $page->values
['jobs'];
165 foreach ($value as $key => &$job) {
166 $job['name'] = trim($job['name']);
167 if ($job['name'] == '' && $entreprise) {
168 $job['tmp_name'] = $entreprise[$entr_val]->name
;
170 } else if ($job['name'] == '') {
171 if ($job['subSubSectorName'] == '' && $job['description'] == '' && $job['w_url'] == ''
172 && $job['w_address']['text'] == '' && $job['w_email'] == ''
173 && count($job['w_phone']) == 1 && $job['w_phone']['tel'] == '') {
174 array_splice($value, $key, 1);
179 $job['name_error'] = true
;
184 if (isset($job['removed']) && $job['removed']) {
185 if ($job['name'] == '' && $entreprise) {
186 $entreprise[$entr_val - 1]->clean();
188 array_splice($value, $key, 1);
190 foreach (array('sectorid', 'subsectorid', 'subsubsectorid') as $key) {
191 if ($job[$key] == 0) {
196 foreach ($value as $key => &$job) {
197 $address = new Address($job['w_address']);
198 $s = $address->format();
199 $job['w_address'] = $address->toFormArray();
200 $this->cleanJob($page, $key, $job, $s);
202 $success = ($success && $s);
208 public function save(ProfilePage
&$page, $field, $value)
210 XDB
::execute("DELETE FROM profile_job
213 Address
::delete($page->pid(), Address
::LINK_JOB
);
214 Phone
::deletePhones($page->pid(), Phone
::LINK_JOB
);
215 $terms_values = array();
216 foreach ($value as $id => &$job) {
217 if (isset($job['name']) && $job['name']) {
218 if (isset($job['jobid']) && $job['jobid']) {
219 XDB
::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
220 subsubsectorid, email, url, pub, email_pub, jobid)
221 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
222 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
223 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
225 XDB
::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
226 subsubsectorid, email, url, pub, email_pub)
227 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
228 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
229 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
231 $address = new Address(array_merge($job['w_address'], array('pid' => $page->pid(), 'id' => $id, 'type' => Address
::LINK_JOB
)));
233 Phone
::savePhones($job['w_phone'], $page->pid(), Phone
::LINK_JOB
, $id);
234 if (isset($job['terms'])) {
235 foreach ($job['terms'] as $term) {
236 $terms_values[] = '('.XDB
::escape($page->pid()).', '. XDB
::escape($id).', '.XDB
::escape($term['jtid']).', "original")';
241 if (count($terms_values) > 0) {
242 XDB
::execute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
243 VALUES '.implode(', ', $terms_values));
247 public function getText($value) {
249 foreach ($value as $id => $job) {
250 $address = Address
::formArrayToString($job['w_address']);
251 $phones = Phone
::formArrayToString($job['w_phone']);
252 $jobs[] = 'Entreprise : ' . $job['name'] . ', secteur : ' . $job['subSubSectorName']
253 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
254 . ', email : ' . $job['w_email']
255 . ($phones ?
', ' . $phones : '') . ($address ?
', ' . $address : '');
257 return implode(' ; ' , $jobs);
261 class ProfileSettingCorps
implements ProfileSetting
263 public function value(ProfilePage
&$page, $field, $value, &$success)
266 if (is_null($value)) {
267 $res = XDB
::query("SELECT original_corpsid AS original, current_corpsid AS current,
268 rankid AS rank, corps_pub AS pub
272 return $res->fetchOneAssoc();
277 public function save(ProfilePage
&$page, $field, $value)
279 XDB
::execute('REPLACE INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
280 VALUES ({?}, {?}, {?}, {?}, {?})',
281 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
284 public function getText($value)
286 $corpsList = DirEnum
::getOptions(DirEnum
::CORPS
);
287 $rankList = DirEnum
::getOptions(DirEnum
::CORPSRANKS
);
288 return 'Corps actuel : ' . $corpsList[$value['current']] . ' , rang : ' . $corpsList[$value['rank']]
289 . ' , corps d\'origine : ' . $corpsList[$value['original']] . ' , affichage : ' . $value['pub'];
293 class ProfileSettingJobs
extends ProfilePage
295 protected $pg_template = 'profile/jobs.tpl';
297 public function __construct(PlWizard
&$wiz)
299 parent
::__construct($wiz);
300 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
301 $this->settings
['cv'] = null
;
303 $this->settings
['corps'] = new ProfileSettingCorps();
304 $this->settings
['jobs'] = new ProfileSettingJob();
305 $this->watched
= array('cv' => true
, 'jobs' => true
, 'corps' => true
);
308 protected function _fetchData()
310 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
312 $res = XDB
::query("SELECT cv
316 $this->values
['cv'] = $res->fetchOneCell();
319 // Build the jobs tree
320 $res = XDB
::iterRow("SELECT j.id, j.jobid, je.name, j.sectorid, j.subsectorid, j.subsubsectorid,
321 s.name, j.description, j.email, j.email_pub, j.url, j.pub,
322 je.acronym, je.url, je.email
323 FROM profile_job AS j
324 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
325 LEFT JOIN profile_job_subsubsector_enum AS s ON (s.id = j.subsubsectorid)
329 $this->values
['jobs'] = array();
331 $compagnies = array();
332 if ($res->numRows() > 0) {
333 while (list($id, $jobid, $name, $sector, $subSector, $subSubSector,
334 $subSubSectorName, $description, $w_email, $w_emailPub, $w_url, $pub,
335 $hq_acronym, $hq_url, $hq_email,
336 $w_subAdministrativeAreaId, $w_administrativeAreaId, $w_countryId,
338 $compagnies[] = $jobid;
339 $this->values
['jobs'][] = array(
344 'subSector' => $subSector,
345 'subSubSector' => $subSubSector,
346 'subSubSectorName' => $subSubSectorName,
347 'description' => $description,
349 'w_email' => $w_email,
350 'w_email_pub' => $w_emailPub,
352 'hq_acronym' => $hq_acronym,
354 'hq_email' => $hq_email,
358 $it = Address
::iterate(array($this->pid()), array(Address
::LINK_JOB
));
359 while ($address = $it->next()) {
360 $this->values
['jobs'][$address->jobid
]['w_address'] = $address->toFormArray();
362 $it = Address
::iterate(array(), array(Address
::LINK_COMPANY
), $companies);
363 while ($address = $it->next()) {
364 $this->values
['jobs'][$address->jobId()]['h_address'] = $address->toFormArray();
366 $it = Phone
::iterate(array($this->pid()), array(Phone
::LINK_JOB
));
367 while ($phone = $it->next()) {
368 $this->values
['jobs'][$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray();
370 $res = XDB
::iterator("SELECT e.jtid, e.full_name, j.jid AS jobid
371 FROM profile_job_term_enum AS e
372 INNER JOIN profile_job_term AS j USING(jtid)
377 $jobNb = count($this->values
['jobs']);
378 while ($term = $res->next()) {
379 $jobid = $term['jobid'];
380 while ($i < $jobNb && $this->values
['jobs'][$i]['id'] < $jobid) {
386 $job =& $this->values
['jobs'][$i];
387 if ($job['id'] != $jobid) {
390 if (!isset($job['terms'])) {
391 $job['terms'] = array();
393 $job['terms'][] = $term;
396 foreach ($this->values
['jobs'] as $id => &$job) {
397 $phone = new Phone();
398 $address = new Address();
399 if (!isset($job['w_phone'])) {
400 $job['w_phone'] = array(0 => $phone->toFormArray());
402 if (!isset($job['w_address'])) {
403 $job['w_address'] = $address->toFormArray();
405 if (!isset($job['h_address'])) {
406 $job['h_address'] = $address->toFormArray();
410 $job['w_email_new'] = '';
411 if (!isset($job['hq_fixed'])) {
412 $job['hq_fixed'] = '';
414 if (!isset($job['hq_fax'])) {
417 if (!isset($job['w_email_pub'])) {
418 $job['w_email_pub'] = 'private';
421 $this->values
['jobs'][] = $this->settings
['jobs']->emptyJob();
425 protected function _saveData()
427 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
428 if ($this->changed
['cv']) {
429 XDB
::execute("UPDATE profiles
432 $this->values
['cv'], $this->pid());
437 public function _prepare(PlPage
&$page, $id)
439 require_once 'emails.combobox.inc.php';
440 fill_email_combobox($page, $this->owner
);
442 $res = XDB
::query("SELECT id, name AS label
443 FROM profile_job_sector_enum");
444 $page->assign('sectors', $res->fetchAllAssoc());
446 $res = XDB
::iterator("SELECT id, name
447 FROM profile_corps_enum
448 ORDER BY id = 1 DESC, name");
449 $page->assign('original_corps', $res->fetchAllAssoc());
451 $res = XDB
::iterator("SELECT id, name
452 FROM profile_corps_enum
453 WHERE still_exists = 1
454 ORDER BY id = 1 DESC, name");
455 $page->assign('current_corps', $res->fetchAllAssoc());
457 $res = XDB
::iterator("SELECT id, name
458 FROM profile_corps_rank_enum
459 ORDER BY id = 1 DESC, name");
460 $page->assign('corps_rank', $res->fetchAllAssoc());
464 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: