]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatusgroup.php
Merge branch '0.9.x'
[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') && !defined('LACONICA')) { exit(1); }
26
27 /**
28  * Key UI methods:
29  *
30  *  showInputForm() - form asking for a remote profile account or URL
31  *                    We end up back here on errors
32  *
33  *  showPreviewForm() - surrounding form for preview-and-confirm
34  *    preview() - display profile for a remote group
35  *
36  *  success() - redirects to groups page on join
37  */
38 class OStatusGroupAction extends OStatusSubAction
39 {
40     protected $profile_uri; // provided acct: or URI of remote entity
41     protected $oprofile; // Ostatus_profile of remote entity, if valid
42
43
44     function validateRemoteProfile()
45     {
46         if (!$this->oprofile->isGroup()) {
47             // Send us to the user subscription form for conf
48             $target = common_local_url('ostatussub', array(), array('profile' => $this->profile_uri));
49             common_redirect($target, 303);
50         }
51     }
52
53     /**
54      * Show the initial form, when we haven't yet been given a valid
55      * remote profile.
56      */
57     function showInputForm()
58     {
59         $user = common_current_user();
60
61         $profile = $user->getProfile();
62
63         $this->elementStart('form', array('method' => 'post',
64                                           'id' => 'form_ostatus_sub',
65                                           'class' => 'form_settings',
66                                           'action' => $this->selfLink()));
67
68         $this->hidden('token', common_session_token());
69
70         $this->elementStart('fieldset', array('id' => 'settings_feeds'));
71
72         $this->elementStart('ul', 'form_data');
73         $this->elementStart('li');
74         $this->input('profile',
75                      // TRANS: Field label.
76                      _m('Join group'),
77                      $this->profile_uri,
78                      // TRANS: Tooltip for field label "Join group".
79                      _m("OStatus group's address, like http://example.net/group/nickname."));
80         $this->elementEnd('li');
81         $this->elementEnd('ul');
82
83         // TRANS: Button text.
84         $this->submit('validate', _m('BUTTON','Continue'));
85
86         $this->elementEnd('fieldset');
87
88         $this->elementEnd('form');
89     }
90
91     /**
92      * Show a preview for a remote group's profile
93      * @return boolean true if we're ok to try joining
94      */
95     function preview()
96     {
97         $oprofile = $this->oprofile;
98         $group = $oprofile->localGroup();
99
100         $cur = common_current_user();
101         if ($cur->isMember($group)) {
102             $this->element('div', array('class' => 'error'),
103                            _m("You are already a member of this group."));
104             $ok = false;
105         } else {
106             $ok = true;
107         }
108
109         $this->showEntity($group,
110                           $group->homeUrl(),
111                           $group->homepage_logo,
112                           $group->description);
113         return $ok;
114     }
115
116     /**
117      * Redirect on successful remote group join
118      */
119     function success()
120     {
121         $cur = common_current_user();
122         $url = common_local_url('usergroups', array('nickname' => $cur->nickname));
123         common_redirect($url, 303);
124     }
125
126     /**
127      * Attempt to finalize subscription.
128      * validateFeed must have been run first.
129      *
130      * Calls showForm on failure or success on success.
131      */
132     function saveFeed()
133     {
134         $user = common_current_user();
135         $group = $this->oprofile->localGroup();
136         if ($user->isMember($group)) {
137             // TRANS: OStatus remote group subscription dialog error.
138             $this->showForm(_m('Already a member!'));
139             return;
140         }
141
142         if (Event::handle('StartJoinGroup', array($group, $user))) {
143             $ok = Group_member::join($this->oprofile->group_id, $user->id);
144             if ($ok) {
145                 Event::handle('EndJoinGroup', array($group, $user));
146                 $this->success();
147             } else {
148                 // TRANS: OStatus remote group subscription dialog error.
149                 $this->showForm(_m('Remote group join failed!'));
150             }
151         } else {
152             // TRANS: OStatus remote group subscription dialog error.
153             $this->showForm(_m('Remote group join aborted!'));
154         }
155     }
156
157     /**
158      * Title of the page
159      *
160      * @return string Title of the page
161      */
162     function title()
163     {
164         // TRANS: Page title for OStatus remote group join form
165         return _m('Confirm joining remote group');
166     }
167
168     /**
169      * Instructions for use
170      *
171      * @return instructions for use
172      */
173     function getInstructions()
174     {
175         // TRANS: Instructions.
176         return _m('You can subscribe to groups from other supported sites. Paste the group\'s profile URI below:');
177     }
178
179     function selfLink()
180     {
181         return common_local_url('ostatusgroup');
182     }
183 }