| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: features.filter.inc,v 1.1.2.12 2010/07/24 21:46:24 yhahn Exp $ 3 4 /** 5 * Implementation of hook_features_api(). 6 */ 7 function filter_features_api() { 8 return array( 9 'filter' => array( 10 'name' => t('Filter formats'), 11 'default_hook' => 'filter_default_formats', 12 'default_file' => FEATURES_DEFAULTS_INCLUDED, 13 ), 14 ); 15 } 16 17 /** 18 * Implementation of hook_features_export(). 19 */ 20 function filter_features_export($data, &$export, $module_name = '') { 21 // The filter_default_formats() hook integration is provided by the 22 // features module so we need to add it as a dependency. 23 $export['dependencies']['features'] = 'features'; 24 25 $formats = _filter_get_formats(); 26 foreach ($formats as $format) { 27 if (in_array($format['name'], $data)) { 28 // Add format to exports 29 $export['features']['filter'][$format['name']] = $format['name']; 30 31 // Iterate through filters and ensure each filter's module is included as a dependency 32 foreach ($format['filters'] as $filter) { 33 $export['dependencies'][$filter['module']] = $filter['module']; 34 } 35 } 36 } 37 38 $pipe = array(); 39 return $pipe; 40 } 41 42 /** 43 * Implementation of hook_features_export_render(). 44 */ 45 function filter_features_export_render($module, $data) { 46 $code = array(); 47 $code[] = ' $formats = array();'; 48 $code[] = ''; 49 50 $formats = _filter_get_formats(); 51 foreach ($formats as $format) { 52 if (in_array($format['name'], $data)) { 53 $format_export = features_var_export($format, ' '); 54 $format_identifier = features_var_export($format['name']); 55 $code[] = " // Exported format: {$format['name']}"; 56 $code[] = " \$formats[{$format_identifier}] = {$format_export};"; 57 $code[] = ""; 58 } 59 } 60 61 $code[] = ' return $formats;'; 62 $code = implode("\n", $code); 63 return array('filter_default_formats' => $code); 64 } 65 66 /** 67 * Implementation of hook_features_revert(). 68 */ 69 function filter_features_revert($module) { 70 return filter_features_rebuild($module); 71 } 72 73 /** 74 * Implementation of hook_features_rebuild(). 75 */ 76 function filter_features_rebuild($module) { 77 if ($defaults = features_get_default('filter', $module)) { 78 foreach ($defaults as $format) { 79 _filter_features_update($format); 80 } 81 } 82 } 83 84 /** 85 * Retrieve all input formats with their respective filters loaded. 86 */ 87 function _filter_get_formats() { 88 $formats = array(); 89 90 // We cannot use user_roles() here because it has the names translated. 91 $roles = _features_get_roles(); 92 $result = db_query("SELECT * FROM {filter_formats}"); 93 while ($row = db_fetch_object($result)) { 94 $format_roles = array(); 95 // Prepare a roles array with roles that may access the filter. 96 foreach ($roles as $name => $role) { 97 if (strstr($row->roles, ','. $role['rid'] .',')) { 98 $format_roles[] = $name; 99 } 100 } 101 $formats[$row->format] = array( 102 'name' => $row->name, 103 'roles' => $format_roles, 104 'filters' => array(), 105 ); 106 foreach (_filter_list_format($row->format, TRUE) as $filter) { 107 $formats[$row->format]['filters'][] = array( 108 'module' => $filter->module, 109 'delta' => $filter->delta, 110 'weight' => $filter->weight, 111 ); 112 } 113 } 114 115 return $formats; 116 } 117 118 /** 119 * Direct copy of filter_list_format() but with a way to clear the static. 120 */ 121 function _filter_list_format($format, $reset = FALSE) { 122 static $filters = array(); 123 124 if (!isset($filters[$format]) || $reset) { 125 $result = db_query("SELECT * FROM {filters} WHERE format = %d ORDER BY weight, module, delta", $format); 126 if (db_affected_rows($result) == 0 && !db_result(db_query("SELECT 1 FROM {filter_formats} WHERE format = %d", $format))) { 127 // The format has no filters and does not exist, use the default input 128 // format. 129 $filters[$format] = filter_list_format(variable_get('filter_default_format', 1)); 130 } 131 else { 132 $filters[$format] = array(); 133 while ($filter = db_fetch_object($result)) { 134 $list = module_invoke($filter->module, 'filter', 'list'); 135 if (isset($list) && is_array($list) && isset($list[$filter->delta])) { 136 $filter->name = $list[$filter->delta]; 137 $filters[$format][$filter->module .'/'. $filter->delta] = $filter; 138 } 139 } 140 } 141 } 142 143 return $filters[$format]; 144 } 145 146 147 /** 148 * Helper function for updating/inserting formats. 149 */ 150 function _filter_features_update($format) { 151 $format_id = db_result(db_query("SELECT format FROM {filter_formats} WHERE name = '%s'", $format['name'])); 152 153 // Format the role IDs into something that can be inserted into the database. 154 // This is so painful. See filter.admin.inc line 218. : ( 155 // We cannot use user_roles() here because it has the names translated. 156 $roles = _features_get_roles(); 157 $format_rids = array(); 158 foreach ($format['roles'] as $role_name) { 159 // Ensure that each role exists. If it does not, create it and store the rid. 160 if (!isset($roles[$role_name])) { 161 $record = array('name' => $role_name); 162 drupal_write_record('role', $record); 163 $roles[$role_name]['rid'] = $record['rid']; 164 $roles[$role_name]['perm'] = array(); 165 } 166 // Retrieve the rid corresponding to each role and add it to the format's rids 167 if (isset($roles[$role_name])) { 168 $format_rids[] = $roles[$role_name]['rid']; 169 } 170 } 171 if ($format_id && $format_id == variable_get('filter_default_format', 1)) { 172 $format['roles'] = ','. implode(',', array_keys(user_roles())) .','; 173 } 174 else { 175 $format['roles'] = ','. implode(',', $format_rids) .','; 176 } 177 178 // Update or insert the format 179 if ($format_id) { 180 $format['format'] = $format_id; 181 drupal_write_record('filter_formats', $format, 'format'); 182 } 183 else { 184 drupal_write_record('filter_formats', $format); 185 } 186 187 // Update the filters for the format 188 if ($format['format']) { 189 db_query("DELETE FROM {filters} WHERE format = %d", $format['format']); 190 foreach ($format['filters'] as $filter) { 191 $filter['format'] = $format['format']; 192 drupal_write_record('filters', $filter); 193 } 194 } 195 }
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 |