3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Widget showing a drop-down of potential addressees
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2011 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Widget showing a drop-down of potential addressees
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2011 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
47 class ToSelector extends Widget
58 * @param HTMLOutputter $out output context
59 * @param User $user Current user
60 * @param mixed $to Default selection for addressee
62 function __construct($out, $user, $to, $private=false, $id='notice_to', $name='notice_to')
64 parent::__construct($out);
68 $this->private = $private;
76 * @param HTMLOutputter $out output context
77 * @param User $user Current user
78 * @param mixed $to Default selection for addressee
83 $default = common_config('site', 'private') ? 'public:site' : 'public:everyone';
85 $groups = $this->user->getGroups();
87 while ($groups instanceof User_group && $groups->fetch()) {
88 $value = 'group:'.$groups->getID();
89 if (($this->to instanceof User_group) && $this->to->id == $groups->id) {
92 $choices[$value] = "!{$groups->getNickname()} [{$groups->getBestName()}]";
95 // Add subscribed users to dropdown menu
96 $users = $this->user->getSubscribed();
97 while ($users->fetch()) {
98 $value = 'profile:'.$users->getID();
100 $choices[$value] = substr($users->getAcctUri(), 5) . " [{$users->getBestName()}]";
101 } catch (ProfileNoAcctUriException $e) {
102 $choices[$value] = "[?@?] " . $e->profile->getBestName();
106 if ($this->to instanceof Profile) {
107 $value = 'profile:'.$this->to->getID();
110 $choices[$value] = substr($this->to->getAcctUri(), 5) . " [{$this->to->getBestName()}]";
111 } catch (ProfileNoAcctUriException $e) {
112 $choices[$value] = "[?@?] " . $e->profile->getBestName();
116 // alphabetical order
119 // Reverse so we can add entries at the end (can't unshift with a key)
120 $choices = array_reverse($choices);
122 if (common_config('notice', 'allowprivate')) {
123 // TRANS: Option in drop-down of potential addressees.
124 // TRANS: %s is a StatusNet sitename.
125 $choices['public:site'] = sprintf(_('Everyone at %s'), common_config('site', 'name'));
128 if (!common_config('site', 'private')) {
129 // TRANS: Option in drop-down of potential addressees.
130 $choices['public:everyone'] = _m('SENDTO','Everyone');
134 $choices = array_reverse($choices);
136 $this->out->dropdown($this->id,
137 // TRANS: Label for drop-down of potential addressees.
144 $this->out->elementStart('span', 'checkbox-wrapper');
145 if (common_config('notice', 'allowprivate')) {
146 $this->out->checkbox('notice_private',
147 // TRANS: Checkbox label in widget for selecting potential addressees to mark the notice private.
151 $this->out->elementEnd('span');
154 static function fillActivity(Action $action, Activity $act, array &$options)
156 if (!$act->context instanceof ActivityContext) {
157 $act->context = new ActivityContext();
159 self::fillOptions($action, $options);
160 if (isset($options['groups'])) {
161 foreach ($options['groups'] as $group_id) {
162 $group = User_group::getByID($group_id);
163 $act->context->attention[$group->getUri()] = $group->getObjectType();
166 if (isset($options['replies'])) {
167 foreach ($options['replies'] as $profile_uri) {
168 $profile = Profile::fromUri($profile_uri);
169 $act->context->attention[$profile->getUri()] = $profile->getObjectType();
174 static function fillOptions($action, &$options)
176 // XXX: make arg name selectable
177 $toArg = $action->trimmed('notice_to');
178 $private = common_config('notice', 'allowprivate') ? $action->boolean('notice_private') : false;
184 list($prefix, $value) = explode(':', $toArg);
187 $options['groups'] = array($value);
189 $options['scope'] = Notice::GROUP_SCOPE;
193 $profile = Profile::getKV('id', $value);
194 $options['replies'] = array($profile->getUri());
196 $options['scope'] = Notice::ADDRESSEE_SCOPE;
200 if ($value == 'everyone' && !common_config('site', 'private')) {
201 $options['scope'] = 0;
202 } else if ($value == 'site') {
203 $options['scope'] = Notice::SITE_SCOPE;
207 // TRANS: Client exception thrown in widget for selecting potential addressees when an invalid fill option was received.
208 throw new ClientException(sprintf(_('Unknown to value: "%s".'),$toArg));