From bf2155634d4276fcb7495e2bfda110ee02fa1ae2 Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Fri, 7 Jan 2011 22:31:59 +0000 Subject: [PATCH] * Add protection against inaccessable resources when their category is removed Resources will be moved to the parent category, default category from setup, or an admin only 'No category'. Resources' ACL requires a category --- resources/inc/class.resources_hooks.inc.php | 53 +++++++++++++++++++++ resources/setup/setup.inc.php | 1 + 2 files changed, 54 insertions(+) diff --git a/resources/inc/class.resources_hooks.inc.php b/resources/inc/class.resources_hooks.inc.php index f935f122c6..c5722e036d 100644 --- a/resources/inc/class.resources_hooks.inc.php +++ b/resources/inc/class.resources_hooks.inc.php @@ -117,4 +117,57 @@ class resources_hooks ) ); } + + /** + * Handle deleted category + * + * Resources' ACL _requires_ a category. + * Moves all resources to parent, if it exists. If it doesn't, another category is created. + */ + function delete_category($args) + { + $cat = categories::read($args['cat_id']); + + if(!$cat) return; // Can't find current cat? + + if($cat['parent'] == 0) + { + // No parent, try the default cat from setup + $categories = new categories('', 'resources'); + $default = $categories->name2id('General resources'); + if($default) + { + $new_cat_id = $default; + } + else + { + // Default missing, look for 'No category' + $new_cat_id = $categories->name2id('No category'); + if($new_cat_id == 0) { + // No category not there, add it + $new_cat_id = $categories->add(array( + 'name' => 'No category', + 'description' => 'This category has been added to rescue resources whose category was deleted.', + 'parent' => 0 + )); + $admin = -2; + ExecMethod2('resources.bo_acl.set_rights', $new_cat_id, array($admin), array($admin), array($admin), array($admin),array($admin)); + } + } + } + else + { + $new_cat_id = $cat['parent']; + } + + // Get any resources affected + $query = array('filter' => $args['cat_id']); + $bo = CreateObject('resources.bo_resources'); + $bo->get_rows($query, $resources, $readonly); + foreach($resources as $resource) + { + $resource['cat_id'] = $new_cat_id; + $bo->save($resource); + } + } } diff --git a/resources/setup/setup.inc.php b/resources/setup/setup.inc.php index c37c018071..044d0d34c5 100755 --- a/resources/setup/setup.inc.php +++ b/resources/setup/setup.inc.php @@ -33,6 +33,7 @@ $setup_info['resources']['hooks']['admin'] = 'resources.resources_hooks.admin_p $setup_info['resources']['hooks']['sidebox_menu'] = 'resources.resources_hooks.admin_prefs_sidebox'; $setup_info['resources']['hooks']['search_link'] = 'resources.resources_hooks.search_link'; $setup_info['resources']['hooks']['calendar_resources'] = 'resources.resources_hooks.calendar_resources'; +$setup_info['resources']['hooks']['delete_category'] = 'resources.resources_hooks.delete_category'; // $setup_info['resources']['hooks'][] = 'home'; // $setup_info['resources']['hooks'][] = 'settings';