SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP OO: implement an error manager to my registration class.

    Hi,

    I am trying to leave the procedural approach and stick with OO. Unfortunately, it's still often beyond me...

    So I need your help on that subject. I am trying to create a class that will handle all the error messages.

    Here is the class itself:

    PHP Code:
    class ErrorManager {

    var 
    $errors;

        function 
    addError($description
        { 
            
            
    $this->errors[] = $description
           
        } 

           function 
    isError() 
           { 
        
              return (
    is_array($this->errors) && count($this->errors) > 0);
           
           } 

        function 
    getErrors() 
        { 
        
            return 
    $this->errors
            
        } 


    I am working on the first plug in of this class within another class.
    First of all, I instantiate the object in my registration page:

    PHP Code:
    $errormanager_registration = & new ErrorManager(); 
    Then I get the submitted data and instantiate the register object in my registration page:

    PHP Code:
       //get the submitted data (it will be cleaned up by the class).
       
    $params = array(   'username'        => $_POST['username']
                        , 
    'password'        => $_POST['password']
                        , 
    'confirmpassword' => $_POST['confirmpassword']
                        , 
    'siteurl'         => $_POST['siteurl']); 
        
       
    //create the registration object and process the registration.
       
    $register = new Registration($db$params$errormanager_registration); 
    Here is the class handling registration (I have added a some sql statement to ease my life during developpement, they will probably be moved to a better place once my error management will be correctly implemented):

    PHP Code:
    <?php

    class Registration {


    var 
    $db;

    var 
    $username;

    var 
    $password;

    var 
    $confirmpassword;

    var 
    $siteurl;

    var 
    $errors;

    var 
    $lastlogin;

    var 
    $sql;

    var 
    $errormanager;
        
       function 
    Registration(&$db$input$errormanager
       { 
        
          if (
          
                
    $this->isInputComplete($input)
                
             ) 
             
          {
                
                
    $this->db              = &$db;        
                
    $this->username        $this->clean($input['username']); 
                
    $this->password        $this->clean($input['password']); 
                
    $this->confirmpassword $this->clean($input['confirmpassword']);
                
    $this->siteurl         $this->clean($input['siteurl']);
                
    $this->lastlogin       time();
                
    $this->errormanager    $errormanager;
                
    $this->CheckUserAndPass();
            
            }     
        } 

       function 
    isInputComplete($input
       { 
         
          
    $checkfields = isset($input['username']) 
                      && isset(
    $input['password']) 
                      && isset(
    $input['confirmpassword'])
                      && isset(
    $input['siteurl']);
                     
          if (
            
                !
    $checkfields
            
             

             
          { 
                
             
    $this->errormanager->addError('Veuillez entrer toutes les informations demand&eacute;es'); 
             return 
    $checkfields
          
          }         
          
          else 
          
          {
          
              return 
    true;
          
          }
          
       
       } 

        function 
    clean($string
        { 
        
            
    $cleaned trim($string); 
            
    $cleaned addslashes($cleaned);
            
            return 
    $cleaned;
        } 
        
        function 
    CheckUserAndPass() 
        {
        
            if (!isset(
    $this->username) || !isset($this->password) || !isset($this->confirmpassword) || !isset($this->siteurl)) { 
                
    $this->errormanager->addError('Veuillez entrer toutes les informations demand&eacute;es'); 
                return 
    false;
            }
        
            else {
                
    $this->CheckSize();
                return 
    true;
            }
            
        }
       
        function 
    CheckSize() {
            if ((
    strlen($this->username) < || strlen($this->password) < 4   || strlen($this->confirmpassword) < 4  || strlen($this->siteurl) < 10
            || (
    strlen($this->username) > 30 || strlen($this->password) > 30) || strlen($this->confirmpassword) > 30 || strlen($this->siteurl) > 100) { 
                
    $this->errormanager->addError('Veuillez vous assurer que vos informations ont la taille demand&eacute;e');
                return 
    false;
            }
            else {
                
    $this->NotTheSame();
                return 
    true;
            }
            
        }

        function 
    NotTheSame() {
            if (
    $this->password !== $this->confirmpassword) {
                
    $this->errormanager->addError('Veuillez entrer deux fois le m&ecirc;me mot de passe');
                return 
    false;
            } 
            else {
                
    $this->DifferentValues();
                return 
    true;
            }
            
        }
        
        function 
    DifferentValues() {
            if (
    $this->username == $this->password) {
                
    $this->errormanager->addError('Veuillez choisir un nom d\'utilisateur et un mot de passe diff&eacute;rents');
                return 
    false;
            }
            else {
                
    $this->HashPassword();
                return 
    true;
            }
        }
        
           function 
    HashPassword() {
            
    $this->password md5($this->password);
            
    $this->createAdminTable();
            return 
    true;
        }
        
        function 
    createAdminTable() {
           
    $this->sql =
           
    "CREATE TABLE admin (
            adminid mediumint(8) unsigned NOT NULL auto_increment,
            username varchar(30) NOT NULL default '',
            password varchar(32) NOT NULL default '',
            permission mediumint(8) unsigned NOT NULL default '0',
            lastlogin int(10) unsigned NOT NULL default '0',
            PRIMARY KEY  (adminid)
            )"
    ;
            
            
    $this->db->query($this->sql);
            
    $this->saveAdminInfo();
            return 
    true
        }
            
        function 
    saveAdminInfo() { 
            
    $this->sql =
            
    "INSERT INTO admin 
            SET 
            username     = '
    $this->username'
            , password   = '
    $this->password'
            , permission = 1
            , lastlogin  = 
    $this->lastlogin";
            
            
    $this->db->query($this->sql);
            
    $this->createGeneralinfoTable();
            return 
    true
        }

            function 
    createGeneralinfoTable() {
            
    $this->sql =
            
    "CREATE TABLE generalinfo (
            generalinfoid tinyint(3) unsigned NOT NULL auto_increment,
            siteurl varchar(100) NOT NULL default '',
            sitekeywords text,
            sitetitle varchar(100) NOT NULL default '',
            maxlength_sitetitle smallint(5) unsigned NOT NULL default '0',
            minlength_sitetitle smallint(5) unsigned NOT NULL default '0',
            minlength_pagetitle tinyint(5) unsigned NOT NULL default '0',
            maxlength_pagetitle smallint(5) unsigned NOT NULL default '0',
            minlength_pagetexttitle tinyint(10) unsigned NOT NULL default '0',
            maxlength_pagetexttitle smallint(5) unsigned NOT NULL default '0',
            maxlength_pagetext smallint(5) unsigned NOT NULL default '0',
            minlength_pagetext tinyint(10) NOT NULL default '0',
            maxlength_keywords mediumint(8) unsigned NOT NULL default '0',
            defaultlanguage tinyint(3) unsigned NOT NULL default '0',
            PRIMARY KEY  (generalinfoid)
            )"
    ;
            
            
    $this->db->query($this->sql);        
            
            
    $this->saveGeneralinfo();
            return 
    true
        }
        
        function 
    saveGeneralinfo() 
        { 
        
            
    //add the info
            
    $this->sql="
            INSERT INTO generalinfo 
            VALUES (
            1
            , '
    $this->siteurl'
            , 'keywords'
            , 'sitetitle'
            , 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , 1)"
    ;
            
            
    $this->db->query($this->sql);
            
            return 
    true
        }

        function 
    getErrors() 
        {
        
            return 
    $this->errormanager->getErrors();
            
        }

    }

    ?>
    Then I check that there are no errors. If no err, move to setup2.php.
    If there are errors, display them:
    PHP Code:
       //if there are no errors, redirect the user to the registered.php with a nice little message.
       
    if (

             !
    $register->errormanager->isError()
             
          ) 
          
       {
       
            
    header('Location:setup2.php'); 
            exit;
       
       }
       
       else
       
       {

          
    //display the error messages.
          
    foreach($register->errormanager->getErrors() as $error
          {

             echo (
    '<p>' $error '</p>');

          }
    //end foreach

    }//end if 
    Here comes my problem. I always get the same error message, the one that is added in the CheckSize function.

    I am seeking some advice and some help on how to implemant this error handling system the best way I can.

    I know it still looks very procedural but hopefully I will be able to improve my abstraction skills in the near future.

    Just to sum it up: here is the (in one PHP code div) code that makes use of the different classes:

    PHP Code:

    $errormanager_registration 
    = & new ErrorManager();

     
    //get the submitted data (it will be cleaned up by the class).
       
    $params = array(   'username'        => $_POST['username']
                        , 
    'password'        => $_POST['password']
                        , 
    'confirmpassword' => $_POST['confirmpassword']
                        , 
    'siteurl'         => $_POST['siteurl']); 
        
       
    //create the registration object and process the registration.
       
    $register = new Registration($db$params$errormanager_registration);
        
       
    //if there are no errors, redirect the user to the registered.php with a nice little message.
       
    if (

             !
    $register->errormanager->isError()
             
          ) 
          
       {
       
            
    header('Location:setup2.php'); 
            exit;
       
       }
       
       else
       
       {

          
    //display the error messages.
          
    foreach($register->errormanager->getErrors() as $error
          {

             echo (
    '<p>' $error '</p>');

          }
    //end foreach

       


  2. #2
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok nevermind, as usual that's when you post that you find the answer


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •