Add a class Group to handler xnet groups.
[platal.git] / classes / group.php
CommitLineData
34ade5a6
FB
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2009 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
22class Group
23{
24 public $id;
25 public $shortname;
26 private $data = array();
27
28 private $members = null;
29 private $admins = null;
30
31 private function __construct(array $data)
32 {
33 foreach ($data as $key=>$value) {
34 $this->data[$key] = $value;
35 }
36 $this->id = intval($this->data['id']);
37 $this->shortname = $this->data['diminutif'];
38 }
39
40 public function __get($name)
41 {
42 if (property_exists($this, $name)) {
43 return $this->$name;
44 }
45
46 if (isset($this->data[$name])) {
47 return $this->data[$name];
48 }
49
50 return null;
51 }
52
53 public function __isset($name)
54 {
55 return property_exists($this, $name) || isset($this->data[$name]);
56 }
57
58 public function getMemberUIDs()
59 {
60 if (is_null($this->members)) {
61 $this->members = XDB::fetchColumn('SELECT uid
62 FROM groupex.membres
63 WHERE asso_id = {?}', $this->id);
64 }
65 return $this->members;
66 }
67
68 public function getMembers($sortby = null, $count = null, $offset = null)
69 {
70 return User::getBuildUsersWithUIDs($this->getMemberUIDs(), $sortby, $count, $offset);
71 }
72
73 public function getMemberCount()
74 {
75 return count($this->getMemberUIDs());
76 }
77
78 public function getAdminUIDs()
79 {
80 if (is_null($this->admins)) {
81 $this->admins = XDB::fetchColumn('SELECT uid
82 FROM groupex.membres
83 WHERE asso_id = {?} AND perms = \'admin\'', $this->id);
84 }
85 return $this->admins;
86 }
87
88 public function getAdmins($sortby = null, $count = null, $offset = null)
89 {
90 return User::getBuildUsersWithUIDs($this->getAdminUIDs(), $sortby, $count, $offset);
91 }
92
93 public function getAdminCount()
94 {
95 return count($this->getAdminUIDs());
96 }
97
98 static public function get($id)
99 {
100 if (!$id) {
101 return null;
102 }
103 if (ctype_digit($id)) {
104 $where = XDB::format('id = {?}', $id);
105 } else {
106 $where = XDB::format('diminutif = {?}', $id);
107 }
108 $res = XDB::query('SELECT a.*, d.nom AS domnom,
109 FIND_IN_SET(\'wiki_desc\', a.flags) AS wiki_desc,
110 FIND_IN_SET(\'notif_unsub\', a.flags) AS notif_unsub
111 FROM groupex.asso AS a
112 LEFT JOIN groupex.dom AS d ON d.id = a.dom
113 WHERE ' . $where);
114 if ($res->numRows() != 1) {
115 return null;
116 }
117 return new Group($res->fetchOneAssoc());
118 }
119}
120
121// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
122?>