Welcome Guest, Not a member yet? Register   Sign In
Seeking Advice
#1

[eluser]xjohnson[/eluser]
Hi -

I'm not a professional coder, and I'm working on code that seems too long. I'd like to share it with you and get your advice on how to make it better.

Code:
public function index()
{
    $this->form_validation->set_rules( // code );
    $this->form_validation->set_message( // code );

    if ( $this->form_validation->run() )
    {
        // create a confirmation url to email to the user to click on to confirm the sign up process
        $conf_url = $this->_get_confirmation_url();

        // collect the creation date and account number
        $created = $account_number = strtotime( "now" );
        $created = date( 'Y-m-d H:i:s', $created );

        // generate an account number
        $account_number = $this->_account_number( $account_number );

        // update database with user's information
        $this->load->model( 'users_model' );
        $insertion_success = $this->users_model->addUser( array(
            'firstname'    =>    $this->input->post( 'firstname' ),
            'lastname'    =>    $this->input->post( 'lastname' ),
            'email'        =>    $this->input->post( 'email' ),
            'password'    =>    $this->input->post( 'password' ),
            'conf_email'    =>    $conf_url,
            'status'        =>    'active',
            'created'        =>    $created,
            'account_number'=>    $account_number
        ) );

        if ( $insertion_success !== FALSE )
        {
        # Great!  Sign-up info was successfully inserted into database.
        # So, send confirmation email to new user.

            $message_data = array(
                // code here
            );
            $message = $this->load->view( 'email/email_signup', $message_data, TRUE );
            $mailing_options = array(
                'message'        => $message,
                'to_address'    => $message_data['email'],
                'to_name'        => $message_data['first_name'],
                'from_address'    => '',
                'from_name'    => 'Service Team',
                'subject'        => ''
            );
            $result_mail_to_user = $this->_send_mail( $mailing_options );

            if ( ! $result_mail_to_user )
            {
            # Uh, oh!  Sending the confirmation email to user failed.  So, send an email to admin about it.
            # Let new user know that they'll receive a confirmation email in 24 hours.

                $message = $this->load->view( 'email/anoncamp_signup_email_confirmation_failed', $message_data, TRUE );
                $mail_options = array(
                    'message'        => $message,
                    'to_address'    => '',
                    'to_name'        => 'Service Team',
                    'from_address'    => '',
                    'from_name'    => 'Server Message',
                    'subject'        => ''
                );
                $mail_result = $this->_send_mail( $mail_options );
                if( ! $mail_result )
                {
                # Uh, oh!  Another email failure!  Save a message in the log file.

                    $this->load->helper( 'file' );

                    $message = "<?php if( ! defined('BASEPATH')) exit('what?');\r\n" . $message;
                    $date = date( 'dMYHis' );
                    write_file( "./logs/$date.php", $message, 'a+' );
                }

                $this->data['menu'] = $this->load->view( 'anoncamp/anoncamp_signup_menu', array(), TRUE );
                $this->load->view( '../template/template_frontpage', $this->data );
            }
            else
            {
            # Great!  Sending the confirmation email to user succeeded.  So, send an email to admin about it.
            # Inform the new user of this great news.

                $message = $this->load->view( 'email/anoncamp_signup_email_confirmation_success', $message_data, TRUE );
                $mail_options = array(
                    'message'        => $message,
                    'to_address'    => '',
                    'to_name'        => 'Service Team',
                    'from_address'    => '',
                    'from_name'    => 'Server Message',
                    'subject'        => ''
                );
                $this->_send_mail( $mail_options );

                $this->data['menu'] = $this->load->view( 'anoncamp/anoncamp_signup_menu', array(), TRUE );
                $this->load->view( '../template/template_frontpage', $this->data );
            }
        }
        else
        {
        # Uh, oh!  Inserting sign-up info into database failed.
        # Alert the user, show a phone number to call, and send email to admin about it.
            $message_data = array(
                'first_name'    => $this->input->post( 'firstname' ),
                'email'        => $this->input->post( 'email' ),
                'href'        => $conf_url
            );
            $message = $this->load->view( 'email/anoncamp_signup_email_confirmation_failed', $message_data, TRUE );
            $mail_options = array(
                // code here
            );
            $this->_send_mail( $mail_options );

            $this->data['menu'] = $this->load->view( 'anoncamp/anoncamp_signup_menu', array(), TRUE );
            $this->load->view( '../template/template_frontpage', $this->data );
        }
    }
    else
    {
    # Uh, oh!  The form data is invalid!
    # So, re-display the form with the info re-populated and validation errors displayed.
        $this->data['menu'] = $this->load->view( 'anoncamp/anoncamp_signup_menu', array(), TRUE );
        $this->load->view( '../template/template_frontpage', $this->data );
    }
}

Thanks in advance


Warm Regards


Messages In This Thread
Seeking Advice - by El Forum - 07-29-2011, 09:10 AM
Seeking Advice - by El Forum - 08-01-2011, 08:49 PM
Seeking Advice - by El Forum - 08-02-2011, 02:45 AM
Seeking Advice - by El Forum - 08-07-2011, 03:42 AM



Theme © iAndrew 2016 - Forum software by © MyBB