A few fixes on upgrade scripts.
[platal.git] / modules / profile / groups.inc.php
index cd2c447..26bfafd 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-class ProfileSection implements ProfileSetting
+class ProfileSettingSection implements ProfileSetting
 {
     public function value(ProfilePage &$page, $field, $value, &$success)
     {
         $success = true;
         if (is_null($value)) {
             $res = XDB::query("SELECT  section
-                                 FROM  auth_user_md5
-                                WHERE  user_id = {?}",
-                              S::i('uid'));
+                                 FROM  profiles
+                                WHERE  pid = {?}",
+                              $page->pid());
             return intval($res->fetchOneCell());
         }
         return intval($value);
@@ -36,24 +36,22 @@ class ProfileSection implements ProfileSetting
 
     public function save(ProfilePage &$page, $field, $value)
     {
-        XDB::execute("UPDATE  auth_user_md5
+        XDB::execute("UPDATE  profiles
                          SET  section = {?}
-                       WHERE  user_id = {?}",
-                     $value, S::i('uid'));
+                       WHERE  pid = {?}",
+                     ($value == 0) ? null : $value, $page->pid());
+    }
+
+    public function getText($value) {
+        $sectionsList = DirEnum::getOptions(DirEnum::SECTIONS);
+        return $sectionsList[$value];
     }
 }
 
-class ProfileGroup implements ProfileSetting
+class ProfileSettingBinets implements ProfileSetting
 {
-    private $table;
-    private $user_field;
-    private $group_field;
-
-    public function __construct($table, $user, $group)
+    public function __construct()
     {
-        $this->table       = $table;
-        $this->user_field  = $user;
-        $this->group_field = $group;
     }
 
     public function value(ProfilePage &$page, $field, $value, &$success)
@@ -61,10 +59,10 @@ class ProfileGroup implements ProfileSetting
         if (is_null($value)) {
             $value = array();
             $res = XDB::iterRow("SELECT  g.id, g.text
-                                   FROM  {$this->table}_def AS g
-                             INNER JOIN  {$this->table}_ins AS i ON (i.{$this->group_field} = g.id)
-                                  WHERE  i.{$this->user_field} = {?}",
-                                S::i('uid'));
+                                   FROM  profile_binet_enum AS g
+                             INNER JOIN  profile_binets AS i ON (i.binet_id = g.id)
+                                  WHERE  i.pid = {?}",
+                                $page->pid());
             while (list($gid, $text) = $res->next()) {
                 $value[intval($gid)] = $text;
             }
@@ -79,48 +77,52 @@ class ProfileGroup implements ProfileSetting
 
     public function save(ProfilePage &$page, $field, $value)
     {
-        XDB::execute("DELETE FROM  {$this->table}_ins
-                            WHERE  {$this->user_field} = {?}",
-                     S::i('uid'));
+        XDB::execute("DELETE FROM  profile_binets
+                            WHERE  pid = {?}",
+                     $page->pid());
         if (!count($value)) {
             return;
         }
         $insert = array();
         foreach ($value as $id=>$text) {
-            $insert[] = '(' . S::i('uid') . ", $id)";
+            $insert[] = XDB::format('({?}, {?})', $page->pid(), $id);
         }
-        XDB::execute("INSERT INTO  {$this->table}_ins ({$this->user_field}, {$this->group_field})
+        XDB::execute("INSERT INTO  profile_binets (pid, binet_id)
                            VALUES  " . implode(',', $insert));
     }
+
+    public function getText($value) {
+        return implode(', ', $value);
+    }
 }
 
-class ProfileGroups extends ProfilePage
+class ProfilePageGroups extends ProfilePage
 {
     protected $pg_template = 'profile/groups.tpl';
 
     public function __construct(PlWizard &$wiz)
     {
         parent::__construct($wiz);
-        $this->settings['section']  = new ProfileSection();
-        $this->settings['binets']   = new ProfileGroup('binets', 'user_id', 'binet_id');
+        $this->settings['section']  = new ProfileSettingSection();
+        $this->settings['binets']   = new ProfileSettingBinets();
         $this->watched['section'] = $this->watched['binets'] = true;
     }
 
     public function _prepare(PlPage &$page, $id)
     {
         $page->assign('mygroups', XDB::iterator("SELECT  a.nom, a.site, a.diminutif, a.unsub_url, a.pub, m.perms
-                                                   FROM  groupex.asso    AS a
-                                             INNER JOIN  groupex.membres AS m ON (m.asso_id = a.id)
+                                                   FROM  groups    AS a
+                                             INNER JOIN  group_members AS m ON (m.asso_id = a.id)
                                                   WHERE  m.uid = {?} AND (a.cat = 'GroupesX' OR a.cat = 'Institutions')",
-                                                  S::i('uid')));
+                                                $this->pid()));
         $page->assign('listgroups', XDB::iterator("SELECT  a.nom, a.diminutif, a.sub_url,
                                                            IF (a.cat = 'Institutions', a.cat, d.nom) AS dom
-                                                     FROM  groupex.asso  AS a
-                                                LEFT JOIN  groupex.dom   AS d ON (d.id = a.dom)
+                                                     FROM  groups  AS a
+                                                LEFT JOIN  group_dom   AS d ON (d.id = a.dom)
                                                     WHERE  a.inscriptible != 0
                                                            AND (a.cat = 'GroupesX' OR a.cat = 'Institutions')
                                                  ORDER BY  a.cat, a.dom, a.nom"));
-        $page->assign('old', (int)date('Y') >= S::i('promo_sortie'));
+        $page->assign('old', (int) date('Y') >= $this->profile->grad_year);
     }
 }