Commit | Line | Data |
---|---|---|
a7c28fff FB |
1 | <?php |
2 | /*************************************************************************** | |
9f5bd98e | 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
a7c28fff 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 | ||
12bcf04b | 22 | class ProfileSettingDeco implements ProfileSetting |
a7c28fff FB |
23 | { |
24 | public function value(ProfilePage &$page, $field, $value, &$success) | |
25 | { | |
26 | $success = true; | |
27 | if (is_null($value)) { | |
28 | // Fetch already attributed medals | |
29 | $res = XDB::iterRow("SELECT m.id AS id, s.gid AS grade | |
5c8a71f2 FB |
30 | FROM profile_medals AS s |
31 | INNER JOIN profile_medal_enum AS m ON ( s.mid = m.id ) | |
bdd977d7 | 32 | WHERE s.pid = {?}", |
e5bcd851 | 33 | $page->pid()); |
a7c28fff FB |
34 | $value = array(); |
35 | while (list($id, $grade) = $res->next()) { | |
46ae38a9 | 36 | $value[$id] = array('grade' => $grade, |
85cc366b | 37 | 'valid' => '1'); |
a7c28fff FB |
38 | } |
39 | ||
40 | // Fetch not yet validated medals | |
024ec1e5 SJ |
41 | require_once 'validations.inc.php'; |
42 | $medals = ProfileValidate::get_typed_requests($page->pid(), 'medal'); | |
a7c28fff | 43 | foreach ($medals as &$medal) { |
85cc366b FB |
44 | $value[$medal->mid] = array('grade' => $medal->gid, |
45 | 'valid' => '0'); | |
a7c28fff FB |
46 | } |
47 | } else if (!is_array($value)) { | |
48 | $value = array(); | |
49 | } | |
50 | ksort($value); | |
51 | return $value; | |
52 | } | |
53 | ||
54 | public function save(ProfilePage &$page, $field, $value) | |
55 | { | |
024ec1e5 | 56 | require_once 'validations.inc.php'; |
a7c28fff | 57 | |
85cc366b FB |
58 | $orig =& $page->orig[$field]; |
59 | ||
a7c28fff | 60 | // Remove old ones |
85cc366b | 61 | foreach ($orig as $id=>&$val) { |
a7c28fff FB |
62 | if (!isset($value[$id]) || $val['grade'] != $value[$id]['grade']) { |
63 | if ($val['valid']) { | |
5c8a71f2 | 64 | XDB::execute("DELETE FROM profile_medals |
bdd977d7 | 65 | WHERE pid = {?} AND mid = {?}", |
e5bcd851 | 66 | $page->pid(), $id); |
a7c28fff | 67 | } else { |
024ec1e5 | 68 | $req = MedalReq::get_request($page->pid(), $id); |
85cc366b FB |
69 | if ($req) { |
70 | $req->clean(); | |
71 | } | |
a7c28fff FB |
72 | } |
73 | } | |
74 | } | |
75 | ||
76 | // Add new ones | |
77 | foreach ($value as $id=>&$val) { | |
85cc366b | 78 | if (!isset($orig[$id]) || $orig[$id]['grade'] != $val['grade']) { |
024ec1e5 | 79 | $req = new MedalReq(S::user(), $page->profile, $id, $val['grade']); |
a7c28fff | 80 | $req->submit(); |
a7e50950 | 81 | sleep(1); |
a7c28fff FB |
82 | } |
83 | } | |
84 | } | |
a0fce0c6 SJ |
85 | |
86 | public function getText($value) { | |
87 | $medalsList = DirEnum::getOptions(DirEnum::MEDALS); | |
88 | $medals = array(); | |
89 | foreach ($value as $id => $medal) { | |
90 | $medals[] = $medalsList[$id]; | |
91 | } | |
92 | return implode(', ', $medals); | |
93 | } | |
a7c28fff FB |
94 | } |
95 | ||
12bcf04b | 96 | class ProfileSettingDecos extends ProfilePage |
a7c28fff FB |
97 | { |
98 | protected $pg_template = 'profile/deco.tpl'; | |
99 | ||
100 | public function __construct(PlWizard &$wiz) | |
101 | { | |
102 | parent::__construct($wiz); | |
12bcf04b RB |
103 | $this->settings['medals'] = new ProfileSettingDeco(); |
104 | $this->settings['medals_pub'] = new ProfileSettingPub(); | |
a2a1c2f2 | 105 | $this->watched['medals'] = true; |
a7c28fff FB |
106 | } |
107 | ||
7c2e0f0d | 108 | protected function _fetchData() |
a7c28fff | 109 | { |
e5bcd851 FB |
110 | $res = XDB::query("SELECT medals_pub |
111 | FROM profiles | |
112 | WHERE pid = {?}", | |
113 | $this->pid()); | |
a7c28fff | 114 | $this->values['medals_pub'] = $res->fetchOneCell(); |
a7c28fff FB |
115 | } |
116 | ||
7c2e0f0d | 117 | protected function _saveData() |
a7c28fff | 118 | { |
a7c28fff | 119 | if ($this->changed['medals_pub']) { |
e5bcd851 FB |
120 | XDB::execute("UPDATE profiles |
121 | SET medals_pub = {?} | |
122 | WHERE pid = {?}", | |
123 | $this->values['medals_pub'], $this->pid()); | |
a7c28fff FB |
124 | } |
125 | } | |
126 | ||
04334c61 | 127 | public function _prepare(PlPage &$page, $id) |
a7c28fff | 128 | { |
cfcb8b62 SJ |
129 | $res = XDB::iterator('SELECT *, FIND_IN_SET(\'validation\', flags) AS validate |
130 | FROM profile_medal_enum | |
131 | ORDER BY type, text'); | |
132 | $mlist = array(); | |
a7c28fff FB |
133 | while ($tmp = $res->next()) { |
134 | $mlist[$tmp['type']][] = $tmp; | |
135 | } | |
136 | $page->assign('medal_list', $mlist); | |
cfcb8b62 SJ |
137 | $fullType = array( |
138 | 'ordre' => 'Ordres', | |
139 | 'croix' => 'Croix', | |
140 | 'militaire' => 'Médailles militaires', | |
141 | 'honneur' => 'Médailles d\'honneur', | |
142 | 'resistance' => 'Médailles de la résistance', | |
143 | 'prix' => 'Prix', | |
144 | 'sport' => 'Médailles sportives' | |
145 | ); | |
146 | $page->assign('fullType', $fullType); | |
a7c28fff FB |
147 | } |
148 | } | |
149 | ||
150 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
151 | ?> |