Medals page working
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 2 Sep 2007 20:44:41 +0000 (22:44 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 2 Sep 2007 20:44:41 +0000 (22:44 +0200)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
include/validations/medals.inc.php
modules/profile.php
modules/profile/assign_deco.inc.php [deleted file]
modules/profile/decos.inc.php
modules/profile/get_deco.inc.php [deleted file]
modules/profile/page.inc.php
modules/profile/update_deco.inc.php [deleted file]
modules/profile/verif_deco.inc.php [deleted file]
templates/profile/deco.medal.tpl [new file with mode: 0644]
templates/profile/deco.tpl

index e419e46..7e65dc5 100644 (file)
@@ -104,6 +104,20 @@ class MedalReq extends Validate
     }
 
     // }}}
+    // {{{ function get_request($medal)
+
+    static public function get_request($uid, $type)
+    {
+        $reqs = Validate::get_typed_requests($uid, 'medal');
+        foreach ($reqs as &$req) {
+            if ($req->mid == $type) {
+                return $req;
+            }
+        }
+        return null;
+    }
+
+    // }}}
 }
 
 // }}}
index a4c2d57..f001599 100644 (file)
@@ -32,8 +32,10 @@ class ProfileModule extends PLModule
             'profile/private'  => $this->make_hook('profile',    AUTH_COOKIE),
             'profile/ax'       => $this->make_hook('ax',         AUTH_COOKIE, 'admin'),
             'profile/edit'     => $this->make_hook('p_edit',     AUTH_MDP),
-            'profile/ajax/address' => $this->make_hook('ajax_address', AUTH_PUBLIC, 'user', NO_AUTH),
+            'profile/ajax/address' => $this->make_hook('ajax_address', AUTH_COOKIE, 'user', NO_AUTH),
             'profile/ajax/tel'     => $this->make_hook('ajax_tel', AUTH_COOKIE, 'user', NO_AUTH),
+            'profile/ajax/medal'   => $this->make_hook('ajax_medal', AUTH_COOKIE, 'user', NO_AUTH),
+            'profile/medal'    => $this->make_hook('medal', AUTH_PUBLIC),
             'profile/orange'   => $this->make_hook('p_orange',   AUTH_MDP),
             'profile/usage'    => $this->make_hook('p_usage',    AUTH_MDP),
 
@@ -97,6 +99,19 @@ class ProfileModule extends PLModule
         exit;
     }
 
+    function handler_medal(&$page, $mid)
+    {
+        $res = XDB::query("SELECT  img
+                             FROM  profile_medals
+                            WHERE  id = {?}",
+                          $mid);
+        $img  = dirname(__FILE__).'/../htdocs/images/medals/' . $res->fetchOneCell();
+        $type = mime_content_type($img);
+        header("Content-Type: $type");
+        echo file_get_contents($img);
+        exit;
+    }
+
     function handler_photo_change(&$page)
     {
         $page->changeTpl('profile/trombino.tpl');
@@ -348,6 +363,14 @@ class ProfileModule extends PLModule
         $page->assign('ajaxtel', true);
     }
 
