Commit | Line | Data |
---|---|---|
3950bc21 FB |
1 | <?php |
2 | /*************************************************************************** | |
9f5bd98e | 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
3950bc21 FB |
4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
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. * | |
10 | * * | |
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. * | |
15 | * * | |
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 * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
eb54852e | 22 | class ProfileSettingJob implements ProfileSetting |
37d44b3b FB |
23 | { |
24 | private $pub; | |
541e8d03 SJ |
25 | private $email_new; |
26 | private $email; | |
27 | private $url; | |
f93fb300 | 28 | private $bool; |
37d44b3b FB |
29 | private $checks; |
30 | ||
31 | public function __construct() | |
32 | { | |
12bcf04b | 33 | $this->pub = new ProfileSettingPub(); |
541e8d03 SJ |
34 | $this->email |
35 | = $this->email_new | |
12bcf04b RB |
36 | = new ProfileSettingEmail(); |
37 | $this->url = new ProfileSettingWeb(); | |
38 | $this->bool = new ProfileSettingBool(); | |
541e8d03 SJ |
39 | $this->checks = array('url' => array('w_url'), |
40 | 'email' => array('w_email'), | |
41 | 'pub' => array('pub', 'w_email_pub'), | |
42 | ); | |
37d44b3b FB |
43 | } |
44 | ||
237f662f | 45 | private function emptyJob() |
afaa2cc7 | 46 | { |
eb54852e | 47 | $address = new Address(); |
972fd3c5 | 48 | $phone = new Phone(); |
afaa2cc7 SJ |
49 | return array( |
50 | 'id' => '0', | |
51 | 'jobid' => '', | |
52 | 'pub' => 'private', | |
53 | 'name' => '', | |
afaa2cc7 SJ |
54 | 'description' => '', |
55 | 'w_url' => '', | |
eb54852e | 56 | 'w_address' => $address->toFormArray(), |
afaa2cc7 SJ |
57 | 'w_email' => '', |
58 | 'w_email_pub' => 'private', | |
59 | 'w_email_new' => '', | |
972fd3c5 SJ |
60 | 'w_phone' => array(0 => $phone->toFormArray()), |
61 | 'terms' => array() | |
afaa2cc7 SJ |
62 | ); |
63 | } | |
64 | ||
237f662f FB |
65 | private function fetchJobs(ProfilePage $page) |
66 | { | |
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 | |
72 | FROM profile_job AS j | |
73 | LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id) | |
74 | WHERE j.pid = {?} | |
75 | ORDER BY j.id', | |
76 | $page->pid()); | |
77 | ||
78 | if (empty($jobs)) { | |
79 | return array($this->emptyJob()); | |
80 | } | |
81 | ||
82 | $compagnies = array(); | |
83 | $backtrack = array(); | |
84 | foreach ($jobs as $key=>$job) { | |
85 | $compagnies[] = $job['jobid']; | |
86 | $backtrack[$job['jobid']] = $key; | |
87 | } | |
88 | ||
89 | $it = Address::iterate(array($page->pid()), array(Address::LINK_JOB)); | |
90 | while ($address = $it->next()) { | |
91 | $jobs[$address->jobid]['w_address'] = $address->toFormArray(); | |
92 | } | |
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(); | |
96 | } | |
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) | |
100 | WHERE pid = {?} | |
101 | ORDER BY j.jid", | |
102 | $page->pid()); | |
103 | while ($term = $res->next()) { | |
104 | $jobid = $term['jobid']; | |
105 | if (!isset($backtrack[$jobid])) { | |
106 | continue; | |
107 | } | |
108 | $job =& $jobs[$backtrack[$jobid]]; | |
109 | if (!isset($job['terms'])) { | |
110 | $job['terms'] = array(); | |
111 | } | |
112 | $job['terms'][] = $term; | |
113 | } | |
114 | ||
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()); | |
120 | } | |
121 | if (!isset($job['w_address'])) { | |
122 | $job['w_address'] = $address->toFormArray(); | |
123 | } | |
124 | ||
125 | $job['w_email_new'] = ''; | |
126 | if (!isset($job['w_email_pub'])) { | |
127 | $job['w_email_pub'] = 'private'; | |
128 | } | |
129 | } | |
130 | return $jobs; | |
131 | } | |
132 | ||
b814a8b8 | 133 | private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success) |
37d44b3b | 134 | { |
541e8d03 SJ |
135 | if ($job['w_email'] == "new@example.org") { |
136 | $job['w_email'] = $job['w_email_new']; | |
137 | } | |
37d44b3b FB |
138 | foreach ($this->checks as $obj=>&$fields) { |
139 | $chk =& $this->$obj; | |
140 | foreach ($fields as $field) { | |
141 | $job[$field] = $chk->value($page, $field, $job[$field], $s); | |
142 | if (!$s) { | |
143 | $success = false; | |
144 | $job[$field . '_error'] = true; | |
145 | } | |
146 | } | |
147 | } | |
3ac45f10 PC |
148 | if (count($job['terms'])) { |
149 | $termsid = array(); | |
150 | foreach ($job['terms'] as $term) { | |
151 | if (!$term['full_name']) { | |
152 | $termsid[] = $term['jtid']; | |
153 | } | |
154 | } | |
155 | if (count($termsid)) { | |
156 | $res = XDB::query("SELECT jtid, full_name | |
157 | FROM profile_job_term_enum | |
158 | WHERE jtid IN {?}", | |
159 | $termsid); | |
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']]; | |
164 | } | |
165 | } | |
166 | } | |
167 | } | |
b814a8b8 | 168 | if ($job['name']) { |
c7139c07 SJ |
169 | $res = XDB::query("SELECT id |
170 | FROM profile_job_enum | |
171 | WHERE name = {?}", | |
172 | $job['name']); | |
173 | if ($res->numRows() != 1) { | |
024ec1e5 | 174 | $req = new EntrReq(S::user(), $page->profile, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'], |
4d7d27fc | 175 | $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']); |
b814a8b8 SJ |
176 | $req->submit(); |
177 | $job['jobid'] = null; | |
858a5b42 | 178 | sleep(1); |
c7139c07 SJ |
179 | } else { |
180 | $job['jobid'] = $res->fetchOneCell(); | |
181 | } | |
182 | } | |
0b6c8b36 | 183 | $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s); |
afaa2cc7 | 184 | |
37d44b3b FB |
185 | unset($job['removed']); |
186 | unset($job['new']); | |
37d44b3b FB |
187 | } |
188 | ||
237f662f FB |
189 | |
190 | ||
37d44b3b FB |
191 | public function value(ProfilePage &$page, $field, $value, &$success) |
192 | { | |
024ec1e5 | 193 | $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise'); |
b814a8b8 SJ |
194 | $entr_val = 0; |
195 | ||
37d44b3b FB |
196 | $init = false; |
197 | if (is_null($value)) { | |
237f662f | 198 | $value = $this->fetchJobs($page); |
37d44b3b FB |
199 | $init = true; |
200 | } | |
201 | $success = true; | |
237f662f | 202 | foreach ($value as $key => $job) { |
b814a8b8 | 203 | $job['name'] = trim($job['name']); |
a6483c12 | 204 | if ($job['name'] == '' && $entreprise) { |
b814a8b8 | 205 | $job['tmp_name'] = $entreprise[$entr_val]->name; |
a6483c12 SJ |
206 | ++$entr_val; |
207 | } else if ($job['name'] == '') { | |
ea9cfc3e | 208 | if ($job['description'] == '' && $job['w_url'] == '' |
ba6ee875 | 209 | && $job['w_address']['text'] == '' && $job['w_email'] == '' |
cbbf5ac9 | 210 | && count($job['w_phone']) >= 1 && $job['w_phone'][0]['display'] == '') { |
237f662f | 211 | unset($value[$key]); |
ba6ee875 SJ |
212 | continue; |
213 | } | |
214 | ||
d1d01361 SJ |
215 | if (!$init) { |
216 | $job['name_error'] = true; | |
217 | $success = false; | |
218 | } | |
b814a8b8 | 219 | } |
b94719c1 | 220 | |
541e8d03 | 221 | if (isset($job['removed']) && $job['removed']) { |
b94719c1 SJ |
222 | if ($job['name'] == '' && $entreprise) { |
223 | $entreprise[$entr_val - 1]->clean(); | |
224 | } | |
237f662f FB |
225 | unset($value[$key]); |
226 | continue; | |
37d44b3b | 227 | } |
237f662f | 228 | $value[$key] = $job; |
37d44b3b | 229 | } |
a6483c12 | 230 | foreach ($value as $key => &$job) { |
eb54852e SJ |
231 | $address = new Address($job['w_address']); |
232 | $s = $address->format(); | |
233 | $job['w_address'] = $address->toFormArray(); | |
bde2be3b | 234 | $this->cleanJob($page, $key, $job, $s); |
37d44b3b | 235 | if (!$init) { |
eb54852e | 236 | $success = ($success && $s); |
37d44b3b FB |
237 | } |
238 | } | |
239 | return $value; | |
240 | } | |
241 | ||
242 | public function save(ProfilePage &$page, $field, $value) | |
243 | { | |
c7139c07 | 244 | XDB::execute("DELETE FROM profile_job |
ce0b2c6f | 245 | WHERE pid = {?}", |
a6483c12 | 246 | $page->pid()); |
237f662f FB |
247 | XDB::execute("DELETE FROM profile_job_term |
248 | WHERE pid = {?}", | |
249 | $page->pid()); | |
eb54852e | 250 | Address::delete($page->pid(), Address::LINK_JOB); |
0b6c8b36 | 251 | Phone::deletePhones($page->pid(), Phone::LINK_JOB); |
3ac45f10 | 252 | $terms_values = array(); |
a6483c12 | 253 | foreach ($value as $id => &$job) { |
afaa2cc7 SJ |
254 | if (isset($job['name']) && $job['name']) { |
255 | if (isset($job['jobid']) && $job['jobid']) { | |
237f662f FB |
256 | XDB::execute('INSERT INTO profile_job (pid, id, description, email, |
257 | url, pub, email_pub, jobid) | |
ea9cfc3e | 258 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})', |
237f662f FB |
259 | $page->pid(), $id, $job['description'], $job['w_email'], |
260 | $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']); | |
afaa2cc7 | 261 | } else { |
237f662f FB |
262 | XDB::execute('INSERT INTO profile_job (pid, id, description, email, |
263 | url, pub, email_pub) | |
ea9cfc3e | 264 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})', |
237f662f FB |
265 | $page->pid(), $id, $job['description'], $job['w_email'], |
266 | $job['w_url'], $job['pub'], $job['w_email_pub']); | |
afaa2cc7 | 267 | } |
237f662f FB |
268 | $address = new Address(array_merge($job['w_address'], |
269 | array('pid' => $page->pid(), | |
270 | 'id' => $id, | |
271 | 'type' => Address::LINK_JOB))); | |
eb54852e | 272 | $address->save(); |
0b6c8b36 | 273 | Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id); |
3ac45f10 PC |
274 | if (isset($job['terms'])) { |
275 | foreach ($job['terms'] as $term) { | |
237f662f FB |
276 | $terms_values[] = XDB::format('({?}, {?}, {?}, {?})', |
277 | $page->pid(), $id, $term['jtid'], "original"); | |
3ac45f10 PC |
278 | } |
279 | } | |
b814a8b8 | 280 | } |
37d44b3b | 281 | } |
3ac45f10 | 282 | if (count($terms_values) > 0) { |
237f662f FB |
283 | XDB::execute('REPLACE INTO profile_job_term (pid, jid, jtid, computed) |
284 | VALUES '.implode(', ', $terms_values)); | |
3ac45f10 | 285 | } |
37d44b3b | 286 | } |
a0fce0c6 SJ |
287 | |
288 | public function getText($value) { | |
289 | $jobs = array(); | |
290 | foreach ($value as $id => $job) { | |
eb54852e | 291 | $address = Address::formArrayToString($job['w_address']); |
0b6c8b36 | 292 | $phones = Phone::formArrayToString($job['w_phone']); |
ea9cfc3e SJ |
293 | // TODO: add jobterms here. |
294 | $jobs[] = 'Entreprise : ' . $job['name'] | |
a0fce0c6 SJ |
295 | . ', description : ' . $job['description'] . ', web : ' . $job['w_url'] |
296 | . ', email : ' . $job['w_email'] | |
eb54852e | 297 | . ($phones ? ', ' . $phones : '') . ($address ? ', ' . $address : ''); |
a0fce0c6 SJ |
298 | } |
299 | return implode(' ; ' , $jobs); | |
300 | } | |
37d44b3b FB |
301 | } |
302 | ||
603aeb6c SJ |
303 | class ProfileSettingCorps implements ProfileSetting |
304 | { | |
305 | public function value(ProfilePage &$page, $field, $value, &$success) | |
306 | { | |
307 | $success = true; | |
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 | |
311 | FROM profile_corps | |
312 | WHERE pid = {?}", | |
313 | $page->pid()); | |
314 | return $res->fetchOneAssoc(); | |
315 | } | |
316 | return $value; | |
317 | } | |
318 | ||
319 | public function save(ProfilePage &$page, $field, $value) | |
320 | { | |
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()); | |
324 | } | |
325 | ||
326 | public function getText($value) | |
327 | { | |
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']; | |
332 | } | |
333 | } | |
334 | ||
66c4bdaf | 335 | class ProfilePageJobs extends ProfilePage |
3950bc21 FB |
336 | { |
337 | protected $pg_template = 'profile/jobs.tpl'; | |
338 | ||
339 | public function __construct(PlWizard &$wiz) | |
340 | { | |
341 | parent::__construct($wiz); | |
b539d596 FB |
342 | if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) { |
343 | $this->settings['cv'] = null; | |
344 | } | |
603aeb6c | 345 | $this->settings['corps'] = new ProfileSettingCorps(); |
12bcf04b | 346 | $this->settings['jobs'] = new ProfileSettingJob(); |
72e96bc0 | 347 | $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true); |
37d44b3b FB |
348 | } |
349 | ||
7c2e0f0d | 350 | protected function _fetchData() |
37d44b3b | 351 | { |
b539d596 FB |
352 | if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) { |
353 | // Checkout the CV | |
354 | $res = XDB::query("SELECT cv | |
355 | FROM profiles | |
356 | WHERE pid = {?}", | |
357 | $this->pid()); | |
358 | $this->values['cv'] = $res->fetchOneCell(); | |
359 | } | |
37d44b3b FB |
360 | } |
361 | ||
7c2e0f0d | 362 | protected function _saveData() |
37d44b3b | 363 | { |
b539d596 FB |
364 | if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) { |
365 | if ($this->changed['cv']) { | |
366 | XDB::execute("UPDATE profiles | |
367 | SET cv = {?} | |
368 | WHERE pid = {?}", | |
369 | $this->values['cv'], $this->pid()); | |
370 | } | |
37d44b3b | 371 | } |
3950bc21 | 372 | } |
2dcac0f5 | 373 | |
04334c61 | 374 | public function _prepare(PlPage &$page, $id) |
2dcac0f5 | 375 | { |
a6483c12 | 376 | require_once 'emails.combobox.inc.php'; |
4e698dc9 | 377 | fill_email_combobox($page, $this->owner); |
b715c1e1 | 378 | |
72e96bc0 SJ |
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()); | |
383 | ||
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()); | |
389 | ||
390 | $res = XDB::iterator("SELECT id, name | |
e489faf7 SJ |
391 | FROM profile_corps_rank_enum |
392 | ORDER BY id = 1 DESC, name"); | |
72e96bc0 | 393 | $page->assign('corps_rank', $res->fetchAllAssoc()); |
2dcac0f5 | 394 | } |
3950bc21 FB |
395 | } |
396 | ||
397 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
398 | ?> |