You are here

function fupload_list_images_imagefield_submit in Image FUpload 6.3

File

includes/images.previewlist.imagefield.inc, line 266

Code

function fupload_list_images_imagefield_submit($form, &$form_state) {
  global $user;

  // define some basic variables
  $imagefield_fields = array(
    "title",
    "description",
    "alt",
  );
  $storage_mode = $form_state['values']['storage_mode'];

  // get fids (--> nids) of images and start batch process (saving)
  foreach ($form_state['values']['image_items'] as $fid => $nid) {

    // split up storage modes to provide a better handling
    // storage mode: multiple images per node
    if ($storage_mode == "multiple") {

      // only load node once
      if (!isset($node)) {
        $node = node_load($nid);
      }
      $image_field = $node->{$form_state}['values']['field_name'];
      for ($x = 0; $x < count($image_field); $x++) {
        if ($image_field[$x]['fid'] == $fid) {

          // found our image to be edited
          foreach ($imagefield_fields as $key => $value) {
            if (isset($form_state['values']['imagefield_' . $value . '_' . $fid])) {
              $image_field[$x]['data'][$value] = $form_state['values']['imagefield_' . $value . '_' . $fid];
            }
          }
          break;
        }
      }

      // map our data to $node
      $node->{$form_state}['values']['field_name'] = $image_field;
    }

    // storage mode: single image per node
    if ($storage_mode == "single") {
      $node = node_load($nid);

      // new changes to node object
      // basic fields (core modules of drupal)
      if (isset($form_state['values']['node_title_' . $fid])) {
        $node->title = !empty($form_state['values']['node_title_' . $fid]) ? $form_state['values']['node_title_' . $fid] : $node->title;

        // work around [#227966]
      }
      if (isset($form_state['values']['node_body_' . $fid])) {
        $node->body = !empty($form_state['values']['node_body_' . $fid]) ? $form_state['values']['node_body_' . $fid] : $node->body;
      }
      $node->teaser = node_teaser($node->body, $form_state['values']['format_' . $fid]);
      if (isset($form_state['values']['format_' . $fid])) {
        $node->format = isset($form_state['values']['format_' . $fid]) ? $form_state['values']['format_' . $fid] : $node->format;
      }
      if (isset($form_state['values']['taxonomy_' . $fid])) {
        $node->taxonomy = !empty($node->taxonomy) ? array_merge($form_state['values']['taxonomy_' . $fid], $node->taxonomy) : $form_state['values']['taxonomy_' . $fid];
      }

      // imagefield (cck)
      $image_field = $node->{$form_state}['values']['field_name'];
      foreach ($imagefield_fields as $key => $value) {
        if (isset($form_state['values']['imagefield_' . $value . '_' . $fid])) {
          $image_field[0]['data'][$value] = $form_state['values']['imagefield_' . $value . '_' . $fid];
        }
      }
      $node->{$form_state}['values']['field_name'] = $image_field;

      // cck fields (contributed module)
      // ... will follow in future version
      // save changes to node
      node_save($node);
    }
  }

  // multiple mode: map our new data into node
  if ($storage_mode == "multiple") {
    node_save($node);
  }

  // delete preview image list for this user
  if (!form_get_errors()) {
    db_query("DELETE FROM {fupload_previewlist} WHERE fieldname = '%s' AND uid = %d", $form_state['values']['field_name'], $user->uid);
  }

  // show message and redirect user to upload page
  drupal_set_message(t('All images have been saved and published.'));
  drupal_redirect_form($form);
}