• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Would like to condense some code.

#2
[eluser]Damien K.[/eluser]
I'm a "noob" myself so take this for what it is worth. This should be self-explanatory with the embedded comments. It is not exactly how I would do it, but it gives the idea. HTH.

In your Contact controller:

Code:
function contact()
{
    ... [same] ...

    if($this->input->post('submitform'))
    {
        $this->_do_contact();
    }
    else
    {
        $this->_populate_contact();
    }

    // you may want to search the forum for various ways to handle this (ie, some mix of templating sub-system)
    $this->load->view('header', $data);
    $this->load->view('contact', $data);
    $this->load->view('footer', $data);
}

function _do_contact()
{
    $this->form_validation->set_rules('name','Name', 'required');
    $this->form_validation->set_rules('email','Email', 'required|valid_email');
    $this->form_validation->set_rules('subject','Subject', 'required');
    $this->form_validation->set_rules('text','Message', 'required');
    
    if($this->form_validation->run())
    {
        $contact = new Contact(); // I like to make a contact object to hold contact info

        $contact->name = $this->input->post('name', TRUE);
        $contact->email = $this->input->post('email', TRUE);
        $contact->subject = $this->input->post('subject', TRUE);
        $contact->text = $this->input->post('text', TRUE);

        $this->Contact_logic->send($contact);
        $this->session->set_flashdata('message', "Successfully sent email. We will get back to you as soon as possible.");
        redirect('site/home');
    }
}

function _populate_contact()
{
    if($this->userlib->logged_in())
    {
        $this->load->model('User_model', 'user_m');
        $userid = $this->session->userdata('userid');
        $result = $this->user_m->name_and_email($userid);

        // populate form with $result using another mean
        // I've extending CI to handle form population
        // you can check out the following forum post for an idea
        // http://ellislab.com/forums/viewthread/106052/#533725
    }
}

Contact_logic class:

Code:
class Contact_logic
{
    public function send($contact)
    {
        $config['protocol'] = 'sendmail'; // probably want to get this from a config file
        $this->email->initialize($config);
        
        $this->email->from($contact->email, $contact->name);
        $this->email->to('samuurai@asdf.com'); // get your to address from a config file
        $this->email->subject($contact->subject);
        $this->email->message($contact->text);
        $this->email->send();

        return true; // proper error handling required
    }
}

EDIT: Look at 'form helper' on how to repopulate the form on input error.


Messages In This Thread
Would like to condense some code. - by El Forum - 09-29-2009, 10:29 AM
Would like to condense some code. - by El Forum - 10-01-2009, 08:53 AM
Would like to condense some code. - by El Forum - 10-02-2009, 10:16 AM
Would like to condense some code. - by El Forum - 10-08-2009, 05:50 AM
Would like to condense some code. - by El Forum - 10-08-2009, 06:10 AM
Would like to condense some code. - by El Forum - 10-08-2009, 07:01 AM
Would like to condense some code. - by El Forum - 10-08-2009, 08:30 AM
Would like to condense some code. - by El Forum - 10-09-2009, 06:42 AM
Would like to condense some code. - by El Forum - 10-09-2009, 09:35 AM
Would like to condense some code. - by El Forum - 10-09-2009, 12:27 PM
Would like to condense some code. - by El Forum - 10-09-2009, 01:19 PM
Would like to condense some code. - by El Forum - 10-09-2009, 02:51 PM
Would like to condense some code. - by El Forum - 10-09-2009, 05:41 PM
Would like to condense some code. - by El Forum - 10-09-2009, 05:44 PM
Would like to condense some code. - by El Forum - 10-11-2009, 12:37 PM
Would like to condense some code. - by El Forum - 10-11-2009, 12:47 PM
Would like to condense some code. - by El Forum - 10-11-2009, 02:58 PM
Would like to condense some code. - by El Forum - 10-13-2009, 08:31 AM
Would like to condense some code. - by El Forum - 10-14-2009, 10:02 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.