Merge commit 'origin/fusionax' into account
[platal.git] / modules / profile / page.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 interface ProfileSetting
23 {
24 /** Get a field and a value, check that the given value is
25 * valid, if not, return a corrected value. If no valid value can be
26 * computed from the input data, the success flag is set to false.
27 *
28 * If value is null, the default value should be returned.
29 * TODO: check this does not conflict with some possible values.
30 *
31 * Whatever happen, this function must always returns the function to
32 * show on the page to the user.
33 */
34 public function value(ProfilePage &$page, $field, $value, &$success);
35
36 /** Save the new value for the given field.
37 */
38 public function save(ProfilePage &$page, $field, $new_value);
39 }
40
41 abstract class ProfileNoSave implements ProfileSetting
42 {
43 public function save(ProfilePage &$page, $field, $new_value) { }
44 }
45
46 class ProfileWeb extends ProfileNoSave
47 {
48 public function value(ProfilePage &$page, $field, $value, &$success)
49 {
50 if (is_null($value)) {
51 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
52 }
53 $value = trim($value);
54 $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
55 if (!$success) {
56 Platal::page()->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
57 . ' et ne pas contenir de caractères interdits');
58 }
59 return $value;
60 }
61 }
62
63 class ProfileEmail extends ProfileNoSave
64 {
65 public function value(ProfilePage &$page, $field, $value, &$success)
66 {
67 if (is_null($value)) {
68 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
69 }
70 $value = trim($value);
71 $success = empty($value) || isvalid_email($value);
72 if (!$success) {
73 Platal::page()->trigError('Adresse Email invalide');
74 }
75 return $value;
76 }
77 }
78
79 class ProfileNumber extends ProfileNoSave
80 {
81 public function value(ProfilePage &$page, $field, $value, &$success)
82 {
83 if (is_null($value)) {
84 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
85 }
86 $value = trim($value);
87 $success = empty($value) || is_numeric($value);
88 if (!$success) {
89 Platal::page()->trigError('Numéro invalide');
90 }
91 return $value;
92 }
93 }
94
95
96 class ProfileTel extends ProfileNoSave
97 {
98 public function value(ProfilePage &$page, $field, $value, &$success)
99 {
100 if (is_null($value)) {
101 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
102 }
103 require_once('profil.func.inc.php');
104 $value = format_phone_number($value);
105 if($value == '') {
106 $success = true;
107 return $value;
108 }
109 $value = format_display_number($value,$error);
110 $success = !$error;
111 if (!$success) {
112 Platal::page()->trigError('Le préfixe international du numéro de téléphone est inconnu. ');
113 }
114 return $value;
115 }
116 }
117
118 class ProfilePhones implements ProfileSetting
119 {
120 private $tel;
121 private $pub;
122 protected $link_type;
123 protected $link_id;
124
125 public function __construct($type, $link_id)
126 {
127 $this->tel = new ProfileTel();
128 $this->pub = new ProfilePub();
129 $this->link_type = $type;
130 $this->link_id = $link_id;
131 }
132
133 public function value(ProfilePage &$page, $field, $value, &$success)
134 {
135 $success = true;
136 if (is_null($value) || !is_array($value)) {
137 $value = array();
138 $res = XDB::iterator("SELECT t.display_tel AS tel, t.tel_type AS type, t.pub, t.comment
139 FROM profile_phones AS t
140 WHERE t.uid = {?} AND t.link_type = {?}
141 ORDER BY t.tel_id",
142 $page->pid(), $this->link_type);
143 $value = $res->fetchAllAssoc();
144 }
145 foreach ($value as $key=>&$phone) {
146 if (@$phone['removed']) {
147 unset($value[$key]);
148 } else {
149 unset($phone['removed']);
150 $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s);
151 $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s);
152 if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
153 $phone['type'] = 'fixed';
154 $s = false;
155 }
156 if (!$s) {
157 $phone['error'] = true;
158 $success = false;
159 }
160 if (!isset($phone['comment'])) {
161 $phone['comment'] = '';
162 }
163 }
164 }
165 return $value;
166 }
167
168 private function saveTel($pid, $telid, array &$phone)
169 {
170 if ($phone['tel'] != '') {
171 XDB::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type,
172 search_tel, display_tel, pub, comment)
173 VALUES ({?}, {?}, {?}, {?}, {?},
174 {?}, {?}, {?}, {?})",
175 $pid, $this->link_type, $this->link_id, $telid, $phone['type'],
176 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
177 }
178 }
179
180 public function save(ProfilePage &$page, $field, $value)
181 {
182 XDB::execute("DELETE FROM profile_phones
183 WHERE uid = {?} AND link_type = {?} AND link_id = {?}",
184 $page->pid(), $this->link_type, $this->link_id);
185 $this->saveTels($page->pid(), $field, $value);
186 }
187
188 //Only saves phones without a delete operation
189 public function saveTels($pid, $field, $value)
190 {
191 foreach ($value as $telid=>&$phone) {
192 $this->saveTel($pid, $telid, $phone);
193 }
194 }
195 }
196
197 class ProfilePub extends ProfileNoSave
198 {
199 public function value(ProfilePage &$page, $field, $value, &$success)
200 {
201 $success = true;
202 if (is_null($value)) {
203 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
204 }
205 if (!$value) {
206 $value = 'private';
207 } elseif ($value == 'on') { // Checkbox
208 $value = 'public';
209 }
210 return $value;
211 }
212 }
213
214 class ProfileBool extends ProfileNoSave
215 {
216 public function value(ProfilePage &$page, $field, $value, &$success)
217 {
218 $success = true;
219 if (is_null($value)) {
220 $value = isset($page->values[$field]) ? $page->values[$field] : null;
221 }
222 return $value ? "1" : "";
223 }
224 }
225
226 class ProfileDate extends ProfileNoSave
227 {
228 public function value(ProfilePage &$page, $field, $value, &$success)
229 {
230 $success = true;
231 if (is_null($value)) {
232 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
233 } else {
234 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
235 if (!$success) {
236 Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
237 } else {
238 $day = (int)$matches[1];
239 $month = (int)$matches[2];
240 $year = (int)$matches[3];
241 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
242 if (!$success) {
243 Platal::page()->trigError("La date n'a pas une valeur valide");
244 }
245 }
246 }
247 return $value;
248 }
249 }
250
251 abstract class ProfileGeocoding implements ProfileSetting
252 {
253 protected function geocodeAddress(array &$address, &$success)
254 {
255 require_once 'geocoding.inc.php';
256 $success = true;
257 if ($address['changed'] == 1) {
258 $gmapsGeocoder = new GMapsGeocoder();
259 $address = $gmapsGeocoder->getGeocodedAddress($address);
260 if (isset($address['geoloc'])) {
261 $success = false;
262 }
263 }
264 if (isset($address['geoloc_choice']) && ($address['geoloc_choice'] == 0)) {
265 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
266 $mailer->assign('text', $address['text']);
267 $mailer->assign('geoloc', $address['geoloc']);
268 $mailer->send();
269 $gmapsGeocoder = new GMapsGeocoder();
270 $address = $gmapsGeocoder->stripGeocodingFromAddress($address);
271 }
272 }
273 }
274
275
276 abstract class ProfilePage implements PlWizardPage
277 {
278 protected $wizard;
279 protected $pg_template;
280 protected $settings = array(); // A set ProfileSetting objects
281 protected $errors = array(); // A set of boolean with the value check errors
282 protected $changed = array(); // A set of boolean indicating wether the value has been changed
283 protected $watched = array(); // A set of boolean indicating the fields that are watched
284
285 public $orig = array();
286 public $values = array();
287 public $profile = null;
288 public $owner = null;
289
290 public function __construct(PlWizard &$wiz)
291 {
292 $this->wizard =& $wiz;
293 $this->profile = $this->wizard->getUserData('profile');
294 $this->owner = $this->wizard->getUserData('owner');
295 }
296
297 protected function _fetchData()
298 {
299 }
300
301 protected function fetchData()
302 {
303 if (count($this->orig) > 0) {
304 $this->values = $this->orig;
305 return;
306 }
307
308 $this->_fetchData();
309 foreach ($this->settings as $field=>&$setting) {
310 $success = false;
311 if (!is_null($setting)) {
312 $this->values[$field] = $setting->value($this, $field, null, $success);
313 } else if (!isset($this->values[$field])) {
314 $this->values[$field] = S::v($field);
315 }
316 $this->errors[$field] = false;
317 }
318 $this->orig = $this->values;
319 }
320
321 protected function _saveData()
322 {
323 }
324
325 protected function saveData()
326 {
327 require_once 'notifs.inc.php';
328 foreach ($this->settings as $field=>&$setting) {
329 if (!is_null($setting) && $this->changed[$field]) {
330 $setting->save($this, $field, $this->values[$field]);
331 }
332 if ($this->changed[$field] && @$this->watched[$field]) {
333 WatchProfileUpdate::register($this->profile, $field);
334 }
335 }
336 $this->_saveData();
337
338 // Update the last modification date
339 XDB::execute('UPDATE profiles
340 SET last_change = NOW()
341 WHERE pid = {?}', $this->pid());
342 global $platal;
343 S::logger()->log('profil', $platal->pl_self(2));
344 }
345
346 protected function checkChanges()
347 {
348 $newvalues = $this->values;
349 $this->values = array();
350 $this->fetchData();
351 $this->values = $newvalues;
352 $changes = false;
353 foreach ($this->settings as $field=>&$setting) {
354 if ($this->orig[$field] != $this->values[$field]) {
355 $this->changed[$field] = true;
356 $changes = true;
357 } else {
358 $this->changed[$field] = false;
359 }
360 }
361 return $changes;
362 }
363
364 protected function markChange()
365 {
366 }
367
368 public function template()
369 {
370 return 'profile/base.tpl';
371 }
372
373 public function pid()
374 {
375 return $this->profile->id();
376 }
377
378 public function hrpid()
379 {
380 return $this->profile->hrpid();
381 }
382
383 protected function _prepare(PlPage &$page, $id)
384 {
385 }
386
387 public function prepare(PlPage &$page, $id)
388 {
389 if (count($this->values) == 0) {
390 $this->fetchData();
391 }
392 foreach ($this->values as $field=>&$value) {
393 $page->assign($field, $value);
394 }
395 $this->_prepare($page, $id);
396 $page->assign('profile', $this->profile);
397 $page->assign('owner', $this->owner);
398 $page->assign('profile_page', $this->pg_template);
399 $page->assign('errors', $this->errors);
400 }
401
402 public function process()
403 {
404 $global_success = true;
405 $this->fetchData();
406 foreach ($this->settings as $field=>&$setting) {
407 $success = false;
408 if (!is_null($setting)) {
409 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
410 } else {
411 $success = true;
412 $this->values[$field] = Post::v($field, '');
413 }
414 $this->errors[$field] = !$success;
415 $global_success = $global_success && $success;
416 }
417 if ($global_success) {
418 if ($this->checkChanges()) {
419 $this->saveData();
420 $this->markChange();
421 }
422 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
423 }
424 Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
425 . "de ton profil et de revalider ta demande");
426 return PlWizard::CURRENT_PAGE;
427 }
428 }
429
430 require_once dirname(__FILE__) . '/general.inc.php';
431 require_once dirname(__FILE__) . '/addresses.inc.php';
432 require_once dirname(__FILE__) . '/groups.inc.php';
433 require_once dirname(__FILE__) . '/decos.inc.php';
434 require_once dirname(__FILE__) . '/jobs.inc.php';
435 require_once dirname(__FILE__) . '/skills.inc.php';
436 require_once dirname(__FILE__) . '/mentor.inc.php';
437
438 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
439 ?>