| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: rules.export.inc,v 1.1.2.12 2010/11/25 11:14:52 fago Exp $ 3 4 /** 5 * @file 6 * Provides export functionality and integrates with the features module. 7 */ 8 9 /** 10 * Export all items given, where $export has to be an array of arrays 11 * keyed by item types, containing the items keyed by their names. 12 */ 13 function rules_export_items($export, $module = NULL) { 14 foreach (array_keys($export) as $item_type) { 15 // Allow item specific adaption before exporting 16 foreach ($export[$item_type] as $item_name => $item) { 17 rules_item_type_invoke($item_type, 'export', array($item_name, &$export[$item_type][$item_name], &$export, $module)); 18 } 19 } 20 return $export; 21 } 22 23 /** 24 * Item type callback: When exporting a rule set, add its rules to the export. 25 */ 26 function rules_item_rule_set_export($set_name, &$rule_set, &$export, $module = NULL) { 27 $rules = rules_get_configured_items('rules'); 28 foreach ($rules as $name => $rule) { 29 if ($rule['#set'] == $set_name) { 30 $export['rules'][$name] = $rule; 31 rules_item_type_invoke('rules', 'export', array($name, &$export['rules'][$name], &$export, $module)); 32 } 33 } 34 if (!empty($module)) { 35 // Add the module name as category. 36 $rule_set += array('categories' => array()); 37 $rule_set['categories'][$module] = $module; 38 $rule_set['status'] = 'default'; 39 } 40 } 41 42 43 /** 44 * Change the status of the rules to default. 45 */ 46 function rules_item_rule_export($rule_name, &$rule, &$export, $module = NULL) { 47 static $counter = 0; 48 49 // Be sure the exported rule has the right version specified, which is 50 // important if the export is used as default. 51 $rule['#version'] = 6003; 52 53 if (!empty($module)) { 54 // Change #status to 'default'. 55 $rule['#status'] = 'default'; 56 // Add the module name as category. 57 if (empty($rule['#categories']) || !in_array($module, $rule['#categories'])) { 58 $rule['#categories'][$module] = $module; 59 } 60 } 61 } 62 63 /** 64 * Implementation of hook_features_export_options() for tags. 65 */ 66 function rules_categories_features_export_options() { 67 rules_include('rules_admin'); 68 return rules_admin_get_categories('rules') + rules_admin_get_categories('rule_sets'); 69 } 70 71 /** 72 * Implementation of hook_features_export_render() for categories. 73 */ 74 function rules_categories_features_export_render($module = 'foo', $data) { 75 $code = array(); 76 $code[] = ' $rules = array();'; 77 $code[] = ''; 78 79 // Build the usual rules $export structure for sets and categories. 80 $rules_export = array(); 81 $items = rules_get_configured_items('rule_sets'); 82 rules_include('rules_admin'); 83 module_load_include('inc', 'rules_admin', 'rules_admin.export'); 84 rules_admin_export_by_category($rules_export, $data); 85 86 $module = strpos($module, '_features_comparison') === 0 ? '' : $module; 87 $export = rules_export_items($rules_export, $module); 88 $defaults = features_var_export($export, ' '); 89 90 return array('rules_defaults' => " return $defaults;"); 91 } 92 93 /** 94 * Implementation of hook_features_export() for categories. 95 */ 96 function rules_categories_features_export($data, &$export, $module_name = '') { 97 rules_include('rules_admin'); 98 module_load_include('inc', 'rules_admin', 'rules_admin.export'); 99 100 $rules_export = array(); 101 rules_admin_export_by_category($rules_export, $data); 102 103 // Process the rules and sets to be exported for dependencies. 104 $pipe = array(); 105 $rules_export += array('rules' => array(), 'rule_sets' => array()); 106 foreach ($rules_export['rules'] as $rule) { 107 rules_features_process_rule($rule, $export, $pipe); 108 } 109 foreach ($rules_export['rule_sets'] as $set_name => $set) { 110 rules_features_process_set($set_name, $export, $pipe); 111 } 112 113 $export['features']['rules_categories'] = drupal_map_assoc($data); 114 $export['dependencies']['rules'] = 'rules'; 115 116 return $pipe; 117 } 118 119 /** 120 * Implementation of hook_features_revert() for rule categories. 121 * 122 * @param $module 123 * name of module to revert content for 124 */ 125 function rules_categories_features_revert($module = NULL) { 126 rules_include('rules_admin'); 127 module_load_include('inc', 'rules_admin', 'rules_admin.export'); 128 $rules_export = array(); 129 rules_admin_export_by_category($rules_export, array($module)); 130 131 // Delete Normal / Overridden rules items that are defined in code 132 foreach ($rules_export as $item_type => $items) { 133 foreach ($items as $item_name => $item) { 134 rules_item_delete($item_type, $item_name); 135 } 136 } 137 rules_clear_cache(); 138 return TRUE; 139 } 140 141 142 /** 143 * Retrieves all necessary module dependencies for a list of rule sets. 144 */ 145 function rules_features_process_set($set_name, &$export, &$pipe) { 146 $rules = rules_get_configured_items('rules'); 147 foreach ($rules as $key => $rule) { 148 if ($rule['#set'] == $set_name) { 149 rules_features_process_rule($rule, $export, $pipe); 150 } 151 } 152 } 153 154 /** 155 * Processes a rule and identifes needed components or dependencies. 156 */ 157 function rules_features_process_rule($rule, &$export, &$pipe) { 158 $dependencies = array(); 159 // If it is an event-triggered rule, add event dependencies 160 if (strpos($rule['#set'], 'event_') === 0) { 161 $event = substr($rule['#set'], strlen('event_')); 162 if ($module = rules_features_providing_module('event_info', $event)) { 163 $export['dependencies'][$module] = $module; 164 } 165 } 166 foreach (array('condition', 'action') as $type) { 167 _rules_features_process_rule((array)$rule['#'. $type .'s'], $export, $pipe); 168 } 169 } 170 171 /** 172 * Helper to recursively process all elements of a rule. 173 */ 174 function _rules_features_process_rule($element, &$export, &$pipe) { 175 if (isset($element['#type']) && isset($element['#name']) && $module = rules_features_providing_module($element['#type'] .'_info', $element['#name'])) { 176 $export['dependencies'][$module] = $module; 177 } 178 // Invoke the features_export callback if any. 179 $element += array('#settings' => array()); 180 rules_element_invoke($element, 'features_export', array(&$export, &$pipe, $element['#settings'], $element), FALSE); 181 182 // Add used input evaluators. 183 $element['#settings'] += array('#eval input' => array()); 184 foreach ($element['#settings']['#eval input'] as $base => $info) { 185 if ($module = rules_features_providing_module('evaluator', $base)) { 186 $export['dependencies'][$module] = $module; 187 } 188 } 189 190 // Recurse 191 foreach (element_children($element) as $key) { 192 _rules_features_process_rule((array)$element[$key], $export, $pipe); 193 } 194 } 195 196 197 /** 198 * Retrieves the providing module for any items defined with rules hooks. 199 * 200 * @param $hook 201 * The name of the hook without the 'rules_' prefix. 202 * @param $name 203 * The name of the item provided by the given hook. 204 * @return 205 * The module name or FALSE if it can't be found. 206 */ 207 function rules_features_providing_module($hook, $name) { 208 static $map = array(); 209 210 if (!isset($map[$hook])) { 211 $map[$hook] = array(); 212 foreach (module_implements('rules_'. $hook ) as $module) { 213 if ($info = module_invoke($module, 'rules_'. $hook)) { 214 $map[$hook] += array_combine(array_keys($info), array_fill(0, count($info), $module)); 215 } 216 } 217 } 218 return isset($map[$hook][$name]) ? $map[$hook][$name] : FALSE; 219 } 220
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |