Adds search on subadministrativearea (Closes #1312).
[platal.git] / modules / profile / decos.inc.php
CommitLineData
a7c28fff
FB
1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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
FB
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
5c8a71f2
FB
30 FROM profile_medals AS s
31 INNER JOIN profile_medal_enum AS m ON ( s.mid = m.id )
bdd977d7 32 WHERE s.pid = {?}",
e5bcd851 33 $page->pid());
a7c28fff
FB
34 $value = array();
35 while (list($id, $grade) = $res->next()) {
46ae38a9 36 $value[$id] = array('grade' => $grade,
85cc366b 37 'valid' => '1');
a7c28fff
FB
38 }
39
40 // Fetch not yet validated medals
024ec1e5 41 $medals = ProfileValidate::get_typed_requests($page->pid(), 'medal');
a7c28fff 42 foreach ($medals as &$medal) {
85cc366b
FB
43 $value[$medal->mid] = array('grade' => $medal->gid,
44 'valid' => '0');
a7c28fff
FB
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 {
85cc366b
FB
55 $orig =& $page->orig[$field];
56
a7c28fff 57 // Remove old ones
85cc366b 58 foreach ($orig as $id=>&$val) {
a7c28fff
FB
59 if (!isset($value[$id]) || $val['grade'] != $value[$id]['grade']) {
60 if ($val['valid']) {
5c8a71f2 61 XDB::execute("DELETE FROM profile_medals
bdd977d7 62 WHERE pid = {?} AND mid = {?}",
e5bcd851 63 $page->pid(), $id);
a7c28fff 64 } else {
024ec1e5 65 $req = MedalReq::get_request($page->pid(), $id);
85cc366b
FB
66 if ($req) {
67 $req->clean();
68 }
a7c28fff
FB
69 }
70 }
71 }
72
73 // Add new ones
74 foreach ($value as $id=>&$val) {
85cc366b 75 if (!isset($orig[$id]) || $orig[$id]['grade'] != $val['grade']) {
024ec1e5 76 $req = new MedalReq(S::user(), $page->profile, $id, $val['grade']);
a7c28fff 77 $req->submit();
a7e50950 78 sleep(1);
a7c28fff
FB
79 }
80 }
81 }
a0fce0c6
SJ
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 }
a7c28fff
FB
91}
92
66c4bdaf 93class ProfilePageDecos extends ProfilePage
a7c28fff
FB
94{
95 protected $pg_template = 'profile/deco.tpl';
96
97 public function __construct(PlWizard &$wiz)
98 {
99 parent::__construct($wiz);
12bcf04b
RB
100 $this->settings['medals'] = new ProfileSettingDeco();
101 $this->settings['medals_pub'] = new ProfileSettingPub();
a2a1c2f2 102 $this->watched['medals'] = true;
a7c28fff
FB
103 }
104
7c2e0f0d 105 protected function _fetchData()
a7c28fff 106 {
e5bcd851
FB
107 $res = XDB::query("SELECT medals_pub
108 FROM profiles
109 WHERE pid = {?}",
110 $this->pid());
a7c28fff 111 $this->values['medals_pub'] = $res->fetchOneCell();
a7c28fff
FB
112 }
113
7c2e0f0d 114 protected function _saveData()
a7c28fff 115 {
a7c28fff 116 if ($this->changed['medals_pub']) {
e5bcd851
FB
117 XDB::execute("UPDATE profiles
118 SET medals_pub = {?}
119 WHERE pid = {?}",
120 $this->values['medals_pub'], $this->pid());
a7c28fff
FB
121 }
122 }
123
04334c61 124 public function _prepare(PlPage &$page, $id)
a7c28fff 125 {
cfcb8b62
SJ
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();
a7c28fff
FB
130 while ($tmp = $res->next()) {
131 $mlist[$tmp['type']][] = $tmp;
132 }
133 $page->assign('medal_list', $mlist);
cfcb8b62
SJ
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);
a7c28fff
FB
144 }
145}
146
147// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
148?>