Fix deco javascript: current grade was not properly selected
[platal.git] / templates / profile / deco.tpl
1 {**************************************************************************}
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
23
24 <script type="text/javascript">//<![CDATA[
25 {literal}
26 var subgrades = new array();
27 var names     = new array();
28
29 function update()
30 {
31   var val = document.forms.prof_annu['medal_sel'].value;
32   if (val == '' || document.getElementById('medal_' + val) != null) {
33     document.getElementById('medal_add').style.display = 'none';
34   } else {
35     document.getElementById('medal_add').style.display = '';
36   }
37 }
38
39 function getMedalName(id)
40 {
41   document.getElementById('medal_name_' + id).innerHTML = names[id];
42 }
43
44 function buildGrade(id, current)
45 {
46   var grade;
47   var subg = subgrades[id];
48   var obj  = $('#medal_grade_' + id);
49   if (!subg) {
50     obj.prepend('<input type="hidden" name="medals[' + id + '][grade]" value="0" />');
51   } else {
52     var html = 'Agrafe : <select name="medals[' + id + '][grade]">';
53     html += '<option value="0">Non précisée</option>';
54     for (grade = 0 ; grade < subg.length ; grade++) {
55       html += '<option value="' + subg[grade][0] + '"';
56       if (subg[grade][0] == current) {
57         html += ' selected="selected"';
58       }
59       html += '>' + subg[grade][1] + '</option>';
60     }
61
62     html += '</select>';
63     obj.prepend(html);
64   }
65 }
66
67 function makeAddProcess(id)
68 {
69   return function(data)
70          {
71            $('#medals').after(data);
72            update();
73            getMedalName(id);
74            buildGrade(id, 0);
75          };
76 }
77
78 function add()
79 {
80   var id = document.forms.prof_annu['medal_sel'].value;
81   $.get(platal_baseurl + 'profile/ajax/medal/' + id, makeAddProcess(id));
82 }
83
84 function remove(id)
85 {
86   $("#medal_" + id).remove();
87   update();
88 }
89
90 {/literal}
91 {foreach from=$medal_list key=type item=list}
92   {foreach from=$list item=m}
93   names[{$m.id}] = "{$m.text}";
94   {if $grades[$m.id]|@count}
95     names[{$m.id}] = "{$m.text}";
96     subgrades[{$m.id}] = new array({$grades[$m.id]|@count});
97     {foreach from=$grades[$m.id] item=g}
98       subgrades[{$m.id}][{$g.gid-1}] = [{$g.gid},"{$g.text}"];
99     {/foreach}
100   {/if}{/foreach}
101 {/foreach}
102 //]]></script>
103
104 <table class="bicol">
105   <tr>
106     <th>
107       Médailles, Décorations, Prix, ...
108     </th>
109   </tr>
110   <tr>
111     <td>
112       <div class="flags">
113         <div class="vert" style="float: left">
114           <input type="checkbox" name="medals_pub"{if $medals_pub eq 'public'} checked="checked"{/if} />
115         </div>
116         <div class="texte">
117           ces informations sont normalement publiques (JO, ...) mais tu peux choisir de les associer a ta fiche publique
118         </div>
119       </div>
120       <div style="clear: both; margin-top: 0.2em" id="medals">
121         <select name="medal_sel" onchange="update()">
122           <option value=''></option>
123           {foreach from=$medal_list key=type item=list}
124           <optgroup label="{$trad[$type]}...">
125             {foreach from=$list item=m}
126             <option value="{$m.id}">{$m.text}</option>
127             {/foreach}
128           </optgroup>
129           {/foreach}
130         </select>
131         <span id="medal_add">
132           <a href="javascript:add();">{icon name=add title="Ajouter cette médaille"}</a>
133         </span>
134       </div>
135       {foreach from=$medals item=medal key=id}
136       {include file="profile/deco.medal.tpl" medal=$medal id=$id}
137       {/foreach}
138     </td>
139   </tr>
140 </table>
141
142 <script type="text/javascript">
143 update();
144 </script>
145
146 {* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}