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