UserRoleModel.php (1526B)
1 <?php 2 3 /** 4 * Class UserRoleModel 5 * 6 * This class contains everything that is related to up- and downgrading accounts. 7 */ 8 class UserRoleModel 9 { 10 /** 11 * Upgrades / downgrades the user's account. Currently it's just the field user_account_type in the database that 12 * can be 1 or 2 (maybe "basic" or "premium"). Put some more complex stuff in here, maybe a pay-process or whatever 13 * you like. 14 * 15 * @param $type 16 * 17 * @return bool 18 */ 19 public static function changeUserRole($type) 20 { 21 if (!$type) { 22 return false; 23 } 24 25 // save new role to database 26 if (self::saveRoleToDatabase($type)) { 27 Session::add('feedback_positive', Text::get('FEEDBACK_ACCOUNT_TYPE_CHANGE_SUCCESSFUL')); 28 return true; 29 } else { 30 Session::add('feedback_negative', Text::get('FEEDBACK_ACCOUNT_TYPE_CHANGE_FAILED')); 31 return false; 32 } 33 } 34 35 /** 36 * Writes the new account type marker to the database and to the session 37 * 38 * @param $type 39 * 40 * @return bool 41 */ 42 public static function saveRoleToDatabase($type) 43 { 44 // if $type is not 1 or 2 45 if (!in_array($type, [1, 2])) { 46 return false; 47 } 48 49 $database = DatabaseFactory::getFactory()->getConnection(); 50 51 $query = $database->prepare("UPDATE users SET user_account_type = :new_type WHERE user_id = :user_id LIMIT 1"); 52 $query->execute(array( 53 ':new_type' => $type, 54 ':user_id' => Session::get('user_id') 55 )); 56 57 if ($query->rowCount() == 1) { 58 // set account type in session 59 Session::set('user_account_type', $type); 60 return true; 61 } 62 63 return false; 64 } 65 }