Do not display warning about awaiting validation for non validated medals.
[platal.git] / modules / profile / decos.inc.php
CommitLineData
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 22class ProfileSettingDeco implements ProfileSetting
a7c28fff 23{
e18807a8
SJ
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
26ba053e 32 public function value(ProfilePage $page, $field, $value, &$success)
a7c28fff
FB
33 {
34 $success = true;
35 if (is_null($value)) {
36 // Fetch already attributed medals
e18807a8
SJ
37 $value = XDB::fetchAllAssoc('SELECT mid AS id, gid AS grade, 1 AS valid
38 FROM profile_medals
39 WHERE pid = {?}',
40 $page->pid());
a7c28fff
FB
41
42 // Fetch not yet validated medals
024ec1e5 43 $medals = ProfileValidate::get_typed_requests($page->pid(), 'medal');
a7c28fff 44 foreach ($medals as &$medal) {
e18807a8
SJ
45 $value[] = array(
46 'id' => $medal->mid,
47 'grade' => $medal->gid,
48 'valid' => '0'
49 );
a7c28fff 50 }
e18807a8 51 } elseif (!is_array($value)) {
a7c28fff
FB
52 $value = array();
53 }
e18807a8 54 usort($value, 'self::compareMedals');
a7c28fff
FB
55 return $value;
56 }
57
26ba053e 58 public function save(ProfilePage $page, $field, $value)
a7c28fff 59 {
e18807a8
SJ
60 $original =& $page->orig[$field];
61
62 $i = $j = 0;
63 $total_original = count($original);
64 $total_value = count($value);
85cc366b 65
e18807a8
SJ
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']);
a7c28fff 77 } else {
e18807a8 78 $req = MedalReq::get_request($page->pid(), $original[$i]['id'], $original[$i]['grade']);
85cc366b
FB
79 if ($req) {
80 $req->clean();
81 }
a7c28fff 82 }
e18807a8
SJ
83 ++$i;
84 } else {
85 ++$i;
86 ++$j;
a7c28fff
FB
87 }
88 }
89 }
a0fce0c6
SJ
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 }
a7c28fff
FB
99}
100
66c4bdaf 101class ProfilePageDecos extends ProfilePage
a7c28fff
FB
102{
103 protected $pg_template = 'profile/deco.tpl';
104
26ba053e 105 public function __construct(PlWizard $wiz)
a7c28fff
FB
106 {
107 parent::__construct($wiz);
12bcf04b
RB
108 $this->settings['medals'] = new ProfileSettingDeco();
109 $this->settings['medals_pub'] = new ProfileSettingPub();
a2a1c2f2 110 $this->watched['medals'] = true;
a7c28fff
FB
111 }
112
7c2e0f0d 113 protected function _fetchData()
a7c28fff 114 {
e5bcd851
FB
115 $res = XDB::query("SELECT medals_pub
116 FROM profiles
117 WHERE pid = {?}",
118 $this->pid());
a7c28fff 119 $this->values['medals_pub'] = $res->fetchOneCell();
a7c28fff
FB
120 }
121
7c2e0f0d 122 protected function _saveData()
a7c28fff 123 {
a7c28fff 124 if ($this->changed['medals_pub']) {
e5bcd851
FB
125 XDB::execute("UPDATE profiles
126 SET medals_pub = {?}
127 WHERE pid = {?}",
128 $this->values['medals_pub'], $this->pid());
a7c28fff
FB
129 }
130 }
131
26ba053e 132 public function _prepare(PlPage $page, $id)
a7c28fff 133 {
cfcb8b62
SJ
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();
a7c28fff
FB
138 while ($tmp = $res->next()) {
139 $mlist[$tmp['type']][] = $tmp;
140 }
141 $page->assign('medal_list', $mlist);
cfcb8b62
SJ
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);
a7c28fff
FB
152 }
153}
154
155// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
156?>