]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GroupPrivateMessage/Group_privacy_settings.php
Merge commit 'refs/merge-requests/157' of git://gitorious.org/statusnet/mainline...
[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 class Group_privacy_settings extends Memcached_DataObject
48 {
49     public $__table = 'group_privacy_settings';
50     /** ID of the group. */
51     public $group_id;
52     /** When to allow privacy: always, sometimes, or never. */
53     public $allow_privacy;
54     /** Who can send private messages: everyone, member, admin */
55     public $allow_sender;
56     /** row creation timestamp */
57     public $created;
58     /** Last-modified timestamp */
59     public $modified;
60
61     /** NEVER is */
62
63     const SOMETIMES = -1;
64     const NEVER  = 0;
65     const ALWAYS = 1;
66
67     /** These are bit-mappy, as a hedge against the future. */
68
69     const EVERYONE = 1;
70     const MEMBER   = 2;
71     const ADMIN    = 4;
72
73     /**
74      * Get an instance by key
75      *
76      * This is a utility method to get a single instance with a given key value.
77      *
78      * @param string $k Key to use to lookup (usually 'user_id' for this class)
79      * @param mixed  $v Value to lookup
80      *
81      * @return User_greeting_count object found, or null for no hits
82      */
83     function staticGet($k, $v=null)
84     {
85         return Memcached_DataObject::staticGet('Group_privacy_settings', $k, $v);
86     }
87
88     /**
89      * return table definition for DB_DataObject
90      *
91      * DB_DataObject needs to know something about the table to manipulate
92      * instances. This method provides all the DB_DataObject needs to know.
93      *
94      * @return array array of column definitions
95      */
96     function table()
97     {
98         return array('group_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
99                      'allow_privacy' => DB_DATAOBJECT_INT,
100                      'allow_sender' => DB_DATAOBJECT_INT,
101                      'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
102                      'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
103
104     }
105
106     /**
107      * return key definitions for DB_DataObject
108      *
109      * DB_DataObject needs to know about keys that the table has, since it
110      * won't appear in StatusNet's own keys list. In most cases, this will
111      * simply reference your keyTypes() function.
112      *
113      * @return array list of key field names
114      */
115     function keys()
116     {
117         return array_keys($this->keyTypes());
118     }
119
120     /**
121      * return key definitions for Memcached_DataObject
122      *
123      * @return array associative array of key definitions, field name to type:
124      *         'K' for primary key: for compound keys, add an entry for each component;
125      *         'U' for unique keys: compound keys are not well supported here.
126      */
127     function keyTypes()
128     {
129         return array('group_id' => 'K');
130     }
131
132     /**
133      * Magic formula for non-autoincrementing integer primary keys
134      *
135      * @return array magic three-false array that stops auto-incrementing.
136      */
137     function sequenceKey()
138     {
139         return array(false, false, false);
140     }
141
142     function forGroup($group)
143     {
144         $gps = Group_privacy_settings::staticGet('group_id', $group->id);
145
146         if (empty($gps)) {
147             // make a fake one with defaults
148             $gps = new Group_privacy_settings();
149             $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
150             $gps->allow_sender  = Group_privacy_settings::MEMBER;
151         }
152
153         return $gps;
154     }
155
156     function ensurePost($user, $group)
157     {
158         $gps = self::forGroup($group);
159
160         if ($gps->allow_privacy == Group_privacy_settings::NEVER) {
161             // TRANS: Exception thrown when trying to set group privacy setting if group %s does not allow private messages.
162             throw new Exception(sprintf(_m('Group %s does not allow private messages.'),
163                                         $group->nickname));
164         }
165
166         switch ($gps->allow_sender) {
167         case Group_privacy_settings::EVERYONE:
168             $profile = $user->getProfile();
169             if (Group_block::isBlocked($group, $profile)) {
170                 // TRANS: Exception thrown when trying to send group private message while blocked from that group.
171                 // TRANS: %1$s is a user nickname, %2$s is a group nickname.
172                 throw new Exception(sprintf(_m('User %1$s is blocked from group %2$s.'),
173                                             $user->nickname,
174                                             $group->nickname));
175             }
176             break;
177         case Group_privacy_settings::MEMBER:
178             if (!$user->isMember($group)) {
179                 // TRANS: Exception thrown when trying to send group private message while not a member.
180                 // TRANS: %1$s is a user nickname, %2$s is a group nickname.
181                 throw new Exception(sprintf(_m('User %1$s is not a member of group %2$s.'),
182                                             $user->nickname,
183                                             $group->nickname));
184             }
185             break;
186         case Group_privacy_settings::ADMIN:
187             if (!$user->isAdmin($group)) {
188                 // TRANS: Exception thrown when trying to send group private message while not a group administrator.
189                 // TRANS: %1$s is a user nickname, %2$s is a group nickname.
190                 throw new Exception(sprintf(_m('User %1$s is not an administrator of group %2$s.'),
191                                             $user->nickname,
192                                             $group->nickname));
193             }
194             break;
195         default:
196             // TRANS: Exception thrown when encountering undefined group privacy settings.
197             // TRANS: %s is a group nickname.
198             throw new Exception(sprintf(_m('Unknown privacy settings for group %s.'),
199                                         $group->nickname));
200         }
201
202         return true;
203     }
204 }