Merge branch 'xorg/maint'
[platal.git] / classes / group.php
CommitLineData
34ade5a6
FB
1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 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
674333e7
SJ
97 public function iterToNotify()
98 {
99 if ($this->data['notify_all']) {
100 $condition = UFC_Group::BOTH;
101 } else {
102 $condition = UFC_Group::NOTIFIED;
103 }
104 $uf = New UserFilter(New UFC_Group($this->id, true, $condition));
105 return $uf->iterUsers();
106 }
107
833a6e86
FB
108 public function getLogo($fallback = true)
109 {
110 if (!empty($this->logo)) {
111 return PlImage::fromData($this->logo, $this->logo_mime);
112 } else if ($fallback) {
113 return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/dflt_carre.jpg', 'image/jpeg');
114 }
115 return null;
116 }
117
f6e35ff3 118 static public function get($id, $can_be_shortname = true)
34ade5a6
FB
119 {
120 if (!$id) {
121 return null;
122 }
f6e35ff3
PC
123 if (!$can_be_shortname) {
124 $where = XDB::format('a.id = {?}', $id);
34ade5a6 125 } else {
f6e35ff3 126 $where = XDB::format('a.diminutif = {?}', $id);
34ade5a6
FB
127 }
128 $res = XDB::query('SELECT a.*, d.nom AS domnom,
129 FIND_IN_SET(\'wiki_desc\', a.flags) AS wiki_desc,
c40a0fb3 130 FIND_IN_SET(\'notif_unsub\', a.flags) AS notif_unsub,
ed3c2129 131 FIND_IN_SET(\'notify_all\', a.flags) AS notify_all,
baab50c7 132 (nls.id IS NOT NULL) AS has_nl, ad.text AS address,
a174a707 133 p.display_tel AS phone, f.display_tel AS fax
eb41eda9
FB
134 FROM groups AS a
135 LEFT JOIN group_dom AS d ON d.id = a.dom
c40a0fb3 136 LEFT JOIN newsletters AS nls ON (nls.group_id = a.id)
a174a707
SJ
137 LEFT JOIN profile_phones AS p ON (p.link_type = \'group\' AND p.link_id = a.id AND p.tel_id = 0)
138 LEFT JOIN profile_phones AS f ON (f.link_type = \'group\' AND f.link_id = a.id AND f.tel_id = 1)
baab50c7 139 LEFT JOIN profile_addresses AS ad ON (ad.type = \'group\' AND ad.groupid = a.id)
34ade5a6
FB
140 WHERE ' . $where);
141 if ($res->numRows() != 1) {
f6e35ff3
PC
142 if ($can_be_shortname && (is_int($id) || ctype_digit($id))) {
143 return Group::get($id, false);
144 }
34ade5a6
FB
145 return null;
146 }
8c5bbd3f
SJ
147 $data = $res->fetchOneAssoc();
148 $positions = XDB::fetchAllAssoc('SELECT position, uid
149 FROM group_members
150 WHERE asso_id = {?} AND position IS NOT NULL
151 ORDER BY position',
152 $data['id']);
153 return new Group(array_merge($data, array('positions' => $positions)));
34ade5a6 154 }
3a299a1a
SJ
155
156 static public function subscribe($group_id, $uid)
157 {
158 XDB::execute('DELETE FROM group_former_members
159 WHERE uid = {?} AND asso_id = {?}',
160 $uid, $group_id);
161 XDB::execute('INSERT IGNORE INTO group_members (asso_id, uid)
162 VALUES ({?}, {?})',
163 $group_id, $uid);
164 }
165
49056fe2 166 static public function unsubscribe($group_id, $uid, $remember)
3a299a1a 167 {
49056fe2 168 XDB::execute('INSERT INTO group_former_members (asso_id, uid, remember, unsubsciption_date)
c4643e47
AL
169 VALUES ({?}, {?}, {?}, NOW())
170 ON DUPLICATE KEY UPDATE remember = {?}, unsubsciption_date = NOW()',
171 $group_id, $uid, $remember, $remember);
3a299a1a
SJ
172 XDB::execute('DELETE FROM group_members
173 WHERE uid = {?} AND asso_id = {?}',
174 $uid, $group_id);
593768bd
SJ
175 self::fix_notification($group_id);
176 }
177
178 static private function fix_notification($group_id)
179 {
180 $count = XDB::fetchOneCell("SELECT COUNT(uid)
181 FROM group_members
182 WHERE asso_id = {?} AND perms = 'admin' AND FIND_IN_SET('notify', flags)",
183 $group_id);
184 if ($count == 0) {
185 XDB::execute("UPDATE groups
186 SET flags = IF(flags = '', 'notify_all', CONCAT(flags, ',', 'notify_all'))
187 WHERE id = {?}",
188 $group_id);
189 }
3a299a1a 190 }
34ade5a6
FB
191}
192
448c8cdc 193// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
34ade5a6 194?>