2d03924d2fc41d0825a43f3a9c5dfcc159212259
[platal.git] / modules / profile / jobs.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 ProfileJob extends ProfileGeocoding
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 ProfilePub();
34 $this->email
35 = $this->email_new
36 = new ProfileEmail();
37 $this->url = new ProfileWeb();
38 $this->bool = new ProfileBool();
39 $this->checks = array('url' => array('w_url'),
40 'email' => array('w_email'),
41 'pub' => array('pub', 'w_email_pub'),
42 );
43 }
44
45 private function cleanJob(ProfilePage &$page, $jobid, array &$job, &$success)
46 {
47 $success = true;
48 if ($job['w_email'] == "new@example.org") {
49 $job['w_email'] = $job['w_email_new'];
50 }
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 }
61 if (!$job['subSubSectorName']) {
62 $res = XDB::query("SELECT name
63 FROM profile_job_subsubsector_enum
64 WHERE id = {?}",
65 $job['subSubSector']);
66 $job['subSubSectorName'] = $res->fetchOneCell();
67 } else {
68 $res = XDB::query("SELECT sectorid, subsectorid, id
69 FROM profile_job_subsubsector_enum
70 WHERE name = {?}",
71 $job['subSubSectorName']);
72 if ($res->numRows() != 1) {
73 $success = false;
74 $job['sector_error'] = true;
75 } else {
76 list($job['sector'], $job['subSector'], $job['subSubSector']) = $res->fetchOneRow();
77 }
78 }
79 if ($job['name']) {
80 $res = XDB::query("SELECT id
81 FROM profile_job_enum
82 WHERE name = {?}",
83 $job['name']);
84 if ($res->numRows() != 1) {
85 $user = S::user();
86 $req = new EntrReq($user, $jobid, $job['name'], $job['hq_²acronym'], $job['hq_url'], $job['hq_email'], $job['hq_fixed'], $job['hq_fax']);
87 $req->submit();
88 $job['jobid'] = null;
89 } else {
90 $job['jobid'] = $res->fetchOneCell();
91 }
92 }
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();
96 }
97 $profiletel = new ProfilePhones('pro', $jobid);
98 $job['w_phone'] = $profiletel->value($page, 'tel', $job['w_phone'], $s);
99 unset($job['removed']);
100 unset($job['new']);
101 }
102
103 public function value(ProfilePage &$page, $field, $value, &$success)
104 {
105 require_once('validations.inc.php');
106 $entreprise = Validate::get_typed_requests(S::i('uid'), 'entreprise');
107 $entr_val = 0;
108
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) {
116 $job['name'] = trim($job['name']);
117 if (!$job['name']) {
118 $job['tmp_name'] = $entreprise[$entr_val]->name;
119 $entr_val ++;
120 }
121 if (isset($job['removed']) && $job['removed']) {
122 unset($value[$key]);
123 }
124 }
125 foreach ($value as $key=>&$job) {
126 $ls = true;
127 $this->geocodeAddress($job['w_address'], $s);
128 $ls = ($ls && $s);
129 $this->cleanJob($page, $key, $job, $s);
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 {
140 require_once('profil.func.inc.php');
141 require_once('validations.inc.php');
142
143 XDB::execute("DELETE FROM profile_job
144 WHERE uid = {?}",
145 S::i('uid'));
146 XDB::execute("DELETE FROM profile_addresses
147 WHERE pid = {?} AND type = 'job'",
148 S::i('uid'));
149 XDB::execute("DELETE FROM profile_phones
150 WHERE uid = {?} AND link_type = 'pro'",
151 S::i('uid'));
152 foreach ($value as $id=>&$job) {
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 ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
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']);
159 } else {
160 XDB::execute("INSERT INTO profile_job (uid, id, functionid, description, sectorid, subsectorid,
161 subsubsectorid, email, url, pub, email_pub)
162 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
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']);
165 }
166 $address = new ProfileAddress();
167 $address->saveAddress($id, $job['w_address'], 'job');
168 $profiletel = new ProfilePhones('pro', $id);
169 $profiletel->saveTels('tel', $job['w_phone']);
170 }
171 }
172 }
173
174 class ProfileJobs extends ProfilePage
175 {
176 protected $pg_template = 'profile/jobs.tpl';
177
178 public function __construct(PlWizard &$wiz)
179 {
180 parent::__construct($wiz);
181 $this->settings['cv'] = null;
182 $this->settings['corps'] = null;
183 $this->settings['jobs'] = new ProfileJob();
184 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
185 }
186
187 protected function _fetchData()
188 {
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
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
204 // Build the jobs tree
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
216 FROM profile_job AS j
217 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
218 LEFT JOIN profile_job_subsubsector_enum AS s ON (s.id = j.subsubsectorid)
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')
221 WHERE j.uid = {?}
222 ORDER BY j.id",
223 S::i('uid'));
224 $this->values['jobs'] = array();
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,
236 ) = $res->next()) {
237 $this->values['jobs'][] = array('id' => $id,
238 'jobid' => $jobid,
239 'name' => $name,
240 'function' => $function,
241 'sector' => $sector,
242 'subSector' => $subSector,
243 'subSubSector' => $subSubSector,
244 'subSubSectorName' => $subSubSectorName,
245 'description' => $description,
246 'pub' => $pub,
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 );
288 }
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']);
297 while ($phone = $res->next()) {
298 $jobid = $phone['jobid'];
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];
306 if (!isset($job['w_phone'])) {
307 $job['w_phone'] = array();
308 }
309 if ($job['id'] == $jobid) {
310 $job['w_phone'][] = $phone;
311 }
312 }
313 foreach ($this->values['jobs'] as $id => &$job) {
314 if (!isset($job['w_phone'])) {
315 $job['w_phone'] = array();
316 }
317 }
318 }
319
320 protected function _saveData()
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 }
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 }
337 }
338
339 public function _prepare(PlPage &$page, $id)
340 {
341 require_once "emails.combobox.inc.php";
342 fill_email_combobox($page);
343
344 $res = XDB::query("SELECT id, name AS label
345 FROM profile_job_sector_enum");
346 $page->assign('sectors', $res->fetchAllAssoc());
347 $res = XDB::query("SELECT id, fonction_fr, FIND_IN_SET('titre', flags) AS title
348 FROM fonctions_def
349 ORDER BY id");
350 $page->assign('functions', $res->fetchAllAssoc());
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());
366 }
367 }
368
369 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
370 ?>