Adds job address to the job interface, makes more uniform address related names.
[platal.git] / modules / profile / jobs.inc.php
CommitLineData
3950bc21
FB
1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 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
73f6c165 22class ProfileJob extends ProfileGeocoding
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 {
c7139c07 33 $this->pub = new ProfilePub();
541e8d03
SJ
34 $this->email
35 = $this->email_new
c7139c07 36 = new ProfileEmail();
541e8d03 37 $this->url = new ProfileWeb();
c7139c07 38 $this->bool = new ProfileBool();
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
b814a8b8 45 private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success)
37d44b3b
FB
46 {
47 $success = true;
541e8d03
SJ
48 if ($job['w_email'] == "new@example.org") {
49 $job['w_email'] = $job['w_email_new'];
50 }
37d44b3b
FB
51 foreach ($this->checks as $obj=>&$fields) {
52 $chk =& $this->$obj;
53 foreach ($fields as $field) {
54 $job[$field] = $chk->value($page, $field, $job[$field], $s);
55 if (!$s) {
56 $success = false;
57 $job[$field . '_error'] = true;
58 }
59 }
60 }
541e8d03 61 if (!$job['subSubSectorName']) {
c7139c07
SJ
62 $res = XDB::query("SELECT name
63 FROM profile_job_subsubsector_enum
64 WHERE id = {?}",
541e8d03
SJ
65 $job['subSubSector']);
66 $job['subSubSectorName'] = $res->fetchOneCell();
c7139c07
SJ
67 } else {
68 $res = XDB::query("SELECT sectorid, subsectorid, id
69 FROM profile_job_subsubsector_enum
70 WHERE name = {?}",
541e8d03 71 $job['subSubSectorName']);
c7139c07
SJ
72 if ($res->numRows() != 1) {
73 $success = false;
74 $job['sector_error'] = true;
75 } else {
541e8d03 76 list($job['sector'], $job['subSector'], $job['subSubSector']) = $res->fetchOneRow();
c7139c07
SJ
77 }
78 }
b814a8b8 79 if ($job['name']) {
c7139c07
SJ
80 $res = XDB::query("SELECT id
81 FROM profile_job_enum
82 WHERE name = {?}",
83 $job['name']);
84 if ($res->numRows() != 1) {
b814a8b8 85 $user = S::user();
541e8d03 86 $req = new EntrReq($user, $jobid, $job['name'], $job['hq_²acronym'], $job['hq_url'], $job['hq_email'], $job['hq_fixed'], $job['hq_fax']);
b814a8b8
SJ
87 $req->submit();
88 $job['jobid'] = null;
c7139c07
SJ
89 } else {
90 $job['jobid'] = $res->fetchOneCell();
91 }
92 }
541e8d03
SJ
93 $job['w_address']['pub'] = $this->pub->value($page, 'address_pub', $job['w_address']['pub'], $s);
94 if (!isset($job['w_phone'])) {
95 $job['w_phone'] = array();
bde2be3b
GB
96 }
97 $profiletel = new ProfilePhones('pro', $jobid);
541e8d03 98 $job['w_phone'] = $profiletel->value($page, 'tel', $job['w_phone'], $s);
37d44b3b
FB
99 unset($job['removed']);
100 unset($job['new']);
37d44b3b
FB
101 }
102
103 public function value(ProfilePage &$page, $field, $value, &$success)
104 {
b814a8b8
SJ
105 require_once('validations.inc.php');
106 $entreprise = Validate::get_typed_requests(S::i('uid'), 'entreprise');
107 $entr_val = 0;
108
37d44b3b
FB
109 $init = false;
110 if (is_null($value)) {
111 $value = $page->values['jobs'];
112 $init = true;
113 }
114 $success = true;
115 foreach ($value as $key=>&$job) {
b814a8b8
SJ
116 $job['name'] = trim($job['name']);
117 if (!$job['name']) {
118 $job['tmp_name'] = $entreprise[$entr_val]->name;
119 $entr_val ++;
120 }
541e8d03 121 if (isset($job['removed']) && $job['removed']) {
37d44b3b
FB
122 unset($value[$key]);
123 }
124 }
125 foreach ($value as $key=>&$job) {
126 $ls = true;
541e8d03 127 $this->geocodeAddress($job['w_address'], $s);
37d44b3b 128 $ls = ($ls && $s);
bde2be3b 129 $this->cleanJob($page, $key, $job, $s);
37d44b3b
FB
130 $ls = ($ls && $s);
131 if (!$init) {
132 $success = ($success && $ls);
133 }
134 }
135 return $value;
136 }
137
138 public function save(ProfilePage &$page, $field, $value)
139 {
5e4417a9 140 require_once('profil.func.inc.php');
b814a8b8
SJ
141 require_once('validations.inc.php');
142
c7139c07 143 XDB::execute("DELETE FROM profile_job
37d44b3b
FB
144 WHERE uid = {?}",
145 S::i('uid'));
541e8d03
SJ
146 XDB::execute("DELETE FROM profile_addresses
147 WHERE pid = {?} AND type = 'job'",
148 S::i('uid'));
b235d980 149 XDB::execute("DELETE FROM profile_phones
5e4417a9
GB
150 WHERE uid = {?} AND link_type = 'pro'",
151 S::i('uid'));
b814a8b8 152 foreach ($value as $id=>&$job) {
b814a8b8
SJ
153 if ($job['jobid']) {
154 XDB::execute("INSERT INTO profile_job (uid, id, functionid, description, sectorid, subsectorid,
155 subsubsectorid, email, url, pub, email_pub, jobid)
156 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
541e8d03
SJ
157 S::i('uid'), $id, $job['fonction'], $job['description'], $job['sector'], $job['subSector'],
158 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub'], $job['jobid']);
b814a8b8
SJ
159 } else {
160 XDB::execute("INSERT INTO profile_job (uid, id, functionid, description, sectorid, subsectorid,
161 subsubsectorid, email, url, pub, email_pub)
162 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
541e8d03
SJ
163 S::i('uid'), $id, $job['fonction'], $job['description'], $job['sector'], $job['subSector'],
164 $job['subSubSector'], $job['w_email'], $job['w_url'], $job['pub'], $job['w_email_pub']);
b814a8b8 165 }
541e8d03
SJ
166 $address = new ProfileAddress();
167 $address->saveAddress($id, $job['w_address'], 'job');
b814a8b8 168 $profiletel = new ProfilePhones('pro', $id);
541e8d03 169 $profiletel->saveTels('tel', $job['w_phone']);
37d44b3b
FB
170 }
171 }
172}
173
3950bc21
FB
174class ProfileJobs extends ProfilePage
175{
176 protected $pg_template = 'profile/jobs.tpl';
177
178 public function __construct(PlWizard &$wiz)
179 {
180 parent::__construct($wiz);
37d44b3b 181 $this->settings['cv'] = null;
72e96bc0 182 $this->settings['corps'] = null;
37d44b3b 183 $this->settings['jobs'] = new ProfileJob();
72e96bc0 184 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
37d44b3b
FB
185 }
186
7c2e0f0d 187 protected function _fetchData()
37d44b3b 188 {
37d44b3b
FB
189 // Checkout the CV
190 $res = XDB::query("SELECT cv
191 FROM auth_user_md5
192 WHERE user_id = {?}",
193 S::i('uid'));
194 $this->values['cv'] = $res->fetchOneCell();
195
72e96bc0
SJ
196 // Checkout the corps
197 $res = XDB::query("SELECT original_corpsid AS original, current_corpsid AS current,
198 rankid AS rank, corps_pub AS pub
199 FROM profile_corps
200 WHERE uid = {?}",
201 S::i('uid'));
202 $this->values['corps'] = $res->fetchOneAssoc();
203
37d44b3b 204 // Build the jobs tree
541e8d03
SJ
205 $res = XDB::iterRow("SELECT j.id, j.jobid, je.name, j.functionid, j.sectorid, j.subsectorid, j.subsubsectorid,
206 s.name, j.description, j.email, j.email_pub, j.url, j.pub,
207 je.acronym, je.url, je.email,
208 aw.accuracy, aw.text, aw.postalText, aw.postalCode, aw.localityId,
209 aw.subAdministrativeAreaId, aw.administrativeAreaId, aw.countryId,
210 aw.latitude, aw.longitude, aw.pub, aw.updateTime,
211 aw.north, aw.south, aw.east, aw.west,
212 ah.accuracy, ah.text, ah.postalText, ah.postalCode, ah.localityId,
213 ah.subAdministrativeAreaId, ah.administrativeAreaId, ah.countryId,
214 ah.latitude, ah.longitude, ah.pub, ah.updateTime,
215 ah.north, ah.south, ah.east, ah.west
c7139c07
SJ
216 FROM profile_job AS j
217 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
c7139c07 218 LEFT JOIN profile_job_subsubsector_enum AS s ON (s.id = j.subsubsectorid)
541e8d03
SJ
219 LEFT JOIN profile_addresses AS aw ON (aw.pid = j.uid AND aw.type = 'job')
220 LEFT JOIN profile_addresses AS ah ON (ah.jobid = j.jobid AND ah.type = 'hq')
c7139c07 221 WHERE j.uid = {?}
541e8d03
SJ
222 ORDER BY j.id",
223 S::i('uid'));
37d44b3b 224 $this->values['jobs'] = array();
541e8d03
SJ
225 while (list($id, $jobid, $name, $function, $sector, $subSector, $subSubSector,
226 $subSubSectorName, $description, $w_email, $w_emailPub, $w_url, $pub,
227 $hq_acronym, $hq_url, $hq_email,
228 $w_accuracy, $w_text, $w_postalText, $w_postalCode, $w_localityId,
229 $w_subAdministrativeAreaId, $w_administrativeAreaId, $w_countryId,
230 $w_latitude, $w_longitude, $w_pub, $w_updateTime,
231 $w_north, $w_south, $w_east, $w_west,
232 $hq_accuracy, $hq_text, $hq_postalText, $hq_postalCode, $hq_localityId,
233 $hq_subAdministrativeAreaId, $hq_administrativeAreaId, $hq_countryId,
234 $hq_latitude, $hq_longitude, $hq_pub, $hq_updateTime,
235 $hq_north, $hq_south, $hq_east, $hq_west,
bde2be3b 236 ) = $res->next()) {
c7139c07 237 $this->values['jobs'][] = array('id' => $id,
541e8d03 238 'jobid' => $jobid,
c7139c07 239 'name' => $name,
541e8d03
SJ
240 'function' => $function,
241 'sector' => $sector,
242 'subSector' => $subSector,
243 'subSubSector' => $subSubSector,
244 'subSubSectorName' => $subSubSectorName,
c7139c07 245 'description' => $description,
c7139c07 246 'pub' => $pub,
541e8d03
SJ
247 'w_email' => $w_email,
248 'w_email_pub' => $w_email_pub,
249 'w_url' => $w_url,
250 'hq_acronym' => $hq_acronym,
251 'hq_url' => $hq_url,
252 'hq_email' => $hq_email,
253 'w_address' => array('accuracy' => $w_accuracy,
254 'text' => $w_text,
255 'postalText' => $w_postalText,
256 'postalCode' => $w_postalCode,
257 'localityId' => $w_localityId,
258 'subAdministrativeAreaId' => $w_subAdministrativeAreaId,
259 'administrativeAreaId' => $w_administrativeAreaId,
260 'countryId' => $w_countryId,
261 'latitude' => $w_latitude,
262 'longitude' => $w_longitude,
263 'pub' => $w_pub,
264 'updateTime' => $w_update,
265 'north' => $w_north,
266 'south' => $w_south,
267 'east' => $w_east,
268 'west' => $w_west,
269 ),
270 'hq_address' => array('accuracy' => $hq_accuracy,
271 'text' => $hq_text,
272 'postalText' => $hq_postalText,
273 'postalCode' => $hq_postalCode,
274 'localityId' => $hq_localityId,
275 'subAdministrativeAreaId' => $hq_subAdministrativeAreaId,
276 'administrativeAreaId' => $hq_administrativeAreaId,
277 'countryId' => $hq_countryId,
278 'latitude' => $hq_latitude,
279 'longitude' => $hq_longitude,
280 'pub' => $hq_pub,
281 'updateTime' => $hq_update,
282 'north' => $hq_north,
283 'south' => $hq_south,
284 'east' => $hq_east,
285 'west' => $hq_west,
286 ),
287 );
37d44b3b 288 }
bde2be3b
GB
289
290 $res = XDB::iterator("SELECT link_id AS jobid, tel_type AS type, pub, display_tel AS tel, comment
291 FROM profile_phones
292 WHERE uid = {?} AND link_type = 'pro'
293 ORDER BY link_id",
294 S::i('uid'));
295 $i = 0;
296 $jobNb = count($this->values['jobs']);
541e8d03
SJ
297 while ($phone = $res->next()) {
298 $jobid = $phone['jobid'];
bde2be3b
GB
299 while ($i < $jobNb && $this->values['jobs'][$i]['id'] < $jobid) {
300 $i++;
301 }
302 if ($i >= $jobNb) {
303 break;
304 }
305 $job =& $this->values['jobs'][$i];
541e8d03
SJ
306 if (!isset($job['w_phone'])) {
307 $job['w_phone'] = array();
bde2be3b
GB
308 }
309 if ($job['id'] == $jobid) {
541e8d03 310 $job['w_phone'][] = $phone;
bde2be3b
GB
311 }
312 }
541e8d03
SJ
313 foreach ($this->values['jobs'] as $id => &$job) {
314 if (!isset($job['w_phone'])) {
315 $job['w_phone'] = array();
bde2be3b 316 }
bde2be3b 317 }
37d44b3b
FB
318 }
319
7c2e0f0d 320 protected function _saveData()
37d44b3b
FB
321 {
322 if ($this->changed['cv']) {
323 XDB::execute("UPDATE auth_user_md5
324 SET cv = {?}
325 WHERE user_id = {?}",
326 $this->values['cv'], S::i('uid'));
327 }
72e96bc0
SJ
328
329 if ($this->changed['corps']) {
330 XDB::execute("UPDATE profile_corps
331 SET original_corpsid = {?}, current_corpsid = {?},
332 rankid = {?}, corps_pub = {?}
333 WHERE uid = {?}",
334 $this->values['corps']['original'], $this->values['corps']['current'],
335 $this->values['corps']['rank'], $this->values['corps']['pub'], S::i('uid'));
336 }
3950bc21 337 }
2dcac0f5 338
04334c61 339 public function _prepare(PlPage &$page, $id)
2dcac0f5 340 {
b715c1e1
SJ
341 require_once "emails.combobox.inc.php";
342 fill_email_combobox($page);
343
33b6f5a0
SJ
344 $res = XDB::query("SELECT id, name AS label
345 FROM profile_job_sector_enum");
541e8d03 346 $page->assign('sectors', $res->fetchAllAssoc());
33b6f5a0
SJ
347 $res = XDB::query("SELECT id, fonction_fr, FIND_IN_SET('titre', flags) AS title
348 FROM fonctions_def
349 ORDER BY id");
541e8d03 350 $page->assign('functions', $res->fetchAllAssoc());
72e96bc0
SJ
351
352 $res = XDB::iterator("SELECT id, name
353 FROM profile_corps_enum
354 ORDER BY id = 1 DESC, name");
355 $page->assign('original_corps', $res->fetchAllAssoc());
356
357 $res = XDB::iterator("SELECT id, name
358 FROM profile_corps_enum
359 WHERE still_exists = 1
360 ORDER BY id = 1 DESC, name");
361 $page->assign('current_corps', $res->fetchAllAssoc());
362
363 $res = XDB::iterator("SELECT id, name
364 FROM profile_corps_rank_enum");
365 $page->assign('corps_rank', $res->fetchAllAssoc());
2dcac0f5 366 }
3950bc21
FB
367}
368
369// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
370?>