You are here

function node_gallery_manage_images_submit in Node Gallery 6.3

1 string reference to 'node_gallery_manage_images_submit'
node_gallery_manage_images_form in ./node_gallery.pages.inc
Displays the content for our "Manage Images" tab, which is a VBO view.

File

./node_gallery.pages.inc, line 341
Handles displaying of Node gallery pages.

Code

function node_gallery_manage_images_submit(&$form, &$form_state) {
  $relationship = node_gallery_get_relationship($form['#gallery']->type);
  $compare_fields = array();
  if ($relationship['settings']['manage_images_show_gallery_list']) {
    $compare_fields[] = 'gid';
  }
  foreach ($relationship['settings']['manage_images_fields'] as $k => $f) {
    if ($f) {
      if ($k == 'body_field') {
        $compare_fields[] = 'body';
      }
      elseif ($k == 'author') {
        $compare_fields[] = 'name';
        $compare_fields[] = 'date';
      }
      elseif ($k == 'options') {

        // publication settings
        $compare_fields[] = 'status';
        $compare_fields[] = 'promote';
        $compare_fields[] = 'sticky';
      }
      else {
        $compare_fields[] = $k;
      }
    }
  }
  if ($form_state['values']['is_cover'] != $form['#gallery']->cover_image) {

    // The user selected a new cover image for the gallery.
    // Build an object with the necessary attributes and update the gallery record.
    $image = new stdClass();
    $image->nid = $form_state['values']['is_cover'];
    $image->is_cover = 1;
    $image->gid = $form['#gallery']->nid;
    node_gallery_set_gallery_cover_image($image);
  }
  foreach ($form_state['values']['images'] as $nid => $form_values) {

    // strip add-more buttons
    foreach ($form_values['edit_form'] as $field => $values) {
      if (strpos($field, 'field_') !== FALSE && is_array($values)) {
        unset($form_values['edit_form'][$field][$field . '_add_more-' . $nid]);
      }
    }
    $image_node = (object) array_merge($form_values['edit_form'], array(
      'gid' => $form_values['gid'],
    ));
    if ($form_values['remove']) {
      $op_images['delete'][] = $image_node->nid;
    }
    else {
      if (node_gallery_images_check_update($form['images'][$image_node->nid]['edit_form']['#node'], $image_node, $compare_fields)) {

        // Setting oldgid and newgid on our node clears caches on node_save
        if ($relationship['settings']['manage_images_show_gallery_list']) {
          if ($form['images'][$image_node->nid]['edit_form']['#node']->gid != $image_node->gid) {
            $image_node->oldgid = $form['images'][$image_node->nid]['edit_form']['#node']->gid;
            $image_node->newgid = $image_node->gid;
          }
        }

        // Use strict comparison because the array can contain zeros.
        if (in_array('author', $relationship['settings']['manage_images_fields'], TRUE)) {
          $new_author = user_load(array(
            'name' => $image_node->name,
          ));
          $image_node->uid = $new_author->uid;
          $image_node->created = strtotime($image_node->date);
        }
        $op_images['update'][] = $image_node;
      }
    }
    if ($form_values['rotate']) {
      $allowed_degrees = array(
        '90',
        '270',
        '180',
      );
      $degrees = (int) $form_values['rotate'];
      if (in_array($degrees, $allowed_degrees)) {
        $node = $form['images'][$image_node->nid]['edit_form']['#node'];
        $op_images['rotate'][] = array(
          $node->{$relationship['imagefield_name']}[0]['filepath'],
          $degrees,
        );
      }
    }
  }
  while (!empty($op_images['update'])) {
    $node = array_shift($op_images['update']);
    $operations[] = array(
      'node_gallery_batch_node_save',
      array(
        $node,
      ),
    );
  }
  while (!empty($op_images['delete'])) {
    $nid = array_shift($op_images['delete']);
    $operations[] = array(
      'node_gallery_image_delete_process',
      array(
        $nid,
      ),
    );
  }
  while (!empty($op_images['rotate'])) {
    $params = array_shift($op_images['rotate']);
    $operations[] = array(
      'node_gallery_batch_rotate_callback',
      $params,
    );
  }
  if (!empty($operations)) {
    $batch = array(
      'operations' => $operations,
      'finished' => 'node_gallery_image_process_finished',
      'title' => t("Modifying Images"),
      'init_message' => t("Images modification process is starting."),
      'progress_message' => t('Processed @current out of @total.'),
      'error_message' => t('Images modification has encountered an error.'),
    );
    batch_set($batch);

    //For memory management, we wipe the values from the form since it's all on the $operations array now
    $tmp = $form_state['values']['form_build_id'];
    $form_state = array();
    $form_state['values']['form_build_id'] = $tmp;
    $form = array();
  }
  $gid = isset($image_node->oldgid) ? $image_node->oldgid : $image_node->gid;
  $form_state['redirect'] = 'node/' . $gid;
}