]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/toselector.php
ToSelector widget to send private notices
[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
48 class ToSelector extends Widget
49 {
50     protected $user;
51     protected $to;
52     protected $id;
53     protected $name;
54     protected $private;
55
56     /**
57      * Constructor
58      *
59      * @param HTMLOutputter $out  output context
60      * @param User          $user Current user
61      * @param mixed         $to   Default selection for addressee
62      */
63     function __construct($out, $user, $to, $private=false, $id='notice_to', $name='notice_to')
64     {
65         parent::__construct($out);
66
67         $this->user    = $user;
68         $this->to      = $to;
69         $this->private = $private;
70         $this->id      = $id;
71         $this->name    = $name;
72     }
73
74     /**
75      * Constructor
76      *
77      * @param HTMLOutputter $out  output context
78      * @param User          $user Current user
79      * @param mixed         $to   Default selection for addressee
80      */
81     function show()
82     {
83         $choices = array();
84         $default = 'public:site';
85
86         if (!common_config('site', 'private')) {
87             $choices['public:everyone'] = _('Everyone');
88             $default = 'public:everyone';
89         }
90         // XXX: better name...?
91         $choices['public:site'] = sprintf(_('My colleagues at %s'), common_config('site', 'name'));
92         
93         $groups = $this->user->getGroups();
94
95         while ($groups->fetch()) {
96             $value = 'group:'.$groups->id;
97             if (($this->to instanceof User_group) && $this->to->id == $groups->id) {
98                 $default = $value;
99             }
100             $choices[$value] = $groups->getBestName();
101         }
102
103         // XXX: add users...?
104
105         if ($this->to instanceof Profile) {
106             $value = 'profile:'.$this->to->id;
107             $default = $value;
108             $choices[$value] = $this->to->getBestName();
109         }
110
111         $this->out->dropdown($this->id,
112                              _('To:'),
113                              $choices,
114                              null,
115                              false,
116                              $default);
117
118         $this->out->checkbox('notice_private',
119                              _('Private'),
120                              $this->private);
121     }
122
123     static function fillOptions($action, &$options)
124     {
125         // XXX: make arg name selectable
126         $toArg = $action->trimmed('notice_to');
127         $private = $action->boolean('notice_private');
128
129         list($prefix, $value) = explode(':', $toArg);
130         switch ($prefix) {
131         case 'group':
132             $options['groups'] = array($value);
133             if ($private) {
134                 $options['scope'] = Notice::GROUP_SCOPE;
135             }
136             break;
137         case 'profile':
138             $profile = Profile::staticGet('id', $value);
139             $options['replies'] = $profile->getUri();
140             if ($private) {
141                 $options['scope'] = Notice::ADDRESSEE_SCOPE;
142             }
143             break;
144         case 'public':
145             if ($value == 'everyone' && !common_config('site', 'private')) {
146                 $options['scope'] = 0;
147             } else if ($value == 'site') {
148                 $options['scope'] = Notice::SITE_SCOPE;
149             }
150             break;
151         default:
152             throw new ClientException('Unknown to value: ' . toArg);
153             break;
154         }
155     }
156 }