3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 class InviteAction extends CurrentUserDesignAction
30 function isReadOnly($args)
35 function handle($args)
37 parent::handle($args);
38 if (!common_config('invite', 'enabled')) {
39 // TRANS: Client error displayed when trying to sent invites while they have been disabled.
40 $this->clientError(_('Invites have been disabled.'));
41 } else if (!common_logged_in()) {
42 // TRANS: Client error displayed when trying to sent invites while not logged in.
43 // TRANS: %s is the StatusNet site name.
44 $this->clientError(sprintf(_('You must be logged in to invite other users to use %s.'),
45 common_config('site', 'name')));
47 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
48 $this->sendInvitations();
54 function sendInvitations()
57 $token = $this->trimmed('token');
58 if (!$token || $token != common_session_token()) {
59 $this->showForm(_('There was a problem with your session token. Try again, please.'));
63 $user = common_current_user();
64 $profile = $user->getProfile();
66 $bestname = $profile->getBestName();
67 $sitename = common_config('site', 'name');
68 $personal = $this->trimmed('personal');
70 $addresses = explode("\n", $this->trimmed('addresses'));
72 foreach ($addresses as $email) {
73 $email = trim($email);
74 if (!Validate::email($email, common_config('email', 'check_domain'))) {
75 // TRANS: Form validation message when providing an e-mail address that does not validate.
76 // TRANS: %s is an invalid e-mail address.
77 $this->showForm(sprintf(_('Invalid email address: %s.'), $email));
82 $this->already = array();
83 $this->subbed = array();
85 foreach ($addresses as $email) {
86 $email = common_canonical_email($email);
87 $other = User::staticGet('email', $email);
89 if ($user->isSubscribed($other)) {
90 $this->already[] = $other;
92 subs_subscribe_to($user, $other);
93 $this->subbed[] = $other;
96 $this->sent[] = $email;
97 $this->sendInvitation($email, $user, $personal);
101 $this->mode = 'sent';
106 function showScripts()
108 parent::showScripts();
109 $this->autofocus('addresses');
114 if ($this->mode == 'sent') {
115 // TRANS: Page title when invitations have been sent.
116 return _('Invitations sent');
118 // TRANS: Page title when inviting potential users.
119 return _('Invite new users');
123 function showContent()
125 if ($this->mode == 'sent') {
126 $this->showInvitationSuccess();
128 $this->showInviteForm();
132 function showInvitationSuccess()
134 if ($this->already) {
135 // TRANS: Message displayed inviting users to use a StatusNet site while the inviting user
136 // TRANS: is already subscribed to one or more users with the given e-mail address(es).
137 // TRANS: Plural form is based on the number of reported already subscribed e-mail addresses.
138 // TRANS: Followed by a bullet list.
139 $this->element('p', null, _m('You are already subscribed to this user:',
140 'You are already subscribed to these users:',
141 count($this->already)));
142 $this->elementStart('ul');
143 foreach ($this->already as $other) {
144 // TRANS: Used as list item for already subscribed users (%1$s is nickname, %2$s is e-mail address).
145 $this->element('li', null, sprintf(_('%1$s (%2$s)'), $other->nickname, $other->email));
147 $this->elementEnd('ul');
150 // TRANS: Message displayed inviting users to use a StatusNet site while the invited user
151 // TRANS: already uses a this StatusNet site. Plural form is based on the number of
152 // TRANS: reported already present people. Followed by a bullet list.
153 $this->element('p', null, _m('This person is already a user and you were automatically subscribed:',
154 'These people are already users and you were automatically subscribed to them:',
155 count($this->subbed)));
156 $this->elementStart('ul');
157 foreach ($this->subbed as $other) {
158 // TRANS: Used as list item for already registered people (%1$s is nickname, %2$s is e-mail address).
159 $this->element('li', null, sprintf(_('%1$s (%2$s)'), $other->nickname, $other->email));
161 $this->elementEnd('ul');
164 // TRANS: Message displayed inviting users to use a StatusNet site. Plural form is
165 // TRANS: based on the number of invitations sent. Followed by a bullet list of
166 // TRANS: e-mail addresses to which invitations were sent.
167 $this->element('p', null, _m('Invitation sent to the following person:',
168 'Invitations sent to the following people:',
169 count($this->sent)));
170 $this->elementStart('ul');
171 foreach ($this->sent as $other) {
172 $this->element('li', null, $other);
174 $this->elementEnd('ul');
175 // TRANS: Generic message displayed after sending out one or more invitations to
176 // TRANS: people to join a StatusNet site.
177 $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
181 function showPageNotice()
183 if ($this->mode != 'sent') {
185 $this->element('p', 'error', $this->error);
187 $this->elementStart('div', 'instructions');
188 $this->element('p', null,
189 // TRANS: Form instructions.
190 _('Use this form to invite your friends and colleagues to use this service.'));
191 $this->elementEnd('div');
196 function showForm($error=null)
198 $this->mode = 'form';
199 $this->error = $error;
203 function showInviteForm()
205 $this->elementStart('form', array('method' => 'post',
206 'id' => 'form_invite',
207 'class' => 'form_settings',
208 'action' => common_local_url('invite')));
209 $this->elementStart('fieldset');
210 // TRANS: Form legend.
211 $this->element('legend', null, 'Send an invitation');
212 $this->hidden('token', common_session_token());
214 $this->elementStart('ul', 'form_data');
215 $this->elementStart('li');
216 // TRANS: Field label for a list of e-mail addresses.
217 $this->textarea('addresses', _('Email addresses'),
218 $this->trimmed('addresses'),
219 // TRANS: Tooltip for field label for a list of e-mail addresses.
220 _('Addresses of friends to invite (one per line)'));
221 $this->elementEnd('li');
222 $this->elementStart('li');
223 // TRANS: Field label for a personal message to send to invitees.
224 $this->textarea('personal', _('Personal message'),
225 $this->trimmed('personal'),
226 // TRANS: Tooltip for field label for a personal message to send to invitees.
227 _('Optionally add a personal message to the invitation.'));
228 $this->elementEnd('li');
229 $this->elementEnd('ul');
230 // TRANS: Send button for inviting friends
231 $this->submit('send', _m('BUTTON', 'Send'));
232 $this->elementEnd('fieldset');
233 $this->elementEnd('form');
236 function sendInvitation($email, $user, $personal)
238 $profile = $user->getProfile();
239 $bestname = $profile->getBestName();
241 $sitename = common_config('site', 'name');
243 $invite = new Invitation();
245 $invite->address = $email;
246 $invite->address_type = 'email';
247 $invite->code = common_confirmation_code(128);
248 $invite->user_id = $user->id;
249 $invite->created = common_sql_now();
251 if (!$invite->insert()) {
252 common_log_db_error($invite, 'INSERT', __FILE__);
256 $recipients = array($email);
258 $headers['From'] = mail_notify_from();
259 $headers['To'] = trim($email);
260 // TRANS: Subject for invitation email. Note that 'them' is correct as a gender-neutral
261 // TRANS: singular 3rd-person pronoun in English. %1$s is the inviting user, $2$s is
262 // TRANS: the StatusNet sitename.
263 $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
265 // TRANS: Body text for invitation email. Note that 'them' is correct as a gender-neutral
266 // TRANS: singular 3rd-person pronoun in English. %1$s is the inviting user, %2$s is the
267 // TRANS: StatusNet sitename, %3$s is the site URL, %4$s is the personal message from the
268 // TRANS: inviting user, %s%5 a link to the timeline for the inviting user, %s$6 is a link
269 // TRANS: to register with the StatusNet site.
270 $body = sprintf(_("%1\$s has invited you to join them on %2\$s (%3\$s).\n\n".
271 "%2\$s is a micro-blogging service that lets you keep up-to-date with people you know and people who interest you.\n\n".
272 "You can also share news about yourself, your thoughts, or your life online with people who know about you. ".
273 "It's also great for meeting new people who share your interests.\n\n".
274 "%1\$s said:\n\n%4\$s\n\n".
275 "You can see %1\$s's profile page on %2\$s here:\n\n".
277 "If you'd like to try the service, click on the link below to accept the invitation.\n\n".
279 "If not, you can ignore this message. Thanks for your patience and your time.\n\n".
280 "Sincerely, %2\$s\n"),
285 common_local_url('showstream', array('nickname' => $user->nickname)),
286 common_local_url('register', array('code' => $invite->code)));
288 mail_send($recipients, $headers, $body);
291 function showLocalNav()
293 $nav = new SubGroupNav($this, common_current_user());