[eluser]johnnybravoh[/eluser]
Hello,
Below is the controller, model and view for adding an insurance agent into a database...
Since the post is too long to accommodate the model and the view, I will post them in another post...
I could easily have written this using standard procedural php but I figured I would give CI a shot. This is my first attempt. Please let me know if I'm doing anything that's not within the spirit of the MVC framework or if I could be more object oriented about things. In particular, I think that the whole address (name, address, city, state, etc....) thing is something that I've used over and over and over again. I just copy and paste code from one application to another. Working within CI, it makes me think about taking a more object oriented approach. Any thoughts???
Here's the code for your review and thanks in advance!
controller agentadmin.php
This controller is for administering an agent (in this case an insurance agent).
Code:
<?
require_once ('ApplicationController.php');
class Agentadmin extends ApplicationController {
function Agentadmin()
{
parent::ApplicationController();
$this->freakauth_light->check();
//handle picture upload, if any...
$config['relative_path']="/assets/uploads/";
$config['upload_path'] = '.'.$config['relative_path'];
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '240';
$config['max_height'] = '360';
$config['overwrite'] = TRUE ;
$this->load->library('upload', $config);
//when we first start out, this should be blank, but after a successful image upload this will contain the path to the uploaded image
//That way if the form is submitted again due to errors, the picture is not lost...
$Img="<img src=\"".$config['relative_path'].$this->session->userdata('img_name')." \" />";
if ( !$this->upload->do_upload('picture'))
{
if ($Img==''){
$this->data['picture_err']=$this->upload->display_errors('<div class="error Formerror">','</div>');
}
}
else
{
$this->data = array('upload_data' => $this->upload->data('picture'));
$this->session->set_userdata('img_name',$this->data['upload_data']['file_name']);
$Img="<img src=\"".$config['relative_path'].$this->session->userdata('img_name')." \" />";
}
$this->data['my_picture']=$Img;
//handle form validation
$this->load->library('validation');
$this->validation->set_error_delimiters('<div class="error Formerror">', '</div>');
$rules['name'] = "required";
$rules['address1'] = "required";
$rules['address2'] = "required";
$rules['city'] = "required";
$rules['state'] = "required";
$rules['zip'] = "required";
$rules['phone'] = "required";
$rules['email'] = "required";
$this->validation->set_rules($rules);
$fields['name'] = 'name';
$fields['address1'] = 'address1';
$fields['address2'] = 'address2';
$fields['city'] = 'city';
$fields['state'] = 'state';
$fields['zip'] = 'zip';
$fields['phone'] = 'phone';
$fields['email'] = 'email';
$this->validation->set_fields($fields);
$this->data['name_options']=array('name'=>'name','id'=>'name','maxlength'=>'30','size'=>'30','value'=>$this->validation->name);
$this->data['address1_options']=array('name'=>'address1','id'=>'address1','maxlength'=>'30','size'=>'30','value'=>$this->validation->address1);
$this->data['address2_options']=array('name'=>'address2','id'=>'address2','maxlength'=>'30','size'=>'30','value'=>$this->validation->address2);
$this->data['city_options']=array('name'=>'city','id'=>'city','maxlength'=>'30','size'=>'30','value'=>$this->validation->city);
$this->data['state_options']=array('name'=>'state','id'=>'state','maxlength'=>'30','size'=>'30','value'=>$this->validation->state);
$this->data['zip_options']=array('name'=>'zip','id'=>'zip','maxlength'=>'30','size'=>'30','value'=>$this->validation->zip);
$this->data['phone_options']=array('name'=>'phone','id'=>'phone','maxlength'=>'30','size'=>'30','value'=>$this->validation->phone);
$this->data['email_options']=array('name'=>'email','id'=>'email','maxlength'=>'30','size'=>'30','value'=>$this->validation->email);
$this->data['picture_options']=array('name'=>'picture','id'=>'picture','maxlength'=>'30','size'=>'30','value'=>'');
$this->template_content['title'] = "Agents";
$this->template_content['right_content'] = "Insurance";
}
function index()
{
$this->template_content['main_content']=$this->load->view('partials/_agentadmin_menu','',TRUE);
$this->load->view('layouts/base', $this->template_content);
}
function add()
{
if ($this->validation->run() == FALSE)
{
$this->load->view('partials/forms/_agent_form', $this->data);
}
else
{
//post
$this->load->model('Agent');
$this->Agent->add();
$this->template_content['main_content']="Agent Added Successfully!";
}
$this->data['heading'] = "Add an Agent";
$this->load->view('layouts/base', $this->template_content);
}
}
?>