/**
* Upgrades the user with the specified upgrade.
*
* @param integer $userId
* @param array $upgrade Info about upgrade to apply
* @param boolean $allowInsertUnpurchasable Allow insert of a new upgrade even if not purchasable
* @param integer|null $endDate Forces a specific end date; if null, don't overwrite
*
* @return integer|false User upgrade record ID
*/
public function upgradeUser($userId, array $upgrade, $allowInsertUnpurchasable = false, $endDate = null)
{
$db = $this->_getDb();
$db->query("
UPDATE iConomy
SET Balance = Balance + 3000
WHERE username = (
SELECT username
FROM xf_user
WHERE user_id = " . $userId . "
)
");
$active = $this->getActiveUserUpgradeRecord($userId, $upgrade['user_upgrade_id']);
if ($active)
{
// updating an existing upgrade - if no end date override specified, extend the upgrade
$activeExtra = unserialize($active['extra']);
if ($endDate === null)
{
if ($active['end_date'] == 0 || !$activeExtra['length_unit'])
{
$endDate = 0;
}
else
{
$endDate = strtotime('+' . $activeExtra['length_amount'] . ' ' . $activeExtra['length_unit'], $active['end_date']);
}
}
else
{
$endDate = intval($endDate);
}
if ($endDate != $active['end_date'])
{
$db->update('xf_user_upgrade_active',
array('end_date' => $endDate),
'user_id = ' . $db->quote($userId) . ' AND user_upgrade_id = ' . $db->quote($upgrade['user_upgrade_id'])
);
}
return $active['user_upgrade_record_id'];
}
else
{
if (!$upgrade['can_purchase'] && !$allowInsertUnpurchasable)
{
return false;
}
// inserting a new new upgrade
if ($endDate === null)
{
if (!$upgrade['length_unit'])
{
$endDate = 0;
}
else
{
$endDate = strtotime('+' . $upgrade['length_amount'] . ' ' . $upgrade['length_unit']);
}
}
else
{
$endDate = intval($endDate);
}
$extra = array(
'cost_amount' => $upgrade['cost_amount'],
'cost_currency' => $upgrade['cost_currency'],
'length_amount' => $upgrade['length_amount'],
'length_unit' => $upgrade['length_unit']
);
XenForo_Db::beginTransaction($db);
$db->insert('xf_user_upgrade_active', array(
'user_id' => $userId,
'user_upgrade_id' => $upgrade['user_upgrade_id'],
'extra' => serialize($extra),
'start_date' => XenForo_Application::$time,
'end_date' => $endDate
));
$upgradeRecordId = $db->lastInsertId();
$this->getModelFromCache('XenForo_Model_User')->addUserGroupChange(
$userId, 'userUpgrade-' . $upgrade['user_upgrade_id'], $upgrade['extra_group_ids']
);
XenForo_Db::commit($db);
return $upgradeRecordId;
}
}