+    function handler_ajax_medal(&$page, $id)
+    {
+        $page->changeTpl('profile/deco.medal.tpl', NO_SKIN);
+        $page->assign('id', $id);
+        $page->assign('medal', array('valid' => 0, 'grade' => 0));
+        $page->assign('ajaxdeco', true);
+    }
+
     function handler_p_orange(&$page)
     {
         $page->changeTpl('profile/orange.tpl');
diff --git a/modules/profile/assign_deco.inc.php b/modules/profile/assign_deco.inc.php
deleted file mode 100644 (file)
index 4f3c931..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-<?php
-/***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
- *  http://opensource.polytechnique.org/                                   *
- *                                                                         *
- *  This program is free software; you can redistribute it and/or modify   *
- *  it under the terms of the GNU General Public License as published by   *
- *  the Free Software Foundation; either version 2 of the License, or      *
- *  (at your option) any later version.                                    *
- *                                                                         *
- *  This program is distributed in the hope that it will be useful,        *
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
- *  GNU General Public License for more details.                           *
- *                                                                         *
- *  You should have received a copy of the GNU General Public License      *
- *  along with this program; if not, write to the Free Software            *
- *  Foundation, Inc.,                                                      *
- *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
- ***************************************************************************/
-
-
-$res    = XDB::iterator("SELECT * FROM profile_medals_grades ORDER BY mid, pos");
-$grades = Array();
-while ($tmp = $res->next()) {
-    $grades[$tmp['mid']][] = $tmp;
-}
-
-$res    = XDB::iterator("SELECT *, FIND_IN_SET('validation', flags) AS validate FROM profile_medals ORDER BY type, text");
-$mlist  = Array();
-while ($tmp = $res->next()) {
-    $mlist[$tmp['type']][] = $tmp;
-}
-
-$trad = Array('ordre' => 'Ordres ...', 'croix' => 'Croix ...', 'militaire' => 'Médailles militaires ...',
-        'honneur' => 'Médailles d\'honneur', 'resistance' => 'Médailles de la résistance ...', 'prix' => 'Prix ...');
-
-$page->assign('grades', $grades);
-$page->assign('medals', $medals);
-$page->assign('trad',   $trad);
-$page->assign('medals_pub', $medals_pub);
-$page->assign('medal_list', $mlist);
-$page->assign('medals_valid', $medals_valid);
-
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
-?>
index ee000d9..ebd8f12 100644 (file)
@@ -34,15 +34,15 @@ class ProfileDeco implements ProfileSetting
             $value = array();
             while (list($id, $grade) = $res->next()) {
                 $value[$id] = array('grade' => $grade, 
-                                    'valid' => true);
+                                    'valid' => '1');
             }
 
             // Fetch not yet validated medals
             require_once('validations.inc.php');
             $medals = Validate::get_typed_requests(S::i('uid'), 'medal');
             foreach ($medals as &$medal) {
-                $medals[$medal->mid] = array('grade' => $medal->gid,
-                                             'valid' => false);
+                $value[$medal->mid] = array('grade' => $medal->gid,
+                                            'valid' => '0');
             }
         } else if (!is_array($value)) {
             $value = array();
@@ -55,23 +55,27 @@ class ProfileDeco implements ProfileSetting
     {
         require_once('validations.inc.php');
 
+        $orig =& $page->orig[$field];
+
         // Remove old ones
-        foreach ($page->orig as $id=>&$val) {
+        foreach ($orig as $id=>&$val) {
             if (!isset($value[$id]) || $val['grade'] != $value[$id]['grade']) {
                 if ($val['valid']) {
                     XDB::execute("DELETE FROM  profile_medals_sub
-                                        WHERE  uid = {?} AND id = {?}",
+                                        WHERE  uid = {?} AND mid = {?}",
                                  S::i('uid'), $id);
                 } else {
-                    $req = Validate::get_typed_request(S::i('uid'), 'medal', $id);
-                    $req->clean();
+                    $req = MedalReq::get_request(S::i('uid'), $id);
+                    if ($req) {
+                        $req->clean();
+                    }
                 }
             }
         }
 
         // Add new ones
         foreach ($value as $id=>&$val) {
-            if (!isset($this->orig[$id]) || $this->orig[$id]['grade'] != $val['grade']) {
+            if (!isset($orig[$id]) || $orig[$id]['grade'] != $val['grade']) {
                 $req = new MedalReq(S::i('uid'), $id, $val['grade']);
                 $req->submit();
             }
diff --git a/modules/profile/get_deco.inc.php b/modules/profile/get_deco.inc.php
deleted file mode 100644 (file)
index f0e658c..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-<?php
-/***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
- *  http://opensource.polytechnique.org/                                   *
- *                                                                         *
- *  This program is free software; you can redistribute it and/or modify   *
- *  it under the terms of the GNU General Public License as published by   *
- *  the Free Software Foundation; either version 2 of the License, or      *
- *  (at your option) any later version.                                    *
- *                                                                         *
- *  This program is distributed in the hope that it will be useful,        *
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
- *  GNU General Public License for more details.                           *
- *                                                                         *
- *  You should have received a copy of the GNU General Public License      *
- *  along with this program; if not, write to the Free Software            *
- *  Foundation, Inc.,                                                      *
- *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
- ***************************************************************************/
-
-require_once('validations.inc.php');
-
-if (Env::has('medal_op')) {
-    if (Env::v('medal_op')=='retirer' && Env::i('medal_id')) {
-        XDB::execute("DELETE FROM profile_medals_sub WHERE uid = {?} AND mid = {?}", S::v('uid', -1), Env::i('medal_id', -1));
-    }
-
-    if (Env::v('medal_op')=='ajouter' && Env::i('medal_id')) {
-        $req = new MedalReq(S::v('uid',-1),Env::i('medal_id'), Env::i('grade_id'));
-        $req->submit();
-        unset($_REQUEST['medal_op']); // pour ne pas avoir le message d'attente de validation
-    }
-
-    if (Env::v('medal_op')=='annuler' && Env::i('medal_id')) {
-        $req = Validate::get_typed_request(S::i('uid'), 'medal', Env::v('medal_id'));
-        $req->clean();
-    }
-}
-if (Post::has('grade')) {
-    foreach (Post::v('grade') as $mid=>$gid) {
-        XDB::execute('UPDATE profile_medals_sub SET gid={?} WHERE uid={?} AND mid={?}', $gid, S::v('uid'), $mid);
-    }
-}
-
-$res    = XDB::query(
-       "SELECT  m.id, m.text AS medal, m.type, m.img, s.gid
-           FROM  profile_medals_sub    AS s
-     INNER JOIN  profile_medals        AS m ON ( s.mid = m.id )
-          WHERE  s.uid = {?}", S::v('uid', -1));
-
-$medals = $res->fetchAllAssoc();
-
-$res   = XDB::query("SELECT profile_medals_pub FROM auth_user_quick WHERE user_id = {?}", S::v('uid', -1));
-$medals_pub = $res->fetchOneCell();
-
-$medals_valid = Validate::get_typed_requests(S::i('uid'), 'medal');
-
-if(Env::has('modifier') || Env::has('suivant')) {
-    $medals_pub = Env::has('medals_pub')?'public':'private';
-}
-
-// vim:set et sws=4 sw=4 sts=4 enc=utf-8:
-?>
index 233a514..2f454af 100644 (file)
@@ -201,10 +201,10 @@ abstract class ProfilePage implements PlWizardPage
         foreach ($this->settings as $field=>&$setting) {
             $success = false;
             if (!is_null($setting)) {
-                $this->values[$field] = $setting->value($this, $field, Post::v($field), $success);
+                $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
             } else {
                 $success = true;
-                $this->values[$field] = Post::v($field);
+                $this->values[$field] = Post::v($field, '');
             }
             $this->errors[$field] = !$success;
             $global_success = $global_success && $success;
diff --git a/modules/profile/update_deco.inc.php b/modules/profile/update_deco.inc.php
deleted file mode 100644 (file)
index da6d8ad..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-<?php
-/***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
- *  http://opensource.polytechnique.org/                                   *
- *                                                                         *
- *  This program is free software; you can redistribute it and/or modify   *
- *  it under the terms of the GNU General Public License as published by   *
- *  the Free Software Foundation; either version 2 of the License, or      *
- *  (at your option) any later version.                                    *
- *                                                                         *
- *  This program is distributed in the hope that it will be useful,        *
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
- *  GNU General Public License for more details.                           *
- *                                                                         *
- *  You should have received a copy of the GNU General Public License      *
- *  along with this program; if not, write to the Free Software            *
- *  Foundation, Inc.,                                                      *
- *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
- ***************************************************************************/
-
-XDB::execute("UPDATE auth_user_quick SET profile_medals_pub = {?} WHERE user_id = {?}", $medals_pub, S::v('uid', -1));
-
-// vim:set et sws=4 sw=4 sts=4 enc=utf-8:
-?>
diff --git a/modules/profile/verif_deco.inc.php b/modules/profile/verif_deco.inc.php
deleted file mode 100644 (file)
index 4c844bd..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php
-/***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
- *  http://opensource.polytechnique.org/                                   *
- *                                                                         *
- *  This program is free software; you can redistribute it and/or modify   *
- *  it under the terms of the GNU General Public License as published by   *
- *  the Free Software Foundation; either version 2 of the License, or      *
- *  (at your option) any later version.                                    *
- *                                                                         *
- *  This program is distributed in the hope that it will be useful,        *
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
- *  GNU General Public License for more details.                           *
- *                                                                         *
- *  You should have received a copy of the GNU General Public License      *
- *  along with this program; if not, write to the Free Software            *
- *  Foundation, Inc.,                                                      *
- *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
- ***************************************************************************/
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
-?>
diff --git a/templates/profile/deco.medal.tpl b/templates/profile/deco.medal.tpl
new file mode 100644 (file)
index 0000000..20dcab6
--- /dev/null
@@ -0,0 +1,42 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  Copyright (C) 2003-2007 Polytechnique.org                             *}
+{*  http://opensource.polytechnique.org/                                  *}
+{*                                                                        *}
+{*  This program is free software; you can redistribute it and/or modify  *}
+{*  it under the terms of the GNU General Public License as published by  *}
+{*  the Free Software Foundation; either version 2 of the License, or     *}
+{*  (at your option) any later version.                                   *}
+{*                                                                        *}
+{*  This program is distributed in the hope that it will be useful,       *}
+{*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *}
+{*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *}
+{*  GNU General Public License for more details.                          *}
+{*                                                                        *}
+{*  You should have received a copy of the GNU General Public License     *}
+{*  along with this program; if not, write to the Free Software           *}
+{*  Foundation, Inc.,                                                     *}
+{*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
+{*                                                                        *}
+{**************************************************************************}
+
+{if $ajaxdeco}
+<?xml version="1.0" encoding="utf-8"?>
+{/if}
+<div id="medal_{$id}" style="clear: both; margin-top: 1em; height: 50px; vertical-align: middle">
+  <div style="float: left; margin-right: 0.3em">
+    <img src="profile/medal/{$id}" height="50" />
+  </div>
+  <div style="float: left; width: 70%">
+    <div><b id="medal_name_{$id}"></b>
+    {if !$medal.valid}(en attente de {if $ajaxdeco}validation{else}modération{/if}){/if}</div>
+    <div id="medal_grade_{$id}"><input type="hidden" name="medals[{$id}][valid]" value="{$medal.valid}" /></div>
+  </div>
+  <a href="javascript:remove({$id})" style="vertical-align: middle">{icon name="cross" title="Supprimer cette médaille"}</a>
+  <script type="text/javascript">
+  getMedalName({$id});
+  buildGrade({$id}, {$medal.grade});
+  </script>
+</div>
+
+{* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}
index 1dd1f24..935ce49 100644 (file)
 {**************************************************************************}
 
 
-{literal}
 <script type="text/javascript">//<![CDATA[
+{literal}
+var subgrades = new array();
+var names     = new array();
+
 function update()
 {
   var val = document.forms.prof_annu['medal_sel'].value;
   if (val == '' || document.getElementById('medal_' + val) != null) {
-    document.getElementById('medal_add').display.style = 'none';
+    document.getElementById('medal_add').style.display = 'none';
   } else {
-    document.getElementById('medal_add').display.style = '';
+    document.getElementById('medal_add').style.display = '';
   }
 }
 
-  var valid = new array();
-  function medal_add()
-  {
-    var selid = document.forms.prof_annu.medal_sel.selectedIndex;
-    document.forms.prof_annu.medal_id.value = document.forms.prof_annu.medal_sel.options[selid].value;
-    document.forms.prof_annu.grade_id.value = document.forms.prof_annu.grade_sel.value;
-    document.forms.prof_annu.medal_op.value = "ajouter";
-    document.forms.prof_annu.submit();
-  }
-
-  function medal_del( id )
-  {
-    document.forms.prof_annu.medal_id.value = id;
-    document.forms.prof_annu.medal_op.value = "retirer";
-    document.forms.prof_annu.submit();
-  }
+function getMedalName(id)
+{
+  document.getElementById('medal_name_' + id).innerHTML = names[id];
+}
 
-  function medal_cancel(stamp)
-  {
-    document.forms.prof_annu.medal_id.value = stamp;
-    document.forms.prof_annu.medal_op.value = "annuler";
-    document.forms.prof_annu.submit();
-  }
-  var subgrades = new array();
-  function getoption( select_input, j)
-  {
-    if (!document.all)
-    {
-      return select_input.options[j];
-    }
-    else
-    {
-      return j;
-    }
-  }
-  function medal_grades( sel_medal )
-  {
-    var subg = subgrades[sel_medal.selectedIndex];
-    document.getElementById("grade_sel_div").style.display = subg?"inline":"none";
-    if (!subg) return;
-    var select = document.getElementById("grade_sel");
-    while (select.length > 1)
-    {
-      select.remove(1);
+function buildGrade(id, current)
+{
+  var grade;
+  var subg = subgrades[id];
+  var obj  = $('#medal_grade_' + id);
+  if (!subg) {
+    obj.prepend('<input type="hidden" name="medals[' + id + '][grade]" value="0" />');
+  } else {
+    var html = 'Agrafe : <select name="medals[' + id + '][grade]">';
+    html += '<option value="0">Non précisée</option>';
+    for (grade = 0 ; grade < subg.length ; grade++) {
+      html += '<option value="' + subg[grade][0] + '">' + subg[grade][1] + '</option>';
     }
 
-    for (i=0; i < subg.length; i++)
-    {
-      var dmc = document.createElement("option");
-      dmc.text= subg[i][1];
-      dmc.value = subg[i][0];
-      select.add(dmc,getoption(select,i));
-    }
-    var vide = document.createElement("option");
-    vide.text = "";
-    vide.value = 0;
-    select.add(vide,getoption(select,0));
-    select.remove(subg.length+1);
+    html += '</select>';
+    obj.prepend(html);
   }
-  //]]>
+}
+
+function add()
+{
+  var id = document.forms.prof_annu['medal_sel'].value;
+  $.get(platal_baseurl + 'profile/ajax/medal/' + id,
+        function(data) { $('#medals').after(data); update(); });
+}
+
+function remove(id)
+{
+  $("#medal_" + id).remove();
+}
+
 {/literal}
 {foreach from=$medal_list key=type item=list}
-  {foreach from=$list item=m}{if $grades[$m.id]|@count}
+  {foreach from=$list item=m}
+  names[{$m.id}] = "{$m.text}";
+  {if $grades[$m.id]|@count}
+    names[{$m.id}] = "{$m.text}";
     subgrades[{$m.id}] = new array({$grades[$m.id]|@count});
-    i = 0;
     {foreach from=$grades[$m.id] item=g}
-      subgrades[{$m.id}][i] = [{$g.gid},"{$g.text}"];
-      i++;
+      subgrades[{$m.id}][{$g.gid-1}] = [{$g.gid},"{$g.text}"];
     {/foreach}
   {/if}{/foreach}
 {/foreach}
-
 </script>
 
 <table class="bicol">
@@ -123,7 +102,7 @@ function update()
           ces informations sont normalement publiques (JO, ...) mais tu peux choisir de les associer a ta fiche publique
         </div>
       </div>
-      <div style="clear: both; margin-top: 0.2em">
+      <div style="clear: both; margin-top: 0.2em" id="medals">
         <select name="medal_sel" onchange="update()">
           <option value=''></option>
           {foreach from=$medal_list key=type item=list}