]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GroupPrivateMessage/Group_privacy_settings.php
Merge branch 'testing' into privategroup
[quix0rs-gnu-social.git] / plugins / GroupPrivateMessage / Group_privacy_settings.php
1 <?php
2 /**
3  * Data class for group privacy settings
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2011, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Data class for group privacy
36  *
37  * Stores admin preferences about the group.
38  *
39  * @category Action
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43  * @link     http://status.net/
44  *
45  * @see      DB_DataObject
46  */
47
48 class Group_privacy_settings extends Memcached_DataObject
49 {
50     public $__table = 'group_privacy_settings';
51     /** ID of the group. */
52     public $group_id;      
53     /** When to allow privacy: always, sometimes, or never. */
54     public $allow_privacy;
55     /** Who can send private messages: everyone, member, admin */
56     public $allow_sender; 
57     /** row creation timestamp */
58     public $created;
59     /** Last-modified timestamp */
60     public $modified;
61
62     /** NEVER is */
63
64     const SOMETIMES = -1;
65     const NEVER  = 0;
66     const ALWAYS = 1;
67
68     /** These are bit-mappy, as a hedge against the future. */
69
70     const EVERYONE = 1;
71     const MEMBER   = 2;
72     const ADMIN    = 4;
73
74     /**
75      * Get an instance by key
76      *
77      * This is a utility method to get a single instance with a given key value.
78      *
79      * @param string $k Key to use to lookup (usually 'user_id' for this class)
80      * @param mixed  $v Value to lookup
81      *
82      * @return User_greeting_count object found, or null for no hits
83      */
84
85     function staticGet($k, $v=null)
86     {
87         return Memcached_DataObject::staticGet('Group_privacy_settings', $k, $v);
88     }
89
90     /**
91      * return table definition for DB_DataObject
92      *
93      * DB_DataObject needs to know something about the table to manipulate
94      * instances. This method provides all the DB_DataObject needs to know.
95      *
96      * @return array array of column definitions
97      */
98
99     function table()
100     {
101         return array('group_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
102                      'allow_privacy' => DB_DATAOBJECT_INT,
103                      'allow_sender' => DB_DATAOBJECT_INT,
104                      'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
105                      'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
106                      
107     }
108
109     /**
110      * return key definitions for DB_DataObject
111      *
112      * DB_DataObject needs to know about keys that the table has, since it
113      * won't appear in StatusNet's own keys list. In most cases, this will
114      * simply reference your keyTypes() function.
115      *
116      * @return array list of key field names
117      */
118
119     function keys()
120     {
121         return array_keys($this->keyTypes());
122     }
123
124     /**
125      * return key definitions for Memcached_DataObject
126      *
127      * @return array associative array of key definitions, field name to type:
128      *         'K' for primary key: for compound keys, add an entry for each component;
129      *         'U' for unique keys: compound keys are not well supported here.
130      */
131
132     function keyTypes()
133     {
134         return array('group_id' => 'K');
135     }
136
137     /**
138      * Magic formula for non-autoincrementing integer primary keys
139      *
140      * @return array magic three-false array that stops auto-incrementing.
141      */
142
143     function sequenceKey()
144     {
145         return array(false, false, false);
146     }
147
148     function ensurePost($user, $group)
149     {
150         $gps = Group_privacy_settings::staticGet('group_id', $group->id);
151
152         if (empty($gps)) {
153             // make a fake one with defaults
154             $gps = new Group_privacy_settings();
155             $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
156             $gps->allow_sender  = Group_privacy_settings::MEMBER;
157         }
158
159         if ($gps->allow_privacy == Group_privacy_settings::NEVER) {
160             throw new Exception(sprintf(_('Group %s does not allow private messages.'),
161                                         $group->nickname));
162         }
163
164         switch ($gps->allow_sender) {
165         case Group_privacy_settings::EVERYONE:
166             $profile = $user->getProfile();
167             if (Group_block::isBlocked($group, $profile)) {
168                 throw new Exception(sprintf(_('User %s is blocked from group %s.'),
169                                             $user->nickname,
170                                             $group->nickname));
171             }
172             break;
173         case Group_privacy_settings::MEMBER:
174             if (!$user->isMember($group)) {
175                 throw new Exception(sprintf(_('User %s is not a member of group %s.'),
176                                             $user->nickname,
177                                             $group->nickname));
178             }
179             break;
180         case Group_privacy_settings::ADMIN:
181             if (!$user->isAdmin($group)) {
182                 throw new Exception(sprintf(_('User %s is not an administrator of group %s.'),
183                                             $user->nickname,
184                                             $group->nickname));
185             }
186             break;
187         default:
188             throw new Exception(sprintf(_('Unknown privacy settings for group %s.'),
189                                         $group->nickname));
190         }
191
192         return true;
193     }
194 }