[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/modules/node/ -> content_types.inc (source)

   1  <?php
   2  // $Id: content_types.inc,v 1.50.2.3 2009/09/14 11:51:23 goba Exp $
   3  
   4  /**
   5   * @file
   6   * Content type editing UI.
   7   */
   8  
   9  /**
  10   * Displays the content type admin overview page.
  11   */
  12  function node_overview_types() {
  13    $types = node_get_types();
  14    $names = node_get_types('names');
  15    $header = array(t('Name'), t('Type'), t('Description'), array('data' => t('Operations'), 'colspan' => '2'));
  16    $rows = array();
  17  
  18    foreach ($names as $key => $name) {
  19      $type = $types[$key];
  20      if (node_hook($type, 'form')) {
  21        $type_url_str = str_replace('_', '-', $type->type);
  22        $row = array(
  23          l($name, 'admin/content/node-type/'. $type_url_str),
  24          check_plain($type->type),
  25          filter_xss_admin($type->description),
  26        );
  27        // Set the edit column.
  28        $row[] = array('data' => l(t('edit'), 'admin/content/node-type/'. $type_url_str));
  29  
  30        // Set the delete column.
  31        if ($type->custom) {
  32          $row[] = array('data' => l(t('delete'), 'admin/content/node-type/'. $type_url_str .'/delete'));
  33        }
  34        else {
  35          $row[] = array('data' => '');
  36        }
  37        $rows[] = $row;
  38      }
  39    }
  40  
  41    if (empty($rows)) {
  42      $rows[] = array(array('data' => t('No content types available.'), 'colspan' => '5', 'class' => 'message'));
  43    }
  44  
  45    return theme('table', $header, $rows);
  46  }
  47  
  48  /**
  49   * Generates the node type editing form.
  50   */
  51  function node_type_form(&$form_state, $type = NULL) {
  52    if (!isset($type->type)) {
  53      $type = new stdClass();
  54      $type->type = $type->name = $type->module = $type->description = $type->help = '';
  55      $type->min_word_count = 0;
  56      $type->has_title = TRUE;
  57      $type->has_body = TRUE;
  58      $type->title_label = t('Title');
  59      $type->body_label = t('Body');
  60      $type->custom = TRUE;
  61      $type->modified = FALSE;
  62      $type->locked = FALSE;
  63    }
  64  
  65    $form['#node_type'] = $type; // Make the type object available to implementations of hook_form_alter.
  66  
  67    $form['identity'] = array(
  68      '#type' => 'fieldset',
  69      '#title' => t('Identification'),
  70    );
  71    $form['identity']['name'] = array(
  72      '#title' => t('Name'),
  73      '#type' => 'textfield',
  74      '#default_value' => $type->name,
  75      '#description' => t('The human-readable name of this content type. This text will be displayed as part of the list on the <em>create content</em> page. It is recommended that this name begin with a capital letter and contain only letters, numbers, and <strong>spaces</strong>. This name must be unique.'),
  76      '#required' => TRUE,
  77    );
  78  
  79    if (!$type->locked) {
  80      $form['identity']['type'] = array(
  81        '#title' => t('Type'),
  82        '#type' => 'textfield',
  83        '#default_value' => $type->type,
  84        '#maxlength' => 32,
  85        '#required' => TRUE,
  86        '#description' => t('The machine-readable name of this content type. This text will be used for constructing the URL of the <em>create content</em> page for this content type. This name must contain only lowercase letters, numbers, and underscores. Underscores will be converted into hyphens when constructing the URL of the <em>create content</em> page. This name must be unique.'),
  87      );
  88    }
  89    else {
  90      $form['identity']['type'] = array(
  91        '#type' => 'value',
  92        '#value' => $type->type,
  93      );
  94      $form['identity']['type_display'] = array(
  95        '#title' => t('Type'),
  96        '#type' => 'item',
  97        '#value' => theme('placeholder', $type->type),
  98        '#description' => t('The machine-readable name of this content type. This field cannot be modified for system-defined content types.'),
  99      );
 100    }
 101  
 102    $form['identity']['description'] = array(
 103      '#title' => t('Description'),
 104      '#type' => 'textarea',
 105      '#default_value' => $type->description,
 106      '#description' => t('A brief description of this content type. This text will be displayed as part of the list on the <em>create content</em> page.'),
 107      );
 108  
 109    $form['submission'] = array(
 110      '#type' => 'fieldset',
 111      '#title' => t('Submission form settings'),
 112      '#collapsible' => TRUE,
 113      '#collapsed' => TRUE,
 114    );
 115    $form['submission']['title_label'] = array(
 116      '#title' => t('Title field label'),
 117      '#type' => 'textfield',
 118      '#default_value' => $type->title_label,
 119      '#required' => TRUE,
 120    );
 121    if (!$type->has_title) {
 122      // Avoid overwriting a content type that intentionally does not have a
 123      // title field.
 124      $form['submission']['title_label']['#attributes'] = array('disabled' => 'disabled');
 125      $form['submission']['title_label']['#description'] = t('This content type does not have a title field.');
 126      $form['submission']['title_label']['#required'] = FALSE;
 127    }
 128    $form['submission']['body_label'] = array(
 129      '#title' => t('Body field label'),
 130      '#type' => 'textfield',
 131      '#default_value' => isset($type->body_label) ? $type->body_label : '',
 132      '#description' => t('To omit the body field for this content type, remove any text and leave this field blank.'),
 133    );
 134    $form['submission']['min_word_count'] = array(
 135      '#type' => 'select',
 136      '#title' => t('Minimum number of words'),
 137      '#default_value' => $type->min_word_count,
 138      '#options' => drupal_map_assoc(array(0, 1, 10, 25, 50, 75, 100, 125, 150, 175, 200)),
 139      '#description' => t('The minimum number of words for the body field to be considered valid for this content type. This can be useful to rule out submissions that do not meet the site\'s standards, such as short test posts.')
 140    );
 141    $form['submission']['help']  = array(
 142      '#type' => 'textarea',
 143      '#title' => t('Explanation or submission guidelines'),
 144      '#default_value' => $type->help,
 145      '#description' => t('This text will be displayed at the top of the submission form for this content type. It is useful for helping or instructing your users.')
 146    );
 147    $form['workflow'] = array(
 148      '#type' => 'fieldset',
 149      '#title' => t('Workflow settings'),
 150      '#collapsible' => TRUE,
 151      '#collapsed' => TRUE,
 152    );
 153    $form['workflow']['node_options'] = array('#type' => 'checkboxes',
 154      '#title' => t('Default options'),
 155      '#default_value' => variable_get('node_options_'. $type->type, array('status', 'promote')),
 156      '#options' => array(
 157        'status' => t('Published'),
 158        'promote' => t('Promoted to front page'),
 159        'sticky' => t('Sticky at top of lists'),
 160        'revision' => t('Create new revision'),
 161      ),
 162      '#description' => t('Users with the <em>administer nodes</em> permission will be able to override these options.'),
 163    );
 164  
 165    $form['old_type'] = array(
 166      '#type' => 'value',
 167      '#value' => $type->type,
 168    );
 169    $form['orig_type'] = array(
 170      '#type' => 'value',
 171      '#value' => isset($type->orig_type) ? $type->orig_type : '',
 172    );
 173    $form['module'] = array(
 174      '#type' => 'value',
 175      '#value' => $type->module,
 176    );
 177    $form['custom'] = array(
 178      '#type' => 'value',
 179      '#value' => $type->custom,
 180    );
 181    $form['modified'] = array(
 182      '#type' => 'value',
 183      '#value' => $type->modified,
 184    );
 185    $form['locked'] = array(
 186      '#type' => 'value',
 187      '#value' => $type->locked,
 188    );
 189  
 190    $form['submit'] = array(
 191      '#type' => 'submit',
 192      '#value' => t('Save content type'),
 193      '#weight' => 40,
 194    );
 195  
 196    if ($type->custom) {
 197      if (!empty($type->type)) {
 198        $form['delete'] = array(
 199          '#type' => 'submit',
 200          '#value' => t('Delete content type'),
 201          '#weight' => 45,
 202        );
 203      }
 204    }
 205    else {
 206      $form['reset'] = array(
 207        '#type' => 'submit',
 208        '#value' => t('Reset to defaults'),
 209        '#weight' => 50,
 210      );
 211    }
 212  
 213    return $form;
 214  }
 215  
 216  /**
 217   * Validates the content type submission form generated by node_type_form().
 218   */
 219  function node_type_form_validate($form, &$form_state) {
 220    $type = new stdClass();
 221    $type->type = trim($form_state['values']['type']);
 222    $type->name = trim($form_state['values']['name']);
 223  
 224    // Work out what the type was before the user submitted this form
 225    $old_type = trim($form_state['values']['old_type']);
 226  
 227    $types = node_get_types('names');
 228  
 229    if (!$form_state['values']['locked']) {
 230      if (isset($types[$type->type]) && $type->type != $old_type) {
 231        form_set_error('type', t('The machine-readable name %type is already taken.', array('%type' => $type->type)));
 232      }
 233      if (!preg_match('!^[a-z0-9_]+$!', $type->type)) {
 234        form_set_error('type', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
 235      }
 236      // 'theme' conflicts with theme_node_form().
 237      // '0' is invalid, since elsewhere we check it using empty().
 238      if (in_array($type->type, array('0', 'theme'))) {
 239        form_set_error('type', t("Invalid machine-readable name. Please enter a name other than %invalid.", array('%invalid' => $type->type)));
 240      }
 241    }
 242  
 243    $names = array_flip($types);
 244  
 245    if (isset($names[$type->name]) && $names[$type->name] != $old_type) {
 246      form_set_error('name', t('The human-readable name %name is already taken.', array('%name' => $type->name)));
 247    }
 248  }
 249  
 250  /**
 251   * Implementation of hook_form_submit().
 252   */
 253  function node_type_form_submit($form, &$form_state) {
 254    $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
 255  
 256    $type = new stdClass();
 257  
 258    $type->type = trim($form_state['values']['type']);
 259    $type->name = trim($form_state['values']['name']);
 260    $type->orig_type = trim($form_state['values']['orig_type']);
 261    $type->old_type = isset($form_state['values']['old_type']) ? $form_state['values']['old_type'] : $type->type;
 262  
 263    $type->description = $form_state['values']['description'];
 264    $type->help = $form_state['values']['help'];
 265    $type->min_word_count = $form_state['values']['min_word_count'];
 266    $type->title_label = $form_state['values']['title_label'];
 267    $type->body_label = $form_state['values']['body_label'];
 268  
 269    // title_label is required in core; has_title will always be true, unless a
 270    // module alters the title field.
 271    $type->has_title = ($type->title_label != '');
 272    $type->has_body = ($type->body_label != '');
 273  
 274    $type->module = !empty($form_state['values']['module']) ? $form_state['values']['module'] : 'node';
 275    $type->custom = $form_state['values']['custom'];
 276    $type->modified = TRUE;
 277    $type->locked = $form_state['values']['locked'];
 278  
 279    if ($op == t('Reset to defaults')) {
 280      node_type_reset($type);
 281    }
 282    elseif ($op == t('Delete content type')) {
 283      $form_state['redirect'] = 'admin/content/node-type/'. str_replace('_', '-', $type->old_type) .'/delete';
 284      return;
 285    }
 286  
 287    $status = node_type_save($type);
 288  
 289    $variables = $form_state['values'];
 290  
 291    // Remove everything that's been saved already - whatever's left is assumed
 292    // to be a persistent variable.
 293    foreach ($variables as $key => $value) {
 294      if (isset($type->$key)) {
 295        unset($variables[$key]);
 296      }
 297    }
 298  
 299    unset($variables['form_token'], $variables['op'], $variables['submit'], $variables['delete'], $variables['reset'], $variables['form_id']);
 300  
 301    // Save or reset persistent variable values.
 302    foreach ($variables as $key => $value) {
 303      $variable_new = $key .'_'. $type->type;
 304      $variable_old = $key .'_'. $type->old_type;
 305  
 306      if ($op == t('Reset to defaults')) {
 307        variable_del($variable_old);
 308      }
 309      else {
 310        if (is_array($value)) {
 311          $value = array_keys(array_filter($value));
 312        }
 313        variable_set($variable_new, $value);
 314  
 315        if ($variable_new != $variable_old) {
 316          variable_del($variable_old);
 317        }
 318      }
 319    }
 320  
 321    node_types_rebuild();
 322    menu_rebuild();
 323    $t_args = array('%name' => $type->name);
 324  
 325    if ($op == t('Reset to defaults')) {
 326      drupal_set_message(t('The content type %name has been reset to its default values.', $t_args));
 327      return;
 328    }
 329  
 330    if ($status == SAVED_UPDATED) {
 331      drupal_set_message(t('The content type %name has been updated.', $t_args));
 332    }
 333    elseif ($status == SAVED_NEW) {
 334      drupal_set_message(t('The content type %name has been added.', $t_args));
 335      watchdog('node', 'Added content type %name.', $t_args, WATCHDOG_NOTICE, l(t('view'), 'admin/content/types'));
 336    }
 337  
 338    $form_state['redirect'] = 'admin/content/types';
 339    return;
 340  }
 341  
 342  /**
 343   * Implementation of hook_node_type().
 344   */
 345  function node_node_type($op, $info) {
 346    if ($op != 'delete' && !empty($info->old_type) && $info->old_type != $info->type) {
 347      $update_count = node_type_update_nodes($info->old_type, $info->type);
 348  
 349      if ($update_count) {
 350        drupal_set_message(format_plural($update_count, 'Changed the content type of 1 post from %old-type to %type.', 'Changed the content type of @count posts from %old-type to %type.', array('%old-type' => $info->old_type, '%type' => $info->type)));
 351      }
 352    }
 353  }
 354  
 355  /**
 356   * Resets all of the relevant fields of a module-defined node type to their
 357   * default values.
 358   *
 359   * @param &$type
 360   *   The node type to reset. The node type is passed back by reference with its
 361   *   resetted values. If there is no module-defined info for this node type,
 362   *   then nothing happens.
 363   */
 364  function node_type_reset(&$type) {
 365    $info_array = module_invoke_all('node_info');
 366    if (isset($info_array[$type->orig_type])) {
 367      $info_array[$type->orig_type]['type'] = $type->orig_type; 
 368      $info = _node_type_set_defaults($info_array[$type->orig_type]);
 369  
 370      foreach ($info as $field => $value) {
 371        $type->$field = $value;
 372      }
 373    }
 374  }
 375  
 376  /**
 377   * Menu callback; delete a single content type.
 378   */
 379  function node_type_delete_confirm(&$form_state, $type) {
 380    $form['type'] = array('#type' => 'value', '#value' => $type->type);
 381    $form['name'] = array('#type' => 'value', '#value' => $type->name);
 382  
 383    $message = t('Are you sure you want to delete the content type %type?', array('%type' => $type->name));
 384    $caption = '';
 385  
 386    $num_nodes = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = '%s'", $type->type));
 387    if ($num_nodes) {
 388      $caption .= '<p>'. format_plural($num_nodes, '<strong>Warning:</strong> there is currently 1 %type post on your site. It may not be able to be displayed or edited correctly, once you have removed this content type.', '<strong>Warning:</strong> there are currently @count %type posts on your site. They may not be able to be displayed or edited correctly, once you have removed this content type.', array('%type' => $type->name)) .'</p>';
 389    }
 390  
 391    $caption .= '<p>'. t('This action cannot be undone.') .'</p>';
 392  
 393    return confirm_form($form, $message, 'admin/content/types', $caption, t('Delete'));
 394  }
 395  
 396  /**
 397   * Process content type delete confirm submissions.
 398   */
 399  function node_type_delete_confirm_submit($form, &$form_state) {
 400    node_type_delete($form_state['values']['type']);
 401  
 402    $t_args = array('%name' => $form_state['values']['name']);
 403    drupal_set_message(t('The content type %name has been deleted.', $t_args));
 404    watchdog('menu', 'Deleted content type %name.', $t_args, WATCHDOG_NOTICE);
 405  
 406    node_types_rebuild();
 407    menu_rebuild();
 408  
 409    $form_state['redirect'] = 'admin/content/types';
 410    return;
 411  }


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