Professional CodeIgniter

(singke) #1

Chapter 7: Improving the Dashboard


207


Now that you have this function in place, you can rewrite the delete() controller function.

function delete($id){
$this- > MCats- > deleteCategory($id);
$orphans = $this- > MCats- > checkOrphans($id);
if (count($orphans)){
$this- > session- > set_userdata(‘orphans’,$orphans);
redirect(‘admin/categories/reassign/’.$id,’refresh’);
}else{
$this- > session- > set_flashdata(‘message’,’Category deleted’);
redirect(‘admin/categories/index’,’refresh’);
}
}

In other words, go ahead and run deleteCategory() , but then run a check on how many orphans were
left behind. If none are detected, send the user back to the index page of categories as before. If you find
orphans, store the information in a session variable and redirect to admin/categories/refresh.


Just as simple is the reassign() controller function. First, check to see if there are any POST data. If
there are POST data, it means that the user has visited the admin_cat_reassign page and assigned the
products to a new category. Run the reassignProducts() model function (you ’ ll add it to the
MProducts model shortly), and redirect the user to the category index with the appropriate message.


Otherwise, build the admin_cat_reassign view.


function reassign($id=0){
if ($_POST){
$this- > MProducts- > reassignProducts();
$this- > session- > set_flashdata(‘message’,’Category deleted and products
reassigned’);
redirect(‘admin/categories/index’,’refresh’);
}else{
$data[‘category’] = $this- > MCats- > getCategory($id);
$data[‘title’] = “Reassign Products”;
$data[‘main’] = ‘admin_cat_reassign’;
$data[‘categories’] = $this- > MCats- > getCategoriesDropDown();
$this- > load- > vars($data);
$this- > load- > view(‘dashboard’);
}
}

The admin_cat_reassign view should simply list all the products that are about to be orphaned, and then
provide the user with a simple dropdown box, so they can choose a new category. Remember to remove
the ID of the category that is being deleted, of course — no sense in having that in the dropdown!

< h1 > < ?php echo $title;? > < /h1 >
< p > The following products are about to be orphaned. They used to belong to the
< b > < ?php echo $category[‘name’];? > < /b > category, but now they need to be
reassigned. < /p >
Free download pdf