Moving to GitHub.
[platal.git] / classes / group.php
index 477bcf9..9fe100b 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -94,6 +94,17 @@ class Group
         return $uf->iterUsers($limit);
     }
 
+    public function iterToNotify()
+    {
+        if ($this->data['notify_all']) {
+            $condition = UFC_Group::BOTH;
+        } else {
+            $condition = UFC_Group::NOTIFIED;
+        }
+        $uf = New UserFilter(New UFC_Group($this->id, true, $condition));
+        return $uf->iterUsers();
+    }
+
     public function getLogo($fallback = true)
     {
         if (!empty($this->logo)) {
@@ -117,13 +128,15 @@ class Group
         $res = XDB::query('SELECT  a.*, d.nom AS domnom,
                                    FIND_IN_SET(\'wiki_desc\', a.flags) AS wiki_desc,
                                    FIND_IN_SET(\'notif_unsub\', a.flags) AS notif_unsub,
-                                   (nls.id IS NOT NULL) AS has_nl,
+                                   FIND_IN_SET(\'notify_all\', a.flags) AS notify_all,
+                                   (nls.id IS NOT NULL) AS has_nl, ad.text AS address,
                                    p.display_tel AS phone, f.display_tel AS fax
                              FROM  groups AS a
                         LEFT JOIN  group_dom  AS d ON d.id = a.dom
                         LEFT JOIN  newsletters AS nls ON (nls.group_id = a.id)
                         LEFT JOIN  profile_phones AS p ON (p.link_type = \'group\' AND p.link_id = a.id AND p.tel_id = 0)
                         LEFT JOIN  profile_phones AS f ON (f.link_type = \'group\' AND f.link_id = a.id AND f.tel_id = 1)
+                        LEFT JOIN  profile_addresses AS ad ON (ad.type = \'group\' AND ad.groupid = a.id)
                             WHERE  ' . $where);
         if ($res->numRows() != 1) {
             if ($can_be_shortname && (is_int($id) || ctype_digit($id))) {
@@ -139,7 +152,43 @@ class Group
                                         $data['id']);
         return new Group(array_merge($data, array('positions' => $positions)));
     }
+
+    static public function subscribe($group_id, $uid)
+    {
+        XDB::execute('DELETE FROM  group_former_members
+                            WHERE  uid = {?} AND asso_id = {?}',
+                     $uid, $group_id);
+        XDB::execute('INSERT IGNORE INTO  group_members (asso_id, uid)
+                                  VALUES  ({?}, {?})',
+                     $group_id, $uid);
+    }
+
+    static public function unsubscribe($group_id, $uid, $remember)
+    {
+        XDB::execute('INSERT INTO  group_former_members (asso_id, uid, remember, unsubsciption_date)
+                           VALUES  ({?}, {?}, {?}, NOW())
+          ON DUPLICATE KEY UPDATE  remember = {?}, unsubsciption_date = NOW()',
+                     $group_id, $uid, $remember, $remember);
+        XDB::execute('DELETE FROM  group_members
+                            WHERE  uid = {?} AND asso_id = {?}',
+                     $uid, $group_id);
+        self::fix_notification($group_id);
+    }
+
+    static private function fix_notification($group_id)
+    {
+        $count = XDB::fetchOneCell("SELECT  COUNT(uid)
+                                      FROM  group_members
+                                     WHERE  asso_id = {?} AND perms = 'admin' AND FIND_IN_SET('notify', flags)",
+                                   $group_id);
+        if ($count == 0) {
+            XDB::execute("UPDATE  groups
+                             SET  flags = IF(flags = '', 'notify_all', CONCAT(flags, ',', 'notify_all'))
+                           WHERE  id = {?}",
+                         $group_id);
+        }
+    }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>