Professional CodeIgniter

(singke) #1

Chapter 7: Improving the Dashboard


206


Of course, if Claudia had wanted to limit her employees ’ accessibility, you would handle the user
privileges differently. The best way to handle user privileges is to create and administer user groups.
Each group would have different modules assigned to it. That way you could easily assign different
users to each group and provide yourself with a lot of flexibility. In other words, if Group A had access
to modules 1, 2, and 3, and if User Y were assigned to Group A, he or she would have the right access. If
you wanted to remove access to a particular group, you could shift the user to a different group, or drop
a module from the group. In your database, your Groups table would have a primary key and a name.
You would need a mapping table (to designate which modules could be accessed by which group),
and you would need to add a group_id designator to a user (and then add a groups dropdown to the
view on the appropriate admin screens).

Reassigning Products from Deleted


Categories


Now that you ’ ve met with Claudia, you know how she wants to handle potentially orphaned products.
Orphaned products come about as a natural result of deleting a “ container ” like a category. If the
category goes away, what do you do with all the items that it is “ holding ”? Now that you know what
Claudia ’ s expectations are, it ’ s an easy matter of adding a bit of processing to the end of the admin/
categories/delete function.

Here ’ s a quick look at that function to refresh your memory:

function delete($id){
$this- > MCats- > deleteCategory($id);
$this- > session- > set_flashdata(‘message’,’Category deleted’);
redirect(‘admin/categories/index’,’refresh’);
}

What you ’ re going to need when you rewrite this function is some way to check for orphaned products.
The best way to do that is to create a checkOrphans() function in the MCats model. This function
accepts a category ID as its only argument and then returns an array of products that match that
category_id.

function checkOrphans($id){
$data = array();
$this- > db- > select(‘id,name’);
$this- > db- > where(‘category_id’,$id);
$Q = $this- > db- > get(‘products’);
if ($Q- > num_rows() > 0){
foreach ($Q- > result_array() as $row){
$data[$row[‘id’]] = $row[‘name’];
}
}
$Q- > free_result();
return $data;
}
Free download pdf