Skip to content

Commit

Permalink
Merge pull request #599 from panique/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
panique committed Feb 6, 2015
2 parents 1998b74 + 53c8813 commit c0db0cb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

**February 2015**

- [panique] AccountTypeModel reduced to one method (removed duplicate code)
- [PR](https://github.com/panique/huge/pull/587) [upperwood] Facebook stuff completely removed from SQL
- tiny text changes
- [panique] tiny text changes

**January 2015**

Expand Down
1 change: 1 addition & 0 deletions application/config/texts.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"FEEDBACK_ACCOUNT_UPGRADE_FAILED" => "Account upgrade failed.",
"FEEDBACK_ACCOUNT_DOWNGRADE_SUCCESSFUL" => "Account downgrade was successful.",
"FEEDBACK_ACCOUNT_DOWNGRADE_FAILED" => "Account downgrade failed.",
"FEEDBACK_ACCOUNT_TYPE_CHANGE_FAILED" => "Account type change failed",
"FEEDBACK_NOTE_CREATION_FAILED" => "Note creation failed.",
"FEEDBACK_NOTE_EDITING_FAILED" => "Note editing failed.",
"FEEDBACK_NOTE_DELETION_FAILED" => "Note deletion failed.",
Expand Down
6 changes: 4 additions & 2 deletions application/controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,12 @@ public function changeAccountType_action()
Auth::checkAuthentication();

if (Request::post('user_account_upgrade')) {
AccountTypeModel::changeAccountTypeUpgrade();
// "2" is quick & dirty account type 2, something like "premium user" maybe. you got the idea :)
AccountTypeModel::changeAccountType(2);
}
if (Request::post('user_account_downgrade')) {
AccountTypeModel::changeAccountTypeDowngrade();
// "1" is quick & dirty account type 1, something like "basic user" maybe.
AccountTypeModel::changeAccountType(1);
}

Redirect::to('login/changeAccountType');
Expand Down
67 changes: 39 additions & 28 deletions application/model/AccountTypeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,66 @@
class AccountTypeModel
{
/**
* Upgrades the user's account (for DEFAULT and FACEBOOK users)
* Currently it's just the field user_account_type in the database that
* can be 1 or 2 (maybe "basic" or "premium"). In this basic method we
* simply increase this value to emulate an account upgrade.
* Put some more complex stuff in here, maybe a pay-process or whatever you like.
* Upgrades / downgrades the user's account. Currently it's just the field user_account_type in the database that
* can be 1 or 2 (maybe "basic" or "premium"). Put some more complex stuff in here, maybe a pay-process or whatever
* you like.
*
* @param $type
*
* @return bool
*/
public static function changeAccountTypeUpgrade()
public static function changeAccountType($type)
{
$database = DatabaseFactory::getFactory()->getConnection();
// this is error-prone, let's rewrite it
if (!$type OR ($type !== 1 AND $type !== 2)) {
return false;
}

$query = $database->prepare("UPDATE users SET user_account_type = 2 WHERE user_id = :user_id LIMIT 1");
$query->execute(array(':user_id' => Session::get('user_id')));
if (AccountTypeModel::saveNewAccountType($type)) {

if ($query->rowCount() == 1) {
// set account type in session to 2
Session::set('user_account_type', 2);
Session::add('feedback_positive', Text::get('FEEDBACK_ACCOUNT_UPGRADE_SUCCESSFUL'));
$feedback = "";

switch ($type) {
case 1:
$feedback = Text::get('FEEDBACK_ACCOUNT_DOWNGRADE_SUCCESSFUL');
break;
case 2:
$feedback = Text::get('FEEDBACK_ACCOUNT_UPGRADE_SUCCESSFUL');
break;
}

Session::add('feedback_positive', $feedback);
return true;
}

// default return
Session::add('feedback_negative', Text::get('FEEDBACK_ACCOUNT_UPGRADE_FAILED'));
Session::add('feedback_negative', Text::get('FEEDBACK_ACCOUNT_TYPE_CHANGE_FAILED'));
return false;
}

/**
* Downgrades the user's account (for DEFAULT and FACEBOOK users)
* Currently it's just the field user_account_type in the database that
* can be 1 or 2 (maybe "basic" or "premium"). In this basic method we
* simply decrease this value to emulate an account downgrade.
* Put some more complex stuff in here, maybe a pay-process or whatever you like.
* Writes the new account type marker to the database and to the session
*
* @param $type
*
* @return bool
*/
public static function changeAccountTypeDowngrade()
public static function saveNewAccountType($type)
{
$database = DatabaseFactory::getFactory()->getConnection();

$query = $database->prepare("UPDATE users SET user_account_type = 1 WHERE user_id = :user_id LIMIT 1");
$query->execute(array(':user_id' => Session::get('user_id')));
$query = $database->prepare("UPDATE users SET user_account_type = :new_type WHERE user_id = :user_id LIMIT 1");
$query->execute(array(
':new_type' => $type,
':user_id' => Session::get('user_id')
));

if ($query->rowCount() == 1) {
// set account type in session to 1
Session::set('user_account_type', 1);
Session::add('feedback_positive', Text::get('FEEDBACK_ACCOUNT_DOWNGRADE_SUCCESSFUL'));
// set account type in session
Session::set('user_account_type', $type);
return true;
}

// default return
Session::add('feedback_negative', Text::get('FEEDBACK_ACCOUNT_DOWNGRADE_FAILED'));
return false;
}

}

0 comments on commit c0db0cb

Please sign in to comment.