From 126b927b5b569a6aeb4858e7e90c67fdac8e045a Mon Sep 17 00:00:00 2001
From: PavelBegunkov <asml.Silence@gmail.com>
Date: Tue, 9 Jun 2015 13:05:00 +0300
Subject: [PATCH] getAccNumBy* & change* functions generalization

---
 ~dev_rating/application/classes/Account.php   | 17 ++++-----
 .../application/classes/Model/Account.php     | 35 ++++++++++++++++++-
 ~dev_rating/application/classes/User.php      | 12 +++----
 3 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/~dev_rating/application/classes/Account.php b/~dev_rating/application/classes/Account.php
index 3f5b660d3..6d86a2c23 100644
--- a/~dev_rating/application/classes/Account.php
+++ b/~dev_rating/application/classes/Account.php
@@ -151,27 +151,28 @@ class Account {
     }
 
     public function loginExists($login) {
-        $login_count = Model_Account::getAccNumByLogin($login);
+        $login_count = Model_Account::checkAccountExistence($login, 'login');
         return $login_count > 0;
     }
 
     public function mailExists($email) {
-        $email_count = Model_Account::getAccNumByMail($email);
+        $email_count = Model_Account::checkAccountExistence($email, 'email');
         return $email_count > 0;
     }
 
     public function changeLogin($id, $newLogin) {
-        $response = Model_Account::changeLogin($id, $newLogin);
+        $response = Model_Account::changeAccountData($id, $newLogin, 'login');
         return $response != -1;
     }
 
-    public function changeEMail($id, $newEMail) {
-        $response = Model_Account::changeMail($id, $newEMail);
-        return $response != -1;
-    }
+//    // We don't change email address:\
+//    public function changeEMail($id, $newEMail) {
+//        $response = Model_Account::changeMail($id, $newEMail);
+//        return $response != -1;
+//    }
 
     public function changePassword($id, $newPassword) {
-        $response = Model_Account::changePassword($id, $newPassword);
+        $response = Model_Account::changeAccountData($id, $newPassword, 'password');
         return $response != -1;
     }
 }
\ No newline at end of file
diff --git a/~dev_rating/application/classes/Model/Account.php b/~dev_rating/application/classes/Model/Account.php
index 321c78c4b..b80519fc8 100644
--- a/~dev_rating/application/classes/Model/Account.php
+++ b/~dev_rating/application/classes/Model/Account.php
@@ -130,6 +130,21 @@ class Model_Account extends Model
         return $query[0];
     }
 
+    /**
+     * @param int $id
+     * @param string $data
+     * @param string $type      'email', 'login' or 'password'
+     */
+    public static function changeAccountData($accountID, $data, $type)
+    {
+        $data = Database::instance()->escape($data);
+        $type = Database::instance()->escape($type);
+        $sql = "SELECT `ChangeAccountData`('$accountID', $data, $type) AS Num;";
+        $query = DB::query(Database::SELECT, $sql)->execute();
+        return $query->get('Num');
+    }
+
+    # TODO: deprecated (use changeAccountData instead)
     public static function changeMail($id, $mail)
     {
         $mail = Database::instance()->escape($mail);
@@ -138,6 +153,7 @@ class Model_Account extends Model
         return $query->get('Num');
     }
 
+    # TODO: deprecated (use changeAccountData instead)
     public static function changeLogin($id, $login)
     {
         $login = Database::instance()->escape($login);
@@ -146,6 +162,7 @@ class Model_Account extends Model
         return $query->get('Num');
     }
 
+    # TODO: deprecated (use changeAccountData instead)
     public static function changePassword($id, $password)
     {
         $password = Database::instance()->escape($password);
@@ -154,6 +171,21 @@ class Model_Account extends Model
         return $query->get('Num');
     }
 
+
+    /**
+     * @param string $data
+     * @param string $type      'login','email' or 'code'
+     */
+    public static function checkAccountExistence($data, $type) {
+        $data = Database::instance()->escape($data);
+        $type = Database::instance()->escape($type);
+        $sql = "SELECT `CheckAccountExistence`($data, $type) AS Num;";
+        $res = DB::query(Database::SELECT, $sql)->execute();
+        return $res->get('Num');
+    }
+
+
+    # TODO: deprecated (use checkAccountExistence instead)
     public static function getAccNumByLogin($login)
     {
         $login = Database::instance()->escape($login);
@@ -162,6 +194,7 @@ class Model_Account extends Model
         return $res->get('Num');
     }
 
+    # TODO: deprecated (use checkAccountExistence instead)
     public static function getAccNumByMail($email)
     {
         $email = Database::instance()->escape($email);
@@ -176,7 +209,7 @@ class Model_Account extends Model
         $sql = "SELECT `CheckAccountExistence`($code, 'code') AS Num;";
         $res = DB::query(Database::SELECT, $sql)->execute();
 
-        $count = $res[0]['Num'];
+        $count = $res->get('Num');
         return $count == 1;
     }
 
diff --git a/~dev_rating/application/classes/User.php b/~dev_rating/application/classes/User.php
index f034f4f46..12ee9c9df 100644
--- a/~dev_rating/application/classes/User.php
+++ b/~dev_rating/application/classes/User.php
@@ -76,7 +76,7 @@ class User implements ArrayAccess
      * аккаунтов с такими же авторизационными данными.
      *
      * @param string $code   Код активации
-     * @param string $email  E-Mail адресс
+     * @param string $email  E-Mail адрес
      * @param string $login
      * @param string $password
      * @return array  Пару вида <tt>(is_ok, err_msg)</tt>
@@ -123,10 +123,10 @@ class User implements ArrayAccess
         }
     }
 
-    protected function completeSignIn($id, $passhash) {
+    protected function completeSignIn($id, $passHash) {
         $source = $id . Request::$user_agent . Request::$client_ip;
         $userHash = $this->hash($source) . $this->_config['hash_key'];
-        $passwordHash = $this->hash($passhash . $this->_config['hash_key']);
+        $passwordHash = $this->hash($passHash . $this->_config['hash_key']);
         Cookie::set('userhash', $passwordHash);
 
         $semester = Model_Account::getCurSemesterID();
@@ -208,7 +208,7 @@ class User implements ArrayAccess
         if (!$this->checkPassword($old))
             return false;
 
-        Model_Account::changePassword($this->ID, $new);
+        Model_Account::changeAccountData($this->ID, $new, 'password');
         $passhash = $this->hash($this->hash($new) . $this->_config['hash_key']);
         $this->_session->set('PasswordHash', $passhash);
         Cookie::set('userhash', $passhash);
@@ -219,7 +219,7 @@ class User implements ArrayAccess
         if (!$this->isSignedIn() || Account::instance()->loginExists($login))
             return false;
 
-        Model_Account::changeLogin($this->ID, $login);
+        Model_Account::changeAccountData($this->ID, $login, 'login');
         return true;
     }
 
@@ -236,7 +236,7 @@ class User implements ArrayAccess
     public function completeChangeMail($token) {
         $email = $this->_session->get('NewMail_Adress');
         if ($token == $this->_session->get('NewMail_Token') AND !Account::instance()->mailExists($email)) {
-            Model_Account::changeMail($this->ID, $email);
+            Model_Account::changeAccountData($this->ID, $email, 'email');
             return true;
         } else {
             return false;
-- 
GitLab