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