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();
55 'w_address' => $address->toFormArray(),
57 'w_email_pub' => 'private',
59 'w_phone' => array(0 => array(
69 private function cleanJob(ProfilePage
&$page, $jobid, array &$job, &$success)
71 if ($job['w_email'] == "new@example.org") {
72 $job['w_email'] = $job['w_email_new'];
74 foreach ($this->checks
as $obj=>&$fields) {
76 foreach ($fields as $field) {
77 $job[$field] = $chk->value($page, $field, $job[$field], $s);
80 $job[$field . '_error'] = true
;
84 if (count($job['terms'])) {
86 foreach ($job['terms'] as $term) {
87 if (!$term['full_name']) {
88 $termsid[] = $term['jtid'];
91 if (count($termsid)) {
92 $res = XDB
::query("SELECT jtid, full_name
93 FROM profile_job_term_enum
96 $term_id_to_name = $res->fetchAllAssoc('jtid', false
);
97 foreach ($job['terms'] as &$term) {
98 if (!$term['full_name']) {
99 $term['full_name'] = $term_id_to_name[$term['jtid']];
105 $res = XDB
::query("SELECT id
106 FROM profile_job_enum
109 if ($res->numRows() != 1) {
110 $req = new EntrReq(S
::user(), $page->profile
, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
111 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
113 $job['jobid'] = null
;
116 $job['jobid'] = $res->fetchOneCell();
119 $job['w_phone'] = Phone
::formatFormArray($job['w_phone'], $s);
121 unset($job['removed']);
125 public function value(ProfilePage
&$page, $field, $value, &$success)
127 require_once 'validations.inc.php';
128 $entreprise = ProfileValidate
::get_typed_requests($page->pid(), 'entreprise');
132 if (is_null($value)) {
133 $value = $page->values
['jobs'];
137 foreach ($value as $key => &$job) {
138 $job['name'] = trim($job['name']);
139 if ($job['name'] == '' && $entreprise) {
140 $job['tmp_name'] = $entreprise[$entr_val]->name
;
142 } else if ($job['name'] == '') {
143 if ($job['description'] == '' && $job['w_url'] == ''
144 && $job['w_address']['text'] == '' && $job['w_email'] == ''
145 && count($job['w_phone']) == 1 && $job['w_phone']['tel'] == '') {
146 array_splice($value, $key, 1);
151 $job['name_error'] = true
;
156 if (isset($job['removed']) && $job['removed']) {
157 if ($job['name'] == '' && $entreprise) {
158 $entreprise[$entr_val - 1]->clean();
160 array_splice($value, $key, 1);
163 foreach ($value as $key => &$job) {
164 $address = new Address($job['w_address']);
165 $s = $address->format();
166 $job['w_address'] = $address->toFormArray();
167 $this->cleanJob($page, $key, $job, $s);
169 $success = ($success && $s);
175 public function save(ProfilePage
&$page, $field, $value)
177 XDB
::execute("DELETE FROM profile_job
180 Address
::delete($page->pid(), Address
::LINK_JOB
);
181 Phone
::deletePhones($page->pid(), Phone
::LINK_JOB
);
182 $terms_values = array();
183 foreach ($value as $id => &$job) {
184 if (isset($job['name']) && $job['name']) {
185 if (isset($job['jobid']) && $job['jobid']) {
186 XDB
::execute('INSERT INTO profile_job (pid, id, description, email, url, pub, email_pub, jobid)
187 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
188 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
190 XDB
::execute('INSERT INTO profile_job (pid, id, description, email, url, pub, email_pub)
191 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
192 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
194 $address = new Address(array_merge($job['w_address'], array('pid' => $page->pid(), 'id' => $id, 'type' => Address
::LINK_JOB
)));
196 Phone
::savePhones($job['w_phone'], $page->pid(), Phone
::LINK_JOB
, $id);
197 if (isset($job['terms'])) {
198 foreach ($job['terms'] as $term) {
199 $terms_values[] = '('.XDB
::escape($page->pid()).', '. XDB
::escape($id).', '.XDB
::escape($term['jtid']).', "original")';
204 if (count($terms_values) > 0) {
205 XDB
::execute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
206 VALUES '.implode(', ', $terms_values));
210 public function getText($value) {
212 foreach ($value as $id => $job) {
213 $address = Address
::formArrayToString($job['w_address']);
214 $phones = Phone
::formArrayToString($job['w_phone']);
215 // TODO: add jobterms here.
216 $jobs[] = 'Entreprise : ' . $job['name']
217 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
218 . ', email : ' . $job['w_email']
219 . ($phones ?
', ' . $phones : '') . ($address ?
', ' . $address : '');
221 return implode(' ; ' , $jobs);
225 class ProfileSettingCorps
implements ProfileSetting
227 public function value(ProfilePage
&$page, $field, $value, &$success)
230 if (is_null($value)) {
231 $res = XDB
::query("SELECT original_corpsid AS original, current_corpsid AS current,
232 rankid AS rank, corps_pub AS pub
236 return $res->fetchOneAssoc();
241 public function save(ProfilePage
&$page, $field, $value)
243 XDB
::execute('REPLACE INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
244 VALUES ({?}, {?}, {?}, {?}, {?})',
245 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
248 public function getText($value)
250 $corpsList = DirEnum
::getOptions(DirEnum
::CORPS
);
251 $rankList = DirEnum
::getOptions(DirEnum
::CORPSRANKS
);
252 return 'Corps actuel : ' . $corpsList[$value['current']] . ' , rang : ' . $corpsList[$value['rank']]
253 . ' , corps d\'origine : ' . $corpsList[$value['original']] . ' , affichage : ' . $value['pub'];
257 class ProfileSettingJobs
extends ProfilePage
259 protected $pg_template = 'profile/jobs.tpl';
261 public function __construct(PlWizard
&$wiz)
263 parent
::__construct($wiz);
264 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
265 $this->settings
['cv'] = null
;
267 $this->settings
['corps'] = new ProfileSettingCorps();
268 $this->settings
['jobs'] = new ProfileSettingJob();
269 $this->watched
= array('cv' => true
, 'jobs' => true
, 'corps' => true
);
272 protected function _fetchData()
274 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
276 $res = XDB
::query("SELECT cv
280 $this->values
['cv'] = $res->fetchOneCell();
283 // Build the jobs tree
284 $res = XDB
::iterRow('SELECT j.id, j.jobid, je.name, j.description, j.email, j.email_pub,
286 FROM profile_job AS j
287 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
291 $this->values
['jobs'] = array();
293 $compagnies = array();
294 if ($res->numRows() > 0) {
295 while (list($id, $jobid, $name, $description, $w_email, $w_emailPub, $w_url, $pub) = $res->next()) {
296 $compagnies[] = $jobid;
297 $this->values
['jobs'][] = array(
301 'description' => $description,
303 'w_email' => $w_email,
304 'w_email_pub' => $w_emailPub,
309 $it = Address
::iterate(array($this->pid()), array(Address
::LINK_JOB
));
310 while ($address = $it->next()) {
311 $this->values
['jobs'][$address->jobid
]['w_address'] = $address->toFormArray();
313 $it = Phone
::iterate(array($this->pid()), array(Phone
::LINK_JOB
));
314 while ($phone = $it->next()) {
315 $this->values
['jobs'][$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray();
317 $res = XDB
::iterator("SELECT e.jtid, e.full_name, j.jid AS jobid
318 FROM profile_job_term_enum AS e
319 INNER JOIN profile_job_term AS j USING(jtid)
324 $jobNb = count($this->values
['jobs']);
325 while ($term = $res->next()) {
326 $jobid = $term['jobid'];
327 while ($i < $jobNb && $this->values
['jobs'][$i]['id'] < $jobid) {
333 $job =& $this->values
['jobs'][$i];
334 if ($job['id'] != $jobid) {
337 if (!isset($job['terms'])) {
338 $job['terms'] = array();
340 $job['terms'][] = $term;
343 $phone = new Phone();
344 $address = new Address();
345 foreach ($this->values
['jobs'] as $id => &$job) {
346 if (!isset($job['w_phone'])) {
347 $job['w_phone'] = array(0 => $phone->toFormArray());
349 if (!isset($job['w_address'])) {
350 $job['w_address'] = $address->toFormArray();
354 $job['w_email_new'] = '';
355 if (!isset($job['w_email_pub'])) {
356 $job['w_email_pub'] = 'private';
359 $this->values
['jobs'][] = $this->settings
['jobs']->emptyJob();
363 protected function _saveData()
365 if (S
::user()->checkPerms(User
::PERM_DIRECTORY_PRIVATE
)) {
366 if ($this->changed
['cv']) {
367 XDB
::execute("UPDATE profiles
370 $this->values
['cv'], $this->pid());
375 public function _prepare(PlPage
&$page, $id)
377 require_once 'emails.combobox.inc.php';
378 fill_email_combobox($page, $this->owner
);
380 $res = XDB
::iterator("SELECT id, name
381 FROM profile_corps_enum
382 ORDER BY id = 1 DESC, name");
383 $page->assign('original_corps', $res->fetchAllAssoc());
385 $res = XDB
::iterator("SELECT id, name
386 FROM profile_corps_enum
387 WHERE still_exists = 1
388 ORDER BY id = 1 DESC, name");
389 $page->assign('current_corps', $res->fetchAllAssoc());
391 $res = XDB
::iterator("SELECT id, name
392 FROM profile_corps_rank_enum
393 ORDER BY id = 1 DESC, name");
394 $page->assign('corps_rank', $res->fetchAllAssoc());
398 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: