You are here

function editablefields_submit in Editable Fields 6.3

Same name and namespace in other branches
  1. 6 editablefields.module \editablefields_submit()
  2. 6.2 editablefields.module \editablefields_submit()

Menu callback: ajax submit.

2 string references to 'editablefields_submit'
editablefields_menu in ./editablefields.module
Implementation of hook_menu().
theme_editablefields_formatter_editable in ./editablefields.module
Theme the editable field.

File

./editablefields.module, line 484
Editable fields module.

Code

function editablefields_submit() {
  $nid = $_POST['nid'];
  $field_name = $_POST['field'];
  $delta = $_POST['delta'];
  $node = node_load($nid);
  $node_options = variable_get('node_options_' . $node->type, array(
    'status',
    'promote',
  ));
  $node->revision = in_array('revision', $node_options);
  if ($node->revision) {
    $node->log = t('%field_name updated by editablefields.', array(
      '%field_name' => $field_name,
    ));
  }
  if (node_access('update', $node)) {
    if (!isset($_POST[$field_name])) {
      $_POST[$field_name] = array();
    }
    $form_state = array(
      'values' => $_POST,
    );

    /* it seems that the serializer does not serialize e.g. un-checked
     * checkboxes. Leaving them as empty arrays. This FILTHY hack fills in the
     * array with 'something' so that when the form is executed, it fills in the
     * right value - I dislike this code - JMB */
    if (is_array($node->{$field_name})) {
      $field = content_fields($field_name, $node->type);
      $items =& $form_state['values'][$field_name];
      if (empty($items)) {
        foreach (array_keys($field['columns']) as $column) {
          if ($field['multiple']) {
            $items[$delta][$column][] = NULL;
          }
          else {
            $items[$column] = NULL;
          }
        }
      }
      if (isset($items['value'])) {
        if (!($field['widget']['type'] == 'optionwidgets_buttons' && $field['multiple'])) {
          $items = array(
            $items,
          );
        }
      }

      // go through content_set_empty if this is NOT a checkbox multi valued element
      if (!($field['widget']['type'] == 'optionwidgets_buttons' && $field['multiple'])) {
        $items = content_set_empty($field, $items);
      }
      switch ($field['type']) {
        case 'nodereference':
        case 'userreference':
          if ($field['multiple']) {
            reset($field['columns']);
            $items[key($field['columns'])] = array_pop($items);
          }
          break;
      }
      drupal_execute('editablefields_form', $form_state, $node, $field_name, $delta);
      $err = drupal_get_messages();
      if (count($err) > 0) {
        drupal_set_header('HTTP/1.1 404 Not Found');

        // format the error message suitable for a popup window in simple text.
        foreach ($err as $type => $messages) {
          foreach ($messages as $message) {
            print $type . ' : ' . $message . "\n";
          }
        }
        exit;
      }

      // the matrix field identifies itself as being multivalue, but in fact, it is not.
      if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE && $field['type'] != matrix) {
        if ($node->{$field_name}[$delta] != $form_state['values'][$field_name][0]) {
          $node->{$field_name}[$delta] = $form_state['values'][$field_name][0];
          node_save($node);
        }
      }
      else {
        if ($node->{$field_name} != $form_state['values'][$field_name]) {
          $node->{$field_name} = $form_state['values'][$field_name];
          node_save($node);
        }
      }

      // make sure sensible headers etc are sent...
      drupal_set_header('Content-Type: text; charset=utf-8');
    }
    else {
      drupal_set_header('HTTP/1.1 404 Not Found');
      print t('No field found, of name: %field', array(
        '%field' => $field_name,
      ));
    }
  }
  else {
    drupal_set_header('HTTP/1.1 404 Not Found');
    print t('No write permissions for %field', array(
      '%field' => $field_name,
    ));
  }
  exit;
}