More code merged from ship-simu
[mailer.git] / inc / classes / main / user / class_User.php
index 280adec1600d86e43d65fc161b223fbe348da7eb..25e2cc3ffd1e5cdd2745da5d444425878062355e 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class User extends BaseFrameworkSystem implements ManageableUser {
+class User extends BaseFrameworkSystem implements ManageableUser, Registerable {
        /**
-        * Username
+        * Username of current user
         */
-       private $username = "";
+       private $userName = "";
+
+       /**
+        * Email of current user
+        */
+       private $email = "";
 
        // Exceptions
-       const EXCEPTION_USERNAME_NOT_FOUND = 0xd00;
+       const EXCEPTION_USERNAME_NOT_FOUND   = 0xd00;
+       const EXCEPTION_USER_EMAIL_NOT_FOUND = 0xd01;
 
        /**
-        * Private constructor
+        * Protected constructor
         *
         * @return      void
         */
@@ -46,7 +52,7 @@ class User extends BaseFrameworkSystem implements ManageableUser {
                $this->setObjectDescription("Generic user class");
 
                // Create unique ID number
-               $this->createUniqueID();
+               $this->generateUniqueId();
 
                // Clean up a little
                $this->removeNumberFormaters();
@@ -58,7 +64,7 @@ class User extends BaseFrameworkSystem implements ManageableUser {
         * factory method will check if the username is already taken and if not
         * so it will throw an exception.
         *
-        * @param       $username               Username we need a class instance for
+        * @param       $userName               Username we need a class instance for
         * @return      $userInstance   An instance of this user class
         * @throws      UsernameMissingException        If the username does not exist
         */
@@ -67,7 +73,7 @@ class User extends BaseFrameworkSystem implements ManageableUser {
                $userInstance = new User();
 
                // Set the username
-               $userInstance->setUsername($userName);
+               $userInstance->setUserName($userName);
 
                // Check if the username exists
                if (!$userInstance->ifUsernameExists()) {
@@ -79,14 +85,42 @@ class User extends BaseFrameworkSystem implements ManageableUser {
                return $userInstance;
        }
 
+       /**
+        * Creates an instance of this user class by a provided email address. This
+        * factory method will not check if the email address is there.
+        *
+        * @param       $email                  Email address of the user
+        * @return      $userInstance   An instance of this user class
+        */
+       public final static function createUserByEmail ($email) {
+               // Get a new instance
+               $userInstance = new User();
+
+               // Set the username
+               $userInstance->setEmail($email);
+
+               // Return the instance
+               return $userInstance;
+       }
+
        /**
         * Setter for username
         *
         * @param       $userName       The username to set
         * @return      void
         */
-       protected final function setUsername ($userName) {
-               $this->userNane = $userName;
+       public final function setUserName ($userName) {
+               $this->userName = $userName;
+       }
+
+       /**
+        * Setter for email
+        *
+        * @param       $email  The email to set
+        * @return      void
+        */
+       protected final function setEmail ($email) {
+               $this->email = $email;
        }
 
        /**
@@ -95,7 +129,16 @@ class User extends BaseFrameworkSystem implements ManageableUser {
         * @return      $userName       The username to get
         */
        public final function getUsername () {
-               return $this->userNane;
+               return $this->userName;
+       }
+
+       /**
+        * Getter for email
+        *
+        * @return      $email  The email to get
+        */
+       public final function getEmail () {
+               return $this->email;
        }
 
        /**
@@ -103,12 +146,12 @@ class User extends BaseFrameworkSystem implements ManageableUser {
         *
         * @return      $exists         Wether the username exists
         */
-       protected function ifUsernameExists () {
+       public function ifUsernameExists () {
                // By default the username does exist
                $exists = true;
 
                // Get a UserDatabaseWrapper instance
-               $wrapperInstance = UserDatabaseWrapper::createUserDatabaseWrapper();
+               $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper');
 
                // Create a search criteria
                $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria');
@@ -121,10 +164,42 @@ class User extends BaseFrameworkSystem implements ManageableUser {
                $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
 
                // Search for it
-               if ($result->next()) {
-                       // Entry found, so all is fine
-                       $exists = true;
-               }
+               if (!$result->next()) {
+                       // Entry not found
+                       $exists = false;
+               } // END - if
+
+               // Return the status
+               return $exists;
+       }
+
+       /**
+        * Determines wether the email exists or not
+        *
+        * @return      $exists         Wether the email exists
+        */
+       public function ifEmailAddressExists () {
+               // By default the username does exist
+               $exists = true;
+
+               // Get a UserDatabaseWrapper instance
+               $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper');
+
+               // Create a search criteria
+               $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria');
+
+               // Add the username as a criteria and set limit to one entry
+               $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
+               $criteriaInstance->setLimit(1);
+
+               // Get a search result
+               $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+
+               // Search for it
+               if (!$result->next()) {
+                       // Entry not found
+                       $exists = false;
+               } // END - if
 
                // Return the status
                return $exists;