You are here

function _jeditable_ajax_save in jEditable inline content editing 7

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

Helper function to save a value using the jeditable callback

1 string reference to '_jeditable_ajax_save'
jeditable_menu in ./jeditable.module
Implements hook_menu().

File

./jeditable.module, line 540
jeditable.module TODO: Provides integration between Drupal and the jEditable jquery plugin @todo: Datepicker support @todo: Ajax upload support @todo: Radioboxes/checkboxes support @todo: add required handler for date @todo: add compatibility for…

Code

function _jeditable_ajax_save() {

  // Retrieve the values needed from the post to this page
  $array = explode('-', $_POST['id']);

  // Assign variables as if they were an array
  list($type, $id, $field_name, $field_type, $delta) = $array;
  $value = check_plain($_POST['value']);
  switch ($type) {
    case 'node':
      $node = node_load($id);
      if (!node_access('update', $node)) {

        // check to see that current user has update permissions on the node
        $value = 'access denied';

        // this is the value that will be returned, but no updates made
      }
      else {
        if ($field_name == 'body') {
          $node->body['und'][0]['value'] = $value;
        }
        else {
          $node->{$field_name} = $value;
        }
        $node->revision = variable_get('jeditable_create_new_revisions', false);
        node_save($node);
      }
      break;
    case 'user':
      $user = user_load($id);
      if (!user_edit_access($user)) {

        // check to see that current user has update permissions on the user
        $value = 'access denied';

        // this is the value that will be returned, but no updates made
      }
      else {
        $field_info = field_info_field($field_name);
        $user->{$field_name}[$field_info['translatable'] ? $user->language : LANGUAGE_NONE][0]['value'] = $value;
        $edit = array(
          $field_name => array(
            $field_info['translatable'] ? $user->language : LANGUAGE_NONE => array(
              $delta => array(
                'value' => $value,
              ),
            ),
          ),
        );
        user_save($user, $edit);
      }
      break;
    case 'field':
      $node = node_load($id);
      $delta = intval($delta);
      if (!node_access('update', $node)) {

        // check to see that current user has update permissions on the node
        $value = 'access denied';

        // this is the value that will be returned, but no updates made
      }
      else {
        $field = field_info_field($field_name);

        // get select, radio & checkbox key values
        $key_value = $field['settings']['allowed_values'][$value];
        $lang = $field['translatable'] ? $node->language : LANGUAGE_NONE;

        // assign nid if nodereference, format date if date, otherwise just assign value
        if ($field['type'] == 'nodereference') {
          $node->{$field_name}[$lang][$delta]['nid'] = $value;
          $referenced = node_load($value);
          $value = $referenced->title;
        }
        elseif ($field['type'] == 'datetime') {
          $unixtime = strtotime($value);
          $value = date('o-m-d H:i:s', $unixtime);
          $node->{$field_name}[$lang][$delta]['value'] = $value;
        }
        else {

          //if type = node
          if (isset($field['bundles']['node'])) {
            $node->{$field_name}[$lang][$delta]['value'] = $value;
          }

          //if type = field_collection
          if (isset($field['bundles']['field_collection_item'])) {

            // get field_collection
            $field_collection_name = $field['bundles']['field_collection_item'][0];
            $field_collection_id = $node->{$field_collection_name}[$lang][$delta]['value'];
            $field_collection = entity_load_single('field_collection_item', array(
              $field_collection_id,
            ));
            $field_collection->{$field_name}[$lang][0]['value'] = $value;
          }
        }
        $node->revision = variable_get('jeditable_create_new_revisions', false);
        node_save($node);
        if (isset($field_collection)) {
          $field_collection
            ->save();
        }
      }
      break;
    case 'workflow':
      $node = node_load($id);
      $value = _jeditable_workflow_save($node, $value);
      break;
  }
  if ($field['type'] == 'list_text' || $field['type'] == 'list_boolean') {
    $value = $key_value;
  }
  print $value;
  exit;
}