3 * Data class for group privacy settings
9 * @author Evan Prodromou <evan@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
30 if (!defined('STATUSNET')) {
35 * Data class for group privacy
37 * Stores admin preferences about the group.
41 * @author Evan Prodromou <evan@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43 * @link http://status.net/
47 class Group_privacy_settings extends Managed_DataObject
49 public $__table = 'group_privacy_settings';
50 /** ID of the group. */
52 /** When to allow privacy: always, sometimes, or never. */
53 public $allow_privacy;
54 /** Who can send private messages: everyone, member, admin */
56 /** row creation timestamp */
58 /** Last-modified timestamp */
67 /** These are bit-mappy, as a hedge against the future. */
74 * return table definition for DB_DataObject
76 * DB_DataObject needs to know something about the table to manipulate
77 * instances. This method provides all the DB_DataObject needs to know.
79 * @return array array of column definitions
83 return array('group_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
84 'allow_privacy' => DB_DATAOBJECT_INT,
85 'allow_sender' => DB_DATAOBJECT_INT,
86 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
87 'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
92 * return key definitions for DB_DataObject
94 * DB_DataObject needs to know about keys that the table has, since it
95 * won't appear in StatusNet's own keys list. In most cases, this will
96 * simply reference your keyTypes() function.
98 * @return array list of key field names
102 return array_keys($this->keyTypes());
106 * return key definitions for Memcached_DataObject
108 * @return array associative array of key definitions, field name to type:
109 * 'K' for primary key: for compound keys, add an entry for each component;
110 * 'U' for unique keys: compound keys are not well supported here.
114 return array('group_id' => 'K');
118 * Magic formula for non-autoincrementing integer primary keys
120 * @return array magic three-false array that stops auto-incrementing.
122 function sequenceKey()
124 return array(false, false, false);
127 function forGroup($group)
129 $gps = Group_privacy_settings::staticGet('group_id', $group->id);
132 // make a fake one with defaults
133 $gps = new Group_privacy_settings();
134 $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
135 $gps->allow_sender = Group_privacy_settings::MEMBER;
141 function ensurePost($user, $group)
143 $gps = self::forGroup($group);
145 if ($gps->allow_privacy == Group_privacy_settings::NEVER) {
146 // TRANS: Exception thrown when trying to set group privacy setting if group %s does not allow private messages.
147 throw new Exception(sprintf(_m('Group %s does not allow private messages.'),
151 switch ($gps->allow_sender) {
152 case Group_privacy_settings::EVERYONE:
153 $profile = $user->getProfile();
154 if (Group_block::isBlocked($group, $profile)) {
155 // TRANS: Exception thrown when trying to send group private message while blocked from that group.
156 // TRANS: %1$s is a user nickname, %2$s is a group nickname.
157 throw new Exception(sprintf(_m('User %1$s is blocked from group %2$s.'),
162 case Group_privacy_settings::MEMBER:
163 if (!$user->isMember($group)) {
164 // TRANS: Exception thrown when trying to send group private message while not a member.
165 // TRANS: %1$s is a user nickname, %2$s is a group nickname.
166 throw new Exception(sprintf(_m('User %1$s is not a member of group %2$s.'),
171 case Group_privacy_settings::ADMIN:
172 if (!$user->isAdmin($group)) {
173 // TRANS: Exception thrown when trying to send group private message while not a group administrator.
174 // TRANS: %1$s is a user nickname, %2$s is a group nickname.
175 throw new Exception(sprintf(_m('User %1$s is not an administrator of group %2$s.'),
181 // TRANS: Exception thrown when encountering undefined group privacy settings.
182 // TRANS: %s is a group nickname.
183 throw new Exception(sprintf(_m('Unknown privacy settings for group %s.'),