Commit | Line | Data |
---|---|---|
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 | ||
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']; | |
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 | 119 | FIND_IN_SET(\'notif_unsub\', a.flags) AS notif_unsub, |
baab50c7 | 120 | (nls.id IS NOT NULL) AS has_nl, ad.text AS address, |
a174a707 | 121 | p.display_tel AS phone, f.display_tel AS fax |
eb41eda9 FB |
122 | FROM groups AS a |
123 | LEFT JOIN group_dom AS d ON d.id = a.dom | |
c40a0fb3 | 124 | LEFT JOIN newsletters AS nls ON (nls.group_id = a.id) |
a174a707 SJ |
125 | LEFT JOIN profile_phones AS p ON (p.link_type = \'group\' AND p.link_id = a.id AND p.tel_id = 0) |
126 | LEFT JOIN profile_phones AS f ON (f.link_type = \'group\' AND f.link_id = a.id AND f.tel_id = 1) | |
baab50c7 | 127 | LEFT JOIN profile_addresses AS ad ON (ad.type = \'group\' AND ad.groupid = a.id) |
34ade5a6 FB |
128 | WHERE ' . $where); |
129 | if ($res->numRows() != 1) { | |
f6e35ff3 PC |
130 | if ($can_be_shortname && (is_int($id) || ctype_digit($id))) { |
131 | return Group::get($id, false); | |
132 | } | |
34ade5a6 FB |
133 | return null; |
134 | } | |
8c5bbd3f SJ |
135 | $data = $res->fetchOneAssoc(); |
136 | $positions = XDB::fetchAllAssoc('SELECT position, uid | |
137 | FROM group_members | |
138 | WHERE asso_id = {?} AND position IS NOT NULL | |
139 | ORDER BY position', | |
140 | $data['id']); | |
141 | return new Group(array_merge($data, array('positions' => $positions))); | |
34ade5a6 FB |
142 | } |
143 | } | |
144 | ||
145 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
146 | ?> |