Fixes sector and function's display in the profile edition interface.
[platal.git] / modules / profile / jobs.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 ProfileGeoloc
23 {
24 private $pub;
25 private $mail_new;
26 private $mail;
27 private $web;
28 private $bool;
29 private $checks;
30
31 public function __construct()
32 {
33 $this->pub = new ProfilePub();
34 $this->mail
35 = $this->mail_new
36 = new ProfileEmail();
37 $this->web = new ProfileWeb();
38 $this->bool = new ProfileBool();
39 $this->checks = array('web' => array('w_web'),
40 'mail_new' => array('w_email_new'),
41 'mail' => array('w_email'),
42 'pub' => array('pub', 'w_email_pub'));
43 }
44
45 private function cleanJob(ProfilePage &$page,$jobid, array &$job, &$success)
46 {
47 $success = true;
48 foreach ($this->checks as $obj=>&$fields) {
49 $chk =& $this->$obj;
50 foreach ($fields as $field) {
51 if ($field == "w_email_new") {
52 if ($job['w_email'] == "new@example.org") {
53 $job['w_email'] = $job[$field];
54 }
55 continue;
56 }
57 $job[$field] = $chk->value($page, $field, $job[$field], $s);
58 if (!$s) {
59 $success = false;
60 $job[$field . '_error'] = true;
61 }
62 }
63 }
64 if (!isset($job['sss_secteur_name'])) {
65 $res = XDB::query("SELECT name
66 FROM profile_job_subsubsector_enum
67 WHERE id = {?}",
68 $job['sss_secteur']);
69 $job['sss_secteur_name'] = $res->fetchOneCell();
70 } else {
71 $res = XDB::query("SELECT sectorid, subsectorid, id
72 FROM profile_job_subsubsector_enum
73 WHERE name = {?}",
74 $job['sss_secteur_name']);
75 if ($res->numRows() != 1) {
76 $success = false;
77 $job['sector_error'] = true;
78 } else {
79 list($job['secteur'], $job['ss_secteur'], $job['sss_secteur']) = $res->fetchOneRow();
80 }
81 }
82 if (!isset($job['jobid'])) {
83 $res = XDB::query("SELECT id
84 FROM profile_job_enum
85 WHERE name = {?}",
86 $job['name']);
87 if ($res->numRows() != 1) {
88 $success = false;
89 $job['name_error'] = true;
90 } else {
91 $job['jobid'] = $res->fetchOneCell();
92 }
93 }
94 $job['w_adr']['pub'] = $this->pub->value($page, 'adr_pub', @$job['w_adr']['pub'], $s);
95 $job['w_adr']['checked'] = $this->bool->value($page, 'adr_checked', @$job['w_adr']['checked'], $s);
96 if (!isset($job['w_tel'])) {
97 $job['w_tel'] = array();
98 }
99 $profiletel = new ProfilePhones('pro', $jobid);
100 $job['w_tel'] = $profiletel->value($page, 'tel', $job['w_tel'], $s);
101 unset($job['removed']);
102 unset($job['new']);
103 unset($job['w_adr']['changed']);
104 unset($job['w_adr']['parsevalid']);
105 unset($job['w_adr']['display']);
106 }
107
108 public function value(ProfilePage &$page, $field, $value, &$success)
109 {
110 $init = false;
111 if (is_null($value)) {
112 $value = $page->values['jobs'];
113 $init = true;
114 }
115 $success = true;
116 foreach ($value as $key=>&$job) {
117 if (@$job['removed'] || !trim($job['name'])) {
118 unset($value[$key]);
119 }
120 }
121 foreach ($value as $key=>&$job) {
122 $ls = true;
123 $this->geolocAddress($job['w_adr'], $s);
124 $ls = ($ls && $s);
125 $this->cleanJob($page, $key, $job, $s);
126 $ls = ($ls && $s);
127 if (!$init) {
128 $success = ($success && $ls);
129 }
130 }
131 return $value;
132 }
133
134 public function save(ProfilePage &$page, $field, $value)
135 {
136 require_once('profil.func.inc.php');
137 XDB::execute("DELETE FROM profile_job
138 WHERE uid = {?}",
139 S::i('uid'));
140 XDB::execute("DELETE FROM profile_phones
141 WHERE uid = {?} AND link_type = 'pro'",
142 S::i('uid'));
143 $i = 0;
144 foreach ($value as $jobid=>&$job) {
145 if ($job['w_email'] == "new@example.org") {
146 $job['w_email'] = $job['w_email_new'];
147 }
148 XDB::execute("INSERT INTO profile_job (uid, id, functionid, description, sectorid, subsectorid,
149 subsubsectorid, email, url, pub, email_pub, jobid)
150 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
151 S::i('uid'), $i, $job['fonction'], $job['description'], $job['secteur'], $job['ss_secteur'],
152 $job['sss_secteur'], $job['w_email'], $job['w_web'], $job['pub'], $job['w_email_pub'], $job['jobid']);
153 $profiletel = new ProfilePhones('pro', $jobid);
154 $profiletel->saveTels('tel', $job['w_tel']);
155 $i++;
156 }
157 }
158 }
159
160 class ProfileJobs extends ProfilePage
161 {
162 protected $pg_template = 'profile/jobs.tpl';
163
164 public function __construct(PlWizard &$wiz)
165 {
166 parent::__construct($wiz);
167 $this->settings['cv'] = null;
168 $this->settings['corps'] = null;
169 $this->settings['jobs'] = new ProfileJob();
170 $this->watched = array('cv' => true, 'jobs' => true, 'corps' => true);
171 }
172
173 protected function _fetchData()
174 {
175 // Checkout the CV
176 $res = XDB::query("SELECT cv
177 FROM auth_user_md5
178 WHERE user_id = {?}",
179 S::i('uid'));
180 $this->values['cv'] = $res->fetchOneCell();
181
182 // Checkout the corps
183 $res = XDB::query("SELECT original_corpsid AS original, current_corpsid AS current,
184 rankid AS rank, corps_pub AS pub
185 FROM profile_corps
186 WHERE uid = {?}",
187 S::i('uid'));
188 $this->values['corps'] = $res->fetchOneAssoc();
189
190 // Build the jobs tree
191 $res = XDB::iterRow("SELECT j.id, je.name, j.functionid, j.sectorid, j.subsectorid,
192 j.subsubsectorid, j.description, e.adr1, e.adr2, e.adr3,
193 e.postcode, e.city, e.cityid, e.region, e.regiontxt,
194 e.country, gp.pays, gp.display,
195 FIND_IN_SET('geoloc', flags),
196 j.email, j.url, j.pub,
197 e.adr_pub, j.email_pub,
198 e.glat, e.glng, s.name
199 FROM profile_job AS j
200 LEFT JOIN profile_job_enum AS je ON (j.jobid = je.id)
201 LEFT JOIN entreprises AS e ON (j.uid = e.uid AND j.id = e.entrid)
202 LEFT JOIN geoloc_pays AS gp ON (gp.a2 = e.country)
203 LEFT JOIN profile_job_subsubsector_enum AS s ON (s.id = j.subsubsectorid)
204 WHERE j.uid = {?}
205 ORDER BY entrid", S::i('uid'));
206 $this->values['jobs'] = array();
207 while (list($id, $name, $function, $secteur, $ss_secteur, $sss_secteur, $description,
208 $w_adr1, $w_adr2, $w_adr3, $w_postcode, $w_city, $w_cityid,
209 $w_region, $w_regiontxt, $w_country, $w_countrytxt, $w_display,
210 $w_checked, $w_email, $w_web,
211 $pub, $w_adr_pub, $w_email_pub, $w_glat, $w_glng, $sss_secteur_name
212 ) = $res->next()) {
213 $this->values['jobs'][] = array('id' => $id,
214 'name' => $name,
215 'fonction' => $function,
216 'secteur' => $secteur,
217 'ss_secteur' => $ss_secteur,
218 'sss_secteur' => $sss_secteur,
219 'sss_secteur_name' => $sss_secteur_name,
220 'description' => $description,
221 'w_adr' => array('adr1' => $w_adr1,
222 'adr2' => $w_adr2,
223 'adr3' => $w_adr3,
224 'postcode' => $w_postcode,
225 'city' => $w_city,
226 'cityid' => $w_cityid,
227 'region' => $w_region,
228 'regiontxt' => $w_regiontxt,
229 'country' => $w_country,
230 'countrytxt' => $w_countrytxt,
231 'display' => $w_display,
232 'pub' => $w_adr_pub,
233 'checked' => $w_checked,
234 'precise_lat' => $w_glat,
235 'precise_lon' => $w_glng),
236 'w_email' => $w_email,
237 'w_web' => $w_web,
238 'pub' => $pub,
239 'w_email_pub' => $w_email_pub);
240 }
241
242 $res = XDB::iterator("SELECT link_id AS jobid, tel_type AS type, pub, display_tel AS tel, comment
243 FROM profile_phones
244 WHERE uid = {?} AND link_type = 'pro'
245 ORDER BY link_id",
246 S::i('uid'));
247 $i = 0;
248 $jobNb = count($this->values['jobs']);
249 while ($tel = $res->next()) {
250 $jobid = $tel['jobid'];
251 unset($tel['jobid']);
252 while ($i < $jobNb && $this->values['jobs'][$i]['id'] < $jobid) {
253 $i++;
254 }
255 if ($i >= $jobNb) {
256 break;
257 }
258 $job =& $this->values['jobs'][$i];
259 if (!isset($job['w_tel'])) {
260 $job['w_tel'] = array();
261 }
262 if ($job['id'] == $jobid) {
263 $job['w_tel'][] = $tel;
264 }
265 }
266 foreach ($this->values['jobs'] as $id=>&$job) {
267 if (!isset($job['w_tel'])) {
268 $job['w_tel'] = array();
269 }
270 unset($job['id']);
271 }
272 }
273
274 protected function _saveData()
275 {
276 if ($this->changed['cv']) {
277 XDB::execute("UPDATE auth_user_md5
278 SET cv = {?}
279 WHERE user_id = {?}",
280 $this->values['cv'], S::i('uid'));
281 }
282
283 if ($this->changed['corps']) {
284 XDB::execute("UPDATE profile_corps
285 SET original_corpsid = {?}, current_corpsid = {?},
286 rankid = {?}, corps_pub = {?}
287 WHERE uid = {?}",
288 $this->values['corps']['original'], $this->values['corps']['current'],
289 $this->values['corps']['rank'], $this->values['corps']['pub'], S::i('uid'));
290 }
291 }
292
293 public function _prepare(PlPage &$page, $id)
294 {
295 require_once "emails.combobox.inc.php";
296 fill_email_combobox($page);
297
298 $res = XDB::query("SELECT id, name AS label
299 FROM profile_job_sector_enum");
300 $page->assign('secteurs', $res->fetchAllAssoc());
301 $res = XDB::query("SELECT id, fonction_fr, FIND_IN_SET('titre', flags) AS title
302 FROM fonctions_def
303 ORDER BY id");
304 $page->assign('fonctions', $res->fetchAllAssoc());
305
306 $res = XDB::iterator("SELECT id, name
307 FROM profile_corps_enum
308 ORDER BY id = 1 DESC, name");
309 $page->assign('original_corps', $res->fetchAllAssoc());
310
311 $res = XDB::iterator("SELECT id, name
312 FROM profile_corps_enum
313 WHERE still_exists = 1
314 ORDER BY id = 1 DESC, name");
315 $page->assign('current_corps', $res->fetchAllAssoc());
316
317 $res = XDB::iterator("SELECT id, name
318 FROM profile_corps_rank_enum");
319 $page->assign('corps_rank', $res->fetchAllAssoc());
320 }
321 }
322
323 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
324 ?>