| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: features.ctools.inc,v 1.1.2.28 2010/08/19 21:55:48 yhahn Exp $ 3 4 /** 5 * This is a wild hack, but effective. 6 * Dynamically declare functions under a ctools component's namespace if they are not already declared. 7 */ 8 foreach (_ctools_features_get_info() as $component => $info) { 9 $code = ''; 10 if (!function_exists("{$info['module']}_features_api")) { 11 $code .= 'function '. $info['module'] .'_features_api() { return ctools_component_features_api("'. $info['module'] .'"); }'; 12 } 13 if (!function_exists("{$component}_features_export")) { 14 $code .= 'function '. $component .'_features_export($data, &$export, $module_name = "") { return ctools_component_features_export("'. $component .'", $data, $export, $module_name); }'; 15 } 16 if (!function_exists("{$component}_features_export_options")) { 17 $code .= 'function '. $component .'_features_export_options() { return ctools_component_features_export_options("'. $component .'"); }'; 18 } 19 if (!function_exists("{$component}_features_export_render")) { 20 $code .= 'function '. $component .'_features_export_render($module, $data) { return ctools_component_features_export_render("'. $component .'", $module, $data); }'; 21 } 22 if (!function_exists("{$component}_features_revert")) { 23 $code .= 'function '. $component .'_features_revert($module) { return ctools_component_features_revert("'. $component .'", $module); }'; 24 } 25 eval($code); 26 } 27 28 /** 29 * Implementation of hook_features_api(). 30 */ 31 function ctools_features_api() { 32 return array( 33 'ctools' => array( 34 'name' => 'CTools export API', 35 'feature_source' => TRUE, 36 'duplicates' => FEATURES_DUPLICATES_ALLOWED, 37 // CTools API integration does not include a default hook declaration as 38 // it is not a proper default hook. 39 // 'default_hook' => 'ctools_plugin_api', 40 ), 41 ); 42 } 43 44 /** 45 * Implementation of hook_features_export(). 46 * Adds references to the ctools mothership hook, ctools_plugin_api(). 47 */ 48 function ctools_features_export($data, &$export, $module_name = '') { 49 // Add ctools dependency 50 $export['dependencies']['ctools'] = 'ctools'; 51 52 // Add the actual ctools components which will need to be accounted for in 53 // hook_ctools_plugin_api(). The components are actually identified by a 54 // delimited list of values: `module_name:api:current_version` 55 foreach ($data as $component) { 56 if ($info = _ctools_features_get_info($component)) { 57 $identifier = "{$info['module']}:{$info['api']}:{$info['current_version']}"; 58 $export['features']['ctools'][$identifier] = $identifier; 59 } 60 } 61 62 return array(); 63 } 64 65 /** 66 * Implementation of hook_features_export_render(). 67 * Adds the ctools mothership hook, ctools_plugin_api(). 68 */ 69 function ctools_features_export_render($module, $data) { 70 $code = array(); 71 $code[] = ' list($module, $api) = func_get_args();'; 72 73 $first = TRUE; 74 foreach ($data as $component) { 75 if ($info = _ctools_features_get_info($component)) { 76 $if = $first ? 'if' : 'elseif'; 77 $code[] = ' '. $if .' ($module == "'. $info['module'] .'" && $api == "'. $info['api'] .'") {'; 78 $code[] = ' return array("version" => '. $info['current_version'] .');'; 79 $code[] = ' }'; 80 81 $first = FALSE; 82 } 83 } 84 return array('ctools_plugin_api' => implode("\n", $code)); 85 } 86 87 /** 88 * Master implementation of hook_features_api() for all ctools components. 89 * 90 * Note that this master hook does not use $component like the others, but uses the 91 * component module's namespace instead. 92 */ 93 function ctools_component_features_api($module_name) { 94 $api = array(); 95 foreach (_ctools_features_get_info() as $component => $info) { 96 if ($info['module'] === $module_name) { 97 $api[$component] = $info; 98 } 99 } 100 return $api; 101 } 102 103 /** 104 * Master implementation of hook_features_export_options() for all ctools components. 105 */ 106 function ctools_component_features_export_options($component) { 107 $options = array(); 108 109 ctools_include('export'); 110 $schema = ctools_export_get_schema($component); 111 if ($schema && $schema['export']['bulk export']) { 112 if (!empty($schema['export']['list callback']) && function_exists($schema['export']['list callback'])) { 113 $options = $schema['export']['list callback'](); 114 } 115 else { 116 $options = _ctools_features_export_default_list($component, $schema); 117 } 118 } 119 asort($options); 120 return $options; 121 } 122 123 /** 124 * Master implementation of hook_features_export() for all ctools components. 125 */ 126 function ctools_component_features_export($component, $data, &$export, $module_name = '') { 127 // Add the actual implementing module as a dependency 128 $info = _ctools_features_get_info(); 129 if ($module_name !== $info[$component]['module']) { 130 $export['dependencies'][$info[$component]['module']] = $info[$component]['module']; 131 } 132 133 // Add the components 134 foreach ($data as $object_name) { 135 if ($object = _ctools_features_export_crud_load($component, $object_name)) { 136 // If this object is provided as a default by a different module, don't 137 // export and add that module as a dependency instead. 138 if (!empty($object->export_module) && $object->export_module !== $module_name) { 139 $export['dependencies'][$object->export_module] = $object->export_module; 140 if (isset($export['features'][$component][$object_name])) { 141 unset($export['features'][$component][$object_name]); 142 } 143 } 144 // Otherwise, add the component. 145 else { 146 $export['features'][$component][$object_name] = $object_name; 147 } 148 } 149 } 150 151 // Let CTools handle API integration for this component. 152 return array('ctools' => array($component)); 153 } 154 155 /** 156 * Master implementation of hook_features_export_render() for all ctools components. 157 */ 158 function ctools_component_features_export_render($component, $module, $data) { 159 ctools_include('export'); 160 $schema = ctools_export_get_schema($component); 161 162 if (function_exists($schema['export']['to hook code callback'])) { 163 $export = $schema['export']['to hook code callback']($data, $module); 164 $code = explode("{\n", $export); 165 array_shift($code); 166 $code = explode('}', implode($code, "{\n")); 167 array_pop($code); 168 $code = implode('}', $code); 169 } 170 else { 171 $code = ' $export = array();'."\n"; 172 foreach ($data as $object_name) { 173 if ($object = _ctools_features_export_crud_load($component, $object_name)) { 174 $identifier = $schema['export']['identifier']; 175 $code .= _ctools_features_export_crud_export($component, $object, ' '); 176 $code .= "\n"; 177 $code .= " \$export[" . ctools_var_export($object_name) . "] = \${$identifier};\n"; 178 } 179 } 180 $code .= ' return $export;'; 181 } 182 183 return array($schema['export']['default hook'] => $code); 184 } 185 186 /** 187 * Master implementation of hook_features_revert() for all ctools components. 188 */ 189 function ctools_component_features_revert($component, $module) { 190 if ($objects = features_get_default($component, $module)) { 191 foreach ($objects as $object) { 192 _ctools_features_export_crud_delete($component, $object); 193 } 194 } 195 } 196 197 /** 198 * Helper function to return various ctools information for components. 199 */ 200 function _ctools_features_get_info($identifier = NULL, $reset = FALSE) { 201 static $components; 202 if (!isset($components) || $reset) { 203 $components = array(); 204 $modules = features_get_info(); 205 ctools_include('export'); 206 foreach (ctools_export_get_schemas_by_module() as $module => $schemas) { 207 foreach ($schemas as $table => $schema) { 208 if ($schema['export']['bulk export']) { 209 // Let the API owner take precedence as the owning module. 210 $api_module = isset($schema['export']['api']['owner']) ? $schema['export']['api']['owner'] : $module; 211 $components[$table] = array( 212 'name' => isset($modules[$api_module]->info['name']) ? $modules[$api_module]->info['name'] : $api_module, 213 'api' => $schema['export']['api']['api'], 214 'default_hook' => $schema['export']['default hook'], 215 'default_file' => FEATURES_DEFAULTS_CUSTOM, 216 'default_filename' => $schema['export']['api']['api'], 217 'current_version' => $schema['export']['api']['current_version'], 218 'module' => $api_module, 219 'feature_source' => TRUE, 220 ); 221 } 222 } 223 } 224 } 225 226 // Return information specific to a particular component. 227 if (isset($identifier)) { 228 // Identified by the table name. 229 if (isset($components[$identifier])) { 230 return $components[$identifier]; 231 } 232 // New API identifier. Allows non-exportables related CTools APIs to be 233 // supported by an explicit `module:api:current_version` key. 234 else if (substr_count($identifier, ':') === 2) { 235 list($module, $api, $current_version) = explode(':', $identifier); 236 // If a schema component matches the provided identifier, provide that 237 // information. This also ensures that the version number is up to date. 238 foreach ($components as $table => $info) { 239 if ($info['module'] == $module && $info['api'] == $api && $info['current_version'] >= $current_version) { 240 return $info; 241 } 242 } 243 // Fallback to just giving back what was provided to us. 244 return array('module' => $module, 'api' => $api, 'current_version' => $current_version); 245 } 246 return FALSE; 247 } 248 249 return $components; 250 } 251 252 /** 253 * Wrapper around ctools_export_crud_export() for < 1.7 compatibility. 254 */ 255 function _ctools_features_export_crud_export($table, $object, $indent = '') { 256 return ctools_api_version('1.7') ? ctools_export_crud_export($table, $object, $indent) : ctools_export_object($table, $object, $indent); 257 } 258 259 /** 260 * Wrapper around ctools_export_crud_load() for < 1.7 compatibility. 261 */ 262 function _ctools_features_export_crud_load($table, $name) { 263 if (ctools_api_version('1.7')) { 264 return ctools_export_crud_load($table, $name); 265 } 266 elseif ($objects = ctools_export_load_object($table, 'names', array($name))) { 267 return array_shift($objects); 268 } 269 return FALSE; 270 } 271 272 /** 273 * Wrapper around ctools_export_default_list() for < 1.7 compatibility. 274 */ 275 function _ctools_features_export_default_list($table, $schema) { 276 if (ctools_api_version('1.7')) { 277 return ctools_export_default_list($table, $schema); 278 } 279 elseif ($objects = ctools_export_load_object($table, 'all')) { 280 return drupal_map_assoc(array_keys($objects)); 281 } 282 return array(); 283 } 284 285 /** 286 * Wrapper around ctools_export_crud_delete() for < 1.7 compatibility. 287 */ 288 function _ctools_features_export_crud_delete($table, $object) { 289 if (ctools_api_version('1.7')) { 290 ctools_export_crud_delete($table, $object); 291 } 292 else { 293 $schema = ctools_export_get_schema($table); 294 $export = $schema['export']; 295 db_query("DELETE FROM {{$table}} WHERE {$export['key']} = '%s'", $object->{$export['key']}); 296 } 297 } 298 299 /** 300 * Implementation of hook_features_export_render() for page_manager. 301 */ 302 function page_manager_pages_features_export_render($module, $data) { 303 // Ensure that handlers have their code included before exporting. 304 page_manager_get_tasks(); 305 return ctools_component_features_export_render('page_manager_pages', $module, $data); 306 }
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 |