Only uses autoload to require validation classes.
[platal.git] / modules / profile / jobs.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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
22 class ProfileSettingJob implements ProfileSetting
23 {
24 private $pub;
25 private $email_new;
26 private $email;
27 private $url;
28 private $bool;
29 private $checks;
30
31 public function __construct()
32 {
33 $this->pub = new ProfileSettingPub();
34 $this->email
35 = $this->email_new
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'),
42 );
43 }
44
45 public function emptyJob()
46 {
47 $address = new Address();
48 $phone = new Phone();
49 return array(
50 'id' => '0',
51 'jobid' => '',
52 'pub' => 'private',
53 'name' => '',
54 'description' => '',
55 'w_url' => '',
56 'w_address' => $address->toFormArray(),
57 'w_email' => '',
58 'w_email_pub' => 'private',
59 'w_email_new' => '',
60 'w_phone' => array(0 => $phone->toFormArray()),
61 'terms' => array()
62 );
63 }
64
65 private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success)
66 {
67 if ($job['w_email'] == "new@example.org") {
68 $job['w_email'] = $job['w_email_new'];
69 }
70 foreach ($this->checks as $obj=>&$fields) {
71 $chk =& $this->$obj;
72 foreach ($fields as $field) {
73 $job[$field] = $chk->value($page, $field, $job[$field], $s);
74 if (!$s) {
75 $success = false;
76 $job[$field . '_error'] = true;
77 }
78 }
79 }
80 if (count($job['terms'])) {
81 $termsid = array();
82 foreach ($job['terms'] as $term) {
83 if (!$term['full_name']) {
84 $termsid[] = $term['jtid'];
85 }
86 }
87 if (count($termsid)) {
88 $res = XDB::query("SELECT jtid, full_name
89 FROM profile_job_term_enum
90 WHERE jtid IN {?}",
91 $termsid);
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']];
96 }
97 }
98 }
99 }
100 if ($job['name']) {
101 $res = XDB::query("SELECT id
102 FROM profile_job_enum
103 WHERE name = {?}",
104 $job['name']);
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']);
108 $req->submit();
109 $job['jobid'] = null;
110 sleep(1);
111 } else {
112 $job['jobid'] = $res->fetchOneCell();
113 }
114 }
115 $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s);
116
117 unset($job['removed']);
118 unset($job['new']);
119 }
120
121 public function value(ProfilePage &$page, $field, $value, &$success)
122 {
123 $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise');
124 $entr_val = 0;
125
126 $init = false;
127 if (is_null($value)) {
128 $value = $page->values['jobs'];
129 $init = true;
130 }
131 $success = true;
132 foreach ($value as $key => &$job) {
133 $job['name'] = trim($job['name']);
134 if ($job['name'] == '' && $entreprise) {
135 $job['tmp_name'] = $entreprise[$entr_val]->name;
136 ++$entr_val;
137 } else if ($job['name'] == '') {
138 if ($job['description'] == '' && $job['w_url'] == ''
139 && $job['w_address']['text'] == '' && $job['w_email'] == ''
140 && count($job['w_phone']) >= 1 && $job['w_phone'][0]['display'] == '') {
141 array_splice($value, $key, 1);
142 continue;
143 }
144
145 if (!$init) {
146 $job['name_error'] = true;
147 $success = false;
148 }
149 }
150
151 if (isset($job['removed']) && $job['removed']) {
152 if ($job['name'] == '' && $entreprise) {
153 $entreprise[$entr_val - 1]->clean();
154 }
155 array_splice($value, $key, 1);
156 }
157 }
158 foreach ($value as $key => &$job) {
159 $address = new Address($job['w_address']);
160 $s = $address->format();
161 $job['w_address'] = $address->toFormArray();
162 $this->cleanJob($page, $key, $job, $s);
163 if (!$init) {
164 $success = ($success && $s);
165 }
166 }
167 return $value;
168 }
169
170 public function save(ProfilePage &$page, $field, $value)
171 {
172 XDB::execute("DELETE FROM profile_job
173 WHERE pid = {?}",
174 $page->pid());
175 Address::delete($page->pid(), Address::LINK_JOB);
176 Phone::deletePhones($page->pid(), Phone::LINK_JOB);
177 $terms_values = array();
178 foreach ($value as $id => &$job) {
179 if (isset($job['name']) && $job['name']) {
180 if (isset($job['jobid']) && $job['jobid']) {
181 XDB::execute('INSERT INTO profile_job (pid, id, description, email, url, pub, email_pub, jobid)
182 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
183 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
184 } else {
185 XDB::execute('INSERT INTO profile_job (pid, id, description, email, url, pub, email_pub)
186 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
187 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
188 }
189 $address = new Address(array_merge($job['w_address'], array('pid' => $page->pid(), 'id' => $id, 'type' => Address::LINK_JOB)));
190 $address->save();
191 Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id);
192 if (isset($job['terms'])) {
193 foreach ($job['terms'] as $term) {
194 $terms_values[] = '('.XDB::escape($page->pid()).', '. XDB::escape($id).', '.XDB::escape($term['jtid']).', "original")';
195 }
196 }
197 }
198 }
199 if (count($terms_values) > 0) {
200 XDB::execute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
201 VALUES '.implode(', ', $terms_values));
202 }
203 }
204
205 public function getText($value) {
206 $jobs = array();
207 foreach ($value as $id => $job) {
208 $address = Address::formArrayToString($job['w_address']);
209 $phones = Phone::formArrayToString($job['w_phone']);
210 // TODO: add jobterms here.
211 $jobs[] = 'Entreprise : ' . $job['name']
212 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
213 . ', email : ' . $job['w_email']
214 . ($phones ? ', ' . $phones : '') . ($address ? ', ' . $address : '');
215 }
216 return implode(' ; ' , $jobs);
217 }
218 }
219
220 class ProfileSettingCorps implements ProfileSetting
221 {
222 public function value(ProfilePage &$page, $field, $value, &$success)
223 {
224 $success = true;
225 if (is_null($value)) {
226 $res = XDB::query("SELECT original_corpsid AS original, current_corpsid AS current,
227 rankid AS rank, corps_pub AS pub
228 FROM profile_corps
229 WHERE pid = {?}",
230 $page->pid());
231 return $res->fetchOneAssoc();
232 }
233 return $value;
234 }
235
236 public function save(ProfilePage &$page, $field, $value)
237 {
238 XDB::execute('REPLACE INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
239 VALUES ({?}, {?}, {?}, {?}, {?})',
240 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
241 }
242
243 public function getText($value)
244 {
245 $corpsList = DirEnum::getOptions(DirEnum::CORPS);
246 $rankList = DirEnum::getOptions(DirEnum::CORPSRANKS);
247 return 'Corps actuel : ' . $corpsList[$value['current']] . ' , rang : ' . $corpsList[$value['rank']]
248 . ' , corps d\'origine : ' . $corpsList[$value['original']] . ' , affichage : ' . $value['pub'];
249 }
250 }
251
252 class ProfilePageJobs extends ProfilePage
253 {
254 protected $pg_template = 'profile/jobs.tpl';
255
256 public function __construct(PlWizard &$wiz)
257 {
258 parent::__construct($wiz);
259 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
260 $this->settings['cv'] = null;
261 }
262 $this->settings['corps'] = new ProfileSettingCorps();
263 $this->settings['jobs'] = new ProfileSettingJob();
264 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
265 }
266
267 protected function _fetchData()
268 {
269 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
270 // Checkout the CV
271 $res = XDB::query("SELECT cv
272 FROM profiles
273 WHERE pid = {?}",
274 $this->pid());
275 $this->values['cv'] = $res->fetchOneCell();
276 }
277
278 // Build the jobs tree
279 $res = XDB::iterRow('SELECT j.id, j.jobid, je.name, j.description, j.email, j.email_pub,
280 j.url, j.pub
281 FROM profile_job AS j
282 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
283 WHERE j.pid = {?}
284 ORDER BY j.id',
285 $this->pid());
286 $this->values['jobs'] = array();
287
288 $compagnies = array();
289 if ($res->numRows() > 0) {
290 while (list($id, $jobid, $name, $description, $w_email, $w_emailPub, $w_url, $pub) = $res->next()) {
291 $compagnies[] = $jobid;
292 $this->values['jobs'][] = array(
293 'id' => $id,
294 'jobid' => $jobid,
295 'name' => $name,
296 'description' => $description,
297 'pub' => $pub,
298 'w_email' => $w_email,
299 'w_email_pub' => $w_emailPub,
300 'w_url' => $w_url,
301 );
302 }
303
304 $it = Address::iterate(array($this->pid()), array(Address::LINK_JOB));
305 while ($address = $it->next()) {
306 $this->values['jobs'][$address->jobid]['w_address'] = $address->toFormArray();
307 }
308 $it = Phone::iterate(array($this->pid()), array(Phone::LINK_JOB));
309 while ($phone = $it->next()) {
310 $this->values['jobs'][$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray();
311 }
312 $res = XDB::iterator("SELECT e.jtid, e.full_name, j.jid AS jobid
313 FROM profile_job_term_enum AS e
314 INNER JOIN profile_job_term AS j USING(jtid)
315 WHERE pid = {?}
316 ORDER BY j.jid",
317 $this->pid());
318 $i = 0;
319 $jobNb = count($this->values['jobs']);
320 while ($term = $res->next()) {
321 $jobid = $term['jobid'];
322 while ($i < $jobNb && $this->values['jobs'][$i]['id'] < $jobid) {
323 $i++;
324 }
325 if ($i >= $jobNb) {
326 break;
327 }
328 $job =& $this->values['jobs'][$i];
329 if ($job['id'] != $jobid) {
330 continue;
331 }
332 if (!isset($job['terms'])) {
333 $job['terms'] = array();
334 }
335 $job['terms'][] = $term;
336 }
337
338 $phone = new Phone();
339 $address = new Address();
340 foreach ($this->values['jobs'] as $id => &$job) {
341 if (!isset($job['w_phone'])) {
342 $job['w_phone'] = array(0 => $phone->toFormArray());
343 }
344 if (!isset($job['w_address'])) {
345 $job['w_address'] = $address->toFormArray();
346 }
347 }
348
349 $job['w_email_new'] = '';
350 if (!isset($job['w_email_pub'])) {
351 $job['w_email_pub'] = 'private';
352 }
353 } else {
354 $this->values['jobs'][] = $this->settings['jobs']->emptyJob();
355 }
356 }
357
358 protected function _saveData()
359 {
360 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
361 if ($this->changed['cv']) {
362 XDB::execute("UPDATE profiles
363 SET cv = {?}
364 WHERE pid = {?}",
365 $this->values['cv'], $this->pid());
366 }
367 }
368 }
369
370 public function _prepare(PlPage &$page, $id)
371 {
372 require_once 'emails.combobox.inc.php';
373 fill_email_combobox($page, $this->owner);
374
375 $res = XDB::iterator("SELECT id, name
376 FROM profile_corps_enum
377 ORDER BY id = 1 DESC, name");
378 $page->assign('original_corps', $res->fetchAllAssoc());
379
380 $res = XDB::iterator("SELECT id, name
381 FROM profile_corps_enum
382 WHERE still_exists = 1
383 ORDER BY id = 1 DESC, name");
384 $page->assign('current_corps', $res->fetchAllAssoc());
385
386 $res = XDB::iterator("SELECT id, name
387 FROM profile_corps_rank_enum
388 ORDER BY id = 1 DESC, name");
389 $page->assign('corps_rank', $res->fetchAllAssoc());
390 }
391 }
392
393 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
394 ?>