Removes var_dump.
[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();
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
b814a8b8 65 private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success)
37d44b3b 66 {
541e8d03
SJ
67 if ($job['w_email'] == "new@example.org") {
68 $job['w_email'] = $job['w_email_new'];
69 }
37d44b3b
FB
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 }
3ac45f10
PC
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 }
b814a8b8 100 if ($job['name']) {
c7139c07
SJ
101 $res = XDB::query("SELECT id
102 FROM profile_job_enum
103 WHERE name = {?}",
104 $job['name']);
105 if ($res->numRows() != 1) {
024ec1e5 106 $req = new EntrReq(S::user(), $page->profile, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
4d7d27fc 107 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
b814a8b8
SJ
108 $req->submit();
109 $job['jobid'] = null;
858a5b42 110 sleep(1);
c7139c07
SJ
111 } else {
112 $job['jobid'] = $res->fetchOneCell();
113 }
114 }
0b6c8b36 115 $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s);
afaa2cc7 116
37d44b3b
FB
117 unset($job['removed']);
118 unset($job['new']);
37d44b3b
FB
119 }
120
121 public function value(ProfilePage &$page, $field, $value, &$success)
122 {
a6483c12 123 require_once 'validations.inc.php';
024ec1e5 124 $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise');
b814a8b8
SJ
125 $entr_val = 0;
126
37d44b3b
FB
127 $init = false;
128 if (is_null($value)) {
129 $value = $page->values['jobs'];
130 $init = true;
131 }
132 $success = true;
a6483c12 133 foreach ($value as $key => &$job) {
b814a8b8 134 $job['name'] = trim($job['name']);
a6483c12 135 if ($job['name'] == '' && $entreprise) {
b814a8b8 136 $job['tmp_name'] = $entreprise[$entr_val]->name;
a6483c12
SJ
137 ++$entr_val;
138 } else if ($job['name'] == '') {
ea9cfc3e 139 if ($job['description'] == '' && $job['w_url'] == ''
ba6ee875
SJ
140 && $job['w_address']['text'] == '' && $job['w_email'] == ''
141 && count($job['w_phone']) == 1 && $job['w_phone']['tel'] == '') {
142 array_splice($value, $key, 1);
143 continue;
144 }
145
d1d01361
SJ
146 if (!$init) {
147 $job['name_error'] = true;
148 $success = false;
149 }
b814a8b8 150 }
b94719c1 151
541e8d03 152 if (isset($job['removed']) && $job['removed']) {
b94719c1
SJ
153 if ($job['name'] == '' && $entreprise) {
154 $entreprise[$entr_val - 1]->clean();
155 }
ed01acac 156 array_splice($value, $key, 1);
37d44b3b
FB
157 }
158 }
a6483c12 159 foreach ($value as $key => &$job) {
eb54852e
SJ
160 $address = new Address($job['w_address']);
161 $s = $address->format();
162 $job['w_address'] = $address->toFormArray();
bde2be3b 163 $this->cleanJob($page, $key, $job, $s);
37d44b3b 164 if (!$init) {
eb54852e 165 $success = ($success && $s);
37d44b3b
FB
166 }
167 }
168 return $value;
169 }
170
171 public function save(ProfilePage &$page, $field, $value)
172 {
c7139c07 173 XDB::execute("DELETE FROM profile_job
ce0b2c6f 174 WHERE pid = {?}",
a6483c12 175 $page->pid());
eb54852e 176 Address::delete($page->pid(), Address::LINK_JOB);
0b6c8b36 177 Phone::deletePhones($page->pid(), Phone::LINK_JOB);
3ac45f10 178 $terms_values = array();
a6483c12 179 foreach ($value as $id => &$job) {
afaa2cc7
SJ
180 if (isset($job['name']) && $job['name']) {
181 if (isset($job['jobid']) && $job['jobid']) {
ea9cfc3e
SJ
182 XDB::execute('INSERT INTO profile_job (pid, id, description, email, url, pub, email_pub, jobid)
183 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
184 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
afaa2cc7 185 } else {
ea9cfc3e
SJ
186 XDB::execute('INSERT INTO profile_job (pid, id, description, email, url, pub, email_pub)
187 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
188 $page->pid(), $id, $job['description'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
afaa2cc7 189 }
eb54852e
SJ
190 $address = new Address(array_merge($job['w_address'], array('pid' => $page->pid(), 'id' => $id, 'type' => Address::LINK_JOB)));
191 $address->save();
0b6c8b36 192 Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id);
3ac45f10
PC
193 if (isset($job['terms'])) {
194 foreach ($job['terms'] as $term) {
195 $terms_values[] = '('.XDB::escape($page->pid()).', '. XDB::escape($id).', '.XDB::escape($term['jtid']).', "original")';
196 }
197 }
b814a8b8 198 }
37d44b3b 199 }
3ac45f10
PC
200 if (count($terms_values) > 0) {
201 XDB::execute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
202 VALUES '.implode(', ', $terms_values));
203 }
37d44b3b 204 }
a0fce0c6
SJ
205
206 public function getText($value) {
207 $jobs = array();
208 foreach ($value as $id => $job) {
eb54852e 209 $address = Address::formArrayToString($job['w_address']);
0b6c8b36 210 $phones = Phone::formArrayToString($job['w_phone']);
ea9cfc3e
SJ
211 // TODO: add jobterms here.
212 $jobs[] = 'Entreprise : ' . $job['name']
a0fce0c6
SJ
213 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
214 . ', email : ' . $job['w_email']
eb54852e 215 . ($phones ? ', ' . $phones : '') . ($address ? ', ' . $address : '');
a0fce0c6
SJ
216 }
217 return implode(' ; ' , $jobs);
218 }
37d44b3b
FB
219}
220
603aeb6c
SJ
221class ProfileSettingCorps implements ProfileSetting
222{
223 public function value(ProfilePage &$page, $field, $value, &$success)
224 {
225 $success = true;
226 if (is_null($value)) {
227 $res = XDB::query("SELECT original_corpsid AS original, current_corpsid AS current,
228 rankid AS rank, corps_pub AS pub
229 FROM profile_corps
230 WHERE pid = {?}",
231 $page->pid());
232 return $res->fetchOneAssoc();
233 }
234 return $value;
235 }
236
237 public function save(ProfilePage &$page, $field, $value)
238 {
239 XDB::execute('REPLACE INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
240 VALUES ({?}, {?}, {?}, {?}, {?})',
241 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
242 }
243
244 public function getText($value)
245 {
246 $corpsList = DirEnum::getOptions(DirEnum::CORPS);
247 $rankList = DirEnum::getOptions(DirEnum::CORPSRANKS);
248 return 'Corps actuel : ' . $corpsList[$value['current']] . ' , rang : ' . $corpsList[$value['rank']]
249 . ' , corps d\'origine : ' . $corpsList[$value['original']] . ' , affichage : ' . $value['pub'];
250 }
251}
252
12bcf04b 253class ProfileSettingJobs extends ProfilePage
3950bc21
FB
254{
255 protected $pg_template = 'profile/jobs.tpl';
256
257 public function __construct(PlWizard &$wiz)
258 {
259 parent::__construct($wiz);
b539d596
FB
260 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
261 $this->settings['cv'] = null;
262 }
603aeb6c 263 $this->settings['corps'] = new ProfileSettingCorps();
12bcf04b 264 $this->settings['jobs'] = new ProfileSettingJob();
72e96bc0 265 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
37d44b3b
FB
266 }
267
7c2e0f0d 268 protected function _fetchData()
37d44b3b 269 {
b539d596
FB
270 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
271 // Checkout the CV
272 $res = XDB::query("SELECT cv
273 FROM profiles
274 WHERE pid = {?}",
275 $this->pid());
276 $this->values['cv'] = $res->fetchOneCell();
277 }
37d44b3b
FB
278
279 // Build the jobs tree
ea9cfc3e
SJ
280 $res = XDB::iterRow('SELECT j.id, j.jobid, je.name, j.description, j.email, j.email_pub,
281 j.url, j.pub
282 FROM profile_job AS j
283 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
ce0b2c6f 284 WHERE j.pid = {?}
ea9cfc3e 285 ORDER BY j.id',
a6391000 286 $this->pid());
37d44b3b 287 $this->values['jobs'] = array();
bde2be3b 288
eb54852e 289 $compagnies = array();
afaa2cc7 290 if ($res->numRows() > 0) {
ea9cfc3e 291 while (list($id, $jobid, $name, $description, $w_email, $w_emailPub, $w_url, $pub) = $res->next()) {
eb54852e 292 $compagnies[] = $jobid;
afaa2cc7
SJ
293 $this->values['jobs'][] = array(
294 'id' => $id,
295 'jobid' => $jobid,
296 'name' => $name,
afaa2cc7
SJ
297 'description' => $description,
298 'pub' => $pub,
299 'w_email' => $w_email,
300 'w_email_pub' => $w_emailPub,
301 'w_url' => $w_url,
afaa2cc7 302 );
bde2be3b 303 }
afaa2cc7 304
eb54852e
SJ
305 $it = Address::iterate(array($this->pid()), array(Address::LINK_JOB));
306 while ($address = $it->next()) {
307 $this->values['jobs'][$address->jobid]['w_address'] = $address->toFormArray();
308 }
0b6c8b36
SJ
309 $it = Phone::iterate(array($this->pid()), array(Phone::LINK_JOB));
310 while ($phone = $it->next()) {
311 $this->values['jobs'][$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray();
bde2be3b 312 }
3ac45f10
PC
313 $res = XDB::iterator("SELECT e.jtid, e.full_name, j.jid AS jobid
314 FROM profile_job_term_enum AS e
315 INNER JOIN profile_job_term AS j USING(jtid)
316 WHERE pid = {?}
317 ORDER BY j.jid",
318 $this->pid());
319 $i = 0;
320 $jobNb = count($this->values['jobs']);
321 while ($term = $res->next()) {
322 $jobid = $term['jobid'];
323 while ($i < $jobNb && $this->values['jobs'][$i]['id'] < $jobid) {
324 $i++;
325 }
326 if ($i >= $jobNb) {
327 break;
328 }
329 $job =& $this->values['jobs'][$i];
330 if ($job['id'] != $jobid) {
331 continue;
332 }
333 if (!isset($job['terms'])) {
334 $job['terms'] = array();
335 }
336 $job['terms'][] = $term;
337 }
338
ea9cfc3e
SJ
339 $phone = new Phone();
340 $address = new Address();
afaa2cc7
SJ
341 foreach ($this->values['jobs'] as $id => &$job) {
342 if (!isset($job['w_phone'])) {
0b6c8b36 343 $job['w_phone'] = array(0 => $phone->toFormArray());
afaa2cc7 344 }
eb54852e
SJ
345 if (!isset($job['w_address'])) {
346 $job['w_address'] = $address->toFormArray();
347 }
bde2be3b 348 }
aab2ffdd 349
afaa2cc7 350 $job['w_email_new'] = '';
afaa2cc7
SJ
351 if (!isset($job['w_email_pub'])) {
352 $job['w_email_pub'] = 'private';
bde2be3b 353 }
afaa2cc7
SJ
354 } else {
355 $this->values['jobs'][] = $this->settings['jobs']->emptyJob();
bde2be3b 356 }
37d44b3b
FB
357 }
358
7c2e0f0d 359 protected function _saveData()
37d44b3b 360 {
b539d596
FB
361 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
362 if ($this->changed['cv']) {
363 XDB::execute("UPDATE profiles
364 SET cv = {?}
365 WHERE pid = {?}",
366 $this->values['cv'], $this->pid());
367 }
37d44b3b 368 }
3950bc21 369 }
2dcac0f5 370
04334c61 371 public function _prepare(PlPage &$page, $id)
2dcac0f5 372 {
a6483c12 373 require_once 'emails.combobox.inc.php';
4e698dc9 374 fill_email_combobox($page, $this->owner);
b715c1e1 375
72e96bc0
SJ
376 $res = XDB::iterator("SELECT id, name
377 FROM profile_corps_enum
378 ORDER BY id = 1 DESC, name");
379 $page->assign('original_corps', $res->fetchAllAssoc());
380
381 $res = XDB::iterator("SELECT id, name
382 FROM profile_corps_enum
383 WHERE still_exists = 1
384 ORDER BY id = 1 DESC, name");
385 $page->assign('current_corps', $res->fetchAllAssoc());
386
387 $res = XDB::iterator("SELECT id, name
e489faf7
SJ
388 FROM profile_corps_rank_enum
389 ORDER BY id = 1 DESC, name");
72e96bc0 390 $page->assign('corps_rank', $res->fetchAllAssoc());
2dcac0f5 391 }
3950bc21
FB
392}
393
394// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
395?>