[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/modules/filter/ -> filter.admin.inc (source)

   1  <?php
   2  // $Id: filter.admin.inc,v 1.8.2.1 2008/02/12 14:25:34 goba Exp $
   3  
   4  /**
   5   * @file
   6   * Admin page callbacks for the filter module.
   7   */
   8  
   9  /**
  10   * Menu callback; Displays a list of all input formats and which
  11   * one is the default.
  12   *
  13   * @ingroup forms
  14   * @see filter_admin_overview_submit()
  15   */
  16  function filter_admin_overview() {
  17  
  18    // Overview of all formats.
  19    $formats = filter_formats();
  20    $error = FALSE;
  21  
  22    foreach ($formats as $id => $format) {
  23      $roles = array();
  24      foreach (user_roles() as $rid => $name) {
  25        // Prepare a roles array with roles that may access the filter.
  26        if (strstr($format->roles, ",$rid,")) {
  27          $roles[] = $name;
  28        }
  29      }
  30      $default = ($id == variable_get('filter_default_format', 1));
  31      $options[$id] = '';
  32      $form[$format->name]['id'] = array('#value' => $id);
  33      $form[$format->name]['roles'] = array('#value' => $default ? t('All roles may use default format') : ($roles ? implode(', ', $roles) : t('No roles may use this format')));
  34      $form[$format->name]['configure'] = array('#value' => l(t('configure'), 'admin/settings/filters/'. $id));
  35      $form[$format->name]['delete'] = array('#value' => $default ? '' : l(t('delete'), 'admin/settings/filters/delete/'. $id));
  36    }
  37    $form['default'] = array('#type' => 'radios', '#options' => $options, '#default_value' => variable_get('filter_default_format', 1));
  38    $form['submit'] = array('#type' => 'submit', '#value' => t('Set default format'));
  39    return $form;
  40  }
  41  
  42  function filter_admin_overview_submit($form, &$form_state) {
  43    // Process form submission to set the default format.
  44    if (is_numeric($form_state['values']['default'])) {
  45      drupal_set_message(t('Default format updated.'));
  46      variable_set('filter_default_format', $form_state['values']['default']);
  47    }
  48  }
  49  
  50  /**
  51   * Theme the admin overview form.
  52   *
  53   * @ingroup themeable
  54   */
  55  function theme_filter_admin_overview($form) {
  56    $rows = array();
  57    foreach ($form as $name => $element) {
  58      if (isset($element['roles']) && is_array($element['roles'])) {
  59        $rows[] = array(
  60          drupal_render($form['default'][$element['id']['#value']]),
  61          check_plain($name),
  62          drupal_render($element['roles']),
  63          drupal_render($element['configure']),
  64          drupal_render($element['delete'])
  65        );
  66        unset($form[$name]);
  67      }
  68    }
  69    $header = array(t('Default'), t('Name'), t('Roles'), array('data' => t('Operations'), 'colspan' => 2));
  70    $output = theme('table', $header, $rows);
  71    $output .= drupal_render($form);
  72  
  73    return $output;
  74  }
  75  
  76  /**
  77   * Menu callback; Display a filter format form.
  78   */
  79  function filter_admin_format_page($format = NULL) {
  80    if (!isset($format->name)) {
  81      drupal_set_title(t("Add input format"));
  82      $format = (object)array('name' => '', 'roles' => '', 'format' => '');
  83    }
  84    return drupal_get_form('filter_admin_format_form', $format);
  85  }
  86  
  87  /**
  88   * Generate a filter format form.
  89   *
  90   * @ingroup forms
  91   * @see filter_admin_format_form_validate()
  92   * @see filter_admin_format_form_submit()
  93   */
  94  function filter_admin_format_form(&$form_state, $format) {
  95    $default = ($format->format == variable_get('filter_default_format', 1));
  96    if ($default) {
  97      $help = t('All roles for the default format must be enabled and cannot be changed.');
  98      $form['default_format'] = array('#type' => 'hidden', '#value' => 1);
  99    }
 100  
 101    $form['name'] = array('#type' => 'textfield',
 102      '#title' => t('Name'),
 103      '#default_value' => $format->name,
 104      '#description' => t('Specify a unique name for this filter format.'),
 105      '#required' => TRUE,
 106    );
 107  
 108    // Add a row of checkboxes for form group.
 109    $form['roles'] = array('#type' => 'fieldset',
 110      '#title' => t('Roles'),
 111      '#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.'),
 112      '#tree' => TRUE,
 113    );
 114  
 115    foreach (user_roles() as $rid => $name) {
 116      $checked = strstr($format->roles, ",$rid,");
 117      $form['roles'][$rid] = array('#type' => 'checkbox',
 118        '#title' => $name,
 119        '#default_value' => ($default || $checked),
 120      );
 121      if ($default) {
 122        $form['roles'][$rid]['#disabled'] = TRUE;
 123      }
 124    }
 125    // Table with filters
 126    $all = filter_list_all();
 127    $enabled = filter_list_format($format->format);
 128  
 129    $form['filters'] = array('#type' => 'fieldset',
 130      '#title' => t('Filters'),
 131      '#description' => t('Choose the filters that will be used in this filter format.'),
 132      '#tree' => TRUE,
 133    );
 134    foreach ($all as $id => $filter) {
 135      $form['filters'][$id] = array('#type' => 'checkbox',
 136        '#title' => $filter->name,
 137        '#default_value' => isset($enabled[$id]),
 138        '#description' => module_invoke($filter->module, 'filter', 'description', $filter->delta),
 139      );
 140    }
 141    if (!empty($format->format)) {
 142      $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
 143  
 144      // Composition tips (guidelines)
 145      $tips = _filter_tips($format->format, FALSE);
 146      $extra = '<p>'. l(t('More information about formatting options'), 'filter/tips') .'</p>';
 147      $tiplist = theme('filter_tips', $tips, FALSE, $extra);
 148      if (!$tiplist) {
 149        $tiplist = '<p>'. t('No guidelines available.') .'</p>';
 150      }
 151      $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>';
 152      $group .= $tiplist;
 153      $form['tips'] = array('#value' => '<h2>'. t('Formatting guidelines') .'</h2>'. $group);
 154    }
 155    $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
 156  
 157    return $form;
 158  }
 159  
 160  /**
 161   * Validate filter format form submissions.
 162   */
 163  function filter_admin_format_form_validate($form, &$form_state) {
 164    if (!isset($form_state['values']['format'])) {
 165      $name = trim($form_state['values']['name']);
 166      $result = db_fetch_object(db_query("SELECT format FROM {filter_formats} WHERE name='%s'", $name));
 167      if ($result) {
 168        form_set_error('name', t('Filter format names need to be unique. A format named %name already exists.', array('%name' => $name)));
 169      }
 170    }
 171  }
 172  
 173  /**
 174   * Process filter format form submissions.
 175   */
 176  function filter_admin_format_form_submit($form, &$form_state) {
 177    $format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL;
 178    $current = filter_list_format($format);
 179    $name = trim($form_state['values']['name']);
 180    $cache = TRUE;
 181  
 182    // Add a new filter format.
 183    if (!$format) {
 184      $new = TRUE;
 185      db_query("INSERT INTO {filter_formats} (name) VALUES ('%s')", $name);
 186      $format = db_result(db_query("SELECT MAX(format) AS format FROM {filter_formats}"));
 187      drupal_set_message(t('Added input format %format.', array('%format' => $name)));
 188    }
 189    else {
 190      drupal_set_message(t('The input format settings have been updated.'));
 191    }
 192  
 193    db_query("DELETE FROM {filters} WHERE format = %d", $format);
 194    foreach ($form_state['values']['filters'] as $id => $checked) {
 195      if ($checked) {
 196        list($module, $delta) = explode('/', $id);
 197        // Add new filters to the bottom.
 198        $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10;
 199        db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $format, $module, $delta, $weight);
 200  
 201        // Check if there are any 'no cache' filters.
 202        $cache &= !module_invoke($module, 'filter', 'no cache', $delta);
 203      }
 204    }
 205  
 206    // We store the roles as a string for ease of use.
 207    // We should always set all roles to TRUE when saving a default role.
 208    // We use leading and trailing comma's to allow easy substring matching.
 209    $roles = array();
 210    if (isset($form_state['values']['roles'])) {
 211      foreach ($form_state['values']['roles'] as $id => $checked) {
 212        if ($checked) {
 213          $roles[] = $id;
 214        }
 215      }
 216    }
 217    if (!empty($form_state['values']['default_format'])) {
 218      $roles = ','. implode(',', array_keys(user_roles())) .',';
 219    }
 220    else {
 221      $roles = ','. implode(',', $roles) .',';
 222    }
 223  
 224    db_query("UPDATE {filter_formats} SET cache = %d, name='%s', roles = '%s' WHERE format = %d", $cache, $name, $roles, $format);
 225  
 226    cache_clear_all($format .':', 'cache_filter', TRUE);
 227  
 228    // If a new filter was added, return to the main list of filters. Otherwise, stay on edit filter page to show new changes.
 229    $return = 'admin/settings/filters';
 230    if (!empty($new)) {
 231      $return .= '/'. $format;
 232    }
 233    $form_state['redirect'] = $return;
 234    return;
 235  }
 236  
 237  /**
 238   * Menu callback; confirm deletion of a format.
 239   *
 240   * @ingroup forms
 241   * @see filter_admin_delete_submit()
 242   */
 243  function filter_admin_delete() {
 244    $format = arg(4);
 245    $format = db_fetch_object(db_query('SELECT * FROM {filter_formats} WHERE format = %d', $format));
 246  
 247    if ($format) {
 248      if ($format->format != variable_get('filter_default_format', 1)) {
 249        $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
 250        $form['name'] = array('#type' => 'hidden', '#value' => $format->name);
 251  
 252        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'));
 253      }
 254      else {
 255        drupal_set_message(t('The default format cannot be deleted.'));
 256        drupal_goto('admin/settings/filters');
 257      }
 258    }
 259    else {
 260      drupal_not_found();
 261    }
 262  }
 263  
 264  /**
 265   * Process filter delete form submission.
 266   */
 267  function filter_admin_delete_submit($form, &$form_state) {
 268    db_query("DELETE FROM {filter_formats} WHERE format = %d", $form_state['values']['format']);
 269    db_query("DELETE FROM {filters} WHERE format = %d", $form_state['values']['format']);
 270  
 271    $default = variable_get('filter_default_format', 1);
 272    // Replace existing instances of the deleted format with the default format.
 273    db_query("UPDATE {node_revisions} SET format = %d WHERE format = %d", $default, $form_state['values']['format']);
 274    db_query("UPDATE {comments} SET format = %d WHERE format = %d", $default, $form_state['values']['format']);
 275    db_query("UPDATE {boxes} SET format = %d WHERE format = %d", $default, $form_state['values']['format']);
 276  
 277    cache_clear_all($form_state['values']['format'] .':', 'cache_filter', TRUE);
 278    drupal_set_message(t('Deleted input format %format.', array('%format' => $form_state['values']['name'])));
 279  
 280    $form_state['redirect'] = 'admin/settings/filters';
 281    return;
 282  }
 283  
 284  
 285  /**
 286   * Menu callback; display settings defined by a format's filters.
 287   */
 288  function filter_admin_configure_page($format) {
 289    drupal_set_title(t("Configure %format", array('%format' => $format->name)));
 290    return drupal_get_form('filter_admin_configure', $format);
 291  }
 292  
 293  /**
 294   * Build a form to change the settings for a format's filters.
 295   *
 296   * @ingroup forms
 297   */
 298  function filter_admin_configure(&$form_state, $format) {
 299    $list = filter_list_format($format->format);
 300    $form = array();
 301    foreach ($list as $filter) {
 302      $form_module = module_invoke($filter->module, 'filter', 'settings', $filter->delta, $format->format);
 303      if (isset($form_module) && is_array($form_module)) {
 304        $form = array_merge($form, $form_module);
 305      }
 306    }
 307  
 308    if (!empty($form)) {
 309      $form = system_settings_form($form);
 310    }
 311    else {
 312      $form['error'] = array('#value' => t('No settings are available.'));
 313    }
 314    $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
 315    $form['#submit'][] = 'filter_admin_configure_submit';
 316    return $form;
 317  }
 318  
 319  /**
 320   * Clear the filter's cache when configuration settings are saved.
 321   */
 322  function filter_admin_configure_submit($form, &$form_state) {
 323    cache_clear_all($form_state['values']['format'] .':', 'cache_filter', TRUE);
 324  }
 325  
 326  /**
 327   * Menu callback; display form for ordering filters for a format.
 328   */
 329  function filter_admin_order_page($format) {
 330    drupal_set_title(t("Rearrange %format", array('%format' => $format->name)));
 331    return drupal_get_form('filter_admin_order', $format);
 332  }
 333  
 334  /**
 335   * Build the form for ordering filters for a format.
 336   *
 337   * @ingroup forms
 338   * @see theme_filter_admin_order()
 339   * @see filter_admin_order_submit()
 340   */
 341  function filter_admin_order(&$form_state, $format = NULL) {
 342    // Get list (with forced refresh).
 343    $filters = filter_list_format($format->format);
 344  
 345    $form['weights'] = array('#tree' => TRUE);
 346    foreach ($filters as $id => $filter) {
 347      $form['names'][$id] = array('#value' => $filter->name);
 348      $form['weights'][$id] = array('#type' => 'weight', '#default_value' => $filter->weight);
 349    }
 350    $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
 351    $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
 352  
 353    return $form;
 354  }
 355  
 356  /**
 357   * Theme filter order configuration form.
 358   *
 359   * @ingroup themeable
 360   */
 361  function theme_filter_admin_order($form) {
 362    $header = array(t('Name'), t('Weight'));
 363    $rows = array();
 364    foreach (element_children($form['names']) as $id) {
 365      // Don't take form control structures.
 366      if (is_array($form['names'][$id])) {
 367        $form['weights'][$id]['#attributes']['class'] = 'filter-order-weight';
 368        $rows[] = array(
 369          'data' => array(drupal_render($form['names'][$id]), drupal_render($form['weights'][$id])),
 370          'class' => 'draggable',
 371        );
 372      }
 373    }
 374  
 375    $output = theme('table', $header, $rows, array('id' => 'filter-order'));
 376    $output .= drupal_render($form);
 377  
 378    drupal_add_tabledrag('filter-order', 'order', 'sibling', 'filter-order-weight', NULL, NULL, FALSE);
 379  
 380    return $output;
 381  }
 382  
 383  /**
 384   * Process filter order configuration form submission.
 385   */
 386  function filter_admin_order_submit($form, &$form_state) {
 387    foreach ($form_state['values']['weights'] as $id => $weight) {
 388      list($module, $delta) = explode('/', $id);
 389      db_query("UPDATE {filters} SET weight = %d WHERE format = %d AND module = '%s' AND delta = %d", $weight, $form_state['values']['format'], $module, $delta);
 390    }
 391    drupal_set_message(t('The filter ordering has been saved.'));
 392  
 393    cache_clear_all($form_state['values']['format'] .':', 'cache_filter', TRUE);
 394  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7