Skip to content

Commit

Permalink
Added data fields for user-initiated invites, updated current code to…
Browse files Browse the repository at this point in the history
… work with admin invites specifically.
  • Loading branch information
Sergey Chernyshev committed Nov 3, 2012
1 parent 1459779 commit 2c48733
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 23 deletions.
65 changes: 46 additions & 19 deletions Invitation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class Invitation
*/
private $issuedby;

/**
* @var boolean Whatever this invitation was generated using admin UI or not
*/
private $is_admin_invite;

/**
* @var string Invitation comment (reminder to issuer why it was sent)
*/
Expand All @@ -48,17 +53,18 @@ class Invitation
* @param string $usagecomment Invitation comment (reminder to issuer why it was sent)
* @param int $user ID of the User who got invited
*/
private function __construct($code, $time_created, $issuedby, $usagecomment = null, $user = null)
private function __construct($code, $time_created, $issuedby, $is_admin_invite = true, $usagecomment = null, $user = null)
{
$this->code = $code;
$this->time_created = $time_created;
$this->issuedby = $issuedby;
$this->is_admin_invite = $is_admin_invite;
$this->usagecomment = $usagecomment;
$this->user = $user;
}

/**
* Returns invitations that were generated, but not sent yet
* Returns admin invitations that were generated, but not sent yet
*
* @return array Array of Invitation objects
*
Expand All @@ -70,7 +76,7 @@ public static function getUnsent()

$db = UserConfig::getDB();

if ($stmt = $db->prepare('SELECT code, created, issuedby FROM '.UserConfig::$mysql_prefix.'invitation WHERE sentto IS NULL AND user IS NULL'))
if ($stmt = $db->prepare('SELECT code, created, issuedby FROM '.UserConfig::$mysql_prefix.'invitation WHERE is_admin_invite = 1 AND sent_to_note IS NULL AND user IS NULL'))
{
if (!$stmt->execute())
{
Expand All @@ -83,7 +89,7 @@ public static function getUnsent()

while($stmt->fetch() === TRUE)
{
$invitations[] = new self($code, $time_created, $issuedby);
$invitations[] = new self($code, $time_created, $issuedby, true);
}

$stmt->close();
Expand All @@ -103,26 +109,34 @@ public static function getUnsent()
*
* @throws DBException
*/
public static function getSent()
public static function getSent($admin = null)
{
$invitations = array();

$db = UserConfig::getDB();

if ($stmt = $db->prepare('SELECT code, created, issuedby, sentto FROM '.UserConfig::$mysql_prefix.'invitation WHERE sentto IS NOT NULL AND user IS NULL'))
$query = 'SELECT code, created, issuedby, is_admin_invite, sent_to_note
FROM '.UserConfig::$mysql_prefix.'invitation
WHERE sent_to_note IS NOT NULL AND user IS NULL';

if (!is_null($admin)) {
$query .= ' AND is_admin_invite = ' . ($admin ? 1 : 0);
}

if ($stmt = $db->prepare($query))
{
if (!$stmt->execute())
{
throw new DBExecuteStmtException($db, $stmt);
}
if (!$stmt->bind_result($code, $time_created, $issuedby, $sentto))
if (!$stmt->bind_result($code, $time_created, $issuedby, $is_admin_invite, $sent_to_note))
{
throw new DBBindResultException($db, $stmt);
}

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

$stmt->close();
Expand Down Expand Up @@ -171,26 +185,34 @@ public static function cancel($code) {
*
* @throws DBException
*/
public static function getAccepted()
public static function getAccepted($admin = null)
{
$invitations = array();

$db = UserConfig::getDB();

if ($stmt = $db->prepare('SELECT code, created, issuedby, sentto, user FROM '.UserConfig::$mysql_prefix.'invitation WHERE user IS NOT NULL'))
$query = 'SELECT code, created, issuedby, is_admin_invite, sent_to_note, user
FROM '.UserConfig::$mysql_prefix.'invitation
WHERE user IS NOT NULL';

if (!is_null($admin)) {
$query .= ' AND is_admin_invite = ' . ($admin ? 1 : 0);
}

if ($stmt = $db->prepare($query))
{
if (!$stmt->execute())
{
throw new DBExecuteStmtException($db, $stmt);
}
if (!$stmt->bind_result($code, $time_created, $issuedby, $sentto, $userid))
if (!$stmt->bind_result($code, $time_created, $issuedby, $is_admin_invite, $sent_to_note, $userid))
{
throw new DBBindResultException($db, $stmt);
}

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

$stmt->close();
Expand Down Expand Up @@ -218,7 +240,7 @@ public static function getByCode($code)

$db = UserConfig::getDB();

if ($stmt = $db->prepare('SELECT code, created, issuedby, sentto, user FROM '.UserConfig::$mysql_prefix.'invitation WHERE code = ?'))
if ($stmt = $db->prepare('SELECT code, created, issuedby, is_admin_invite, sent_to_note, user FROM '.UserConfig::$mysql_prefix.'invitation WHERE code = ?'))
{
if (!$stmt->bind_param('s', $code))
{
Expand All @@ -228,14 +250,14 @@ public static function getByCode($code)
{
throw new DBExecuteStmtException($db, $stmt);
}
if (!$stmt->bind_result($code, $time_created, $issuedby, $sentto, $userid))
if (!$stmt->bind_result($code, $time_created, $issuedby, $is_admin_invite, $sent_to_note, $userid))
{
throw new DBBindResultException($db, $stmt);
}

if ($stmt->fetch() === TRUE)
{
$invitation = new self($code, $time_created, $issuedby, $sentto, $userid);
$invitation = new self($code, $time_created, $issuedby, $is_admin_invite ? true : false, $sent_to_note, $userid);
}

$stmt->close();
Expand All @@ -249,7 +271,7 @@ public static function getByCode($code)
}

/**
* Creates new invitation codes to be used for inviting new users
* Creates new invitation codes to be used for inviting new users in admin UI
*
* @param int $howmany How many codes to generate
*
Expand All @@ -259,7 +281,7 @@ public static function generate($howmany)
{
$db = UserConfig::getDB();

if ($stmt = $db->prepare('INSERT INTO '.UserConfig::$mysql_prefix.'invitation (code) VALUES (?)'))
if ($stmt = $db->prepare('INSERT INTO '.UserConfig::$mysql_prefix.'invitation (code, is_admin_invite) VALUES (?, 1)'))
{
for ($i = 0; $i < $howmany; $i++)
{
Expand Down Expand Up @@ -384,9 +406,14 @@ public function save()

$comment = mb_convert_encoding($this->usagecomment, 'UTF-8');

if ($stmt = $db->prepare('UPDATE '.UserConfig::$mysql_prefix.'invitation SET sentto = ?, issuedby = ?, user = ? WHERE code = ?'))
if ($stmt = $db->prepare('UPDATE '.UserConfig::$mysql_prefix.'invitation SET
sent_to_note = ?,
issuedby = ?,
is_admin_invite = ?,
user = ?
WHERE code = ?'))
{
if (!$stmt->bind_param('siis', $comment, $this->issuedby, $this->user, $this->code))
if (!$stmt->bind_param('siiis', $comment, $this->issuedby, $this->is_admin_invite, $this->user, $this->code))
{
throw new DBBindParamException($db, $stmt);
}
Expand Down
4 changes: 2 additions & 2 deletions admin/invitations.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<?php
}

$invitations = Invitation::getSent();
$invitations = Invitation::getSent(true);

if (count($invitations) > 0)
{
Expand Down Expand Up @@ -164,7 +164,7 @@
<?php
}

$invitations = Invitation::getAccepted();
$invitations = Invitation::getAccepted(true);

if (count($invitations) > 0)
{
Expand Down
34 changes: 32 additions & 2 deletions dbupgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,38 @@
*/

/* -------------------------------------------------------------------------------------------------------
* VERSION _
* ... add version description here ...
* VERSION 22
* Adding email invitations for users
*/
$versions[22]['up'][] = "ALTER TABLE `".UserConfig::$mysql_prefix."invitation`
ADD is_admin_invite TINYINT NOT NULL DEFAULT '1'
COMMENT 'Whatever it is an invitation sent using admin UI or not'
AFTER `issuedby`,
ADD sent_to_email VARCHAR(255) NULL DEFAULT NULL
COMMENT 'Email address we sent invitation to'
AFTER is_admin_invite,
ADD sent_to_name TEXT
CHARACTER SET utf8 COLLATE utf8_general_ci
COMMENT 'Name of the person who got invited'
AFTER sent_to_email,
CHANGE sentto sent_to_note
TEXT CHARACTER SET utf8 COLLATE utf8_general_ci
NULL DEFAULT NULL
COMMENT 'Note about who this invitation was sent to'
";
$versions[22]['down'][] = "ALTER TABLE `".UserConfig::$mysql_prefix."invitation`
DROP is_admin_invite,
DROP sent_to_email,
DROP sent_to_name,
CHANGE sent_to_note sentto
TEXT CHARACTER SET utf8 COLLATE utf8_general_ci
NULL DEFAULT NULL
COMMENT 'Note about who this invitation was sent to'
";

/* -------------------------------------------------------------------------------------------------------
* VERSION 21
* Adding missing primary key on account user
*/
$versions[21]['up'][] = 'ALTER TABLE `'.UserConfig::$mysql_prefix.'account_users` ADD PRIMARY KEY ( `account_id` , `user_id` )';
$versions[21]['up'][] = 'ALTER TABLE `'.UserConfig::$mysql_prefix.'account_users` DROP KEY user_account';
Expand Down

0 comments on commit 2c48733

Please sign in to comment.