]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/toselector.php
Only show "public:site" in ToSelector if notice/allowprivate is true
[quix0rs-gnu-social.git] / lib / toselector.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Widget showing a drop-down of potential addressees
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  Widget
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Widget showing a drop-down of potential addressees
39  *
40  * @category  Widget
41  * @package   StatusNet
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/
46  */
47 class ToSelector extends Widget
48 {
49     protected $user;
50     protected $to;
51     protected $id;
52     protected $name;
53     protected $private;
54
55     /**
56      * Constructor
57      *
58      * @param HTMLOutputter $out  output context
59      * @param User          $user Current user
60      * @param mixed         $to   Default selection for addressee
61      */
62     function __construct($out, $user, $to, $private=false, $id='notice_to', $name='notice_to')
63     {
64         parent::__construct($out);
65
66         $this->user    = $user;
67         $this->to      = $to;
68         $this->private = $private;
69         $this->id      = $id;
70         $this->name    = $name;
71     }
72
73     /**
74      * Constructor
75      *
76      * @param HTMLOutputter $out  output context
77      * @param User          $user Current user
78      * @param mixed         $to   Default selection for addressee
79      */
80     function show()
81     {
82         $choices = array();
83         $default = common_config('site', 'private') ? 'public:site' : 'public:everyone';
84
85         $groups = $this->user->getGroups();
86
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) {
90                 $default = $value;
91             }
92             $choices[$value] = "!{$groups->getNickname()} [{$groups->getBestName()}]";
93         }
94
95         // Add subscribed users to dropdown menu
96         $users = $this->user->getSubscribed();
97         while ($users->fetch()) {
98             $value = 'profile:'.$users->getID();
99             try {
100                 $choices[$value] = substr($users->getAcctUri(), 5) . " [{$users->getBestName()}]";
101             } catch (ProfileNoAcctUriException $e) {
102                 $choices[$value] = "[?@?] " . $e->profile->getBestName();
103             }
104         }
105
106         if ($this->to instanceof Profile) {
107             $value = 'profile:'.$this->to->getID();
108             $default = $value;
109             try {
110                 $choices[$value] = substr($this->to->getAcctUri(), 5) . " [{$this->to->getBestName()}]";
111             } catch (ProfileNoAcctUriException $e) {
112                 $choices[$value] = "[?@?] " . $e->profile->getBestName();
113             }
114         }
115
116         // alphabetical order
117         asort($choices);
118
119         // Reverse so we can add entries at the end (can't unshift with a key)
120         $choices = array_reverse($choices);
121
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'));
126         }
127
128         if (!common_config('site', 'private')) {
129             // TRANS: Option in drop-down of potential addressees.
130             $choices['public:everyone'] = _m('SENDTO','Everyone');
131         }
132
133         // Return the order
134         $choices = array_reverse($choices);
135
136         $this->out->dropdown($this->id,
137                              // TRANS: Label for drop-down of potential addressees.
138                              _m('LABEL','To:'),
139                              $choices,
140                              null,
141                              false,
142                              $default);
143
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.
148                                  _('Private?'),
149                                  $this->private);
150         }
151         $this->out->elementEnd('span');
152     }
153
154     static function fillActivity(Action $action, Activity $act, array &$options)
155     {
156         if (!$act->context instanceof ActivityContext) {
157             $act->context = new ActivityContext();
158         }
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();
164             }
165         }
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();
170             }
171         }
172     }
173
174     static function fillOptions($action, &$options)
175     {
176         // XXX: make arg name selectable
177         $toArg = $action->trimmed('notice_to');
178         $private = common_config('notice', 'allowprivate') ? $action->boolean('notice_private') : false;
179
180         if (empty($toArg)) {
181             return;
182         }
183
184         list($prefix, $value) = explode(':', $toArg);
185         switch ($prefix) {
186         case 'group':
187             $options['groups'] = array($value);
188             if ($private) {
189                 $options['scope'] = Notice::GROUP_SCOPE;
190             }
191             break;
192         case 'profile':
193             $profile = Profile::getKV('id', $value);
194             $options['replies'] = array($profile->getUri());
195             if ($private) {
196                 $options['scope'] = Notice::ADDRESSEE_SCOPE;
197             }
198             break;
199         case 'public':
200             if ($value == 'everyone' && !common_config('site', 'private')) {
201                 $options['scope'] = 0;
202             } else if ($value == 'site') {
203                 $options['scope'] = Notice::SITE_SCOPE;
204             }
205             break;
206         default:
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));
209             break;
210         }
211     }
212 }