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