You are here

function node_gallery_api_manage_items_submit in Node Gallery 7

Submit handler for Manage Items form.

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

File

./node_gallery_api.pages.inc, line 415
Node Gallery module.

Code

function node_gallery_api_manage_items_submit(&$form, &$form_state) {
  $relationship_type = node_gallery_api_get_relationship_type($form['#gallery']->type);
  $file_field = field_info_field($relationship_type->filefield_name);
  $compare_fields = array();
  foreach ($relationship_type->settings['manage_items']['items_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']->node_gallery['cover_item']) {
    node_gallery_api_set_gallery_cover_item($form_state['values']['is_cover'], $form['#gallery']->nid);
  }
  foreach ($form_state['values']['items'] 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) $form_values['edit_form'];
    if ($form_values['remove']) {
      $op_images['delete'][] = $image_node->nid;
    }
    else {
      if (node_gallery_api_items_check_update($form['items'][$image_node->nid]['edit_form']['#node'], $image_node, $compare_fields)) {

        // Use strict comparison because the array can contain zeros.
        if (in_array('author', $relationship_type->settings['manage_items']['items_fields'], TRUE)) {
          $new_author = array_shift(user_load_multiple(array(), array(
            'name' => $image_node->name,
          )));
          $image_node->uid = $new_author->uid;
          $image_node->created = strtotime($image_node->date);
        }
        $op_images['update'][] = $image_node;
      }
    }
    if (!empty($form_values['rotate'])) {
      $allowed_degrees = array(
        '90',
        '270',
        '180',
      );
      $degrees = (int) $form_values['rotate'];
      if (in_array($degrees, $allowed_degrees)) {
        $node = $form['items'][$image_node->nid]['edit_form']['#node'];
        $node_wrapper = entity_metadata_wrapper('node', $node);
        if ($file_field['cardinality'] == 1) {
          $file = $node_wrapper->{$relationship_type->filefield_name}
            ->value();
        }
        else {
          $file = reset($node_wrapper->{$relationship_type->filefield_name}
            ->value());
        }
        if (!empty($file)) {
          $op_images['rotate'][] = array(
            $file['fid'],
            $degrees,
          );
        }
      }
    }
  }
  while (!empty($op_images['update'])) {
    $node = array_shift($op_images['update']);
    $operations[] = array(
      'node_gallery_api_batch_node_save',
      array(
        $node,
      ),
    );
  }
  while (!empty($op_images['delete'])) {
    $nid = array_shift($op_images['delete']);
    $operations[] = array(
      'node_gallery_api_batch_node_delete',
      array(
        $nid,
      ),
    );
  }
  while (!empty($op_images['rotate'])) {
    $params = array_shift($op_images['rotate']);
    $operations[] = array(
      'node_gallery_api_batch_rotate',
      $params,
    );
  }
  $form_state['redirect'] = 'node/' . $form_state['values']['gallery_nid'];
  if (!empty($operations)) {
    $batch = array(
      'operations' => $operations,
      'finished' => 'node_gallery_api_batch_item_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.'),
      'file' => drupal_get_path('module', 'node_gallery_api') . '/node_gallery_api.inc',
    );
    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['programmed'] = FALSE;
    $form_state['values']['form_build_id'] = $tmp;
    $form = array();
  }
}