]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GroupPrivateMessage/Group_privacy_settings.php
More info for a proper, fancy-url lighttpd setup
[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 Managed_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     public static function schemaDef()
74     {
75         return array(
76             'fields' => array(
77                 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'group_privacy_settings'),
78                 'allow_privacy' => array('type' => 'int', 'not null' => true, 'description' => 'sometimes=-1, never=0, always=1'),
79                 'allow_sender' => array('type' => 'int', 'not null' => true, 'description' => 'list of bit-mappy values in source code'),
80                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
81                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
82             ),
83             'primary key' => array('group_id'),
84             'foreign keys' => array(
85                 'group_privacy_settings_group_id_fkey' => array('user_group', array('group_id' => 'id')),
86             ),
87         );
88     }
89
90     function forGroup($group)
91     {
92         $gps = Group_privacy_settings::getKV('group_id', $group->id);
93
94         if (empty($gps)) {
95             // make a fake one with defaults
96             $gps = new Group_privacy_settings();
97             $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
98             $gps->allow_sender  = Group_privacy_settings::MEMBER;
99         }
100
101         return $gps;
102     }
103
104     function ensurePost($user, $group)
105     {
106         $gps = self::forGroup($group);
107
108         if ($gps->allow_privacy == Group_privacy_settings::NEVER) {
109             // TRANS: Exception thrown when trying to set group privacy setting if group %s does not allow private messages.
110             throw new Exception(sprintf(_m('Group %s does not allow private messages.'),
111                                         $group->nickname));
112         }
113
114         switch ($gps->allow_sender) {
115         case Group_privacy_settings::EVERYONE:
116             $profile = $user->getProfile();
117             if (Group_block::isBlocked($group, $profile)) {
118                 // TRANS: Exception thrown when trying to send group private message while blocked from that group.
119                 // TRANS: %1$s is a user nickname, %2$s is a group nickname.
120                 throw new Exception(sprintf(_m('User %1$s is blocked from group %2$s.'),
121                                             $user->nickname,
122                                             $group->nickname));
123             }
124             break;
125         case Group_privacy_settings::MEMBER:
126             if (!$user->isMember($group)) {
127                 // TRANS: Exception thrown when trying to send group private message while not a member.
128                 // TRANS: %1$s is a user nickname, %2$s is a group nickname.
129                 throw new Exception(sprintf(_m('User %1$s is not a member of group %2$s.'),
130                                             $user->nickname,
131                                             $group->nickname));
132             }
133             break;
134         case Group_privacy_settings::ADMIN:
135             if (!$user->isAdmin($group)) {
136                 // TRANS: Exception thrown when trying to send group private message while not a group administrator.
137                 // TRANS: %1$s is a user nickname, %2$s is a group nickname.
138                 throw new Exception(sprintf(_m('User %1$s is not an administrator of group %2$s.'),
139                                             $user->nickname,
140                                             $group->nickname));
141             }
142             break;
143         default:
144             // TRANS: Exception thrown when encountering undefined group privacy settings.
145             // TRANS: %s is a group nickname.
146             throw new Exception(sprintf(_m('Unknown privacy settings for group %s.'),
147                                         $group->nickname));
148         }
149
150         return true;
151     }
152 }