00001 <?php
00002
00003
00016 function filter_admin_overview() {
00017
00018
00019 $formats = filter_formats();
00020 $error = FALSE;
00021
00022 $form = array('#tree' => TRUE);
00023 foreach ($formats as $id => $format) {
00024 $roles = array();
00025 foreach (user_roles() as $rid => $name) {
00026
00027 if (strstr($format->roles, ",$rid,")) {
00028 $roles[] = $name;
00029 }
00030 }
00031 $default = ($id == variable_get('filter_default_format', 1));
00032 $options[$id] = '';
00033 $form[$id]['name'] = array('#value' => $format->name);
00034 $form[$id]['roles'] = array('#value' => $default ? t('All roles may use default format') : ($roles ? implode(', ', $roles) : t('No roles may use this format')));
00035 $form[$id]['configure'] = array('#value' => l(t('configure'), 'admin/settings/filters/' . $id));
00036 $form[$id]['delete'] = array('#value' => $default ? '' : l(t('delete'), 'admin/settings/filters/delete/' . $id));
00037 $form[$id]['weight'] = array('#type' => 'weight', '#default_value' => $format->weight);
00038 }
00039 $form['default'] = array('#type' => 'radios', '#options' => $options, '#default_value' => variable_get('filter_default_format', 1));
00040 $form['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
00041 return $form;
00042 }
00043
00044 function filter_admin_overview_submit($form, &$form_state) {
00045
00046 if (is_numeric($form_state['values']['default'])) {
00047 drupal_set_message(t('Default format updated.'));
00048 variable_set('filter_default_format', $form_state['values']['default']);
00049 }
00050 foreach ($form_state['values'] as $id => $data) {
00051 if (is_array($data) && isset($data['weight'])) {
00052
00053 db_query("UPDATE {filter_formats} SET weight = %d WHERE format = %d", $data['weight'], $id);
00054 }
00055 }
00056 drupal_set_message(t('The input format ordering has been saved.'));
00057 }
00058
00064 function theme_filter_admin_overview($form) {
00065 $rows = array();
00066 foreach ($form as $id => $element) {
00067 if (isset($element['roles']) && is_array($element['roles'])) {
00068 $element['weight']['#attributes']['class'] = 'input-format-order-weight';
00069 $rows[] = array(
00070 'data' => array(
00071 check_plain($element['name']['#value']),
00072 drupal_render($element['roles']),
00073 drupal_render($form['default'][$id]),
00074 drupal_render($element['weight']),
00075 drupal_render($element['configure']),
00076 drupal_render($element['delete']),
00077 ),
00078 'class' => 'draggable',
00079 );
00080 unset($form[$id]);
00081 }
00082 }
00083 $header = array(t('Name'), t('Roles'), t('Default'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
00084 $output = theme('table', $header, $rows, array('id' => 'input-format-order'));
00085 $output .= drupal_render($form);
00086
00087 drupal_add_tabledrag('input-format-order', 'order', 'sibling', 'input-format-order-weight');
00088
00089 return $output;
00090 }
00091
00095 function filter_admin_format_page($format = NULL) {
00096 if (!isset($format->name)) {
00097 drupal_set_title(t("Add input format"));
00098 $format = (object)array('name' => '', 'roles' => '', 'format' => '');
00099 }
00100 return drupal_get_form('filter_admin_format_form', $format);
00101 }
00102
00110 function filter_admin_format_form(&$form_state, $format) {
00111 $default = ($format->format == variable_get('filter_default_format', 1));
00112 if ($default) {
00113 $help = t('All roles for the default format must be enabled and cannot be changed.');
00114 $form['default_format'] = array('#type' => 'hidden', '#value' => 1);
00115 }
00116
00117 $form['name'] = array('#type' => 'textfield',
00118 '#title' => t('Name'),
00119 '#default_value' => $format->name,
00120 '#description' => t('Specify a unique name for this filter format.'),
00121 '#required' => TRUE,
00122 );
00123
00124
00125 $form['roles'] = array('#type' => 'fieldset',
00126 '#title' => t('Roles'),
00127 '#description' => $default ? $help : t('Choose which roles may use this filter format. Note that roles with the "administer filters" permission can always use all the filter formats.'),
00128 '#tree' => TRUE,
00129 );
00130
00131 foreach (user_roles() as $rid => $name) {
00132 $checked = strstr($format->roles, ",$rid,");
00133 $form['roles'][$rid] = array('#type' => 'checkbox',
00134 '#title' => $name,
00135 '#default_value' => ($default || $checked),
00136 );
00137 if ($default) {
00138 $form['roles'][$rid]['#disabled'] = TRUE;
00139 }
00140 }
00141
00142 $all = filter_list_all();
00143 $enabled = filter_list_format($format->format);
00144
00145 $form['filters'] = array('#type' => 'fieldset',
00146 '#title' => t('Filters'),
00147 '#description' => t('Choose the filters that will be used in this filter format.'),
00148 '#tree' => TRUE,
00149 );
00150 foreach ($all as $id => $filter) {
00151 $form['filters'][$id] = array('#type' => 'checkbox',
00152 '#title' => $filter->name,
00153 '#default_value' => isset($enabled[$id]),
00154 '#description' => module_invoke($filter->module, 'filter', 'description', $filter->delta),
00155 );
00156 }
00157 if (!empty($format->format)) {
00158 $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
00159
00160
00161 $tips = _filter_tips($format->format, FALSE);
00162 $extra = '<p>' . l(t('More information about formatting options'), 'filter/tips') . '</p>';
00163 $tiplist = theme('filter_tips', $tips, FALSE, $extra);
00164 if (!$tiplist) {
00165 $tiplist = '<p>' . t('No guidelines available.') . '</p>';
00166 }
00167 $group = '<p>' . t('These are the guidelines that users will see for posting in this input format. They are automatically generated from the filter settings.') . '</p>';
00168 $group .= $tiplist;
00169 $form['tips'] = array('#value' => '<h2>' . t('Formatting guidelines') . '</h2>' . $group);
00170 }
00171 $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
00172
00173 return $form;
00174 }
00175
00179 function filter_admin_format_form_validate($form, &$form_state) {
00180 if (!isset($form_state['values']['format'])) {
00181 $name = trim($form_state['values']['name']);
00182 $result = db_fetch_object(db_query("SELECT format FROM {filter_formats} WHERE name='%s'", $name));
00183 if ($result) {
00184 form_set_error('name', t('Filter format names need to be unique. A format named %name already exists.', array('%name' => $name)));
00185 }
00186 }
00187 }
00188
00192 function filter_admin_format_form_submit($form, &$form_state) {
00193 $format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL;
00194 $current = filter_list_format($format);
00195 $name = trim($form_state['values']['name']);
00196 $cache = TRUE;
00197
00198
00199 if (!$format) {
00200 $new = TRUE;
00201 db_query("INSERT INTO {filter_formats} (name) VALUES ('%s')", $name);
00202 $format = db_result(db_query("SELECT MAX(format) AS format FROM {filter_formats}"));
00203 drupal_set_message(t('Added input format %format.', array('%format' => $name)));
00204 }
00205 else {
00206 drupal_set_message(t('The input format settings have been updated.'));
00207 }
00208
00209 db_query("DELETE FROM {filters} WHERE format = %d", $format);
00210 foreach ($form_state['values']['filters'] as $id => $checked) {
00211 if ($checked) {
00212 list($module, $delta) = explode('/', $id);
00213
00214 $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10;
00215 db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $format, $module, $delta, $weight);
00216
00217
00218 $cache &= !module_invoke($module, 'filter', 'no cache', $delta);
00219 }
00220 }
00221
00222
00223
00224
00225 $roles = array();
00226 if (isset($form_state['values']['roles'])) {
00227 foreach ($form_state['values']['roles'] as $id => $checked) {
00228 if ($checked) {
00229 $roles[] = $id;
00230 }
00231 }
00232 }
00233 if (!empty($form_state['values']['default_format'])) {
00234 $roles = ',' . implode(',', array_keys(user_roles())) . ',';
00235 }
00236 else {
00237 $roles = ',' . implode(',', $roles) . ',';
00238 }
00239
00240 db_query("UPDATE {filter_formats} SET cache = %d, name='%s', roles = '%s' WHERE format = %d", $cache, $name, $roles, $format);
00241
00242 cache_clear_all($format . ':', 'cache_filter', TRUE);
00243
00244
00245 $return = 'admin/settings/filters';
00246 if (!empty($new)) {
00247 $return .= '/' . $format;
00248 }
00249 $form_state['redirect'] = $return;
00250 return;
00251 }
00252
00259 function filter_admin_delete() {
00260 $format = arg(4);
00261 $format = db_fetch_object(db_query('SELECT * FROM {filter_formats} WHERE format = %d', $format));
00262
00263 if ($format) {
00264 if ($format->format != variable_get('filter_default_format', 1)) {
00265 $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
00266 $form['name'] = array('#type' => 'hidden', '#value' => $format->name);
00267
00268 return confirm_form($form, t('Are you sure you want to delete the input format %format?', array('%format' => $format->name)), 'admin/settings/filters', t('If you have any content left in this input format, it will be switched to the default input format. This action cannot be undone.'), t('Delete'), t('Cancel'));
00269 }
00270 else {
00271 drupal_set_message(t('The default format cannot be deleted.'));
00272 drupal_goto('admin/settings/filters');
00273 }
00274 }
00275 else {
00276 drupal_not_found();
00277 }
00278 }
00279
00283 function filter_admin_delete_submit($form, &$form_state) {
00284 db_query("DELETE FROM {filter_formats} WHERE format = %d", $form_state['values']['format']);
00285 db_query("DELETE FROM {filters} WHERE format = %d", $form_state['values']['format']);
00286
00287 $default = variable_get('filter_default_format', 1);
00288
00289 db_query("UPDATE {node_revisions} SET format = %d WHERE format = %d", $default, $form_state['values']['format']);
00290 db_query("UPDATE {comments} SET format = %d WHERE format = %d", $default, $form_state['values']['format']);
00291 db_query("UPDATE {boxes} SET format = %d WHERE format = %d", $default, $form_state['values']['format']);
00292
00293 cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE);
00294 drupal_set_message(t('Deleted input format %format.', array('%format' => $form_state['values']['name'])));
00295
00296 $form_state['redirect'] = 'admin/settings/filters';
00297 return;
00298 }
00299
00300
00304 function filter_admin_configure_page($format) {
00305 drupal_set_title(t("Configure %format", array('%format' => $format->name)));
00306 return drupal_get_form('filter_admin_configure', $format);
00307 }
00308
00314 function filter_admin_configure(&$form_state, $format) {
00315 $list = filter_list_format($format->format);
00316 $form = array();
00317 foreach ($list as $filter) {
00318 $form_module = module_invoke($filter->module, 'filter', 'settings', $filter->delta, $format->format);
00319 if (isset($form_module) && is_array($form_module)) {
00320 $form = array_merge($form, $form_module);
00321 }
00322 }
00323
00324 if (!empty($form)) {
00325 $form = system_settings_form($form);
00326 }
00327 else {
00328 $form['error'] = array('#value' => t('No settings are available.'));
00329 }
00330 $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
00331 $form['#submit'][] = 'filter_admin_configure_submit';
00332 return $form;
00333 }
00334
00338 function filter_admin_configure_submit($form, &$form_state) {
00339 cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE);
00340 }
00341
00345 function filter_admin_order_page($format) {
00346 drupal_set_title(t("Rearrange %format", array('%format' => $format->name)));
00347 return drupal_get_form('filter_admin_order', $format);
00348 }
00349
00357 function filter_admin_order(&$form_state, $format = NULL) {
00358
00359 $filters = filter_list_format($format->format);
00360
00361 $form['weights'] = array('#tree' => TRUE);
00362 foreach ($filters as $id => $filter) {
00363 $form['names'][$id] = array('#value' => $filter->name);
00364 $form['weights'][$id] = array('#type' => 'weight', '#default_value' => $filter->weight);
00365 }
00366 $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
00367 $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
00368
00369 return $form;
00370 }
00371
00377 function theme_filter_admin_order($form) {
00378 $header = array(t('Name'), t('Weight'));
00379 $rows = array();
00380 foreach (element_children($form['names']) as $id) {
00381
00382 if (is_array($form['names'][$id])) {
00383 $form['weights'][$id]['#attributes']['class'] = 'filter-order-weight';
00384 $rows[] = array(
00385 'data' => array(drupal_render($form['names'][$id]), drupal_render($form['weights'][$id])),
00386 'class' => 'draggable',
00387 );
00388 }
00389 }
00390
00391 $output = theme('table', $header, $rows, array('id' => 'filter-order'));
00392 $output .= drupal_render($form);
00393
00394 drupal_add_tabledrag('filter-order', 'order', 'sibling', 'filter-order-weight', NULL, NULL, FALSE);
00395
00396 return $output;
00397 }
00398
00402 function filter_admin_order_submit($form, &$form_state) {
00403 foreach ($form_state['values']['weights'] as $id => $weight) {
00404 list($module, $delta) = explode('/', $id);
00405 db_query("UPDATE {filters} SET weight = %d WHERE format = %d AND module = '%s' AND delta = %d", $weight, $form_state['values']['format'], $module, $delta);
00406 }
00407 drupal_set_message(t('The filter ordering has been saved.'));
00408
00409 cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE);
00410 }