Prevents warnings in flags template.
[platal.git] / modules / profile / jobs.inc.php
CommitLineData
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 22class 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
afaa2cc7
SJ
45 public function emptyJob()
46 {
eb54852e 47 $address = new Address();
afaa2cc7
SJ
48 return array(
49 'id' => '0',
50 'jobid' => '',
51 'pub' => 'private',
52 'name' => '',
afaa2cc7
SJ
53 'description' => '',
54 'w_url' => '',
eb54852e 55 'w_address' => $address->toFormArray(),
afaa2cc7
SJ
56 'w_email' => '',
57 'w_email_pub' => 'private',
58 'w_email_new' => '',
59 'w_phone' => array(0 => array(
60 'type' => 'fixed',
61 'tel' => '',
62 'pub' => 'private',
63 'comment' => '',
3ac45f10
PC
64 ),
65 'terms' => array()),
afaa2cc7
SJ
66 );
67 }
68
b814a8b8 69 private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success)
37d44b3b 70 {
541e8d03
SJ
71 if ($job['w_email'] == "new@example.org") {
72 $job['w_email'] = $job['w_email_new'];
73 }
37d44b3b
FB
74 foreach ($this->checks as $obj=>&$fields) {
75 $chk =& $this->$obj;
76 foreach ($fields as $field) {
77 $job[$field] = $chk->value($page, $field, $job[$field], $s);
78 if (!$s) {
79 $success = false;
80 $job[$field . '_error'] = true;
81 }
82 }
83 }
3ac45f10
PC
84 if (count($job['terms'])) {
85 $termsid = array();
86 foreach ($job['terms'] as $term) {
87 if (!$term['full_name']) {
88 $termsid[] = $term['jtid'];
89 }
90 }
91 if (count($termsid)) {
92 $res = XDB::query("SELECT jtid, full_name
93 FROM profile_job_term_enum
94 WHERE jtid IN {?}",
95 $termsid);
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']];
100 }
101 }
102 }
103 }
b814a8b8 104 if ($job['name']) {
c7139c07
SJ
105 $res = XDB::query("SELECT id
106 FROM profile_job_enum
107 WHERE name = {?}",
108 $job['name']);
109 if ($res->numRows() != 1) {
024ec1e5 110 $req = new EntrReq(S::user(), $page->profile, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
4d7d27fc 111 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
b814a8b8
SJ
112 $req->submit();
113 $job['jobid'] = null;
858a5b42 114 sleep(1);
c7139c07
SJ
115 } else {
116 $job['jobid'] = $res->fetchOneCell();
117 }
118 }
0b6c8b36 119 $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s);
afaa2cc7 120
37d44b3b
FB
121 unset($job['removed']);
122 unset($job['new']);
37d44b3b
FB
123 }
124
125 public function value(ProfilePage &$page, $field, $value, &$success)
126 {
a6483c12 127 require_once 'validations.inc.php';
024ec1e5 128 $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise');
b814a8b8
SJ
129 $entr_val = 0;
130
37d44b3b
FB
131 $init = false;
132 if (is_null($value)) {
133 $value = $page->values['jobs'];
134 $init = true;
135 }
136 $success = true;
a6483c12 137 foreach ($value as $key => &$job) {
b814a8b8 138 $job['name'] = trim($job['name']);
a6483c12 139 if ($job['name'] == '' && $entreprise) {
b814a8b8 140 $job['tmp_name'] = $entreprise[$entr_val]->name;
a6483c12
SJ
141 ++$entr_val;
142 } else if ($job['name'] == '') {
ea9cfc3e 143 if ($job['description'] == '' && $job['w_url'] == ''
ba6ee875
SJ
144 && $job['w_address']['text'] == '' && $job['w_email'] == ''
145 && count($job['w_phone']) == 1 && $job['w_phone']['tel'] == '') {
146 array_splice($value, $key, 1);
147 continue;
148 }
149
d1d01361
SJ
150 if (!$init) {
151 $job['name_error'] = true;
152 $success = false;
153 }
b814a8b8 154 }
b94719c1 155
541e8d03 156 if (isset($job['removed']) && $job['removed']) {
b94719c1
SJ
157 if ($job['name'] == '' && $entreprise) {
158 $entreprise[$entr_val - 1]->clean();
159 }
ed01acac 160 array_splice($value, $key, 1);
37d44b3b
FB
161 }
162 }
a6483c12 163 foreach ($value as $key => &$job) {
eb54852e
SJ
164 $address = new Address($job['w_address']);
165 $s = $address->format();
166 $job['w_address'] = $address->toFormArray();
bde2be3b 167 $this->cleanJob($page, $key, $job, $s);
37d44b3b 168 if (!$init) {
eb54852e 169 $success = ($success && $s);
37d44b3b
FB
170 }
171 }
172 return $value;
173 }
174
175 public function save(ProfilePage &$page, $field, $value)
176 {
c7139c07 177 XDB::execute("DELETE FROM profile_job
ce0b2c6f 178 WHERE pid = {?}",
a6483c12 179 $page->pid());
eb54852e 180 Address::delete($page->pid(), Address::LINK_JOB);
0b6c8b36 181 Phone::deletePhones($page->pid(), Phone::LINK_JOB);
3ac45f10 182 $terms_values = array();
a6483c12 183 foreach ($value as $id => &$job) {
afaa2cc7
SJ
184 if (isset($job['name']) && $job['name']) {
185 if (isset($job['jobid']) && $job['jobid']) {
ea9cfc3e
SJ
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']);
afaa2cc7 189 } else {
ea9cfc3e
SJ
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']);
afaa2cc7 193 }
eb54852e
SJ
194 $address = new Address(array_merge($job['w_address'], array('pid' => $page->pid(), 'id' => $id, 'type' => Address::LINK_JOB)));
195 $address->save();
0b6c8b36 196 Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id);
3ac45f10
PC
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")';
200 }
201 }
b814a8b8 202 }
37d44b3b 203 }
3ac45f10
PC
204 if (count($terms_values) > 0) {
205 XDB::execute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
206 VALUES '.implode(', ', $terms_values));
207 }
37d44b3b 208 }
a0fce0c6
SJ
209
210 public function getText($value) {
211 $jobs = array();
212 foreach ($value as $id => $job) {
eb54852e 213 $address = Address::formArrayToString($job['w_address']);
0b6c8b36 214 $phones = Phone::formArrayToString($job['w_phone']);
ea9cfc3e
SJ
215 // TODO: add jobterms here.
216 $jobs[] = 'Entreprise : ' . $job['name']
a0fce0c6
SJ
217 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
218 . ', email : ' . $job['w_email']
eb54852e 219 . ($phones ? ', ' . $phones : '') . ($address ? ', ' . $address : '');
a0fce0c6
SJ
220 }
221 return implode(' ; ' , $jobs);
222 }
37d44b3b
FB
223}
224
603aeb6c
SJ
225class ProfileSettingCorps implements ProfileSetting
226{
227 public function value(ProfilePage &$page, $field, $value, &$success)
228 {
229 $success = true;
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
233 FROM profile_corps
234 WHERE pid = {?}",
235 $page->pid());
236 return $res->fetchOneAssoc();
237 }
238 return $value;
239 }
240
241 public function save(ProfilePage &$page, $field, $value)
242 {
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());
246 }
247
248 public function getText($value)
249 {
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'];
254 }
255}
256
12bcf04b 257class ProfileSettingJobs extends ProfilePage
3950bc21
FB
258{
259 protected $pg_template = 'profile/jobs.tpl';
260
261 public function __construct(PlWizard &$wiz)
262 {
263 parent::__construct($wiz);
b539d596
FB
264 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
265 $this->settings['cv'] = null;
266 }
603aeb6c 267 $this->settings['corps'] = new ProfileSettingCorps();
12bcf04b 268 $this->settings['jobs'] = new ProfileSettingJob();
72e96bc0 269 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
37d44b3b
FB
270 }
271
7c2e0f0d 272 protected function _fetchData()
37d44b3b 273 {
b539d596
FB
274 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
275 // Checkout the CV
276 $res = XDB::query("SELECT cv
277 FROM profiles
278 WHERE pid = {?}",
279 $this->pid());
280 $this->values['cv'] = $res->fetchOneCell();
281 }
37d44b3b
FB
282
283 // Build the jobs tree
ea9cfc3e
SJ
284 $res = XDB::iterRow('SELECT j.id, j.jobid, je.name, j.description, j.email, j.email_pub,
285 j.url, j.pub
286 FROM profile_job AS j
287 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
ce0b2c6f 288 WHERE j.pid = {?}
ea9cfc3e 289 ORDER BY j.id',
a6391000 290 $this->pid());
37d44b3b 291 $this->values['jobs'] = array();
bde2be3b 292
eb54852e 293 $compagnies = array();
afaa2cc7 294 if ($res->numRows() > 0) {
ea9cfc3e 295 while (list($id, $jobid, $name, $description, $w_email, $w_emailPub, $w_url, $pub) = $res->next()) {
eb54852e 296 $compagnies[] = $jobid;
afaa2cc7
SJ
297 $this->values['jobs'][] = array(
298 'id' => $id,
299 'jobid' => $jobid,
300 'name' => $name,
afaa2cc7
SJ
301 'description' => $description,
302 'pub' => $pub,
303 'w_email' => $w_email,
304 'w_email_pub' => $w_emailPub,
305 'w_url' => $w_url,
afaa2cc7 306 );
bde2be3b 307 }
afaa2cc7 308
eb54852e
SJ
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();
312 }
0b6c8b36
SJ
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();
bde2be3b 316 }
3ac45f10
PC
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)
320 WHERE pid = {?}
321 ORDER BY j.jid",
322 $this->pid());
323 $i = 0;
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) {
328 $i++;
329 }
330 if ($i >= $jobNb) {
331 break;
332 }
333 $job =& $this->values['jobs'][$i];
334 if ($job['id'] != $jobid) {
335 continue;
336 }
337 if (!isset($job['terms'])) {
338 $job['terms'] = array();
339 }
340 $job['terms'][] = $term;
341 }
342
ea9cfc3e
SJ
343 $phone = new Phone();
344 $address = new Address();
afaa2cc7
SJ
345 foreach ($this->values['jobs'] as $id => &$job) {
346 if (!isset($job['w_phone'])) {
0b6c8b36 347 $job['w_phone'] = array(0 => $phone->toFormArray());
afaa2cc7 348 }
eb54852e
SJ
349 if (!isset($job['w_address'])) {
350 $job['w_address'] = $address->toFormArray();
351 }
bde2be3b 352 }
aab2ffdd 353
afaa2cc7 354 $job['w_email_new'] = '';
afaa2cc7
SJ
355 if (!isset($job['w_email_pub'])) {
356 $job['w_email_pub'] = 'private';
bde2be3b 357 }
afaa2cc7
SJ
358 } else {
359 $this->values['jobs'][] = $this->settings['jobs']->emptyJob();
bde2be3b 360 }
37d44b3b
FB
361 }
362
7c2e0f0d 363 protected function _saveData()
37d44b3b 364 {
b539d596
FB
365 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
366 if ($this->changed['cv']) {
367 XDB::execute("UPDATE profiles
368 SET cv = {?}
369 WHERE pid = {?}",
370 $this->values['cv'], $this->pid());
371 }
37d44b3b 372 }
3950bc21 373 }
2dcac0f5 374
04334c61 375 public function _prepare(PlPage &$page, $id)
2dcac0f5 376 {
a6483c12 377 require_once 'emails.combobox.inc.php';
4e698dc9 378 fill_email_combobox($page, $this->owner);
b715c1e1 379
72e96bc0
SJ
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());
384
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());
390
391 $res = XDB::iterator("SELECT id, name
e489faf7
SJ
392 FROM profile_corps_rank_enum
393 ORDER BY id = 1 DESC, name");
72e96bc0 394 $page->assign('corps_rank', $res->fetchAllAssoc());
2dcac0f5 395 }
3950bc21
FB
396}
397
398// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
399?>