Fix mentor edition page (Closes #764)
[platal.git] / modules / profile / mentor.inc.php
CommitLineData
6457b5e4
FB
1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
6457b5e4
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
22class ProfileSecteurs implements ProfileSetting
23{
24 public function value(ProfilePage &$page, $field, $value, &$success)
25 {
26 $success = true;
27 if (is_null($value)) {
28 $value = array();
29 $res = XDB::iterRow("SELECT m.secteur, m.ss_secteur, ss.label
30 FROM mentor_secteurs AS m
31 INNER JOIN emploi_secteur AS s ON(m.secteur = s.id)
32 INNER JOIN emploi_ss_secteur AS ss ON(s.id = ss.secteur AND m.ss_secteur = ss.id)
33 WHERE m.uid = {?}",
34 S::i('uid'));
35 while (list($s, $ss, $ssname) = $res->next()) {
36 if (!isset($value[$s])) {
37 $value[$s] = array($ss => $ssname);
38 } else {
39 $value[$s][$ss] = $ssname;
40 }
41 }
42 } else if (!is_array($value)) {
43 $value = array();
44 } else if (count($value) > 10) {
45 global $page;
46 $page->trig("Le nombre de secteurs d'expertise est limité à 10");
47 $success = false;
48 }
49 ksort($value);
50 foreach ($value as &$sss) {
51 ksort($sss);
52 }
53 return $value;
54 }
55
56 public function save(ProfilePage &$page, $field, $value)
57 {
58
59 XDB::execute("DELETE FROM mentor_secteurs
60 WHERE uid = {?}",
61 S::i('uid'));
62 if (!count($value)) {
63 return;
64 }
65 foreach ($value as $id=>&$sect) {
66 foreach ($sect as $sid=>&$name) {
67 XDB::execute("INSERT INTO mentor_secteurs (uid, secteur, ss_secteur)
68 VALUES ({?}, {?}, {?})",
69 S::i('uid'), $id, $sid);
70 }
71 }
72 }
73}
74
75class ProfileCountry implements ProfileSetting
76{
77 public function value(ProfilePage &$page, $field, $value, &$success)
78 {
79 $success = true;
80 if (is_null($value)) {
81 $value = array();
82 $res = XDB::iterRow("SELECT m.pid, p.pays
83 FROM mentor_pays AS m
84 INNER JOIN geoloc_pays AS p ON(m.pid = p.a2)
85 WHERE m.uid = {?}",
86 S::i('uid'));
87 while (list($id, $name) = $res->next()) {
88 $value[$id] = $name;
89 }
90 } else if (!is_array($value)) {
91 $value = array();
92 } else if (count($value) > 10) {
93 global $page;
94 $page->trig("Le nombre de secteurs d'expertise est limité à 10");
95 $success = false;
96 }
97 ksort($value);
98 return $value;
99 }
100
101 public function save(ProfilePage &$page, $field, $value)
102 {
103 XDB::execute("DELETE FROM mentor_pays
104 WHERE uid = {?}",
105 S::i('uid'));
106 foreach ($value as $id=>&$name) {
107 XDB::execute("INSERT INTO mentor_pays (uid, pid)
108 VALUES ({?}, {?})",
109 S::i('uid'), $id);
110 }
111 }
112}
113
114
115class ProfileMentor extends ProfilePage
116{
117 protected $pg_template = 'profile/mentor.tpl';
118
119 public function __construct(PlWizard &$wiz)
120 {
121 parent::__construct($wiz);
122 $this->settings['expertise'] = null;
123 $this->settings['secteurs'] = new ProfileSecteurs();
124 $this->settings['countries'] = new ProfileCountry();
125 }
126
7c2e0f0d 127 protected function _fetchData()
6457b5e4 128 {
6457b5e4
FB
129 $res = XDB::query("SELECT expertise
130 FROM mentor
131 WHERE uid = {?}",
132 S::i('uid'));
e21e0b11 133 $this->values['expertise'] = $res->fetchOneCell();
6457b5e4
FB
134 }
135
7c2e0f0d 136 protected function _saveData()
6457b5e4 137 {
6457b5e4
FB
138 if ($this->changed['expertise']) {
139 XDB::execute("REPLACE INTO mentor (uid, expertise)
140 VALUES ({?}, {?})",
141 S::i('uid'), $this->values['expertise']);
142 }
143 }
144
7c2e0f0d 145 public function _prepare(PlatalPage &$page, $id)
6457b5e4 146 {
6457b5e4
FB
147 $page->assign('secteurs_sel', XDB::iterator("SELECT id, label
148 FROM emploi_secteur"));
149 }
150}
151
152// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
153?>