Copy missing tables: all the tables needed by profile/edit are present.
[platal.git] / modules / profile / groups.inc.php
CommitLineData
92412b28
FB
1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 Polytechnique.org *
92412b28
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
57b73804 22class ProfileSection implements ProfileSetting
92412b28 23{
57b73804
FB
24 public function value(ProfilePage &$page, $field, $value, &$success)
25 {
26 $success = true;
27 if (is_null($value)) {
28 $res = XDB::query("SELECT section
e5bcd851
FB
29 FROM profiles
30 WHERE pid = {?}",
31 $page->pid());
57b73804
FB
32 return intval($res->fetchOneCell());
33 }
34 return intval($value);
35 }
92412b28 36
57b73804 37 public function save(ProfilePage &$page, $field, $value)
92412b28 38 {
e5bcd851 39 XDB::execute("UPDATE profiles
57b73804 40 SET section = {?}
e5bcd851
FB
41 WHERE pid = {?}",
42 $value, $page->pid());
57b73804
FB
43 }
44}
45
46class ProfileGroup implements ProfileSetting
47{
48 private $table;
49 private $user_field;
50 private $group_field;
51
52 public function __construct($table, $user, $group)
53 {
54 $this->table = $table;
55 $this->user_field = $user;
56 $this->group_field = $group;
92412b28
FB
57 }
58
57b73804 59 public function value(ProfilePage &$page, $field, $value, &$success)
92412b28 60 {
57b73804
FB
61 if (is_null($value)) {
62 $value = array();
63 $res = XDB::iterRow("SELECT g.id, g.text
5c8a71f2
FB
64 FROM profile_{$this->table}_enum AS g
65 INNER JOIN profile_{$this->table}s AS i ON (i.{$this->group_field} = g.id)
57b73804 66 WHERE i.{$this->user_field} = {?}",
e5bcd851 67 $page->pid());
57b73804
FB
68 while (list($gid, $text) = $res->next()) {
69 $value[intval($gid)] = $text;
70 }
71 }
72 if (!is_array($value)) {
73 $value = array();
74 }
75 ksort($value);
76 $success = true;
77 return $value;
92412b28
FB
78 }
79
57b73804
FB
80 public function save(ProfilePage &$page, $field, $value)
81 {
82 XDB::execute("DELETE FROM {$this->table}_ins
83 WHERE {$this->user_field} = {?}",
e5bcd851 84 $page->pid());
57b73804
FB
85 if (!count($value)) {
86 return;
87 }
88 $insert = array();
89 foreach ($value as $id=>$text) {
e5bcd851 90 $insert[] = XDB::format('({?}, {?})', $page->pid(), $id);
57b73804
FB
91 }
92 XDB::execute("INSERT INTO {$this->table}_ins ({$this->user_field}, {$this->group_field})
93 VALUES " . implode(',', $insert));
94 }
95}
96
97class ProfileGroups extends ProfilePage
98{
99 protected $pg_template = 'profile/groups.tpl';
100
101 public function __construct(PlWizard &$wiz)
92412b28 102 {
57b73804
FB
103 parent::__construct($wiz);
104 $this->settings['section'] = new ProfileSection();
5c8a71f2 105 $this->settings['binets'] = new ProfileGroup('binet', 'user_id', 'binet_id');
a2a1c2f2 106 $this->watched['section'] = $this->watched['binets'] = true;
b37aacd9
FB
107 }
108
04334c61 109 public function _prepare(PlPage &$page, $id)
b37aacd9 110 {
b37aacd9 111 $page->assign('mygroups', XDB::iterator("SELECT a.nom, a.site, a.diminutif, a.unsub_url, a.pub, m.perms
eb41eda9
FB
112 FROM groups AS a
113 INNER JOIN group_members AS m ON (m.asso_id = a.id)
b37aacd9 114 WHERE m.uid = {?} AND (a.cat = 'GroupesX' OR a.cat = 'Institutions')",
e5bcd851 115 $this->pid()));
b37aacd9
FB
116 $page->assign('listgroups', XDB::iterator("SELECT a.nom, a.diminutif, a.sub_url,
117 IF (a.cat = 'Institutions', a.cat, d.nom) AS dom
eb41eda9
FB
118 FROM groups AS a
119 LEFT JOIN group_dom AS d ON (d.id = a.dom)
b37aacd9
FB
120 WHERE a.inscriptible != 0
121 AND (a.cat = 'GroupesX' OR a.cat = 'Institutions')
122 ORDER BY a.cat, a.dom, a.nom"));
e5bcd851 123 # XXX: FIXME: promo_sortie
46ae38a9 124 $page->assign('old', (int)date('Y') >= S::i('promo_sortie'));
92412b28
FB
125 }
126}
127
128// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
129?>