You are here

function slickgrid_callback_update in Slickgrid 6

Same name and namespace in other branches
  1. 7.2 includes/slickgrid.callbacks.inc \slickgrid_callback_update()
  2. 7 includes/slickgrid.callbacks.inc \slickgrid_callback_update()

File

./slickgrid.callbacks.inc, line 43

Code

function slickgrid_callback_update() {

  // TODO - Batch processing
  $field_name = $_POST['field_name'];
  $field_id = $_POST['field_id'];
  module_load_include('inc', 'node', 'node.pages');
  $updated = array();

  // Array of NIDs that have been updated
  foreach ($_POST['nids'] as $nid) {

    // $form_state gets changed by the form submission so re-intialise for every loop
    $form_state = array(
      'values' => $_POST,
    );
    $form_state['values']['op'] = t('Save');
    $form_state['submitted'] = true;
    $form_state['submitted'] = 1;
    $node = node_load($nid);
    if (node_access('update', $node)) {

      // Get the vid before it is updated
      $revision_vid = $node->vid;
      $form_id = $node->type . '_node_form';

      // Execute the node form
      drupal_execute($form_id, $form_state, $node);

      // Has there been an error?
      if ($err = form_get_errors()) {
        $errors[$nid] = $err;
      }
      else {

        // If not, mark it as updated nids
        $updated_nodes[$nid] = array(
          'vid' => $revision_vid,
          'value' => $_POST['reload_from_view'] ? '' : $_POST[$field_name],
        );
      }
    }
    else {
      $errors[$nid] = t('You do not have access to edit @node', array(
        '@node' => $node->title,
      ));
    }
  }

  // If we want to reload the value from the view, get it now
  if (count($updated_nodes) && $_POST['reload_from_view']) {
    $view = slickgrid_callback_get_view($_POST['view'], $_POST['display_id'], array_keys($updated_nodes));
    foreach ($view->result as $count => $row) {
      $updated_nodes[$row->nid]['value'] = $view
        ->render_field($field_id, $count);
    }
  }

  // Clear the message queue
  drupal_get_messages();

  // Add our own messages
  if ($count_updated = count($updated_nodes)) {
    drupal_set_message(format_plural($count_updated, '@title was updated succesfully.', '@count nodes were updated succesfully.', array(
      '@title' => $node->title,
    )));
  }
  if ($count_errors = count($errors)) {
    $error_message = format_plural($count_errors, 'There was 1 error', 'There were @count errors');
    drupal_set_message($error_message, 'error');
    $error_message .= ': ';
    foreach ($errors as $error) {
      foreach ($error as $err) {
        $error_message .= $err . '<br />';
      }
    }
    watchdog('slickgrid', $error_message, array(), WATCHDOG_ERROR);
  }

  // If batch page exists, we need to clear the cache
  if (module_exists('views_batch_page') && strpos($_POST['display_id'], 'views_batch_page') !== false) {

    // If we haven't already retrieved the view, build a dummy object to build the cache id from
    if (!isset($view)) {
      $view = new stdClass();
      $view->name = $_POST['view'];
      $view->current_display = $_POST['display_id'];
    }
    views_batch_page_cache_clear($view);
  }
  return array(
    'errors' => $errors,
    'updated' => $updated_nodes,
    'field_name' => $field_name,
    'field_id' => $field_id,
  );
}