2 /***************************************************************************
3 * Copyright (C) 2003-2013 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
24 const CAT_GROUPESX
= "GroupesX";
25 const CAT_BINETS
= "Binets";
26 const CAT_PROMOTIONS
= "Promotions";
27 const CAT_INSTITUTIONS
= "Institutions";
31 private $data = array();
33 private function __construct(array $data)
35 foreach ($data as $key=>$value) {
36 $this->data
[$key] = $value;
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');
45 public function __get($name)
47 if (property_exists($this, $name)) {
51 if (isset($this->data
[$name])) {
52 return $this->data
[$name];
58 public function __isset($name)
60 return property_exists($this, $name) ||
isset($this->data
[$name]);
63 private function getUF($admin = false
, $extra_cond = null
, $sort = null
)
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);
69 if ($this->cat
== self
::CAT_PROMOTIONS
) {
70 $cond->addChild(new UFC_Registered());
72 return new UserFilter($cond, $sort);
75 public function getMembersFilter($extra_cond = null
, $sort = null
)
77 return $this->getUF(false
, $extra_cond, $sort);
80 public function getAdminsFilter($extra_cond = null
, $sort = null
)
82 return $this->getUF(true
, $extra_cond, $sort);
85 public function iterMembers($extra_cond = null
, $sort = null
, $limit = null
)
87 $uf = $this->getMembersFilter($extra_cond, $sort);
88 return $uf->iterUsers($limit);
91 public function iterAdmins($extra_cond = null
, $sort = null
, $limit = null
)
93 $uf = $this->getAdminsFilter($extra_cond, $sort);
94 return $uf->iterUsers($limit);
97 public function iterToNotify()
99 if ($this->data
['notify_all']) {
100 $condition = UFC_Group
::BOTH
;
102 $condition = UFC_Group
::NOTIFIED
;
104 $uf = New UserFilter(New UFC_Group($this->id
, true
, $condition));
105 return $uf->iterUsers();
108 public function getLogo($fallback = true
)
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');
118 static public function get($id, $can_be_shortname = true
)
123 if (!$can_be_shortname) {
124 $where = XDB
::format('a.id = {?}', $id);
126 $where = XDB
::format('a.diminutif = {?}', $id);
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
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)
141 if ($res->numRows() != 1) {
142 if ($can_be_shortname && (is_int($id) ||
ctype_digit($id))) {
143 return Group
::get($id, false
);
147 $data = $res->fetchOneAssoc();
148 $positions = XDB
::fetchAllAssoc('SELECT position, uid
150 WHERE asso_id = {?} AND position IS NOT NULL
153 return new Group(array_merge($data, array('positions' => $positions)));
156 static public function subscribe($group_id, $uid)
158 XDB
::execute('DELETE FROM group_former_members
159 WHERE uid = {?} AND asso_id = {?}',
161 XDB
::execute('INSERT IGNORE INTO group_members (asso_id, uid)
166 static public function unsubscribe($group_id, $uid, $remember)
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 = {?}',
174 self
::fix_notification($group_id);
177 static private function fix_notification($group_id)
179 $count = XDB
::fetchOneCell("SELECT COUNT(uid)
181 WHERE asso_id = {?} AND perms = 'admin' AND FIND_IN_SET('notify', flags)",
184 XDB
::execute("UPDATE groups
185 SET flags = IF(flags = '', 'notify_all', CONCAT(flags, ',', 'notify_all'))
192 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: