2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 private function emptyJob()
47 $address = new Address();
56 'w_address' => $address->toFormArray(),
58 'w_email_pub' => 'ax',
60 'w_phone' => array(0 => $phone->toFormArray()),
65 private function fetchJobs(ProfilePage
$page)
67 // Build the jobs tree
68 $jobs = XDB
::fetchAllAssoc('SELECT j.id, j.jobid, je.name,
69 j.description, j.email AS w_email,
70 j.email_pub AS w_email_pub,
71 j.url AS w_url, j.pub, j.entry_year AS w_entry_year
73 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
79 return array($this->emptyJob());
82 $compagnies = array();
84 foreach ($jobs as $key=>$job) {
85 $compagnies[] = $job['jobid'];
86 $backtrack[$job['id']] = $key;
89 $it = Address
::iterate(array($page->pid()), array(Address
::LINK_JOB
));
90 while ($address = $it->next()) {
91 $jobs[$address->id
]['w_address'] = $address->toFormArray();
93 $it = Phone
::iterate(array($page->pid()), array(Phone
::LINK_JOB
));
94 while ($phone = $it->next()) {
95 $jobs[$phone->link_id
]['w_phone'][$phone->id
] = $phone->toFormArray();
97 $res = XDB
::iterator("SELECT e.jtid, e.full_name, j.jid
98 FROM profile_job_term_enum AS e
99 INNER JOIN profile_job_term AS j USING(jtid)
103 while ($term = $res->next()) {
104 // $jid is the ID of the job among this user's jobs
106 if (!isset($backtrack[$jid])) {
109 $job =& $jobs[$backtrack[$jid]];
110 if (!isset($job['terms'])) {
111 $job['terms'] = array();
113 $job['terms'][] = $term;
116 $phone = new Phone();
117 $address = new Address();
118 foreach ($jobs as $id => &$job) {
119 if (!isset($job['w_phone'])) {
120 $job['w_phone'] = array(0 => $phone->toFormArray());
122 if (!isset($job['w_address'])) {
123 $job['w_address'] = $address->toFormArray();
126 $job['w_email_new'] = '';
127 if (!isset($job['w_email_pub'])) {
128 $job['w_email_pub'] = 'private';
134 private function cleanJob(ProfilePage
$page, $jobid, array &$job, &$success, $job_level)
136 if ($job['w_email'] == "new@example.org") {
137 $job['w_email'] = $job['w_email_new'];
139 foreach ($this->checks
as $obj=>&$fields) {
141 foreach ($fields as $field) {
142 $job[$field] = $chk->value($page, $field, $job[$field], $s);
145 $job[$field . '_error'] = true
;
149 if (count($job['terms'])) {
151 foreach ($job['terms'] as $term) {
152 if (!isset($term['full_name'])) {
153 $termsid[] = $term['jtid'];
156 if (count($termsid)) {
157 $res = XDB
::query("SELECT jtid, full_name
158 FROM profile_job_term_enum
161 $term_id_to_name = $res->fetchAllAssoc('jtid', false
);
162 foreach ($job['terms'] as &$term) {
163 if (!isset($term['full_name'])) {
164 $term['full_name'] = $term_id_to_name[$term['jtid']];
170 $res = XDB
::query("SELECT id
171 FROM profile_job_enum
174 if ($res->numRows() != 1) {
175 $job['jobid'] = null
;
177 $job['jobid'] = $res->fetchOneCell();
181 if (Visibility
::isLessRestrictive($job['w_email_pub'], $job_level)) {
182 $job['w_email_pub'] = $job_level;
184 $job['w_phone'] = Phone
::formatFormArray($job['w_phone'], $s, $job_level);
186 if ($job['w_entry_year'] && strlen($job['w_entry_year']) != 4) {
187 $job['w_entry_year_error'] = true
;
191 unset($job['removed']);
197 public function value(ProfilePage
$page, $field, $value, &$success)
199 $entreprise = ProfileValidate
::get_typed_requests($page->pid(), 'entreprise');
203 if (is_null($value)) {
204 $value = $this->fetchJobs($page);
208 foreach ($value as $key => $job) {
209 $job['name'] = trim($job['name']);
210 if ($job['name'] == '' && $entreprise[$entr_val]->id
== $key) {
211 $job['tmp_name'] = $entreprise[$entr_val]->name
;
213 } else if ($job['name'] == '') {
214 if ($job['description'] == '' && $job['w_url'] == ''
215 && $job['w_address']['text'] == '' && $job['w_email'] == ''
216 && count($job['w_phone']) >= 1 && $job['w_phone'][0]['display'] == '') {
222 $job['name_error'] = true
;
227 if (isset($job['removed']) && $job['removed']) {
228 if (!S
::user()->checkPerms('directory_private')
229 && (Phone
::hasPrivate($job['w_phone']) || Address
::hasPrivate($job['w_address']) ||
$job['w_email_pub'] == 'private')) {
230 Platal
::page()->trigWarning("L'entreprise ne peut être supprimée car elle contient des informations pour lesquelles vous n'avez pas le droit d'édition.");
232 if ($job['name'] == '' && $entreprise && isset($entreprise[$entr_val - 1])) {
233 $entreprise[$entr_val - 1]->clean();
239 if (!isset($job['pub']) ||
!$job['pub']) {
240 $job['pub'] = 'private';
244 foreach ($value as $key => &$job) {
245 $address = new Address($job['w_address']);
246 $s = $address->format();
248 // Force the address publicity to be at least as restricted as
249 // the job publicity.
250 $job_level = $job['pub'];
251 if (Visibility
::isLessRestrictive($address->pub
, $job_level)) {
252 $address->pub
= $job_level;
254 $job['w_address'] = $address->toFormArray();
255 $this->cleanJob($page, $key, $job, $s, $job_level);
257 $success = ($success && $s);
260 usort($value, 'Visibility::comparePublicity');
264 public function save(ProfilePage
$page, $field, $value)
267 $deletePrivate = S
::user()->isMe($page->owner
) || S
::admin();
268 XDB
::execute('DELETE FROM pj, pjt
269 USING profile_job AS pj
270 LEFT JOIN profile_job_term AS pjt ON (pj.pid = pjt.pid AND pj.id = pjt.jid)
271 WHERE pj.pid = {?}' . (($deletePrivate) ?
'' : ' AND pj.pub IN (\'public\', \'ax\')'),
273 Address
::deleteAddresses($page->pid(), Address
::LINK_JOB
, null
, null
, $deletePrivate);
274 Phone
::deletePhones($page->pid(), Phone
::LINK_JOB
, null
, $deletePrivate);
275 $previous_requests = EntrReq
::get_typed_requests($page->pid(), 'entreprise');
276 foreach ($previous_requests as $request) {
279 $terms_values = array();
280 foreach ($value as $id => &$job) {
281 if (($job['pub'] != 'private' ||
$deletePrivate) && (isset($job['name']) && $job['name'])) {
282 if (isset($job['jobid']) && $job['jobid']) {
283 XDB
::execute('INSERT INTO profile_job (pid, id, description, email, entry_year,
284 url, pub, email_pub, jobid)
285 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
286 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_entry_year'],
287 $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
289 XDB
::execute('INSERT INTO profile_job (pid, id, description, email, entry_year,
291 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
292 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_entry_year'],
293 $job['w_url'], $job['pub'], $job['w_email_pub']);
294 $request = new EntrReq(S
::user(), $page->profile
, $id, $job['name'], $job['hq_acronym'], $job['hq_url'],
295 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
299 $address = new Address(array_merge($job['w_address'],
300 array('pid' => $page->pid(),
302 'type' => Address
::LINK_JOB
)));
304 Phone
::savePhones($job['w_phone'], $page->pid(), Phone
::LINK_JOB
, $id);
305 if (isset($job['terms'])) {
306 foreach ($job['terms'] as $term) {
307 $terms_values[] = XDB
::format('({?}, {?}, {?}, {?})',
308 $page->pid(), $id, $term['jtid'], "original");
313 if (count($terms_values) > 0) {
314 XDB
::rawExecute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
315 VALUES ' . implode(', ', $terms_values) . '
316 ON DUPLICATE KEY UPDATE computed = VALUES(computed)');
318 if (S
::user()->isMe($page->owner
) && count($value) > 1) {
319 Platal
::page()->trigWarning('Attention, tu as plusieurs emplois sur ton profil. Pense à supprimer ceux qui sont obsolètes.');
323 public function getText($value)
325 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
327 foreach ($value as $id => $job) {
328 $address = Address
::formArrayToString(array($job['w_address']));
329 $phones = Phone
::formArrayToString($job['w_phone']);
330 $jobs[$id] = $job['name'];
331 $jobs[$id] .= ($job['description'] ?
(', ' . $job['description']) : '');
332 $jobs[$id] .= ' (affichage ' . $pubs[$job['pub']];
333 if (count($job['terms'])) {
335 foreach ($job['terms'] as $term) {
336 $terms[] = $term['full_name'];
338 $jobs[$id] .= ', mots-clefs : ' . implode(', ', $terms);
341 $jobs[$id] .= ', page perso : ' . $job['w_url'];
344 $jobs[$id] .= ', adresse : ' . $address;
346 if ($job['w_email']) {
347 $jobs[$id] .= ', email : ' . $job['w_email'];
350 $jobs[$id] .= ', téléphones : ' . $phones;
354 return implode(' ; ' , $jobs);
358 class ProfileSettingCorps
implements ProfileSetting
360 public function value(ProfilePage
$page, $field, $value, &$success)
363 if (is_null($value)) {
364 $res = XDB
::query('SELECT c.original_corpsid AS original, e.name AS originalText,
365 c.current_corpsid AS current, c.rankid AS rank, c.corps_pub AS pub
366 FROM profile_corps AS c
367 INNER JOIN profile_corps_enum AS e ON (c.original_corpsid = e.id)
370 return $res->fetchOneAssoc();
375 public function save(ProfilePage
$page, $field, $value)
377 if (!S
::user()->isMe($page->owner
)) {
378 XDB
::execute('INSERT INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
379 VALUES ({?}, {?}, {?}, {?}, {?})
380 ON DUPLICATE KEY UPDATE original_corpsid = VALUES(original_corpsid), current_corpsid = VALUES(current_corpsid),
381 rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
382 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
384 XDB
::execute('INSERT INTO profile_corps (current_corpsid, rankid, corps_pub, pid)
385 VALUES ({?}, {?}, {?}, {?})
386 ON DUPLICATE KEY UPDATE current_corpsid = VALUES(current_corpsid),
387 rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
388 $value['current'], $value['rank'], $value['pub'], $page->pid());
392 public function getText($value)
394 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
395 $corpsList = DirEnum
::getOptions(DirEnum
::ORIGINCORPS
);
396 $rankList = DirEnum
::getOptions(DirEnum
::CORPSRANKS
);
397 return $corpsList[$value['current']] . ', ' . $corpsList[$value['rank']] . ' ('
398 . 'corps d\'origine : ' . $corpsList[$value['original']] . ', affichage ' . $pubs[$value['pub']] . ')';
402 class ProfilePageJobs
extends ProfilePage
404 protected $pg_template = 'profile/jobs.tpl';
406 public function __construct(PlWizard
$wiz)
408 parent
::__construct($wiz);
409 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
410 $this->settings
['cv'] = null
;
412 $this->settings
['corps'] = new ProfileSettingCorps();
413 $this->settings
['jobs'] = new ProfileSettingJob();
414 $this->watched
= array('cv' => true
, 'jobs' => true
, 'corps' => true
);
415 Platal
::page()->assign('geocoding_removal', true
);
418 protected function _fetchData()
420 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
422 $res = XDB
::query("SELECT cv
426 $this->values
['cv'] = $res->fetchOneCell();
430 protected function _saveData()
432 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
433 if ($this->changed
['cv']) {
434 XDB
::execute("UPDATE profiles
437 $this->values
['cv'], $this->pid());
442 public function _prepare(PlPage
$page, $id)
444 require_once 'emails.combobox.inc.php';
445 fill_email_combobox($page, array('redirect', 'job', 'stripped_directory'), $this->owner
);
447 if (!S
::user()->isMe($this->owner
)) {
448 $res = XDB
::iterator('SELECT id, name
449 FROM profile_corps_enum
450 ORDER BY id = 1 DESC, name');
451 $page->assign('original_corps', $res->fetchAllAssoc());
454 $res = XDB
::iterator("SELECT id, name
455 FROM profile_corps_enum
456 WHERE still_exists = 1
457 ORDER BY id = 1 DESC, name");
458 $page->assign('current_corps', $res->fetchAllAssoc());
460 $res = XDB
::iterator("SELECT id, name
461 FROM profile_corps_rank_enum
462 ORDER BY id = 1 DESC, name");
463 $page->assign('corps_rank', $res->fetchAllAssoc());
467 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: