A few fixes on entreprise validation.
[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' => '',
53 'hq_acronym' => '',
54 'hq_url' => '',
55 'hq_email' => '',
eb54852e 56 'hq_address' => $address->toFormArray(),
0b6c8b36 57 'hq_fixed' => '',
afaa2cc7
SJ
58 'hq_fax' => '',
59 'subSubSectorName' => null,
60 'sector' => '0',
61 'subSector' => '0',
62 'subSubSector' => '0',
63 'description' => '',
64 'w_url' => '',
eb54852e 65 'w_address' => $address->toFormArray(),
afaa2cc7
SJ
66 'w_email' => '',
67 'w_email_pub' => 'private',
68 'w_email_new' => '',
69 'w_phone' => array(0 => array(
70 'type' => 'fixed',
71 'tel' => '',
72 'pub' => 'private',
73 'comment' => '',
3ac45f10
PC
74 ),
75 'terms' => array()),
afaa2cc7
SJ
76 );
77 }
78
b814a8b8 79 private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success)
37d44b3b 80 {
541e8d03
SJ
81 if ($job['w_email'] == "new@example.org") {
82 $job['w_email'] = $job['w_email_new'];
83 }
37d44b3b
FB
84 foreach ($this->checks as $obj=>&$fields) {
85 $chk =& $this->$obj;
86 foreach ($fields as $field) {
87 $job[$field] = $chk->value($page, $field, $job[$field], $s);
88 if (!$s) {
89 $success = false;
90 $job[$field . '_error'] = true;
91 }
92 }
93 }
541e8d03 94 if (!$job['subSubSectorName']) {
c7139c07
SJ
95 $res = XDB::query("SELECT name
96 FROM profile_job_subsubsector_enum
97 WHERE id = {?}",
541e8d03
SJ
98 $job['subSubSector']);
99 $job['subSubSectorName'] = $res->fetchOneCell();
c7139c07
SJ
100 } else {
101 $res = XDB::query("SELECT sectorid, subsectorid, id
102 FROM profile_job_subsubsector_enum
103 WHERE name = {?}",
541e8d03 104 $job['subSubSectorName']);
c7139c07
SJ
105 if ($res->numRows() != 1) {
106 $success = false;
107 $job['sector_error'] = true;
108 } else {
541e8d03 109 list($job['sector'], $job['subSector'], $job['subSubSector']) = $res->fetchOneRow();
c7139c07
SJ
110 }
111 }
3ac45f10
PC
112 if (count($job['terms'])) {
113 $termsid = array();
114 foreach ($job['terms'] as $term) {
115 if (!$term['full_name']) {
116 $termsid[] = $term['jtid'];
117 }
118 }
119 if (count($termsid)) {
120 $res = XDB::query("SELECT jtid, full_name
121 FROM profile_job_term_enum
122 WHERE jtid IN {?}",
123 $termsid);
124 $term_id_to_name = $res->fetchAllAssoc('jtid', false);
125 foreach ($job['terms'] as &$term) {
126 if (!$term['full_name']) {
127 $term['full_name'] = $term_id_to_name[$term['jtid']];
128 }
129 }
130 }
131 }
b814a8b8 132 if ($job['name']) {
c7139c07
SJ
133 $res = XDB::query("SELECT id
134 FROM profile_job_enum
135 WHERE name = {?}",
136 $job['name']);
137 if ($res->numRows() != 1) {
024ec1e5 138 $req = new EntrReq(S::user(), $page->profile, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
4d7d27fc 139 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
b814a8b8
SJ
140 $req->submit();
141 $job['jobid'] = null;
858a5b42 142 sleep(1);
c7139c07
SJ
143 } else {
144 $job['jobid'] = $res->fetchOneCell();
145 }
146 }
0b6c8b36 147 $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s);
afaa2cc7 148
37d44b3b
FB
149 unset($job['removed']);
150 unset($job['new']);
37d44b3b
FB
151 }
152
153 public function value(ProfilePage &$page, $field, $value, &$success)
154 {
a6483c12 155 require_once 'validations.inc.php';
024ec1e5 156 $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise');
b814a8b8
SJ
157 $entr_val = 0;
158
37d44b3b
FB
159 $init = false;
160 if (is_null($value)) {
161 $value = $page->values['jobs'];
162 $init = true;
163 }
164 $success = true;
a6483c12 165 foreach ($value as $key => &$job) {
b814a8b8 166 $job['name'] = trim($job['name']);
a6483c12 167 if ($job['name'] == '' && $entreprise) {
b814a8b8 168 $job['tmp_name'] = $entreprise[$entr_val]->name;
a6483c12
SJ
169 ++$entr_val;
170 } else if ($job['name'] == '') {
ba6ee875
SJ
171 if ($job['subSubSectorName'] == '' && $job['description'] == '' && $job['w_url'] == ''
172 && $job['w_address']['text'] == '' && $job['w_email'] == ''
173 && count($job['w_phone']) == 1 && $job['w_phone']['tel'] == '') {
174 array_splice($value, $key, 1);
175 continue;
176 }
177
d1d01361
SJ
178 if (!$init) {
179 $job['name_error'] = true;
180 $success = false;
181 }
b814a8b8 182 }
b94719c1 183
541e8d03 184 if (isset($job['removed']) && $job['removed']) {
b94719c1
SJ
185 if ($job['name'] == '' && $entreprise) {
186 $entreprise[$entr_val - 1]->clean();
187 }
ed01acac 188 array_splice($value, $key, 1);
37d44b3b 189 }
8768e5af
SJ
190 foreach (array('sectorid', 'subsectorid', 'subsubsectorid') as $key) {
191 if ($job[$key] == 0) {
192 $job[$key] = null;
193 }
b139d0e6 194 }
37d44b3b 195 }
a6483c12 196 foreach ($value as $key => &$job) {
eb54852e
SJ
197 $address = new Address($job['w_address']);
198 $s = $address->format();
199 $job['w_address'] = $address->toFormArray();
bde2be3b 200 $this->cleanJob($page, $key, $job, $s);
37d44b3b 201 if (!$init) {
eb54852e 202 $success = ($success && $s);
37d44b3b
FB
203 }
204 }
205 return $value;
206 }
207
208 public function save(ProfilePage &$page, $field, $value)
209 {
c7139c07 210 XDB::execute("DELETE FROM profile_job
ce0b2c6f 211 WHERE pid = {?}",
a6483c12 212 $page->pid());
eb54852e 213 Address::delete($page->pid(), Address::LINK_JOB);
0b6c8b36 214 Phone::deletePhones($page->pid(), Phone::LINK_JOB);
3ac45f10 215 $terms_values = array();
a6483c12 216 foreach ($value as $id => &$job) {
afaa2cc7
SJ
217 if (isset($job['name']) && $job['name']) {
218 if (isset($job['jobid']) && $job['jobid']) {
ce0b2c6f 219 XDB::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
afaa2cc7
SJ
220 subsubsectorid, email, url, pub, email_pub, jobid)
221 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
a6483c12 222 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
afaa2cc7
SJ
223 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
224 } else {
ce0b2c6f 225 XDB::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
afaa2cc7
SJ
226 subsubsectorid, email, url, pub, email_pub)
227 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
a6483c12 228 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
afaa2cc7
SJ
229 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
230 }
eb54852e
SJ
231 $address = new Address(array_merge($job['w_address'], array('pid' => $page->pid(), 'id' => $id, 'type' => Address::LINK_JOB)));
232 $address->save();
0b6c8b36 233 Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id);
3ac45f10
PC
234 if (isset($job['terms'])) {
235 foreach ($job['terms'] as $term) {
236 $terms_values[] = '('.XDB::escape($page->pid()).', '. XDB::escape($id).', '.XDB::escape($term['jtid']).', "original")';
237 }
238 }
b814a8b8 239 }
37d44b3b 240 }
3ac45f10
PC
241 if (count($terms_values) > 0) {
242 XDB::execute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
243 VALUES '.implode(', ', $terms_values));
244 }
37d44b3b 245 }
a0fce0c6
SJ
246
247 public function getText($value) {
248 $jobs = array();
249 foreach ($value as $id => $job) {
eb54852e 250 $address = Address::formArrayToString($job['w_address']);
0b6c8b36 251 $phones = Phone::formArrayToString($job['w_phone']);
a0fce0c6
SJ
252 $jobs[] = 'Entreprise : ' . $job['name'] . ', secteur : ' . $job['subSubSectorName']
253 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
254 . ', email : ' . $job['w_email']
eb54852e 255 . ($phones ? ', ' . $phones : '') . ($address ? ', ' . $address : '');
a0fce0c6
SJ
256 }
257 return implode(' ; ' , $jobs);
258 }
37d44b3b
FB
259}
260
603aeb6c
SJ
261class ProfileSettingCorps implements ProfileSetting
262{
263 public function value(ProfilePage &$page, $field, $value, &$success)
264 {
265 $success = true;
266 if (is_null($value)) {
267 $res = XDB::query("SELECT original_corpsid AS original, current_corpsid AS current,
268 rankid AS rank, corps_pub AS pub
269 FROM profile_corps
270 WHERE pid = {?}",
271 $page->pid());
272 return $res->fetchOneAssoc();
273 }
274 return $value;
275 }
276
277 public function save(ProfilePage &$page, $field, $value)
278 {
279 XDB::execute('REPLACE INTO profile_corps (original_corpsid, current_corpsid, rankid, corps_pub, pid)
280 VALUES ({?}, {?}, {?}, {?}, {?})',
281 $value['original'], $value['current'], $value['rank'], $value['pub'], $page->pid());
282 }
283
284 public function getText($value)
285 {
286 $corpsList = DirEnum::getOptions(DirEnum::CORPS);
287 $rankList = DirEnum::getOptions(DirEnum::CORPSRANKS);
288 return 'Corps actuel : ' . $corpsList[$value['current']] . ' , rang : ' . $corpsList[$value['rank']]
289 . ' , corps d\'origine : ' . $corpsList[$value['original']] . ' , affichage : ' . $value['pub'];
290 }
291}
292
12bcf04b 293class ProfileSettingJobs extends ProfilePage
3950bc21
FB
294{
295 protected $pg_template = 'profile/jobs.tpl';
296
297 public function __construct(PlWizard &$wiz)
298 {
299 parent::__construct($wiz);
b539d596
FB
300 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
301 $this->settings['cv'] = null;
302 }
603aeb6c 303 $this->settings['corps'] = new ProfileSettingCorps();
12bcf04b 304 $this->settings['jobs'] = new ProfileSettingJob();
72e96bc0 305 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
37d44b3b
FB
306 }
307
7c2e0f0d 308 protected function _fetchData()
37d44b3b 309 {
b539d596
FB
310 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
311 // Checkout the CV
312 $res = XDB::query("SELECT cv
313 FROM profiles
314 WHERE pid = {?}",
315 $this->pid());
316 $this->values['cv'] = $res->fetchOneCell();
317 }
37d44b3b
FB
318
319 // Build the jobs tree
563f86f5 320 $res = XDB::iterRow("SELECT j.id, j.jobid, je.name, j.sectorid, j.subsectorid, j.subsubsectorid,
541e8d03 321 s.name, j.description, j.email, j.email_pub, j.url, j.pub,
eb54852e 322 je.acronym, je.url, je.email
c7139c07
SJ
323 FROM profile_job AS j
324 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
c7139c07 325 LEFT JOIN profile_job_subsubsector_enum AS s ON (s.id = j.subsubsectorid)
ce0b2c6f 326 WHERE j.pid = {?}
541e8d03 327 ORDER BY j.id",
a6391000 328 $this->pid());
37d44b3b 329 $this->values['jobs'] = array();
bde2be3b 330
eb54852e 331 $compagnies = array();
afaa2cc7
SJ
332 if ($res->numRows() > 0) {
333 while (list($id, $jobid, $name, $sector, $subSector, $subSubSector,
334 $subSubSectorName, $description, $w_email, $w_emailPub, $w_url, $pub,
335 $hq_acronym, $hq_url, $hq_email,
afaa2cc7 336 $w_subAdministrativeAreaId, $w_administrativeAreaId, $w_countryId,
afaa2cc7 337 ) = $res->next()) {
eb54852e 338 $compagnies[] = $jobid;
afaa2cc7
SJ
339 $this->values['jobs'][] = array(
340 'id' => $id,
341 'jobid' => $jobid,
342 'name' => $name,
343 'sector' => $sector,
344 'subSector' => $subSector,
345 'subSubSector' => $subSubSector,
346 'subSubSectorName' => $subSubSectorName,
347 'description' => $description,
348 'pub' => $pub,
349 'w_email' => $w_email,
350 'w_email_pub' => $w_emailPub,
351 'w_url' => $w_url,
352 'hq_acronym' => $hq_acronym,
353 'hq_url' => $hq_url,
354 'hq_email' => $hq_email,
afaa2cc7 355 );
bde2be3b 356 }
afaa2cc7 357
eb54852e
SJ
358 $it = Address::iterate(array($this->pid()), array(Address::LINK_JOB));
359 while ($address = $it->next()) {
360 $this->values['jobs'][$address->jobid]['w_address'] = $address->toFormArray();
361 }
362 $it = Address::iterate(array(), array(Address::LINK_COMPANY), $companies);
363 while ($address = $it->next()) {
364 $this->values['jobs'][$address->jobId()]['h_address'] = $address->toFormArray();
365 }
0b6c8b36
SJ
366 $it = Phone::iterate(array($this->pid()), array(Phone::LINK_JOB));
367 while ($phone = $it->next()) {
368 $this->values['jobs'][$phone->linkId()]['w_phone'][$phone->id()] = $phone->toFormArray();
bde2be3b 369 }
3ac45f10
PC
370 $res = XDB::iterator("SELECT e.jtid, e.full_name, j.jid AS jobid
371 FROM profile_job_term_enum AS e
372 INNER JOIN profile_job_term AS j USING(jtid)
373 WHERE pid = {?}
374 ORDER BY j.jid",
375 $this->pid());
376 $i = 0;
377 $jobNb = count($this->values['jobs']);
378 while ($term = $res->next()) {
379 $jobid = $term['jobid'];
380 while ($i < $jobNb && $this->values['jobs'][$i]['id'] < $jobid) {
381 $i++;
382 }
383 if ($i >= $jobNb) {
384 break;
385 }
386 $job =& $this->values['jobs'][$i];
387 if ($job['id'] != $jobid) {
388 continue;
389 }
390 if (!isset($job['terms'])) {
391 $job['terms'] = array();
392 }
393 $job['terms'][] = $term;
394 }
395
afaa2cc7 396 foreach ($this->values['jobs'] as $id => &$job) {
0b6c8b36 397 $phone = new Phone();
eb54852e 398 $address = new Address();
afaa2cc7 399 if (!isset($job['w_phone'])) {
0b6c8b36 400 $job['w_phone'] = array(0 => $phone->toFormArray());
afaa2cc7 401 }
eb54852e
SJ
402 if (!isset($job['w_address'])) {
403 $job['w_address'] = $address->toFormArray();
404 }
405 if (!isset($job['h_address'])) {
406 $job['h_address'] = $address->toFormArray();
407 }
bde2be3b 408 }
aab2ffdd 409
afaa2cc7 410 $job['w_email_new'] = '';
0b6c8b36
SJ
411 if (!isset($job['hq_fixed'])) {
412 $job['hq_fixed'] = '';
bde2be3b 413 }
afaa2cc7
SJ
414 if (!isset($job['hq_fax'])) {
415 $job['hq_fax'] = '';
bde2be3b 416 }
afaa2cc7
SJ
417 if (!isset($job['w_email_pub'])) {
418 $job['w_email_pub'] = 'private';
bde2be3b 419 }
afaa2cc7
SJ
420 } else {
421 $this->values['jobs'][] = $this->settings['jobs']->emptyJob();
bde2be3b 422 }
37d44b3b
FB
423 }
424
7c2e0f0d 425 protected function _saveData()
37d44b3b 426 {
b539d596
FB
427 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
428 if ($this->changed['cv']) {
429 XDB::execute("UPDATE profiles
430 SET cv = {?}
431 WHERE pid = {?}",
432 $this->values['cv'], $this->pid());
433 }
37d44b3b 434 }
3950bc21 435 }
2dcac0f5 436
04334c61 437 public function _prepare(PlPage &$page, $id)
2dcac0f5 438 {
a6483c12 439 require_once 'emails.combobox.inc.php';
4e698dc9 440 fill_email_combobox($page, $this->owner);
b715c1e1 441
33b6f5a0
SJ
442 $res = XDB::query("SELECT id, name AS label
443 FROM profile_job_sector_enum");
541e8d03 444 $page->assign('sectors', $res->fetchAllAssoc());
72e96bc0
SJ
445
446 $res = XDB::iterator("SELECT id, name
447 FROM profile_corps_enum
448 ORDER BY id = 1 DESC, name");
449 $page->assign('original_corps', $res->fetchAllAssoc());
450
451 $res = XDB::iterator("SELECT id, name
452 FROM profile_corps_enum
453 WHERE still_exists = 1
454 ORDER BY id = 1 DESC, name");
455 $page->assign('current_corps', $res->fetchAllAssoc());
456
457 $res = XDB::iterator("SELECT id, name
e489faf7
SJ
458 FROM profile_corps_rank_enum
459 ORDER BY id = 1 DESC, name");
72e96bc0 460 $page->assign('corps_rank', $res->fetchAllAssoc());
2dcac0f5 461 }
3950bc21
FB
462}
463
464// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
465?>