Improve newsletter.bounces.processor.py:
[platal.git] / classes / group.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2013 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
22 class Group
23 {
24 const CAT_GROUPESX = "GroupesX";
25 const CAT_BINETS = "Binets";
26 const CAT_PROMOTIONS = "Promotions";
27 const CAT_INSTITUTIONS = "Institutions";
28
29 public $id;
30 public $shortname;
31 private $data = array();
32
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 if (!is_null($this->axDate)) {
41 $this->axDate = format_datetime($this->axDate, '%d/%m/%Y');
42 }
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
63 private function getUF($admin = false, $extra_cond = null, $sort = null)
64 {
65 $cond = new PFC_And(new UFC_Group($this->id, $admin), new PFC_Not(new UFC_Dead()));
66 if (!is_null($extra_cond)) {
67 $cond->addChild($extra_cond);
68 }
69 if ($this->cat == self::CAT_PROMOTIONS) {
70 $cond->addChild(new UFC_Registered());
71 }
72 return new UserFilter($cond, $sort);
73 }
74
75 public function getMembersFilter($extra_cond = null, $sort = null)
76 {
77 return $this->getUF(false, $extra_cond, $sort);
78 }
79
80 public function getAdminsFilter($extra_cond = null, $sort = null)
81 {
82 return $this->getUF(true, $extra_cond, $sort);
83 }
84
85 public function iterMembers($extra_cond = null, $sort = null, $limit = null)
86 {
87 $uf = $this->getMembersFilter($extra_cond, $sort);
88 return $uf->iterUsers($limit);
89 }
90
91 public function iterAdmins($extra_cond = null, $sort = null, $limit = null)
92 {
93 $uf = $this->getAdminsFilter($extra_cond, $sort);
94 return $uf->iterUsers($limit);
95 }
96
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
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
118 static public function get($id, $can_be_shortname = true)
119 {
120 if (!$id) {
121 return null;
122 }
123 if (!$can_be_shortname) {
124 $where = XDB::format('a.id = {?}', $id);
125 } else {
126 $where = XDB::format('a.diminutif = {?}', $id);
127 }
128 $res = XDB::query('SELECT a.*, d.nom AS domnom,
129 FIND_IN_SET(\'wiki_desc\', a.flags) AS wiki_desc,
130 FIND_IN_SET(\'notif_unsub\', a.flags) AS notif_unsub,
131 FIND_IN_SET(\'notify_all\', a.flags) AS notify_all,
132 (nls.id IS NOT NULL) AS has_nl, ad.text AS address,
133 p.display_tel AS phone, f.display_tel AS fax
134 FROM groups AS a
135 LEFT JOIN group_dom AS d ON d.id = a.dom
136 LEFT JOIN newsletters AS nls ON (nls.group_id = a.id)
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)
139 LEFT JOIN profile_addresses AS ad ON (ad.type = \'group\' AND ad.groupid = a.id)
140 WHERE ' . $where);
141 if ($res->numRows() != 1) {
142 if ($can_be_shortname && (is_int($id) || ctype_digit($id))) {
143 return Group::get($id, false);
144 }
145 return null;
146 }
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)));
154 }
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
166 static public function unsubscribe($group_id, $uid, $remember)
167 {
168 XDB::execute('INSERT INTO group_former_members (asso_id, uid, remember, unsubsciption_date)
169 VALUES ({?}, {?}, {?}, NOW())',
170 $group_id, $uid, $remember);
171 XDB::execute('DELETE FROM group_members
172 WHERE uid = {?} AND asso_id = {?}',
173 $uid, $group_id);
174 self::fix_notification($group_id);
175 }
176
177 static private function fix_notification($group_id)
178 {
179 $count = XDB::fetchOneCell("SELECT COUNT(uid)
180 FROM group_members
181 WHERE asso_id = {?} AND perms = 'admin' AND FIND_IN_SET('notify', flags)",
182 $group_id);
183 if ($count == 0) {
184 XDB::execute("UPDATE groups
185 SET flags = IF(flags = '', 'notify_all', CONCAT(flags, ',', 'notify_all'))
186 WHERE id = {?}",
187 $group_id);
188 }
189 }
190 }
191
192 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
193 ?>