1c914b5609889a2514b990c0f6ea8c21424eb534
[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 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 AS s
31 INNER JOIN profile_medal_enum AS m ON ( s.mid = m.id )
32 WHERE s.pid = {?}",
33 $page->pid());
34 $value = array();
35 while (list($id, $grade) = $res->next()) {
36 $value[$id] = array('grade' => $grade,
37 'valid' => '1');
38 }
39
40 // Fetch not yet validated medals
41 $medals = ProfileValidate::get_typed_requests($page->pid(), 'medal');
42 foreach ($medals as &$medal) {
43 $value[$medal->mid] = array('grade' => $medal->gid,
44 'valid' => '0');
45 }
46 } else if (!is_array($value)) {
47 $value = array();
48 }
49 ksort($value);
50 return $value;
51 }
52
53 public function save(ProfilePage &$page, $field, $value)
54 {
55 $orig =& $page->orig[$field];
56
57 // Remove old ones
58 foreach ($orig as $id=>&$val) {
59 if (!isset($value[$id]) || $val['grade'] != $value[$id]['grade']) {
60 if ($val['valid']) {
61 XDB::execute("DELETE FROM profile_medals
62 WHERE pid = {?} AND mid = {?}",
63 $page->pid(), $id);
64 } else {
65 $req = MedalReq::get_request($page->pid(), $id);
66 if ($req) {
67 $req->clean();
68 }
69 }
70 }
71 }
72
73 // Add new ones
74 foreach ($value as $id=>&$val) {
75 if (!isset($orig[$id]) || $orig[$id]['grade'] != $val['grade']) {
76 $req = new MedalReq(S::user(), $page->profile, $id, $val['grade']);
77 $req->submit();
78 sleep(1);
79 }
80 }
81 }
82
83 public function getText($value) {
84 $medalsList = DirEnum::getOptions(DirEnum::MEDALS);
85 $medals = array();
86 foreach ($value as $id => $medal) {
87 $medals[] = $medalsList[$id];
88 }
89 return implode(', ', $medals);
90 }
91 }
92
93 class ProfilePageDecos extends ProfilePage
94 {
95 protected $pg_template = 'profile/deco.tpl';
96
97 public function __construct(PlWizard &$wiz)
98 {
99 parent::__construct($wiz);
100 $this->settings['medals'] = new ProfileSettingDeco();
101 $this->settings['medals_pub'] = new ProfileSettingPub();
102 $this->watched['medals'] = true;
103 }
104
105 protected function _fetchData()
106 {
107 $res = XDB::query("SELECT medals_pub
108 FROM profiles
109 WHERE pid = {?}",
110 $this->pid());
111 $this->values['medals_pub'] = $res->fetchOneCell();
112 }
113
114 protected function _saveData()
115 {
116 if ($this->changed['medals_pub']) {
117 XDB::execute("UPDATE profiles
118 SET medals_pub = {?}
119 WHERE pid = {?}",
120 $this->values['medals_pub'], $this->pid());
121 }
122 }
123
124 public function _prepare(PlPage &$page, $id)
125 {
126 $res = XDB::iterator('SELECT *, FIND_IN_SET(\'validation\', flags) AS validate
127 FROM profile_medal_enum
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 $fullType = array(
135 '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 'sport' => 'Médailles sportives'
142 );
143 $page->assign('fullType', $fullType);
144 }
145 }
146
147 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
148 ?>