Merge commit 'origin/master' into hruid
[platal.git] / modules / profile / decos.inc.php
index ee000d9..85d2a86 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -33,16 +33,16 @@ class ProfileDeco implements ProfileSetting
                                 S::i('uid'));
             $value = array();
             while (list($id, $grade) = $res->next()) {
-                $value[$id] = array('grade' => $grade, 
-                                    'valid' => true);
+                $value[$id] = array('grade' => $grade,
+                                    '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,24 +55,28 @@ 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']) {
-                $req = new MedalReq(S::i('uid'), $id, $val['grade']);
+            if (!isset($orig[$id]) || $orig[$id]['grade'] != $val['grade']) {
+                $req = new MedalReq(S::user(), $id, $val['grade']);
                 $req->submit();
             }
         }
@@ -88,21 +92,20 @@ class ProfileDecos extends ProfilePage
         parent::__construct($wiz);
         $this->settings['medals'] = new ProfileDeco();
         $this->settings['medals_pub'] = new ProfilePub();
+        $this->watched['medals'] = true;
     }
 
-    protected function fetchData()
+    protected function _fetchData()
     {
         $res = XDB::query("SELECT  profile_medals_pub
                              FROM  auth_user_quick
                             WHERE  user_id = {?}",
                           S::i('uid'));
         $this->values['medals_pub'] = $res->fetchOneCell();
-        parent::fetchData();
     }
 
-    protected function saveData()
+    protected function _saveData()
     {
-        parent::saveData();
         if ($this->changed['medals_pub']) {
             XDB::execute("UPDATE  auth_user_quick
                              SET  profile_medals_pub = {?}
@@ -111,18 +114,8 @@ class ProfileDecos extends ProfilePage
         }
     }
 
-    public function prepare(PlatalPage &$page)
+    public function _prepare(PlPage &$page, $id)
     {
-        parent::prepare($page);
-        $res    = XDB::iterator("SELECT  *
-                                   FROM  profile_medals_grades
-                               ORDER BY  mid, pos");
-        $grades = array();
-        while ($tmp = $res->next()) {
-            $grades[$tmp['mid']][] = $tmp;
-        }
-        $page->assign('grades', $grades);
-
         $res    = XDB::iterator("SELECT  *, FIND_IN_SET('validation', flags) AS validate
                                    FROM  profile_medals
                                ORDER BY  type, text");
@@ -131,7 +124,6 @@ class ProfileDecos extends ProfilePage
             $mlist[$tmp['type']][] = $tmp;
         }
         $page->assign('medal_list', $mlist);
-
         $trad = Array('ordre'      => 'Ordres',
                       'croix'      => 'Croix',
                       'militaire'  => 'Médailles militaires',