Integrates new phone table in profile and other pages
[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 $tel;
29 private $bool;
30 private $checks;
31
32 public function __construct()
33 {
34 $this->pub = new ProfilePub();
35 $this->mail
36 = $this->mail_new
37 = new ProfileEmail();
38 $this->web = new ProfileWeb();
39 $this->tel = new ProfileTel();
40 $this->bool = new ProfileBool();
41 $this->checks = array('web' => array('web'),
42 'mail_new' => array('email_new'),
43 'mail' => array('email'),
44 'tel' => array('tel', 'fax', 'mobile'),
45 'pub' => array('pub', 'tel_pub', 'email_pub'));
46 }
47
48 private function cleanJob(ProfilePage &$page, array &$job, &$success)
49 {
50 $success = true;
51 foreach ($this->checks as $obj=>&$fields) {
52 $chk =& $this->$obj;
53 foreach ($fields as $field) {
54 if ($field == "email_new") {
55 if ($job['email'] == "new@new.new") {
56 $job['email'] = $job[$field];
57 }
58 continue;
59 }
60 $job[$field] = $chk->value($page, $field, $job[$field], $s);
61 if (!$s) {
62 $success = false;
63 $job[$field . '_error'] = true;
64 }
65 }
66 }
67 $job['adr']['pub'] = $this->pub->value($page, 'adr_pub', @$job['adr']['pub'], $s);
68 $job['adr']['checked'] = $this->bool->value($page, 'adr_checked', @$job['adr']['checked'], $s);
69 unset($job['removed']);
70 unset($job['new']);
71 unset($job['adr']['changed']);
72 unset($job['adr']['parsevalid']);
73 unset($job['adr']['display']);
74 }
75
76 public function value(ProfilePage &$page, $field, $value, &$success)
77 {
78 $init = false;
79 if (is_null($value)) {
80 $value = $page->values['jobs'];
81 $init = true;
82 }
83 $success = true;
84 foreach ($value as $key=>&$job) {
85 if (@$job['removed'] || !trim($job['name'])) {
86 unset($value[$key]);
87 }
88 }
89 foreach ($value as $key=>&$job) {
90 $ls = true;
91 $this->geolocAddress($job['adr'], $s);
92 $ls = ($ls && $s);
93 $this->cleanJob($page, $job, $s);
94 $ls = ($ls && $s);
95 if (!$init) {
96 $success = ($success && $ls);
97 }
98 }
99 return $value;
100 }
101
102 public function save(ProfilePage &$page, $field, $value)
103 {
104 require_once('profil.func.inc.php');
105 XDB::execute("DELETE FROM entreprises
106 WHERE uid = {?}",
107 S::i('uid'));
108 XDB::execute("DELETE FROM telephone
109 WHERE uid = {?} AND link_type = 'pro'",
110 S::i('uid'));
111 $i = 0;
112 foreach ($value as &$job) {
113 if ($job['email'] == "new@new.new") {
114 $job['email'] = $job['email_new'];
115 }
116
117 XDB::execute("INSERT INTO entreprises (uid, entrid, entreprise, secteur, ss_secteur,
118 fonction, poste, adr1, adr2, adr3, postcode,
119 city, cityid, country, region, regiontxt,
120 email, web,
121 pub, adr_pub, email_pub, flags,
122 glat, glng)
123 VALUES ({?}, {?}, {?}, {?}, {?},
124 {?}, {?}, {?}, {?}, {?}, {?},
125 {?}, {?}, {?}, {?}, {?},
126 {?}, {?},
127 {?}, {?}, {?}, {?},
128 {?}, {?})",
129 S::i('uid'), $i, $job['name'], $job['secteur'], $job['ss_secteur'],
130 $job['fonction'], $job['poste'], $job['adr']['adr1'], $job['adr']['adr2'], $job['adr']['adr3'],
131 $job['adr']['postcode'],
132 $job['adr']['city'], $job['adr']['cityid'], $job['adr']['country'], $job['adr']['region'],
133 $job['adr']['regiontxt'],
134 $job['email'], $job['web'],
135 $job['pub'], $job['adr']['pub'], $job['email_pub'],
136 $job['adr']['checked'] ? 'geoloc' : '', $job['adr']['precise_lat'],
137 $job['adr']['precise_lon']);
138 if ($job['tel'] != '') {
139 XDB::execute("INSERT INTO telephone (uid, link_type, link_id, tel_id,
140 tel_type, search_tel, display_tel, pub)
141 VALUES ({?}, 'pro', {?}, 0,
142 'fixed', {?}, {?}, {?})",
143 S::i('uid'), $i, format_phone_number($job['tel']), $job['tel'], $job['tel_pub']);
144 }
145 if ($job['fax'] != '') {
146 XDB::execute("INSERT INTO telephone (uid, link_type, link_id, tel_id,
147 tel_type, search_tel, display_tel, pub)
148 VALUES ({?}, 'pro', {?}, 1,
149 'fax', {?}, {?}, {?})",
150 S::i('uid'), $i, format_phone_number($job['fax']), $job['fax'], $job['tel_pub']);
151 }
152 if ($job['mobile'] != '') {
153 XDB::execute("INSERT INTO telephone (uid, link_type, link_id, tel_id,
154 tel_type, search_tel, display_tel, pub)
155 VALUES ({?}, 'pro', {?}, 2,
156 'mobile', {?}, {?}, {?})",
157 S::i('uid'), $i, format_phone_number($job['mobile']), $job['mobile'], $job['tel_pub']);
158 }
159 $i++;
160 }
161 }
162 }
163
164 class ProfileJobs extends ProfilePage
165 {
166 protected $pg_template = 'profile/jobs.tpl';
167
168 public function __construct(PlWizard &$wiz)
169 {
170 parent::__construct($wiz);
171 $this->settings['cv'] = null;
172 $this->settings['jobs'] = new ProfileJob();
173 $this->watched['cv'] = $this->watched['jobs'] = true;
174 }
175
176 protected function _fetchData()
177 {
178 // Checkout the CV
179 $res = XDB::query("SELECT cv
180 FROM auth_user_md5
181 WHERE user_id = {?}",
182 S::i('uid'));
183 $this->values['cv'] = $res->fetchOneCell();
184
185 // Build the jobs tree
186 $res = XDB::iterRow("SELECT e.entreprise, e.secteur, e.ss_secteur,
187 e.fonction, e.poste, e.adr1, e.adr2, e.adr3,
188 e.postcode, e.city, e.cityid, e.region, e.regiontxt,
189 e.country, gp.pays, gp.display,
190 FIND_IN_SET('geoloc', flags),
191 e.email, e.web, e.pub,
192 e.adr_pub, e.email_pub,
193 e.glat AS precise_lat, e.glng AS precise_lon,
194 tt.display_tel AS tel, tt.pub AS tel_pub,
195 tf.display_tel AS fax, tm.display_tel AS mobile
196 FROM entreprises AS e
197 LEFT JOIN geoloc_pays AS gp ON(gp.a2 = e.country)
198 LEFT JOIN telephone AS tt ON(tt.uid = e.uid AND tt.link_type = 'pro' AND tt.link_id = entrid AND tt.tel_id = 0)
199 LEFT JOIN telephone AS tf ON(tf.uid = e.uid AND tf.link_type = 'pro' AND tf.link_id = entrid AND tf.tel_id = 1)
200 LEFT JOIN telephone AS tm ON(tm.uid = e.uid AND tm.link_type = 'pro' AND tm.link_id = entrid AND tm.tel_id = 2)
201 WHERE e.uid = {?} AND entreprise != ''
202 ORDER BY entrid", S::i('uid'));
203 $this->values['jobs'] = array();
204 while (list($name, $secteur, $ss_secteur, $fonction, $poste,
205 $adr1, $adr2, $adr3, $postcode, $city, $cityid,
206 $region, $regiontxt, $country, $countrytxt, $display,
207 $checked, $email, $web,
208 $pub, $adr_pub, $email_pub, $glat, $glng,
209 $tel, $tel_pub, $fax, $mobile) = $res->next()) {
210 $this->values['jobs'][] = array('name' => $name,
211 'secteur' => $secteur,
212 'ss_secteur' => $ss_secteur,
213 'fonction' => $fonction,
214 'poste' => $poste,
215 'adr' => array('adr1' => $adr1,
216 'adr2' => $adr2,
217 'adr3' => $adr3,
218 'postcode' => $postcode,
219 'city' => $city,
220 'cityid' => $cityid,
221 'region' => $region,
222 'regiontxt' => $regiontxt,
223 'country' => $country,
224 'countrytxt' => $countrytxt,
225 'display' => $display,
226 'pub' => $adr_pub,
227 'checked' => $checked,
228 'precise_lat'=> $glat,
229 'precise_lon'=> $glng),
230 'tel' => $tel,
231 'fax' => $fax,
232 'mobile' => $mobile,
233 'email' => $email,
234 'web' => $web,
235 'pub' => $pub,
236 'tel_pub' => $tel_pub,
237 'email_pub' => $email_pub);
238 }
239 }
240
241 protected function _saveData()
242 {
243 if ($this->changed['cv']) {
244 XDB::execute("UPDATE auth_user_md5
245 SET cv = {?}
246 WHERE user_id = {?}",
247 $this->values['cv'], S::i('uid'));
248 }
249 }
250
251 public function _prepare(PlatalPage &$page, $id)
252 {
253 require_once "emails.combobox.inc.php";
254 fill_email_combobox($page);
255
256 $page->assign('secteurs', XDB::iterator("SELECT id, label
257 FROM emploi_secteur"));
258 $page->assign('fonctions', XDB::iterator("SELECT id, fonction_fr, FIND_IN_SET('titre', flags) AS title
259 FROM fonctions_def
260 ORDER BY id"));
261 }
262 }
263
264 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
265 ?>