Fixes display of member aliases in Xnet.
[platal.git] / classes / group.php
CommitLineData
34ade5a6
FB
1<?php
2/***************************************************************************
5e1513f6 3 * Copyright (C) 2003-2011 Polytechnique.org *
34ade5a6
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
22class Group
23{
c1e906cb
FB
24 const CAT_GROUPESX = "GroupesX";
25 const CAT_BINETS = "Binets";
26 const CAT_PROMOTIONS = "Promotions";
27 const CAT_INSTITUTIONS = "Institutions";
28
34ade5a6
FB
29 public $id;
30 public $shortname;
31 private $data = array();
32
34ade5a6
FB
33 private function __construct(array $data)
34 {
35 foreach ($data as $key=>$value) {
36 $this->data[$key] = $value;
37 }
38 $this->id = intval($this->data['id']);
39 $this->shortname = $this->data['diminutif'];
004ff79e
SJ
40 if (!is_null($this->axDate)) {
41 $this->axDate = format_datetime($this->axDate, '%d/%m/%Y');
42 }
34ade5a6
FB
43 }
44
45 public function __get($name)
46 {
47 if (property_exists($this, $name)) {
48 return $this->$name;
49 }
50
51 if (isset($this->data[$name])) {
52 return $this->data[$name];
53 }
54
55 return null;
56 }
57
58 public function __isset($name)
59 {
60 return property_exists($this, $name) || isset($this->data[$name]);
61 }
62
d865c296 63 private function getUF($admin = false, $extra_cond = null, $sort = null)
34ade5a6 64 {
c1e906cb 65 $cond = new PFC_And(new UFC_Group($this->id, $admin), new PFC_Not(new UFC_Dead()));
d865c296 66 if (!is_null($extra_cond)) {
c1e906cb
FB
67 $cond->addChild($extra_cond);
68 }
69 if ($this->cat == self::CAT_PROMOTIONS) {
70 $cond->addChild(new UFC_Registered());
34ade5a6 71 }
d865c296 72 return new UserFilter($cond, $sort);
34ade5a6
FB
73 }
74
184b012d 75 public function getMembersFilter($extra_cond = null, $sort = null)
34ade5a6 76 {
d865c296 77 return $this->getUF(false, $extra_cond, $sort);
34ade5a6
FB
78 }
79
184b012d 80 public function getAdminsFilter($extra_cond = null, $sort = null)
34ade5a6 81 {
d865c296 82 return $this->getUF(true, $extra_cond, $sort);
34ade5a6
FB
83 }
84
184b012d
SJ
85 public function iterMembers($extra_cond = null, $sort = null, $limit = null)
86 {
23ba40c4 87 $uf = $this->getMembersFilter($extra_cond, $sort);
184b012d
SJ
88 return $uf->iterUsers($limit);
89 }
90
91 public function iterAdmins($extra_cond = null, $sort = null, $limit = null)
92 {
23ba40c4 93 $uf = $this->getAdminsFilter($extra_cond, $sort);
184b012d
SJ
94 return $uf->iterUsers($limit);
95 }
96
833a6e86
FB
97 public function getLogo($fallback = true)
98 {
99 if (!empty($this->logo)) {
100 return PlImage::fromData($this->logo, $this->logo_mime);
101 } else if ($fallback) {
102 return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/dflt_carre.jpg', 'image/jpeg');
103 }
104 return null;
105 }
106
f6e35ff3 107 static public function get($id, $can_be_shortname = true)
34ade5a6
FB
108 {
109 if (!$id) {
110 return null;
111 }
f6e35ff3
PC
112 if (!$can_be_shortname) {
113 $where = XDB::format('a.id = {?}', $id);
34ade5a6 114 } else {
f6e35ff3 115 $where = XDB::format('a.diminutif = {?}', $id);
34ade5a6
FB
116 }
117 $res = XDB::query('SELECT a.*, d.nom AS domnom,
118 FIND_IN_SET(\'wiki_desc\', a.flags) AS wiki_desc,
c40a0fb3
RB
119 FIND_IN_SET(\'notif_unsub\', a.flags) AS notif_unsub,
120 (nls.id IS NOT NULL) AS has_nl
eb41eda9
FB
121 FROM groups AS a
122 LEFT JOIN group_dom AS d ON d.id = a.dom
c40a0fb3 123 LEFT JOIN newsletters AS nls ON (nls.group_id = a.id)
34ade5a6
FB
124 WHERE ' . $where);
125 if ($res->numRows() != 1) {
f6e35ff3
PC
126 if ($can_be_shortname && (is_int($id) || ctype_digit($id))) {
127 return Group::get($id, false);
128 }
34ade5a6
FB
129 return null;
130 }
8c5bbd3f
SJ
131 $data = $res->fetchOneAssoc();
132 $positions = XDB::fetchAllAssoc('SELECT position, uid
133 FROM group_members
134 WHERE asso_id = {?} AND position IS NOT NULL
135 ORDER BY position',
136 $data['id']);
137 return new Group(array_merge($data, array('positions' => $positions)));
34ade5a6
FB
138 }
139}
140
141// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
142?>