453d21fa84e3630b9eac03e17763bd048a690186
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 'w_address' => $address->toFormArray(),
58 'w_email_pub' => 'private',
60 'w_phone' => array(0 => $phone->toFormArray()),
65 private function cleanJob(ProfilePage
&$page, $jobid, array &$job, &$success)
67 if ($job['w_email'] == "new@example.org") {
68 $job['w_email'] = $job['w_email_new'];
70 foreach ($this->checks
as $obj=>&$fields) {
72 foreach ($fields as $field) {
73 $job[$field] = $chk->value($page, $field, $job[$field], $s);
76 $job[$field . '_error'] = true
;
80 if (count($job['terms'])) {
82 foreach ($job['terms'] as $term) {
83 if (!$term['full_name']) {
84 $termsid[] = $term['jtid'];
87 if (count($termsid)) {
88 $res = XDB
::query("SELECT jtid, full_name
89 FROM profile_job_term_enum
92 $term_id_to_name = $res->fetchAllAssoc('jtid', false
);
93 foreach ($job['terms'] as &$term) {
94 if (!$term['full_name']) {
95 $term['full_name'] = $term_id_to_name[$term['jtid']];
101 $res = XDB
::query("SELECT id
102 FROM profile_job_enum
105 if ($res->numRows() != 1) {
106 $req = new EntrReq(S
::user(), $page->profile
, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
107 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
109 $job['jobid'] = null
;
112 $job['jobid'] = $res->fetchOneCell();
115 $job['w_phone'] = Phone
::formatFormArray($job['w_phone'], $s);
117 unset($job['removed']);
121 public function value(ProfilePage
&$page, $field, $value, &$success)
123 require_once 'validations.inc.php';
124 $entreprise = ProfileValidate
::get_typed_requests($page->pid(), 'entreprise');
128 if (is_null($value)) {
129 $value = $page->values
['jobs'];
133 foreach ($value as $key => &$job) {
134 $job['name'] = trim($job['name']);
135 if ($job['name'] == '' && $entreprise) {
136 $job['tmp_name'] = $entreprise[$entr_val]->name
;
138 } else if ($job['name'] == '') {
139 if ($job['description'] == '' && $job['w_url'] == ''
140 && $job['w_address']['text'] == '' && $job['w_email'] == ''
141 && count($job['w_phone']) == 1 && $job['w_phone']['tel'] == '') {
142 array_splice($value, $key, 1);
147 $job['name_error'] = true
;
152 if (isset($job['removed']) && $job['removed']) {
153 if ($job['name'] == '' && $entreprise) {
154 $entreprise[$entr_val - 1]->clean();
156 array_splice($value, $key, 1);
159 foreach ($value as $key => &$job) {
160 $address = new Address($job['w_address']);
161 $s = $address->format();
162 $job['w_address'] = $address->toFormArray();
163 $this->cleanJob($page, $key, $job, $s);
165 $success = ($success && $s);
171 public function save(ProfilePage
&$page, $field, $value)
173 XDB
::execute("DELETE FROM profile_job
176 Address
::delete($page->pid(), Address
::LINK_JOB
);
177 Phone
::deletePhones($page->pid(), Phone
::LINK_JOB
);
178 $terms_values = array();
179 foreach ($value as $id => &$job) {
180 if (isset($job['name']) && $job['name']) {
181 if (isset($job['jobid']) && $job['jobid']) {
182 XDB
::execute('INSERT INTO profile_job (pid, id, description, email, url, pub, email_pub, jobid)
183 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
184 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
186 XDB
::execute('INSERT INTO profile_job (pid, id, description, email, url, pub, email_pub)
187 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
188 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
190 $address = new Address(array_merge($job['w_address'], array('pid' => $page->pid(), 'id' => $id, 'type' => Address
::LINK_JOB
)));
192 Phone
::savePhones($job['w_phone'], $page->pid(), Phone
::LINK_JOB
, $id);
193 if (isset($job['terms'])) {
194 foreach ($job['terms'] as $term) {
195 $terms_values[] = '('.XDB
::escape($page->pid()).', '. XDB
::escape($id).', '.XDB
::escape($term['jtid']).', "original")';
200 if (count($terms_values) > 0) {
201 XDB
::execute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
202 VALUES '.implode(', ', $terms_values));
206 public function getText($value) {
208 foreach ($value as $id => $job) {
209 $address = Address
::formArrayToString($job['w_address']);
210 $phones = Phone
::formArrayToString($job['w_phone']);
211 // TODO: add jobterms here.
212 $jobs[] = 'Entreprise : ' . $job['name']
213 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
214 . ', email : ' . $job['w_email']
215 . ($phones ?
', ' . $phones : '') . ($address ?
', ' . $address : '');
217 return implode(' ; ' , $jobs);
221 class ProfileSettingCorps
implements ProfileSetting
223 public function value(ProfilePage
&$page, $field, $value, &$success)
226 if (is_null($value)) {
227 $res = XDB
::query("SELECT original_corpsid AS original, current_corpsid AS current,
228 rankid AS rank, corps_pub AS pub
232 return $res->fetchOneAssoc();
237 public function save(ProfilePage
&$page, $field, $value)
239 XDB
::execute('REPLACE INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
240 VALUES ({?}, {?}, {?}, {?}, {?})',
241 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
244 public function getText($value)
246 $corpsList = DirEnum
::getOptions(DirEnum
::CORPS
);
247 $rankList = DirEnum
::getOptions(DirEnum
::CORPSRANKS
);
248 return 'Corps actuel : ' . $corpsList[$value['current']] . ' , rang : ' . $corpsList[$value['rank']]
249 . ' , corps d\'origine : ' . $corpsList[$value['original']] . ' , affichage : ' . $value['pub'];
253 class ProfileSettingJobs
extends ProfilePage
255 protected $pg_template = 'profile/jobs.tpl';
257 public function __construct(PlWizard
&$wiz)
259 parent
::__construct($wiz);
260 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
261 $this->settings
['cv'] = null
;
263 $this->settings
['corps'] = new ProfileSettingCorps();
264 $this->settings
['jobs'] = new ProfileSettingJob();
265 $this->watched
= array('cv' => true
, 'jobs' => true
, 'corps' => true
);
268 protected function _fetchData()
270 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
272 $res = XDB
::query("SELECT cv
276 $this->values
['cv'] = $res->fetchOneCell();
279 // Build the jobs tree
280 $res = XDB
::iterRow('SELECT j.id, j.jobid, je.name, j.description, j.email, j.email_pub,
282 FROM profile_job AS j
283 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
287 $this->values
['jobs'] = array();
289 $compagnies = array();
290 if ($res->numRows() > 0) {
291 while (list($id, $jobid, $name, $description, $w_email, $w_emailPub, $w_url, $pub) = $res->next()) {
292 $compagnies[] = $jobid;
293 $this->values
['jobs'][] = array(
297 'description' => $description,
299 'w_email' => $w_email,
300 'w_email_pub' => $w_emailPub,
305 $it = Address
::iterate(array($this->pid()), array(Address
::LINK_JOB
));
306 while ($address = $it->next()) {
307 $this->values
['jobs'][$address->jobid
]['w_address'] = $address->toFormArray();
309 $it = Phone
::iterate(array($this->pid()), array(Phone
::LINK_JOB
));
310 while ($phone = $it->next()) {
311 $this->values
['jobs'][$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray();
313 $res = XDB
::iterator("SELECT e.jtid, e.full_name, j.jid AS jobid
314 FROM profile_job_term_enum AS e
315 INNER JOIN profile_job_term AS j USING(jtid)
320 $jobNb = count($this->values
['jobs']);
321 while ($term = $res->next()) {
322 $jobid = $term['jobid'];
323 while ($i < $jobNb && $this->values
['jobs'][$i]['id'] < $jobid) {
329 $job =& $this->values
['jobs'][$i];
330 if ($job['id'] != $jobid) {
333 if (!isset($job['terms'])) {
334 $job['terms'] = array();
336 $job['terms'][] = $term;
339 $phone = new Phone();
340 $address = new Address();
341 foreach ($this->values
['jobs'] as $id => &$job) {
342 if (!isset($job['w_phone'])) {
343 $job['w_phone'] = array(0 => $phone->toFormArray());
345 if (!isset($job['w_address'])) {
346 $job['w_address'] = $address->toFormArray();
350 $job['w_email_new'] = '';
351 if (!isset($job['w_email_pub'])) {
352 $job['w_email_pub'] = 'private';
355 $this->values
['jobs'][] = $this->settings
['jobs']->emptyJob();
359 protected function _saveData()
361 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
362 if ($this->changed
['cv']) {
363 XDB
::execute("UPDATE profiles
366 $this->values
['cv'], $this->pid());
371 public function _prepare(PlPage
&$page, $id)
373 require_once 'emails.combobox.inc.php';
374 fill_email_combobox($page, $this->owner
);
376 $res = XDB
::iterator("SELECT id, name
377 FROM profile_corps_enum
378 ORDER BY id = 1 DESC, name");
379 $page->assign('original_corps', $res->fetchAllAssoc());
381 $res = XDB
::iterator("SELECT id, name
382 FROM profile_corps_enum
383 WHERE still_exists = 1
384 ORDER BY id = 1 DESC, name");
385 $page->assign('current_corps', $res->fetchAllAssoc());
387 $res = XDB
::iterator("SELECT id, name
388 FROM profile_corps_rank_enum
389 ORDER BY id = 1 DESC, name");
390 $page->assign('corps_rank', $res->fetchAllAssoc());
394 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: