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