[eluser]Devon Lambert[/eluser]
While working on the Admin section of my site, I find that I'm constantly writing same chunks of code over and over again in my controllers.
Can someone please help me break this
insane cycle? :-)
Example of the code:
Bit of code from admin.php for my site management
Code:
function add($form_submission = false)
{
$this->data->themes = $this->themes_m->get_all();
if ($form_submission == true) {
$this->form_validation->set_rules($this->rules);
if ($this->form_validation->run() == false) {
$this->template->build('admin/add_form', $this->data);
} else {
if ($this->sites_m->create($_POST) > 0) {
$this->session->set_flashdata("success", "The site got saved!");
redirect('admin/sites');
} else {
$this->session->set_flashdata("error", "The site was not saved!");
redirect('admin/sites');
}
}
} else {
$this->template->build('admin/add_form', $this->data);
}
}
function edit($form_submission = 'false', $id = null)
{
if ($form_submission === 'true') {
$this->form_validation->set_rules($this->rules);
if ($this->form_validation->run() == false) {
$this->template->build('admin/edit_form', $this->data);
} else {
if ($this->sites_m->update($_POST) > 0) {
$this->session->set_flashdata("success", "Your changes were saved!");
redirect('admin/sites');
} else {
$this->session->set_flashdata("error", "Something went wrong. The changes didn't take!");
redirect('admin/sites');
}
}
} else {
$this->data->site = $this->sites_m->get($id);
$this->template->build('admin/edit_form', $this->data);
}
}
Is there some way for me to make use of this code in a reusable way? Realize that I have tried, and failed, to place this code in both a Parent Controller as well as in a helper.