forked from StartupAPI/users
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invitations.php
110 lines (90 loc) · 3.34 KB
/
invitations.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
require_once(__DIR__ . '/global.php');
$user = User::require_login();
UserTools::preventCSRF();
$template_info = StartupAPI::getTemplateInfo();
$current_account = null;
$template_info['can_invite_to_account'] = false;
$current_account = $user->getCurrentAccount();
if (!$current_account->isIndividual()
&& $current_account->getUserRole($user) === Account::ROLE_ADMIN
) {
$template_info['can_invite_to_account'] = true;
}
$errors = array();
if (array_key_exists('send', $_POST)) {
$invitation_name = trim($_POST['invitation_name']);
if (empty($invitation_name)) {
$errors['name'][] = "Please provide persons name";
}
$invitation_email = trim($_POST['invitation_email']);
if (empty($invitation_email)) {
$errors['email'][] = "Please provide persons email";
}
$invitation_note = trim($_POST['invitation_note']);
$invite_to_account = null;
if ($template_info['can_invite_to_account'] && array_key_exists('invite_to_account', $_POST)) {
// can only invite to user's current account
$invite_to_account = $current_account;
}
if (count($errors) == 0) {
Invitation::sendUserInvitation($user, $invitation_name, $invitation_email, $invitation_note, $invite_to_account);
header("Location: #message=sent");
exit;
}
}
if (array_key_exists('resend', $_POST)) {
foreach (array_keys($_POST['resend']) as $code) {
$invitation = Invitation::getByCode($code);
if ($invitation->getIssuer()->isTheSameAs($user)) {
$invitation->send();
header("Location: #message=resent");
exit;
} else {
header("Location: #message=wrongissuer");
exit;
}
}
}
if (array_key_exists('cancel', $_POST)) {
foreach (array_keys($_POST['cancel']) as $code) {
$invitation = Invitation::getByCode($code);
if ($invitation->getIssuer()->isTheSameAs($user)) {
$invitation->cancel();
header("Location: #message=cancelled");
exit;
} else {
header("Location: #message=wrongissuer");
exit;
}
}
}
$template_info['PAGE']['SECTION'] = 'invitations';
$template_info['errors']['invitations'] = $errors;
$template_info['message_placeholder'] = call_user_func_array(UserConfig::$onRenderUserInvitationMessagePlaceholder, array($user));
$template_info['join_account'] = array_key_exists('joinaccount', $_GET);
$now = time();
foreach ($user->getSentInvitations() as $invitation) {
$template_info['sent_invitations'][] = array(
'code' => $invitation->getCode(),
'email' => trim($invitation->getSentToEmail()),
'email_spaceencoded' => UserTools::spaceencode(trim($invitation->getSentToEmail())),
'name' => $invitation->getSentToName(),
'note' => trim($invitation->getNote()),
'created' => date('M j, h:iA', $invitation->getTimeCreated()),
'ago' => intval(floor(($now - $invitation->getTimeCreated()) / 86400))
);
}
foreach ($user->getAcceptedInvitations() as $invitation) {
$template_info['accepted_invitations'][] = array(
'code' => $invitation->getCode(),
'invited_name' => $invitation->getUser()->getName(),
'email' => trim($invitation->getSentToEmail()),
'email_spaceencoded' => UserTools::spaceencode(trim($invitation->getSentToEmail())),
'name' => $invitation->getSentToName(),
'note' => trim($invitation->getNote()),
'created' => date('M j, h:iA', $invitation->getTimeCreated()),
'ago' => intval(floor(($now - $invitation->getTimeCreated()) / 86400))
);
}
StartupAPI::$template->display('@startupapi/invitations.html.twig', $template_info);