Class Address.
[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 return array(
49 'id' => '0',
50 'jobid' => '',
51 'pub' => 'private',
52 'name' => '',
53 'hq_acronym' => '',
54 'hq_url' => '',
55 'hq_email' => '',
56 'hq_address' => $address->toFormArray(),
57 'hq_fixed' => '',
58 'hq_fax' => '',
59 'subSubSectorName' => null,
60 'sector' => '0',
61 'subSector' => '0',
62 'subSubSector' => '0',
63 'description' => '',
64 'w_url' => '',
65 'w_address' => $address->toFormArray(),
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' => '',
74 ),
75 'terms' => array()),
76 );
77 }
78
79 private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success)
80 {
81 if ($job['w_email'] == "new@example.org") {
82 $job['w_email'] = $job['w_email_new'];
83 }
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 }
94 if (!$job['subSubSectorName']) {
95 $res = XDB::query("SELECT name
96 FROM profile_job_subsubsector_enum
97 WHERE id = {?}",
98 $job['subSubSector']);
99 $job['subSubSectorName'] = $res->fetchOneCell();
100 } else {
101 $res = XDB::query("SELECT sectorid, subsectorid, id
102 FROM profile_job_subsubsector_enum
103 WHERE name = {?}",
104 $job['subSubSectorName']);
105 if ($res->numRows() != 1) {
106 $success = false;
107 $job['sector_error'] = true;
108 } else {
109 list($job['sector'], $job['subSector'], $job['subSubSector']) = $res->fetchOneRow();
110 }
111 }
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 }
132 if ($job['name']) {
133 $res = XDB::query("SELECT id
134 FROM profile_job_enum
135 WHERE name = {?}",
136 $job['name']);
137 if ($res->numRows() != 1) {
138 $req = new EntrReq(S::user(), $page->profile, $jobid, $job['name'], $job['hq_acronym'], $job['hq_url'],
139 $job['hq_email'], $job['hq_fixed'], $job['hq_fax'], $job['hq_address']);
140 $req->submit();
141 $job['jobid'] = null;
142 sleep(1);
143 } else {
144 $job['jobid'] = $res->fetchOneCell();
145 }
146 }
147 $job['w_phone'] = Phone::formatFormArray($job['w_phone'], $s);
148
149 unset($job['removed']);
150 unset($job['new']);
151 }
152
153 public function value(ProfilePage &$page, $field, $value, &$success)
154 {
155 require_once 'validations.inc.php';
156 $entreprise = ProfileValidate::get_typed_requests($page->pid(), 'entreprise');
157 $entr_val = 0;
158
159 $init = false;
160 if (is_null($value)) {
161 $value = $page->values['jobs'];
162 $init = true;
163 }
164 $success = true;
165 foreach ($value as $key => &$job) {
166 $job['name'] = trim($job['name']);
167 if ($job['name'] == '' && $entreprise) {
168 $job['tmp_name'] = $entreprise[$entr_val]->name;
169 ++$entr_val;
170 } else if ($job['name'] == '') {
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
178 if (!$init) {
179 $job['name_error'] = true;
180 $success = false;
181 }
182 }
183
184 if (isset($job['removed']) && $job['removed']) {
185 if ($job['name'] == '' && $entreprise) {
186 $entreprise[$entr_val - 1]->clean();
187 }
188 array_splice($value, $key, 1);
189 }
190 foreach (array('sectorid', 'subsectorid', 'subsubsectorid') as $key) {
191 if ($job[$key] == 0) {
192 $job[$key] = null;
193 }
194 }
195 }
196 foreach ($value as $key => &$job) {
197 $address = new Address($job['w_address']);
198 $s = $address->format();
199 $job['w_address'] = $address->toFormArray();
200 $this->cleanJob($page, $key, $job, $s);
201 if (!$init) {
202 $success = ($success && $s);
203 }
204 }
205 return $value;
206 }
207
208 public function save(ProfilePage &$page, $field, $value)
209 {
210 XDB::execute("DELETE FROM profile_job
211 WHERE pid = {?}",
212 $page->pid());
213 Address::delete($page->pid(), Address::LINK_JOB);
214 Phone::deletePhones($page->pid(), Phone::LINK_JOB);
215 $terms_values = array();
216 foreach ($value as $id => &$job) {
217 if (isset($job['name']) && $job['name']) {
218 if (isset($job['jobid']) && $job['jobid']) {
219 XDB::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
220 subsubsectorid, email, url, pub, email_pub, jobid)
221 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
222 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
223 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
224 } else {
225 XDB::execute("INSERT INTO profile_job (pid, id, description, sectorid, subsectorid,
226 subsubsectorid, email, url, pub, email_pub)
227 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
228 $page->pid(), $id, $job['description'], $job['sector'], $job['subSector'],
229 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
230 }
231 $address = new Address(array_merge($job['w_address'], array('pid' => $page->pid(), 'id' => $id, 'type' => Address::LINK_JOB)));
232 $address->save();
233 Phone::savePhones($job['w_phone'], $page->pid(), Phone::LINK_JOB, $id);
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 }
239 }
240 }
241 if (count($terms_values) > 0) {
242 XDB::execute('INSERT INTO profile_job_term (pid, jid, jtid, computed)
243 VALUES '.implode(', ', $terms_values));
244 }
245 }
246
247 public function getText($value) {
248 $jobs = array();
249 foreach ($value as $id => $job) {
250 $address = Address::formArrayToString($job['w_address']);
251 $phones = Phone::formArrayToString($job['w_phone']);
252 $jobs[] = 'Entreprise : ' . $job['name'] . ', secteur : ' . $job['subSubSectorName']
253 . ', description : ' . $job['description'] . ', web : ' . $job['w_url']
254 . ', email : ' . $job['w_email']
255 . ($phones ? ', ' . $phones : '') . ($address ? ', ' . $address : '');
256 }
257 return implode(' ; ' , $jobs);
258 }
259 }
260
261 class 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
293 class ProfileSettingJobs extends ProfilePage
294 {
295 protected $pg_template = 'profile/jobs.tpl';
296
297 public function __construct(PlWizard &$wiz)
298 {
299 parent::__construct($wiz);
300 if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
301 $this->settings['cv'] = null;
302 }
303 $this->settings['corps'] = new ProfileSettingCorps();
304 $this->settings['jobs'] = new ProfileSettingJob();
305 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
306 }
307
308 protected function _fetchData()
309 {
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 }
318
319 // Build the jobs tree
320 $res = XDB::iterRow("SELECT j.id, j.jobid, je.name, j.sectorid, j.subsectorid, j.subsubsectorid,
321 s.name, j.description, j.email, j.email_pub, j.url, j.pub,
322 je.acronym, je.url, je.email
323 FROM profile_job AS j
324 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
325 LEFT JOIN profile_job_subsubsector_enum AS s ON (s.id = j.subsubsectorid)
326 WHERE j.pid = {?}
327 ORDER BY j.id",
328 $this->pid());
329 $this->values['jobs'] = array();
330
331 $compagnies = array();
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,
336 $w_subAdministrativeAreaId, $w_administrativeAreaId, $w_countryId,
337 ) = $res->next()) {
338 $compagnies[] = $jobid;
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,
355 );
356 }
357
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 }
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();
369 }
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
396 foreach ($this->values['jobs'] as $id => &$job) {
397 $phone = new Phone();
398 $address = new Address();
399 if (!isset($job['w_phone'])) {
400 $job['w_phone'] = array(0 => $phone->toFormArray());
401 }
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 }
408 }
409
410 $job['w_email_new'] = '';
411 if (!isset($job['hq_fixed'])) {
412 $job['hq_fixed'] = '';
413 }
414 if (!isset($job['hq_fax'])) {
415 $job['hq_fax'] = '';
416 }
417 if (!isset($job['w_email_pub'])) {
418 $job['w_email_pub'] = 'private';
419 }
420 } else {
421 $this->values['jobs'][] = $this->settings['jobs']->emptyJob();
422 }
423 }
424
425 protected function _saveData()
426 {
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 }
434 }
435 }
436
437 public function _prepare(PlPage &$page, $id)
438 {
439 require_once 'emails.combobox.inc.php';
440 fill_email_combobox($page, $this->owner);
441
442 $res = XDB::query("SELECT id, name AS label
443 FROM profile_job_sector_enum");
444 $page->assign('sectors', $res->fetchAllAssoc());
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
458 FROM profile_corps_rank_enum
459 ORDER BY id = 1 DESC, name");
460 $page->assign('corps_rank', $res->fetchAllAssoc());
461 }
462 }
463
464 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
465 ?>