Commit | Line | Data |
---|---|---|
34ade5a6 FB |
1 | <?php |
2 | /*************************************************************************** | |
12262f13 | 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 | ||
22 | class 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']; | |
40 | } | |
41 | ||
42 | public function __get($name) | |
43 | { | |
44 | if (property_exists($this, $name)) { | |
45 | return $this->$name; | |
46 | } | |
47 | ||
48 | if (isset($this->data[$name])) { | |
49 | return $this->data[$name]; | |
50 | } | |
51 | ||
52 | return null; | |
53 | } | |
54 | ||
55 | public function __isset($name) | |
56 | { | |
57 | return property_exists($this, $name) || isset($this->data[$name]); | |
58 | } | |
59 | ||
d865c296 | 60 | private function getUF($admin = false, $extra_cond = null, $sort = null) |
34ade5a6 | 61 | { |
c1e906cb | 62 | $cond = new PFC_And(new UFC_Group($this->id, $admin), new PFC_Not(new UFC_Dead())); |
d865c296 | 63 | if (!is_null($extra_cond)) { |
c1e906cb FB |
64 | $cond->addChild($extra_cond); |
65 | } | |
66 | if ($this->cat == self::CAT_PROMOTIONS) { | |
67 | $cond->addChild(new UFC_Registered()); | |
34ade5a6 | 68 | } |
d865c296 | 69 | return new UserFilter($cond, $sort); |
34ade5a6 FB |
70 | } |
71 | ||
184b012d | 72 | public function getMembersFilter($extra_cond = null, $sort = null) |
34ade5a6 | 73 | { |
d865c296 | 74 | return $this->getUF(false, $extra_cond, $sort); |
34ade5a6 FB |
75 | } |
76 | ||
184b012d | 77 | public function getAdminsFilter($extra_cond = null, $sort = null) |
34ade5a6 | 78 | { |
d865c296 | 79 | return $this->getUF(true, $extra_cond, $sort); |
34ade5a6 FB |
80 | } |
81 | ||
184b012d SJ |
82 | public function iterMembers($extra_cond = null, $sort = null, $limit = null) |
83 | { | |
23ba40c4 | 84 | $uf = $this->getMembersFilter($extra_cond, $sort); |
184b012d SJ |
85 | return $uf->iterUsers($limit); |
86 | } | |
87 | ||
88 | public function iterAdmins($extra_cond = null, $sort = null, $limit = null) | |
89 | { | |
23ba40c4 | 90 | $uf = $this->getAdminsFilter($extra_cond, $sort); |
184b012d SJ |
91 | return $uf->iterUsers($limit); |
92 | } | |
93 | ||
833a6e86 FB |
94 | public function getLogo($fallback = true) |
95 | { | |
96 | if (!empty($this->logo)) { | |
97 | return PlImage::fromData($this->logo, $this->logo_mime); | |
98 | } else if ($fallback) { | |
99 | return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/dflt_carre.jpg', 'image/jpeg'); | |
100 | } | |
101 | return null; | |
102 | } | |
103 | ||
f6e35ff3 | 104 | static public function get($id, $can_be_shortname = true) |
34ade5a6 FB |
105 | { |
106 | if (!$id) { | |
107 | return null; | |
108 | } | |
f6e35ff3 PC |
109 | if (!$can_be_shortname) { |
110 | $where = XDB::format('a.id = {?}', $id); | |
34ade5a6 | 111 | } else { |
f6e35ff3 | 112 | $where = XDB::format('a.diminutif = {?}', $id); |
34ade5a6 FB |
113 | } |
114 | $res = XDB::query('SELECT a.*, d.nom AS domnom, | |
115 | FIND_IN_SET(\'wiki_desc\', a.flags) AS wiki_desc, | |
c40a0fb3 RB |
116 | FIND_IN_SET(\'notif_unsub\', a.flags) AS notif_unsub, |
117 | (nls.id IS NOT NULL) AS has_nl | |
eb41eda9 FB |
118 | FROM groups AS a |
119 | LEFT JOIN group_dom AS d ON d.id = a.dom | |
c40a0fb3 | 120 | LEFT JOIN newsletters AS nls ON (nls.group_id = a.id) |
34ade5a6 FB |
121 | WHERE ' . $where); |
122 | if ($res->numRows() != 1) { | |
f6e35ff3 PC |
123 | if ($can_be_shortname && (is_int($id) || ctype_digit($id))) { |
124 | return Group::get($id, false); | |
125 | } | |
34ade5a6 FB |
126 | return null; |
127 | } | |
128 | return new Group($res->fetchOneAssoc()); | |
129 | } | |
130 | } | |
131 | ||
132 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
133 | ?> |