2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class ProfileSettingDeco
implements ProfileSetting
24 private static function compareMedals(array $a, array $b)
26 if ($a['id'] == $b['id']) {
27 return $a['grade'] > $b['grade'];
29 return $a['id'] > $b['id'];
32 public function value(ProfilePage
$page, $field, $value, &$success)
35 if (is_null($value)) {
36 // Fetch already attributed medals
37 $value = XDB
::fetchAllAssoc('SELECT mid AS id, gid AS grade, 1 AS valid
42 // Fetch not yet validated medals
43 $medals = ProfileValidate
::get_typed_requests($page->pid(), 'medal');
44 foreach ($medals as &$medal) {
47 'grade' => $medal->gid
,
51 } elseif (!is_array($value)) {
54 usort($value, 'self::compareMedals');
58 public function save(ProfilePage
$page, $field, $value)
60 $original =& $page->orig
[$field];
63 $total_original = count($original);
64 $total_value = count($value);
66 while ($i < $total_original ||
$j < $total_value) {
67 if (isset($value[$j]) && (!isset($original[$i]) || self
::compareMedals($original[$i], $value[$j]))) {
68 $req = new MedalReq(S
::user(), $page->profile
, $value[$j]['id'], $value[$j]['grade']);
72 } elseif (isset($original[$i]) && (!isset($value[$j]) || self
::compareMedals($value[$j], $original[$i]))) {
73 if ($original[$i]['valid']) {
74 XDB
::execute('DELETE FROM profile_medals
75 WHERE pid = {?} AND mid = {?} AND gid = {?}',
76 $page->pid(), $original[$i]['id'], $original[$i]['grade']);
78 $req = MedalReq
::get_request($page->pid(), $original[$i]['id'], $original[$i]['grade']);
91 public function getText($value) {
92 $medalsList = DirEnum
::getOptions(DirEnum
::MEDALS
);
94 foreach ($value as $id => $medal) {
95 $medals[] = $medalsList[$id];
97 return implode(', ', $medals);
101 class ProfilePageDecos
extends ProfilePage
103 protected $pg_template = 'profile/deco.tpl';
105 public function __construct(PlWizard
$wiz)
107 parent
::__construct($wiz);
108 $this->settings
['medals'] = new ProfileSettingDeco();
109 $this->settings
['medals_pub'] = new ProfileSettingPub();
110 $this->watched
['medals'] = true
;
113 protected function _fetchData()
115 $res = XDB
::query("SELECT medals_pub
119 $this->values
['medals_pub'] = $res->fetchOneCell();
122 protected function _saveData()
124 if ($this->changed
['medals_pub']) {
125 XDB
::execute("UPDATE profiles
128 $this->values
['medals_pub'], $this->pid());
132 public function _prepare(PlPage
$page, $id)
134 $res = XDB
::iterator('SELECT *, FIND_IN_SET(\'validation\', flags) AS validate
135 FROM profile_medal_enum
136 ORDER BY type, text');
138 while ($tmp = $res->next()) {
139 $mlist[$tmp['type']][] = $tmp;
141 $page->assign('medal_list', $mlist);
145 'militaire' => 'Médailles militaires',
146 'honneur' => 'Médailles d\'honneur',
147 'resistance' => 'Médailles de la résistance',
149 'sport' => 'Médailles sportives'
151 $page->assign('fullType', $fullType);
155 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: