Integrates the AX emails into the marketing emails, implements an email combobox...
[platal.git] / modules / profile / general.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 ProfileNom implements ProfileSetting
23 {
24 private function matchWord($old, $new, $newLen) {
25 return ($i = strpos($old, $new)) !== false
26 && ($i == 0 || $old{$i-1} == ' ')
27 && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' ');
28 }
29
30 private function prepareField($value)
31 {
32 $value = strtoupper(replace_accent($value));
33 return preg_replace('/[^A-Z]/', ' ', $value);
34 }
35
36 public function value(ProfilePage &$page, $field, $value, &$success)
37 {
38 $success = true;
39 $current = S::v($field);
40 $init = S::v($field . '_ini');
41 if (is_null($value)) {
42 return $current;
43 }
44 if ($value == $current || $value == $init) {
45 return $value;
46 }
47 $ini = $this->prepareField($init);
48 $old = $this->prepareField($current);
49 $new = $this->prepareField($value);
50 $newLen = strlen($new);
51 $success = $this->matchWord($old, $new, $newLen)
52 || $this->matchWord($ini, $new, $newLen)
53 || ($field == 'nom' && $new == 'DE ' . $old);
54 if (!$success) {
55 global $page;
56 $page->trigError("Le $field que tu as choisi ($value) est trop loin de ton $field initial ($init)"
57 . (($init == $current)? "" : " et de ton prénom précédent ($current)"));
58 }
59 return $success ? $value : $current;
60 }
61
62 public function save(ProfilePage &$page, $field, $new_value)
63 {
64 $_SESSION[$field] = $new_value;
65 }
66 }
67
68 class ProfileSearchName implements ProfileSetting
69 {
70
71 public function __construct()
72 {
73 }
74
75 public function value(ProfilePage &$page, $field, $value, &$success)
76 {
77 }
78
79 public function save(ProfilePage &$page, $field, $new_value)
80 {
81 }
82 }
83
84 class ProfileAppli implements ProfileSetting
85 {
86 public function value(ProfilePage &$page, $field, $value, &$success)
87 {
88 $success = true;
89 if (is_null($value)) {
90 return $page->values[$field];
91 }
92 return $value;
93 }
94
95 public function save(ProfilePage &$page, $field, $new_value)
96 {
97 $index = ($field == 'appli1' ? 0 : 1);
98 if ($new_value['id'] > 0) {
99 XDB::execute("REPLACE INTO applis_ins
100 SET uid = {?}, aid = {?}, type = {?}, ordre = {?}",
101 S::i('uid'), $new_value['id'], $new_value['type'], $index);
102 } else {
103 XDB::execute("DELETE FROM applis_ins
104 WHERE uid = {?} AND ordre = {?}",
105 S::i('uid'), $index);
106 }
107 }
108 }
109
110 class ProfileEmailDirectory implements ProfileSetting
111 {
112 private $email;
113
114 public function __construct()
115 {
116 }
117
118 public function value(ProfilePage &$page, $field, $value, &$success)
119 {
120 $p = $page;
121 global $page;
122
123 $success = true;
124 if (!is_null($value)) {
125 $email_stripped = strtolower(trim($value));
126 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($p->values['email_directory'] == "new@new.new")) {
127 $page->assign('email_error', '1');
128 $page->assign('email_directory_error', $email_stripped);
129 $page->trigError('Adresse Email invalide');
130 $success = false;
131 } else {
132 $page->assign('email_error', '0');
133 }
134 }
135 return $value;
136 }
137
138 public function save(ProfilePage &$page, $field, $value)
139 {
140 }
141 }
142
143 class ProfileNetworking implements ProfileSetting
144 {
145 private $email;
146 private $pub;
147 private $web;
148 private $number;
149
150 public function __construct()
151 {
152 $this->email = new ProfileEmail();
153 $this->pub = new ProfilePub();
154 $this->web = new ProfileWeb();
155 $this->number = new ProfileNumber();
156 }
157
158 public function value(ProfilePage &$page, $field, $value, &$success)
159 {
160 if (is_null($value)) {
161 $value = array();
162 $res = XDB::iterator("SELECT n.address, n.network_type AS type, n.pub, m.name
163 FROM profile_networking AS n
164 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
165 WHERE n.uid = {?}",
166 S::i('uid'));
167 while($network = $res->next()) {
168 $value[] = $network;
169 }
170 }
171 if (!is_array($value)) {
172 $value = array();
173 }
174 $res = XDB::iterator("SELECT filter, network_type AS type
175 FROM profile_networking_enum;");
176 $filters = array();
177 while($filter = $res->next()) {
178 $filters[$filter['type']] = $filter['filter'];
179 }
180 $success = true;
181 foreach($value as $i=>&$network) {
182 if(!isset($network['pub'])) {
183 $network['pub'] = 'private';
184 }
185 $network['error'] = false;
186 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
187 $s = true;
188 if ($filters[$network['type']] == 'web') {
189 $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
190 } elseif ($filters[$network['type']] == 'email') {
191 $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
192 } elseif ($filters[$network['type']] == 'number') {
193 $network['address'] = $this->number->value($page, 'address', $network['address'], $s);
194 }
195 if (!$s) {
196 $success = false;
197 $network['error'] = true;
198 }
199 }
200 return $value;
201 }
202
203 public function save(ProfilePage &$page, $field, $value)
204 {
205 XDB::execute("DELETE FROM profile_networking
206 WHERE uid = {?}",
207 S::i('uid'));
208 if (!count($value)) {
209 return;
210 }
211 $insert = array();
212 foreach ($value as $id=>$network) {
213 XDB::execute("INSERT INTO profile_networking (uid, nwid, network_type, address, pub)
214 VALUES ({?}, {?}, {?}, {?}, {?})",
215 S::i('uid'), $id, $network['type'], $network['address'], $network['pub']);
216 }
217 }
218 }
219
220 class ProfileGeneral extends ProfilePage
221 {
222 protected $pg_template = 'profile/general.tpl';
223
224 public function __construct(PlWizard &$wiz)
225 {
226 parent::__construct($wiz);
227 $this->settings['nom'] = $this->settings['prenom']
228 = new ProfileNom();
229 $this->settings['naissance'] = new ProfileDate();
230 $this->settings['mobile_pub']
231 = $this->settings['freetext_pub']
232 = $this->settings['photo_pub']
233 = new ProfilePub();
234 $this->settings['freetext']
235 = $this->settings['nationalite']
236 = $this->settings['nick']
237 = $this->settings['yourself']
238 = $this->settings['display_name']
239 = $this->settings['sort_name']
240 = $this->settings['tooltip_name']
241 = null;
242 $this->settings['synchro_ax']
243 = new ProfileBool();
244 $this->settings['mobile'] = new ProfileTel();
245 $this->settings['email_directory']
246 = new ProfileEmail();
247 $this->settings['email_directory_new']
248 = new ProfileEmailDirectory();
249 $this->settings['networking'] = new ProfileNetworking();
250 $this->settings['appli1']
251 = $this->settings['appli2']
252 = new ProfileAppli();
253 $this->watched= array('nom' => true, 'freetext' => true, 'mobile' => true,
254 'networking' => true, 'appli1' => true, 'appli2' => true,
255 'nationalite' => true, 'nick' => true);
256 }
257
258 protected function _fetchData()
259 {
260 // Checkout all data...
261 $res = XDB::query("SELECT u.promo, u.promo_sortie, u.nom_usage, u.nationalite, u.naissance,
262 q.profile_mobile as mobile, q.profile_mobile_pub as mobile_pub,
263 d.email_directory as email_directory,
264 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
265 q.profile_nick as nick, q.profile_from_ax as synchro_ax, u.matricule_ax,
266 IF(a1.aid IS NULL, -1, a1.aid) as appli_id1, a1.type as appli_type1,
267 IF(a2.aid IS NULL, -1, a2.aid) as appli_id2, a2.type as appli_type2,
268 n.yourself, n.display AS display_name, n.sort AS sort_name,
269 n.tooltip AS tooltip_name
270 FROM auth_user_md5 AS u
271 INNER JOIN auth_user_quick AS q ON(u.user_id = q.user_id)
272 INNER JOIN profile_names_display AS n ON(n.user_id = u.user_id)
273 LEFT JOIN profile_directory AS d ON(d.uid = u.user_id)
274 LEFT JOIN applis_ins AS a1 ON(a1.uid = u.user_id and a1.ordre = 0)
275 LEFT JOIN applis_ins AS a2 ON(a2.uid = u.user_id and a2.ordre = 1)
276 WHERE u.user_id = {?}", S::v('uid', -1));
277 $this->values = $res->fetchOneAssoc();
278
279 // Reformat formation data
280 $this->values['appli1'] = array('id' => $this->values['appli_id1'],
281 'type' => $this->values['appli_type1']);
282 unset($this->values['appli_id1']);
283 unset($this->values['appli_type1']);
284 $this->values['appli2'] = array('id' => $this->values['appli_id2'],
285 'type' => $this->values['appli_type2']);
286 unset($this->values['appli_id2']);
287 unset($this->values['appli_type2']);
288
289 // Retreive photo informations
290 $res = XDB::query("SELECT pub
291 FROM photo
292 WHERE uid = {?}", S::v('uid'));
293 $this->values['photo_pub'] = $res->fetchOneCell();
294
295 $res = XDB::query("SELECT COUNT(*)
296 FROM requests
297 WHERE type='photo' AND user_id = {?}",
298 S::v('uid'));
299 $this->values['nouvellephoto'] = $res->fetchOneCell();
300
301 // Retreive search names info
302 $this->values['search_names'] = XDB::iterator("
303 SELECT sn.search_name, sn.name_type, sn.pub, sn.sn_id
304 FROM profile_names_search AS sn
305 WHERE sn.user_id = {?}
306 ORDER BY sn.name_type, search_score, search_name",
307 S::v('uid'));
308 }
309
310 protected function _saveData()
311 {
312 if ($this->changed['nationalite'] || $this->changed['nom'] || $this->changed['prenom']
313 || $this->changed['naissance']) {
314 XDB::execute("UPDATE auth_user_md5
315 SET nationalite = {?}, nom={?}, prenom={?}, naissance={?}
316 WHERE user_id = {?}",
317 $this->values['nationalite'], $this->values['nom'], $this->values['prenom'],
318 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values['naissance']),
319 S::v('uid'));
320 }
321 if ($this->changed['nick'] || $this->changed['mobile'] || $this->changed['mobile_pub']
322 || $this->changed['freetext']
323 || $this->changed['freetext_pub'] || $this->changed['synchro_ax']) {
324 XDB::execute("UPDATE auth_user_quick
325 SET profile_nick= {?}, profile_mobile={?}, profile_mobile_pub={?},
326 profile_freetext={?},
327 profile_freetext_pub={?}, profile_from_ax = {?}
328 WHERE user_id = {?}",
329 $this->values['nick'], $this->values['mobile'], $this->values['mobile_pub'],
330 $this->values['freetext'], $this->values['freetext_pub'],
331 $this->values['synchro_ax'], S::v('uid'));
332 }
333 if ($this->changed['email_directory']) {
334 $new_email = ($this->values['email_directory'] == "new@new.new") ?
335 $this->values['email_directory_new'] : $this->values['email_directory'];
336 XDB::execute("REPLACE INTO profile_directory (uid, email_directory)
337 VALUES ({?}, {?})",
338 S::v('uid'), $new_email);
339 }
340 if ($this->changed['nick']) {
341 require_once('user.func.inc.php');
342 user_reindex(S::v('uid'));
343 }
344 if ($this->changed['photo_pub']) {
345 XDB::execute("UPDATE photo
346 SET pub = {?}
347 WHERE uid = {?}",
348 $this->values['photo_pub'], S::v('uid'));
349 }
350 if ($this->changed['yourself'] || $this->changed['sort_name'] ||
351 $this-> changed['display_name'] || $this->changed['tooltip_name']) {
352 XDB::execute("UPDATE profile_names_display AS n
353 SET n.yourself = {?},
354 n.sort = {?}, ". // SET
355 "n.display = {?}, ". // SET
356 "n.tooltip = {?} ". // SET
357 "WHERE n.user_id = {?}",
358 $this->values['yourself'],
359 $this->values['sort_name'],
360 $this->values['display_name'],
361 $this->values['tooltip_name'],
362 S::v('uid'));
363 }
364 }
365
366 public function _prepare(PlatalPage &$page, $id)
367 {
368 require_once "applis.func.inc.php";
369
370 require_once "emails.combobox.inc.php";
371 fill_email_combobox($page);
372
373 $res = XDB::iterator("SELECT nw.network_type AS type, nw.name
374 FROM profile_networking_enum AS nw
375 ORDER BY name");
376 $page->assign('network_list', $res->fetchAllAssoc());
377 }
378 }
379
380 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
381 ?>