7e1a1ef112babacac2f8229232a9d5376dd08d13
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 private function emptyJob()
47 $address = new Address();
56 'w_address' => $address->toFormArray(),
58 'w_email_pub' => 'private',
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,
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->linkId()]['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)
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 (!$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 (!$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 $req = new EntrReq(S
::user(), $page->profile
, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
176 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
178 $job['jobid'] = null
;
181 $job['jobid'] = $res->fetchOneCell();
184 $job['w_phone'] = Phone
::formatFormArray($job['w_phone'], $s);
186 unset($job['removed']);
192 public function value(ProfilePage
&$page, $field, $value, &$success)
194 $entreprise = ProfileValidate
::get_typed_requests($page->pid(), 'entreprise');
198 if (is_null($value)) {
199 $value = $this->fetchJobs($page);
203 foreach ($value as $key => $job) {
204 $job['name'] = trim($job['name']);
205 if ($job['name'] == '' && $entreprise[$entr_val]->id
== $key) {
206 $job['tmp_name'] = $entreprise[$entr_val]->name
;
208 } else if ($job['name'] == '') {
209 if ($job['description'] == '' && $job['w_url'] == ''
210 && $job['w_address']['text'] == '' && $job['w_email'] == ''
211 && count($job['w_phone']) >= 1 && $job['w_phone'][0]['display'] == '') {
217 $job['name_error'] = true
;
222 if (isset($job['removed']) && $job['removed']) {
223 if ($job['name'] == '' && $entreprise) {
224 $entreprise[$entr_val - 1]->clean();
229 if (!isset($job['pub']) ||
!$job['pub']) {
230 $job['pub'] = 'private';
234 foreach ($value as $key => &$job) {
235 $address = new Address($job['w_address']);
236 $s = $address->format();
237 $job['w_address'] = $address->toFormArray();
238 $this->cleanJob($page, $key, $job, $s);
240 $success = ($success && $s);
246 public function save(ProfilePage
&$page, $field, $value)
248 XDB
::execute("DELETE FROM profile_job
251 XDB
::execute("DELETE FROM profile_job_term
254 Address
::deleteAddresses($page->pid(), Address
::LINK_JOB
);
255 Phone
::deletePhones($page->pid(), Phone
::LINK_JOB
);
256 $terms_values = array();
257 foreach ($value as $id => &$job) {
258 if (isset($job['name']) && $job['name']) {
259 if (isset($job['jobid']) && $job['jobid']) {
260 XDB
::execute('INSERT INTO profile_job (pid, id, description, email,
261 url, pub, email_pub, jobid)
262 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
263 $page->pid(), $id, $job['description'], $job['w_email'],
264 $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
266 XDB
::execute('INSERT INTO profile_job (pid, id, description, email,
268 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
269 $page->pid(), $id, $job['description'], $job['w_email'],
270 $job['w_url'], $job['pub'], $job['w_email_pub']);
272 $address = new Address(array_merge($job['w_address'],
273 array('pid' => $page->pid(),
275 'type' => Address
::LINK_JOB
)));
277 Phone
::savePhones($job['w_phone'], $page->pid(), Phone
::LINK_JOB
, $id);
278 if (isset($job['terms'])) {
279 foreach ($job['terms'] as $term) {
280 $terms_values[] = XDB
::format('({?}, {?}, {?}, {?})',
281 $page->pid(), $id, $term['jtid'], "original");
286 if (count($terms_values) > 0) {
287 XDB
::rawExecute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
288 VALUES ' . implode(', ', $terms_values) . '
289 ON DUPLICATE KEY UPDATE computed = VALUES(computed)');
293 public function getText($value)
296 foreach ($value as $id => $job) {
297 $address = Address
::formArrayToString(array($job['w_address']));
298 $phones = Phone
::formArrayToString($job['w_phone']);
299 // TODO: add jobterms here.
300 $jobs[] = 'Entreprise : ' . $job['name']
301 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
302 . ', email : ' . $job['w_email']
303 . ($phones ?
', ' . $phones : '') . ($address ?
', ' . $address : '');
305 return implode(' ; ' , $jobs);
309 class ProfileSettingCorps
implements ProfileSetting
311 public function value(ProfilePage
&$page, $field, $value, &$success)
314 if (is_null($value)) {
315 $res = XDB
::query('SELECT c.original_corpsid AS original, e.name AS originalText,
316 c.current_corpsid AS current, c.rankid AS rank, c.corps_pub AS pub
317 FROM profile_corps AS c
318 INNER JOIN profile_corps_enum AS e ON (c.original_corpsid = e.id)
321 return $res->fetchOneAssoc();
326 public function save(ProfilePage
&$page, $field, $value)
328 if (!S
::user()->isMe($page->owner
)) {
329 XDB
::execute('INSERT INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
330 VALUES ({?}, {?}, {?}, {?}, {?})
331 ON DUPLICATE KEY UPDATE original_corpsid = VALUES(original_corpsid), current_corpsid = VALUES(current_corpsid),
332 rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
333 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
335 XDB
::execute('INSERT INTO profile_corps (current_corpsid, rankid, corps_pub, pid)
336 VALUES ({?}, {?}, {?}, {?})
337 ON DUPLICATE KEY UPDATE current_corpsid = VALUES(current_corpsid),
338 rankid = VALUES(rankid), corps_pub = VALUES(corps_pub)',
339 $value['current'], $value['rank'], $value['pub'], $page->pid());
343 public function getText($value)
345 $corpsList = DirEnum
::getOptions(DirEnum
::CORPS
);
346 $rankList = DirEnum
::getOptions(DirEnum
::CORPSRANKS
);
347 return 'Corps actuel : ' . $corpsList[$value['current']] . ' , rang : ' . $corpsList[$value['rank']]
348 . ' , corps d\'origine : ' . $corpsList[$value['original']] . ' , affichage : ' . $value['pub'];
352 class ProfilePageJobs
extends ProfilePage
354 protected $pg_template = 'profile/jobs.tpl';
356 public function __construct(PlWizard
&$wiz)
358 parent
::__construct($wiz);
359 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
360 $this->settings
['cv'] = null
;
362 $this->settings
['corps'] = new ProfileSettingCorps();
363 $this->settings
['jobs'] = new ProfileSettingJob();
364 $this->watched
= array('cv' => true
, 'jobs' => true
, 'corps' => true
);
367 protected function _fetchData()
369 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
371 $res = XDB
::query("SELECT cv
375 $this->values
['cv'] = $res->fetchOneCell();
379 protected function _saveData()
381 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
382 if ($this->changed
['cv']) {
383 XDB
::execute("UPDATE profiles
386 $this->values
['cv'], $this->pid());
391 public function _prepare(PlPage
&$page, $id)
393 require_once 'emails.combobox.inc.php';
394 fill_email_combobox($page, $this->owner
);
396 if (!S
::user()->isMe($page->owner
)) {
397 $res = XDB
::iterator('SELECT id, name
398 FROM profile_corps_enum
399 ORDER BY id = 1 DESC, name');
400 $page->assign('original_corps', $res->fetchAllAssoc());
403 $res = XDB
::iterator("SELECT id, name
404 FROM profile_corps_enum
405 WHERE still_exists = 1
406 ORDER BY id = 1 DESC, name");
407 $page->assign('current_corps', $res->fetchAllAssoc());
409 $res = XDB
::iterator("SELECT id, name
410 FROM profile_corps_rank_enum
411 ORDER BY id = 1 DESC, name");
412 $page->assign('corps_rank', $res->fetchAllAssoc());
416 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: