Skip to content

Commit

Permalink
Added ability to define account plan with admin invitation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Chernyshev committed May 10, 2014
1 parent 1b2b3ea commit b3fb6fb
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 29 deletions.
81 changes: 73 additions & 8 deletions admin/invitations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require_once(dirname(__DIR__) . '/classes/Invitation.php');

if (array_key_exists('save', $_POST)) {
$saved_one = false;
foreach (array_keys($_POST['email']) as $code) {
$invitation = Invitation::getByCode($code);
$need_to_save = false;
Expand All @@ -30,13 +31,20 @@
$need_to_save = true;
}

$invitation->setPlan(Plan::getPlanBySlug($_POST['plan_slug'][$code]));

if ($need_to_save) {
$invitation->setIssuer($user);
$invitation->save();
$saved_one = true;
}
}

header("Location: #message=saved");
if ($saved_one) {
header("Location: #message=saved");
} else {
header("Location: #message=nothing_to_save");
}
exit;
}

Expand Down Expand Up @@ -93,11 +101,12 @@
<?php
} else {
?>
<form class="form-inline" action="" method="POST">
<form class="form" action="" method="POST">
<table class="table">
<tr>
<th>Code</th>
<th>Sent to</th>
<th>Service Plan</th>
<?php if (!is_null(UserConfig::$onRenderUserInvitationAction)) { ?><th>Actions</th><?php } ?>
</tr>

Expand All @@ -111,19 +120,62 @@
</td>
<td>
<div class="controls controls-row">
<input type="text" class="span4"
<input type="text" class="span6"
name="name[<?php echo UserTools::escape($code) ?>]"
id="name_<?php echo UserTools::escape($code) ?>"
placeholder="John Smith">
<input type="email" class="span4"
<input type="email" class="span6"
name="email[<?php echo UserTools::escape($code) ?>]"
id="email_<?php echo UserTools::escape($code) ?>"
placeholder="[email protected]">
<input type="text" class="span4"
name="note[<?php echo UserTools::escape($code) ?>]"
id="note_<?php echo UserTools::escape($code) ?>"
placeholder="note">
</div>
<div class="controls controls-row">
<textarea class="span12"
name="note[<?php echo UserTools::escape($code) ?>]"
id="note_<?php echo UserTools::escape($code) ?>"
placeholder="(optional note)"></textarea>
</div>
</td>
<td>
<?php
$plan_slugs = Plan::getPlanSlugs();

if (count($plan_slugs) > 1) {
foreach ($plan_slugs as $plan_slug) {
$plan = Plan::getPlanBySlug($plan_slug);
?>
<label class="radio">
<input type="radio"
name="plan_slug[<?php echo UserTools::escape($code) ?>]"
value="<?php echo UserTools::escape($plan_slug) ?>"
<?php
if ($plan_slug == UserConfig::$default_plan_slug) {
?>
checked
<?php
}
?>
>
<span class="badge badge-info"><i class="icon-briefcase icon-white"></i> <?php echo UserTools::escape($plan->getName()) ?></span>
</label>
<?php
}

if (is_null(UserConfig::$default_plan_slug)) {
?>
<label class="radio">
<input type="radio"
name="plan_slug[<?php echo UserTools::escape($code) ?>]"
value=""
checked
>
<span class="badge badge-important">NONE</span>
</label>
<?php
}
}
?>

</td>

<?php
Expand Down Expand Up @@ -164,6 +216,7 @@
<th>Code</th>
<th>Invited By</th>
<th>Sent to</th>
<th>Subscription plan</th>
<?php if (!is_null(UserConfig::$onRenderUserInvitationFollowUpAction)) { ?>
<th>Actions</th>
<?php } ?>
Expand All @@ -173,6 +226,7 @@
$issuer = $invitation->getIssuer();
$email = trim($invitation->getSentToEmail());
$note = trim($invitation->getNote());
$plan = $invitation->getPlan();
?><tr>
<td><span class="badge badge-important"><?php echo UserTools::escape($invitation->getCode()) ?></span></td>
<td>
Expand All @@ -194,6 +248,17 @@
}
?>
</td>
<td>
<?php
if ($plan) {
?>
<span class="badge badge-info"><i class="icon-briefcase icon-white"></i>
<?php echo $plan->getName(); ?>
</span>
<?php
}
?>
</td>
<?php
if (!is_null(UserConfig::$onRenderUserInvitationFollowUpAction)) {
?>
Expand Down
2 changes: 1 addition & 1 deletion classes/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ public function getNextSchedule() {
*
* @throws DBException
*/
public static function createAccount($name, $plan_slug, $schedule_slug = null, $user = null, $role = Account::ROLE_USER, $engine_slug = null) {
public static function createAccount($name, $plan_slug, $schedule_slug = null, $user = null, $role = self::ROLE_ADMIN, $engine_slug = null) {
$name = mb_convert_encoding($name, 'UTF-8');

$db = UserConfig::getDB();
Expand Down
58 changes: 43 additions & 15 deletions classes/Invitation.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class Invitation {
*/
private $account_id;

/**
* @var string Subscription plan slug (or null if no plan, e.g. there is no default)
*/
private $plan_slug;

/**
* Creates new invitation object
*
Expand All @@ -70,8 +75,9 @@ class Invitation {
* @param string $sent_to_note Invitation comment (reminder to issuer why it was sent)
* @param int $user_id ID of the User who got invited
* @param int $account_id ID of the account invitation is for, null if user is not invited to join an account
* @param string|null $plan_slug Slug of the plan or null if default or no (if UserConfig::$default_plan_slug is null) plan to be set once user registers
*/
private function __construct($code, $time_created, $issuedby, $is_admin_invite = true, $sent_to_email = null, $sent_to_name = null, $sent_to_note = null, $user_id = null, $account_id = null) {
private function __construct($code, $time_created, $issuedby, $is_admin_invite = true, $sent_to_email = null, $sent_to_name = null, $sent_to_note = null, $user_id = null, $account_id = null, $plan_slug = null) {
$this->code = $code;
$this->time_created = $time_created;
$this->issuedby = $issuedby;
Expand All @@ -81,6 +87,7 @@ private function __construct($code, $time_created, $issuedby, $is_admin_invite =
$this->sent_to_note = $sent_to_note;
$this->user_id = $user_id;
$this->account_id = $account_id;
$this->plan_slug = $plan_slug;
}

/**
Expand Down Expand Up @@ -134,7 +141,7 @@ public static function getSent($admin = null, $issuer = null) {

$query = 'SELECT code, UNIX_TIMESTAMP(created), issuedby, is_admin_invite,
sent_to_email, sent_to_name, sent_to_note,
user_id, account_id
user_id, account_id, plan_slug
FROM ' . UserConfig::$mysql_prefix . 'invitation
WHERE sent_to_note IS NOT NULL AND user_id IS NULL';

Expand All @@ -150,13 +157,13 @@ public static function getSent($admin = null, $issuer = null) {
if (!$stmt->execute()) {
throw new DBExecuteStmtException($db, $stmt);
}
if (!$stmt->bind_result($code, $time_created, $issuedby, $is_admin_invite, $sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id)) {
if (!$stmt->bind_result($code, $time_created, $issuedby, $is_admin_invite, $sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id, $plan_slug)) {
throw new DBBindResultException($db, $stmt);
}

while ($stmt->fetch() === TRUE) {
$invitations[] = new self($code, $time_created, $issuedby, $is_admin_invite ? true : false,
$sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id);
$sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id, $plan_slug);
}

$stmt->close();
Expand Down Expand Up @@ -214,7 +221,7 @@ public static function getAccepted($admin = null, $issuer = null) {
$db = UserConfig::getDB();

$query = 'SELECT i.code, UNIX_TIMESTAMP(i.created), i.issuedby, i.is_admin_invite,
i.sent_to_email, i.sent_to_name, i.sent_to_note, i.user_id, i.account_id
i.sent_to_email, i.sent_to_name, i.sent_to_note, i.user_id, i.account_id, i.plan_slug
FROM ' . UserConfig::$mysql_prefix . 'invitation i
INNER JOIN ' . UserConfig::$mysql_prefix . 'users u
ON i.user_id = u.id
Expand All @@ -234,13 +241,13 @@ public static function getAccepted($admin = null, $issuer = null) {
if (!$stmt->execute()) {
throw new DBExecuteStmtException($db, $stmt);
}
if (!$stmt->bind_result($code, $time_created, $issuedby, $is_admin_invite, $sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id)) {
if (!$stmt->bind_result($code, $time_created, $issuedby, $is_admin_invite, $sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id, $plan_slug)) {
throw new DBBindResultException($db, $stmt);
}

while ($stmt->fetch() === TRUE) {
$invitations[] = new self($code, $time_created, $issuedby, $is_admin_invite ? true : false,
$sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id);
$sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id, $plan_slug);
}

$stmt->close();
Expand All @@ -266,7 +273,7 @@ public static function getByCode($code) {
$db = UserConfig::getDB();

if ($stmt = $db->prepare('SELECT code, UNIX_TIMESTAMP(created), issuedby, is_admin_invite,
sent_to_email, sent_to_name, sent_to_note, user_id, account_id
sent_to_email, sent_to_name, sent_to_note, user_id, account_id, plan_slug
FROM ' . UserConfig::$mysql_prefix . 'invitation
WHERE code = ?')) {
if (!$stmt->bind_param('s', $code)) {
Expand All @@ -275,13 +282,13 @@ public static function getByCode($code) {
if (!$stmt->execute()) {
throw new DBExecuteStmtException($db, $stmt);
}
if (!$stmt->bind_result($code, $time_created, $issuedby, $is_admin_invite, $sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id)) {
if (!$stmt->bind_result($code, $time_created, $issuedby, $is_admin_invite, $sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id, $plan_slug)) {
throw new DBBindResultException($db, $stmt);
}

if ($stmt->fetch() === TRUE) {
$invitation = new self($code, $time_created, $issuedby, $is_admin_invite ? true : false,
$sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id);
$sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id, $plan_slug);
}

$stmt->close();
Expand All @@ -308,7 +315,7 @@ public static function getUserInvitation($user) {
$db = UserConfig::getDB();

if ($stmt = $db->prepare('SELECT code, UNIX_TIMESTAMP(created), issuedby, is_admin_invite,
sent_to_email, sent_to_name, sent_to_note, user_id, account_id
sent_to_email, sent_to_name, sent_to_note, user_id, account_id, plan_slug
FROM ' . UserConfig::$mysql_prefix . 'invitation
WHERE user_id = ?')) {
if (!$stmt->bind_param('i', $user_id)) {
Expand All @@ -317,13 +324,13 @@ public static function getUserInvitation($user) {
if (!$stmt->execute()) {
throw new DBExecuteStmtException($db, $stmt);
}
if (!$stmt->bind_result($code, $time_created, $issuedby, $is_admin_invite, $sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id)) {
if (!$stmt->bind_result($code, $time_created, $issuedby, $is_admin_invite, $sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id, $plan_slug)) {
throw new DBBindResultException($db, $stmt);
}

if ($stmt->fetch() === TRUE) {
$invitation = new self($code, $time_created, $issuedby, $is_admin_invite ? true : false,
$sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id);
$sent_to_email, $sent_to_name, $sent_to_note, $user_id, $account_id, $plan_slug);
}

$stmt->close();
Expand Down Expand Up @@ -549,6 +556,26 @@ public function setNote($comment) {
$this->sent_to_note = $comment;
}

/**
* Returns a subscription plan associated with invitation
*
* @return Plan Subscription Plan
*/
public function getPlan() {
return Plan::getPlanBySlug($this->plan_slug);
}

/**
* Sets a subscription plan associated with invitation
*
* @param Plan|null $plan Plan Subscription Plan
*/
public function setPlan($plan) {
if ($plan) {
$this->plan_slug = $plan->getSlug();
}
}

/**
* Persists invitation object in database
*
Expand All @@ -568,9 +595,10 @@ public function save() {
issuedby = ?,
is_admin_invite = ?,
user_id = ?,
account_id = ?
account_id = ?,
plan_slug = ?
WHERE code = ?')) {
if (!$stmt->bind_param('sssiiiis', $email, $name, $note, $this->issuedby, $this->is_admin_invite, $this->user_id, $this->account_id, $this->code)) {
if (!$stmt->bind_param('sssiiiiss', $email, $name, $note, $this->issuedby, $this->is_admin_invite, $this->user_id, $this->account_id, $this->plan_slug, $this->code)) {
throw new DBBindParamException($db, $stmt);
}
if (!$stmt->execute()) {
Expand Down
15 changes: 10 additions & 5 deletions classes/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,19 +523,24 @@ private function init() {

// only add to account if invited by the admin of a non-individual account
if ($invitation_account !== NULL &&
!$invitation_account->isIndividual() &&
$invitation_account->getUserRole($invitation->getIssuer()) === Account::ROLE_ADMIN
!$invitation_account->isIndividual() &&
$invitation_account->getUserRole($invitation->getIssuer()) === Account::ROLE_ADMIN
) {
$invitation_account->addUser($this);
}
}

$personal_account = null;
$new_user_account = null;
if (is_null($invitation_account) || UserConfig::$createPersonalAccountsIfInvitedToGroupAccount) {
$personal_account = $this->createPersonalAccount(false);
$plan = $invitation->getPlan();
if ($plan) {
$new_user_account = Account::createAccount($this->name, $plan->getSlug(), null, $this, Account::ROLE_ADMIN);
} else {
$new_user_account = $this->createPersonalAccount(false);
}
}

$current_account = is_null($invitation_account) ? $personal_account : $invitation_account;
$current_account = is_null($invitation_account) ? $new_user_account : $invitation_account;
$current_account->setAsCurrent($this);

if (!is_null(UserConfig::$onCreate)) {
Expand Down
9 changes: 9 additions & 0 deletions dbupgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
$versions[_]['down'][] = "";
*/

/* -------------------------------------------------------------------------------------------------------
* VERSION 34
* Setting plans for invitations
*/
$versions[34]['up'][] = "ALTER TABLE ".UserConfig::$mysql_prefix."invitation
ADD COLUMN `plan_slug` varchar(256) DEFAULT NULL COMMENT 'Invitation subscription plan'";
$versions[34]['down'][] = "ALTER TABLE ".UserConfig::$mysql_prefix."invitation
DROP COLUMN plan_slug";

// Use boilerplate above to Add new migrations on top, right below this line.

/* -------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit b3fb6fb

Please sign in to comment.