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['jobid']] = $key;
89 $it = Address
::iterate(array($page->pid()), array(Address
::LINK_JOB
));
90 while ($address = $it->next()) {
91 $jobs[$address->jobid
]['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 AS jobid
98 FROM profile_job_term_enum AS e
99 INNER JOIN profile_job_term AS j USING(jtid)
103 while ($term = $res->next()) {
104 $jobid = $term['jobid'];
105 if (!isset($backtrack[$jobid])) {
108 $job =& $jobs[$backtrack[$jobid]];
109 if (!isset($job['terms'])) {
110 $job['terms'] = array();
112 $job['terms'][] = $term;
115 $phone = new Phone();
116 $address = new Address();
117 foreach ($jobs as $id => &$job) {
118 if (!isset($job['w_phone'])) {
119 $job['w_phone'] = array(0 => $phone->toFormArray());
121 if (!isset($job['w_address'])) {
122 $job['w_address'] = $address->toFormArray();
125 $job['w_email_new'] = '';
126 if (!isset($job['w_email_pub'])) {
127 $job['w_email_pub'] = 'private';
133 private function cleanJob(ProfilePage
&$page, $jobid, array &$job, &$success)
135 if ($job['w_email'] == "new@example.org") {
136 $job['w_email'] = $job['w_email_new'];
138 foreach ($this->checks
as $obj=>&$fields) {
140 foreach ($fields as $field) {
141 $job[$field] = $chk->value($page, $field, $job[$field], $s);
144 $job[$field . '_error'] = true
;
148 if (count($job['terms'])) {
150 foreach ($job['terms'] as $term) {
151 if (!$term['full_name']) {
152 $termsid[] = $term['jtid'];
155 if (count($termsid)) {
156 $res = XDB
::query("SELECT jtid, full_name
157 FROM profile_job_term_enum
160 $term_id_to_name = $res->fetchAllAssoc('jtid', false
);
161 foreach ($job['terms'] as &$term) {
162 if (!$term['full_name']) {
163 $term['full_name'] = $term_id_to_name[$term['jtid']];
169 $res = XDB
::query("SELECT id
170 FROM profile_job_enum
173 if ($res->numRows() != 1) {
174 $req = new EntrReq(S
::user(), $page->profile
, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
175 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
177 $job['jobid'] = null
;
180 $job['jobid'] = $res->fetchOneCell();
183 $job['w_phone'] = Phone
::formatFormArray($job['w_phone'], $s);
185 unset($job['removed']);
191 public function value(ProfilePage
&$page, $field, $value, &$success)
193 $entreprise = ProfileValidate
::get_typed_requests($page->pid(), 'entreprise');
197 if (is_null($value)) {
198 $value = $this->fetchJobs($page);
202 foreach ($value as $key => $job) {
203 $job['name'] = trim($job['name']);
204 if ($job['name'] == '' && $entreprise) {
205 $job['tmp_name'] = $entreprise[$entr_val]->name
;
207 } else if ($job['name'] == '') {
208 if ($job['description'] == '' && $job['w_url'] == ''
209 && $job['w_address']['text'] == '' && $job['w_email'] == ''
210 && count($job['w_phone']) >= 1 && $job['w_phone'][0]['display'] == '') {
216 $job['name_error'] = true
;
221 if (isset($job['removed']) && $job['removed']) {
222 if ($job['name'] == '' && $entreprise) {
223 $entreprise[$entr_val - 1]->clean();
230 foreach ($value as $key => &$job) {
231 $address = new Address($job['w_address']);
232 $s = $address->format();
233 $job['w_address'] = $address->toFormArray();
234 $this->cleanJob($page, $key, $job, $s);
236 $success = ($success && $s);
242 public function save(ProfilePage
&$page, $field, $value)
244 XDB
::execute("DELETE FROM profile_job
247 XDB
::execute("DELETE FROM profile_job_term
250 Address
::delete($page->pid(), Address
::LINK_JOB
);
251 Phone
::deletePhones($page->pid(), Phone
::LINK_JOB
);
252 $terms_values = array();
253 foreach ($value as $id => &$job) {
254 if (isset($job['name']) && $job['name']) {
255 if (isset($job['jobid']) && $job['jobid']) {
256 XDB
::execute('INSERT INTO profile_job (pid, id, description, email,
257 url, pub, email_pub, jobid)
258 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
259 $page->pid(), $id, $job['description'], $job['w_email'],
260 $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
262 XDB
::execute('INSERT INTO profile_job (pid, id, description, email,
264 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
265 $page->pid(), $id, $job['description'], $job['w_email'],
266 $job['w_url'], $job['pub'], $job['w_email_pub']);
268 $address = new Address(array_merge($job['w_address'],
269 array('pid' => $page->pid(),
271 'type' => Address
::LINK_JOB
)));
273 Phone
::savePhones($job['w_phone'], $page->pid(), Phone
::LINK_JOB
, $id);
274 if (isset($job['terms'])) {
275 foreach ($job['terms'] as $term) {
276 $terms_values[] = XDB
::format('({?}, {?}, {?}, {?})',
277 $page->pid(), $id, $term['jtid'], "original");
282 if (count($terms_values) > 0) {
283 XDB
::execute('REPLACE INTO profile_job_term (pid, jid, jtid, computed)
284 VALUES '.implode(', ', $terms_values));
288 public function getText($value) {
290 foreach ($value as $id => $job) {
291 $address = Address
::formArrayToString($job['w_address']);
292 $phones = Phone
::formArrayToString($job['w_phone']);
293 // TODO: add jobterms here.
294 $jobs[] = 'Entreprise : ' . $job['name']
295 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
296 . ', email : ' . $job['w_email']
297 . ($phones ?
', ' . $phones : '') . ($address ?
', ' . $address : '');
299 return implode(' ; ' , $jobs);
303 class ProfileSettingCorps
implements ProfileSetting
305 public function value(ProfilePage
&$page, $field, $value, &$success)
308 if (is_null($value)) {
309 $res = XDB
::query("SELECT original_corpsid AS original, current_corpsid AS current,
310 rankid AS rank, corps_pub AS pub
314 return $res->fetchOneAssoc();
319 public function save(ProfilePage
&$page, $field, $value)
321 XDB
::execute('REPLACE INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
322 VALUES ({?}, {?}, {?}, {?}, {?})',
323 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
326 public function getText($value)
328 $corpsList = DirEnum
::getOptions(DirEnum
::CORPS
);
329 $rankList = DirEnum
::getOptions(DirEnum
::CORPSRANKS
);
330 return 'Corps actuel : ' . $corpsList[$value['current']] . ' , rang : ' . $corpsList[$value['rank']]
331 . ' , corps d\'origine : ' . $corpsList[$value['original']] . ' , affichage : ' . $value['pub'];
335 class ProfilePageJobs
extends ProfilePage
337 protected $pg_template = 'profile/jobs.tpl';
339 public function __construct(PlWizard
&$wiz)
341 parent
::__construct($wiz);
342 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
343 $this->settings
['cv'] = null
;
345 $this->settings
['corps'] = new ProfileSettingCorps();
346 $this->settings
['jobs'] = new ProfileSettingJob();
347 $this->watched
= array('cv' => true
, 'jobs' => true
, 'corps' => true
);
350 protected function _fetchData()
352 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
354 $res = XDB
::query("SELECT cv
358 $this->values
['cv'] = $res->fetchOneCell();
362 protected function _saveData()
364 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
365 if ($this->changed
['cv']) {
366 XDB
::execute("UPDATE profiles
369 $this->values
['cv'], $this->pid());
374 public function _prepare(PlPage
&$page, $id)
376 require_once 'emails.combobox.inc.php';
377 fill_email_combobox($page, $this->owner
);
379 $res = XDB
::iterator("SELECT id, name
380 FROM profile_corps_enum
381 ORDER BY id = 1 DESC, name");
382 $page->assign('original_corps', $res->fetchAllAssoc());
384 $res = XDB
::iterator("SELECT id, name
385 FROM profile_corps_enum
386 WHERE still_exists = 1
387 ORDER BY id = 1 DESC, name");
388 $page->assign('current_corps', $res->fetchAllAssoc());
390 $res = XDB
::iterator("SELECT id, name
391 FROM profile_corps_rank_enum
392 ORDER BY id = 1 DESC, name");
393 $page->assign('corps_rank', $res->fetchAllAssoc());
397 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: