Start working on the medals page
[platal.git] / modules / profile / decos.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 ProfileDeco implements ProfileSetting
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
30 FROM profile_medals_sub AS s
31 INNER JOIN profile_medals AS m ON ( s.mid = m.id )
32 WHERE s.uid = {?}",
33 S::i('uid'));
34 $value = array();
35 while (list($id, $grade) = $res->next()) {
36 $value[$id] = array('grade' => $grade,
37 'valid' => true);
38 }
39
40 // Fetch not yet validated medals
41 require_once('validations.inc.php');
42 $medals = Validate::get_typed_requests(S::i('uid'), 'medal');
43 foreach ($medals as &$medal) {
44 $medals[$medal->mid] = array('grade' => $medal->gid,
45 'valid' => false);
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 {
56 require_once('validations.inc.php');
57
58 // Remove old ones
59 foreach ($page->orig as $id=>&$val) {
60 if (!isset($value[$id]) || $val['grade'] != $value[$id]['grade']) {
61 if ($val['valid']) {
62 XDB::execute("DELETE FROM profile_medals_sub
63 WHERE uid = {?} AND id = {?}",
64 S::i('uid'), $id);
65 } else {
66 $req = Validate::get_typed_request(S::i('uid'), 'medal', $id);
67 $req->clean();
68 }
69 }
70 }
71
72 // Add new ones
73 foreach ($value as $id=>&$val) {
74 if (!isset($this->orig[$id]) || $this->orig[$id]['grade'] != $val['grade']) {
75 $req = new MedalReq(S::i('uid'), $id, $val['grade']);
76 $req->submit();
77 }
78 }
79 }
80 }
81
82 class ProfileDecos extends ProfilePage
83 {
84 protected $pg_template = 'profile/deco.tpl';
85
86 public function __construct(PlWizard &$wiz)
87 {
88 parent::__construct($wiz);
89 $this->settings['medals'] = new ProfileDeco();
90 $this->settings['medals_pub'] = new ProfilePub();
91 }
92
93 protected function fetchData()
94 {
95 $res = XDB::query("SELECT profile_medals_pub
96 FROM auth_user_quick
97 WHERE user_id = {?}",
98 S::i('uid'));
99 $this->values['medals_pub'] = $res->fetchOneCell();
100 parent::fetchData();
101 }
102
103 protected function saveData()
104 {
105 parent::saveData();
106 if ($this->changed['medals_pub']) {
107 XDB::execute("UPDATE auth_user_quick
108 SET profile_medals_pub = {?}
109 WHERE user_id = {?}",
110 $this->values['medals_pub'], S::i('uid'));
111 }
112 }
113
114 public function prepare(PlatalPage &$page)
115 {
116 parent::prepare($page);
117 $res = XDB::iterator("SELECT *
118 FROM profile_medals_grades
119 ORDER BY mid, pos");
120 $grades = array();
121 while ($tmp = $res->next()) {
122 $grades[$tmp['mid']][] = $tmp;
123 }
124 $page->assign('grades', $grades);
125
126 $res = XDB::iterator("SELECT *, FIND_IN_SET('validation', flags) AS validate
127 FROM profile_medals
128 ORDER BY type, text");
129 $mlist = array();
130 while ($tmp = $res->next()) {
131 $mlist[$tmp['type']][] = $tmp;
132 }
133 $page->assign('medal_list', $mlist);
134
135 $trad = Array('ordre' => 'Ordres',
136 'croix' => 'Croix',
137 'militaire' => 'Médailles militaires',
138 'honneur' => 'Médailles d\'honneur',
139 'resistance' => 'Médailles de la résistance',
140 'prix' => 'Prix');
141 $page->assign('trad', $trad);
142 }
143 }
144
145 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
146 ?>