Geoloc and other fixes
[platal.git] / modules / profile / jobs.inc.php
CommitLineData
3950bc21
FB
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2007 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
37d44b3b
FB
22class ProfileJob extends ProfileGeoloc
23{
24 private $pub;
25 private $mail;
26 private $web;
27 private $tel;
28 private $checks;
29
30 public function __construct()
31 {
32 $this->pub = new ProfilePub();
33 $this->mail = new ProfileEmail();
34 $this->web = new ProfileWeb();
35 $this->tel = new ProfileTel();
36 $this->checks = array('web' => array('web'),
37 'mail' => array('email'),
38 'tel' => array('tel', 'fax', 'mobile'),
39 'pub' => array('pub', 'tel_pub', 'email_pub'));
40 }
41
42 private function cleanJob(ProfilePage &$page, array &$job, &$success)
43 {
44 $success = true;
45 foreach ($this->checks as $obj=>&$fields) {
46 $chk =& $this->$obj;
47 foreach ($fields as $field) {
48 $job[$field] = $chk->value($page, $field, $job[$field], $s);
49 if (!$s) {
50 $success = false;
51 $job[$field . '_error'] = true;
52 }
53 }
54 }
55 unset($job['removed']);
56 unset($job['new']);
57 unset($job['adr']['changed']);
58 unset($job['adr']['parsevalid']);
59 unset($job['adr']['display']);
60 }
61
62 public function value(ProfilePage &$page, $field, $value, &$success)
63 {
64 $init = false;
65 if (is_null($value)) {
66 $value = $page->values['jobs'];
67 $init = true;
68 }
69 $success = true;
70 foreach ($value as $key=>&$job) {
1879c7b2 71 if (@$job['removed'] || !trim($job['name'])) {
37d44b3b
FB
72 unset($value[$key]);
73 }
74 }
75 foreach ($value as $key=>&$job) {
76 $ls = true;
77 $this->geolocAddress($job['adr'], $s);
78 $ls = ($ls && $s);
79 $this->cleanJob($page, $job, $s);
80 $ls = ($ls && $s);
81 if (!$init) {
82 $success = ($success && $ls);
83 }
84 }
85 return $value;
86 }
87
88 public function save(ProfilePage &$page, $field, $value)
89 {
90 XDB::execute("DELETE FROM entreprises
91 WHERE uid = {?}",
92 S::i('uid'));
93 $i = 0;
94 foreach ($value as &$job) {
95 XDB::execute("INSERT INTO entreprises (uid, entrid, entreprise, secteur, ss_secteur,
96 fonction, poste, adr1, adr2, adr3, postcode,
97 city, cityid, country, region, regiontxt,
98 tel, fax, mobile, email, web,
99 pub, adr_pub, tel_pub, email_pub)
100 VALUES ({?}, {?}, {?}, {?}, {?},
101 {?}, {?}, {?}, {?}, {?}, {?},
102 {?}, {?}, {?}, {?}, {?},
103 {?}, {?}, {?}, {?}, {?},
104 {?}, {?}, {?}, {?})",
105 S::i('uid'), $i++, $job['name'], $job['secteur'], $job['ss_secteur'],
106 $job['fonction'], $job['poste'], $job['adr']['adr1'], $job['adr']['adr2'], $job['adr']['adr3'],
107 $job['adr']['postcode'],
108 $job['adr']['city'], $job['adr']['cityid'], $job['adr']['country'], $job['adr']['region'],
109 $job['adr']['regiontxt'],
110 $job['tel'], $job['fax'], $job['mobile'], $job['email'], $job['web'],
111 $job['pub'], $job['adr']['pub'], $job['tel_pub'], $job['email_pub']);
112 }
113 }
114}
115
3950bc21
FB
116class ProfileJobs extends ProfilePage
117{
118 protected $pg_template = 'profile/jobs.tpl';
119
120 public function __construct(PlWizard &$wiz)
121 {
122 parent::__construct($wiz);
37d44b3b
FB
123 $this->settings['cv'] = null;
124 $this->settings['jobs'] = new ProfileJob();
125 }
126
127 protected function fetchData()
128 {
129 if (count($this->orig) > 0) {
130 $this->values = $this->orig;
131 return;
132 }
133 // Checkout the CV
134 $res = XDB::query("SELECT cv
135 FROM auth_user_md5
136 WHERE user_id = {?}",
137 S::i('uid'));
138 $this->values['cv'] = $res->fetchOneCell();
139
140 // Build the jobs tree
141 $res = XDB::iterRow("SELECT e.entreprise, e.secteur, e.ss_secteur,
142 e.fonction, e.poste, e.adr1, e.adr2, e.adr3,
143 e.postcode, e.city, e.cityid, e.region, e.regiontxt,
144 e.country, gp.pays, gp.display,
145 e.tel, e.fax, e.mobile, e.email, e.web, e.pub,
146 e.adr_pub, e.tel_pub, e.email_pub
147 FROM entreprises AS e
148 INNER JOIN geoloc_pays AS gp ON(gp.a2 = e.country)
149 WHERE uid = {?} AND entreprise != ''
150 ORDER BY entrid", S::i('uid'));
151 $this->values['jobs'] = array();
152 while (list($name, $secteur, $ss_secteur, $fonction, $poste,
153 $adr1, $adr2, $adr3, $postcode, $city, $cityid,
154 $region, $regiontxt, $country, $countrytxt, $display,
155 $tel, $fax, $mobile, $email, $web,
156 $pub, $adr_pub, $tel_pub, $email_pub) = $res->next()) {
157 $this->values['jobs'][] = array('name' => $name,
158 'secteur' => $secteur,
159 'ss_secteur' => $ss_secteur,
160 'fonction' => $fonction,
161 'poste' => $poste,
162 'adr' => array('adr1' => $adr1,
163 'adr2' => $adr2,
164 'adr3' => $adr3,
165 'postcode' => $postcode,
166 'city' => $city,
167 'cityid' => $cityid,
168 'region' => $region,
169 'regiontxt' => $regiontxt,
170 'country' => $country,
171 'countrytxt' => $countrytxt,
172 'display' => $display,
173 'pub' => $adr_pub),
174 'tel' => $tel,
175 'fax' => $fax,
176 'mobile' => $mobile,
177 'email' => $email,
178 'web' => $web,
179 'pub' => $pub,
180 'adr_pub' => $adr_pub,
181 'tel_pub' => $tel_pub,
182 'email_pub' => $email_pub);
183 }
184 parent::fetchData();
185 }
186
187 protected function saveData()
188 {
189 if ($this->changed['cv']) {
190 XDB::execute("UPDATE auth_user_md5
191 SET cv = {?}
192 WHERE user_id = {?}",
193 $this->values['cv'], S::i('uid'));
194 }
195 parent::saveData();
3950bc21 196 }
2dcac0f5
FB
197
198 public function prepare(PlatalPage &$page)
199 {
200 parent::prepare($page);
201 $page->assign('secteurs', XDB::iterator("SELECT id, label
202 FROM emploi_secteur"));
203 $page->assign('fonctions', XDB::iterator("SELECT id, fonction_fr, FIND_IN_SET('titre', flags) AS title
204 FROM fonctions_def
205 ORDER BY id"));
206 }
3950bc21
FB
207}
208
209// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
210?>