]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatusgroup.php
f2170c03466115fd913660e46080094509830982
[quix0rs-gnu-social.git] / plugins / OStatus / actions / ostatusgroup.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009-2010, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * @package OStatusPlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET')) {
26     exit(1);
27 }
28
29 /**
30  * Key UI methods:
31  *
32  *  showInputForm() - form asking for a remote profile account or URL
33  *                    We end up back here on errors
34  *
35  *  showPreviewForm() - surrounding form for preview-and-confirm
36  *    preview() - display profile for a remote group
37  *
38  *  success() - redirects to groups page on join
39  */
40 class OStatusGroupAction extends OStatusSubAction
41 {
42     protected $profile_uri; // provided acct: or URI of remote entity
43     protected $oprofile; // Ostatus_profile of remote entity, if valid
44
45
46     function validateRemoteProfile()
47     {
48         if (!$this->oprofile->isGroup()) {
49             // Send us to the user subscription form for conf
50             $target = common_local_url('ostatussub', array(), array('profile' => $this->profile_uri));
51             common_redirect($target, 303);
52         }
53     }
54
55     /**
56      * Show the initial form, when we haven't yet been given a valid
57      * remote profile.
58      */
59     function showInputForm()
60     {
61         $user = common_current_user();
62
63         $profile = $user->getProfile();
64
65         $this->elementStart('form', array('method' => 'post',
66                                           'id' => 'form_ostatus_sub',
67                                           'class' => 'form_settings',
68                                           'action' => $this->selfLink()));
69
70         $this->hidden('token', common_session_token());
71
72         $this->elementStart('fieldset', array('id' => 'settings_feeds'));
73
74         $this->elementStart('ul', 'form_data');
75         $this->elementStart('li');
76         $this->input('profile',
77                      // TRANS: Field label.
78                      _m('Join group'),
79                      $this->profile_uri,
80                      // TRANS: Tooltip for field label "Join group".
81                      _m("OStatus group's address, like http://example.net/group/nickname."));
82         $this->elementEnd('li');
83         $this->elementEnd('ul');
84
85         // TRANS: Button text.
86         $this->submit('validate', _m('BUTTON','Continue'));
87
88         $this->elementEnd('fieldset');
89
90         $this->elementEnd('form');
91     }
92
93     /**
94      * Show a preview for a remote group's profile
95      * @return boolean true if we're ok to try joining
96      */
97     function preview()
98     {
99         $oprofile = $this->oprofile;
100         $group = $oprofile->localGroup();
101
102         $cur = common_current_user();
103         if ($cur->isMember($group)) {
104             $this->element('div', array('class' => 'error'),
105                            // TRANS: Error text displayed when trying to join a remote group the user is already a member of.
106                            _m('You are already a member of this group.'));
107             $ok = false;
108         } else {
109             $ok = true;
110         }
111
112         $this->showEntity($group,
113                           $group->homeUrl(),
114                           $group->homepage_logo,
115                           $group->description);
116         return $ok;
117     }
118
119     /**
120      * Redirect on successful remote group join
121      */
122     function success()
123     {
124         $cur = common_current_user();
125         $url = common_local_url('usergroups', array('nickname' => $cur->nickname));
126         common_redirect($url, 303);
127     }
128
129     /**
130      * Attempt to finalize subscription.
131      * validateFeed must have been run first.
132      *
133      * Calls showForm on failure or success on success.
134      */
135     function saveFeed()
136     {
137         $user = common_current_user();
138         $group = $this->oprofile->localGroup();
139         if ($user->isMember($group)) {
140             // TRANS: OStatus remote group subscription dialog error.
141             $this->showForm(_m('Already a member!'));
142             return;
143         }
144
145         try {
146             $user->joinGroup($group);
147         } catch (Exception $e) {
148             // TRANS: OStatus remote group subscription dialog error.
149             $this->showForm(_m('Remote group join failed!'));
150             return;
151         }
152     }
153
154     /**
155      * Title of the page
156      *
157      * @return string Title of the page
158      */
159     function title()
160     {
161         // TRANS: Page title for OStatus remote group join form
162         return _m('Confirm joining remote group');
163     }
164
165     /**
166      * Instructions for use
167      *
168      * @return instructions for use
169      */
170     function getInstructions()
171     {
172         // TRANS: Form instructions.
173         return _m('You can subscribe to groups from other supported sites. Paste the group\'s profile URI below:');
174     }
175
176     function selfLink()
177     {
178         return common_local_url('ostatusgroup');
179     }
180 }