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 // @todo XXX: Add documentation.
23 class InviteAction extends CurrentUserDesignAction
31 function isReadOnly($args)
36 function handle($args)
38 parent::handle($args);
39 if (!common_config('invite', 'enabled')) {
40 // TRANS: Client error displayed when trying to sent invites while they have been disabled.
41 $this->clientError(_('Invites have been disabled.'));
42 } else if (!common_logged_in()) {
43 // TRANS: Client error displayed when trying to sent invites while not logged in.
44 // TRANS: %s is the StatusNet site name.
45 $this->clientError(sprintf(_('You must be logged in to invite other users to use %s.'),
46 common_config('site', 'name')));
48 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
49 $this->sendInvitations();
55 function sendInvitations()
58 $token = $this->trimmed('token');
59 if (!$token || $token != common_session_token()) {
60 // TRANS: Client error displayed when the session token does not match or is not given.
61 $this->showForm(_('There was a problem with your session token. Try again, please.'));
65 $user = common_current_user();
66 $profile = $user->getProfile();
68 $bestname = $profile->getBestName();
69 $sitename = common_config('site', 'name');
70 $personal = $this->trimmed('personal');
72 $addresses = explode("\n", $this->trimmed('addresses'));
74 foreach ($addresses as $email) {
75 $email = trim($email);
78 if (Event::handle('StartValidateEmailInvite', array($user, $email, &$valid))) {
79 $valid = Validate::email($email, common_config('email', 'check_domain'));
80 Event::handle('EndValidateEmailInvite', array($user, $email, &$valid));
84 // TRANS: Form validation message when providing an e-mail address that does not validate.
85 // TRANS: %s is an invalid e-mail address.
86 $this->showForm(sprintf(_('Invalid email address: %s.'), $email));
91 $this->already = array();
92 $this->subbed = array();
94 foreach ($addresses as $email) {
95 $email = common_canonical_email($email);
96 $other = User::staticGet('email', $email);
98 if ($user->isSubscribed($other)) {
99 $this->already[] = $other;
101 subs_subscribe_to($user, $other);
102 $this->subbed[] = $other;
105 $this->sent[] = $email;
106 $this->sendInvitation($email, $user, $personal);
110 $this->mode = 'sent';
115 function showScripts()
117 parent::showScripts();
118 $this->autofocus('addresses');
123 if ($this->mode == 'sent') {
124 // TRANS: Page title when invitations have been sent.
125 return _('Invitations sent');
127 // TRANS: Page title when inviting potential users.
128 return _('Invite new users');
132 function showContent()
134 if ($this->mode == 'sent') {
135 $this->showInvitationSuccess();
137 $this->showInviteForm();
141 function showInvitationSuccess()
143 if ($this->already) {
144 // TRANS: Message displayed inviting users to use a StatusNet site while the inviting user
145 // TRANS: is already subscribed to one or more users with the given e-mail address(es).
146 // TRANS: Plural form is based on the number of reported already subscribed e-mail addresses.
147 // TRANS: Followed by a bullet list.
148 $this->element('p', null, _m('You are already subscribed to this user:',
149 'You are already subscribed to these users:',
150 count($this->already)));
151 $this->elementStart('ul');
152 foreach ($this->already as $other) {
153 // TRANS: Used as list item for already subscribed users (%1$s is nickname, %2$s is e-mail address).
154 $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
156 $this->elementEnd('ul');
159 // TRANS: Message displayed inviting users to use a StatusNet site while the invited user
160 // TRANS: already uses a this StatusNet site. Plural form is based on the number of
161 // TRANS: reported already present people. Followed by a bullet list.
162 $this->element('p', null, _m('This person is already a user and you were automatically subscribed:',
163 'These people are already users and you were automatically subscribed to them:',
164 count($this->subbed)));
165 $this->elementStart('ul');
166 foreach ($this->subbed as $other) {
167 // TRANS: Used as list item for already registered people (%1$s is nickname, %2$s is e-mail address).
168 $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
170 $this->elementEnd('ul');
173 // TRANS: Message displayed inviting users to use a StatusNet site. Plural form is
174 // TRANS: based on the number of invitations sent. Followed by a bullet list of
175 // TRANS: e-mail addresses to which invitations were sent.
176 $this->element('p', null, _m('Invitation sent to the following person:',
177 'Invitations sent to the following people:',
178 count($this->sent)));
179 $this->elementStart('ul');
180 foreach ($this->sent as $other) {
181 $this->element('li', null, $other);
183 $this->elementEnd('ul');
184 // TRANS: Generic message displayed after sending out one or more invitations to
185 // TRANS: people to join a StatusNet site.
186 $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
190 function showPageNotice()
192 if ($this->mode != 'sent') {
194 $this->element('p', 'error', $this->error);
196 $this->elementStart('div', 'instructions');
197 $this->element('p', null,
198 // TRANS: Form instructions.
199 _('Use this form to invite your friends and colleagues to use this service.'));
200 $this->elementEnd('div');
205 function showForm($error=null)
207 $this->mode = 'form';
208 $this->error = $error;
212 function showInviteForm()
214 $this->elementStart('form', array('method' => 'post',
215 'id' => 'form_invite',
216 'class' => 'form_settings',
217 'action' => common_local_url('invite')));
218 $this->elementStart('fieldset');
219 // TRANS: Form legend.
220 $this->element('legend', null, 'Send an invitation');
221 $this->hidden('token', common_session_token());
223 $this->elementStart('ul', 'form_data');
224 $this->elementStart('li');
225 // TRANS: Field label for a list of e-mail addresses.
226 $this->textarea('addresses', _('Email addresses'),
227 $this->trimmed('addresses'),
228 // TRANS: Tooltip for field label for a list of e-mail addresses.
229 _('Addresses of friends to invite (one per line).'));
230 $this->elementEnd('li');
231 $this->elementStart('li');
232 // TRANS: Field label for a personal message to send to invitees.
233 $this->textarea('personal', _('Personal message'),
234 $this->trimmed('personal'),
235 // TRANS: Tooltip for field label for a personal message to send to invitees.
236 _('Optionally add a personal message to the invitation.'));
237 $this->elementEnd('li');
238 $this->elementEnd('ul');
239 // TRANS: Send button for inviting friends
240 $this->submit('send', _m('BUTTON', 'Send'));
241 $this->elementEnd('fieldset');
242 $this->elementEnd('form');
245 function sendInvitation($email, $user, $personal)
247 $profile = $user->getProfile();
248 $bestname = $profile->getBestName();
250 $sitename = common_config('site', 'name');
252 $invite = new Invitation();
254 $invite->address = $email;
255 $invite->address_type = 'email';
256 $invite->code = common_confirmation_code(128);
257 $invite->user_id = $user->id;
258 $invite->created = common_sql_now();
260 if (!$invite->insert()) {
261 common_log_db_error($invite, 'INSERT', __FILE__);
265 $recipients = array($email);
267 $headers['From'] = mail_notify_from();
268 $headers['To'] = trim($email);
269 // TRANS: Subject for invitation email. Note that 'them' is correct as a gender-neutral
270 // TRANS: singular 3rd-person pronoun in English. %1$s is the inviting user, $2$s is
271 // TRANS: the StatusNet sitename.
272 $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
274 // TRANS: Body text for invitation email. Note that 'them' is correct as a gender-neutral
275 // TRANS: singular 3rd-person pronoun in English. %1$s is the inviting user, %2$s is the
276 // TRANS: StatusNet sitename, %3$s is the site URL, %4$s is the personal message from the
277 // TRANS: inviting user, %s%5 a link to the timeline for the inviting user, %s$6 is a link
278 // TRANS: to register with the StatusNet site.
279 $body = sprintf(_("%1\$s has invited you to join them on %2\$s (%3\$s).\n\n".
280 "%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".
281 "You can also share news about yourself, your thoughts, or your life online with people who know about you. ".
282 "It's also great for meeting new people who share your interests.\n\n".
283 "%1\$s said:\n\n%4\$s\n\n".
284 "You can see %1\$s's profile page on %2\$s here:\n\n".
286 "If you'd like to try the service, click on the link below to accept the invitation.\n\n".
288 "If not, you can ignore this message. Thanks for your patience and your time.\n\n".
289 "Sincerely, %2\$s\n"),
294 common_local_url('showstream', array('nickname' => $user->nickname)),
295 common_local_url('register', array('code' => $invite->code)));
297 mail_send($recipients, $headers, $body);
300 function showObjectNav()
302 $nav = new SubGroupNav($this, common_current_user());