You are here

function image_fupload_image_node_create in Image FUpload 6.2

Same name and namespace in other branches
  1. 6.3 image_fupload_image/image_fupload_image.module \image_fupload_image_node_create()
1 string reference to 'image_fupload_image_node_create'
image_fupload_image_menu in image_fupload_image/image_fupload_image.module
Implementation of hook_menu().

File

image_fupload_image/image_fupload_image.module, line 124

Code

function image_fupload_image_node_create() {
  global $user;
  global $file_cache;

  // Remove images which couldn't be processed completly (--> mostly because of memory excaustion) --> real removal done by cron
  db_query("UPDATE {files} SET filename = '%s' WHERE uid = %d AND status = %d AND filename = '%s' LIMIT 3", IMAGE_PROCESSED, $user->uid, FILE_STATUS_TEMPORARY, IMAGE_HALFPROCESSED);

  // Get some POST Variables
  $form_build_id = $_POST['form_build_id'];
  $form_id = $_POST['form_id'];
  if (isset($form_build_id) && isset($form_id)) {
    $form_error = 0;
    $message = '';

    // Load the form from the Form API cache and check if valid
    $form_state = array(
      'rebuild' => TRUE,
      'values' => $_POST,
    );

    // rebuild option needed to prevent that "_image_node_form_submit" gets executed by drupal_process_form
    if (!($form = form_get_cache($form_build_id, &$form_state))) {

      // code based on upload.module (15/08/2008)
      form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
      $output = theme('status_messages');
      drupal_json(array(
        'status' => TRUE,
        'data' => $output,
      ));
      exit;
    }

    // Some form manipulations
    $form['#post'] = $_POST;

    // Default Value; Title will be overwritten in the next steps
    $form['#post']['title'] = 'Image';
    $form['#post']['form_id'] = $form['form_id']['#value'];

    // New generated fields
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => 'Image',
      '#default_value' => '',
    );

    // Needed that validation is successful
    // for preview list: body will be replaced
    if (user_access('edit captions')) {

      //old: !isset($form['body_field']['body']['#type']) &&
      $form['body_field']['body'] = array(
        '#type' => 'textarea',
        '#title' => 'Body',
        '#default_value' => IMAGE_NOT_COMPLETED,
      );
    }
    drupal_process_form($form_id, $form, $form_state);

    // Only validate input data
    if (!form_get_errors()) {
      $result = db_query("SELECT * FROM {files} WHERE uid = %d AND status = %d AND filename = '%s' LIMIT 0 , 3", $user->uid, FILE_STATUS_TEMPORARY, IMAGE_UNMACHINED);
      while ($image = db_fetch_object($result)) {

        // ahh.. first image to process
        $file_cache['image'] = $image;

        // Add image to cache for image.module
        // Set status flag on image (work on picture can begin); if there are problems with this image, it will be kicked next time
        db_query("UPDATE {files} SET filename = '%s' WHERE fid = %d LIMIT 1", IMAGE_HALFPROCESSED, $image->fid);

        // Create a filename out of the given image information; used a theme function so that it can be customised; mapping new title
        $form['title']['#value'] = check_plain(theme('fupload_create_filename', $image));
        $form_state['values']['title'] = $form['title']['#value'];

        // for preview list if bodyfield is deactivated by default
        if ($form['body_field']['body']['#default_value'] == IMAGE_NOT_COMPLETED) {
          $form['body']['#value'] = IMAGE_NOT_COMPLETED;
          $form_state['values']['body'] = IMAGE_NOT_COMPLETED;
        }
        node_form_submit($form, $form_state);

        // Submit form --> Save it
        // Prevent that same image is processed twice; deleted later by cron
        db_query("UPDATE {files} SET filename = '%s' WHERE fid = %d LIMIT 1", IMAGE_PROCESSED, $image->fid);
      }

      // no files in cache, so no files have been processed yet because of empty queue
      if (empty($file_cache['image'])) {
        drupal_set_message(t('No images yet in queue.'));
      }

      // Examine how many images are left in queue and inform JS by sending a hidden element
      $result = db_fetch_object(db_query("SELECT COUNT(*) AS img_count FROM {files} WHERE uid = %d AND status = %d AND filename = '%s'", $user->uid, FILE_STATUS_TEMPORARY, IMAGE_UNMACHINED));
      $message .= '<input type="hidden" name="num_queued_images" id="num_queued_images" value="' . $result->img_count . '" />';
    }
    else {

      // Error in received form (for example a required field was not filled); inform JS => user
      $form_error = 1;
      $message .= '<input type="hidden" name="num_queued_images" id="num_queued_images" value="0" />';
    }

    // Inform JS about errors
    $message .= '<input type="hidden" name="form_errors" id="form_errors" value="' . $form_error . '"  />';
    $message .= theme('status_messages');

    // Theme all messages
    drupal_json(array(
      'status' => TRUE,
      'data' => $message,
    ));
  }
  else {
    drupal_json(array(
      'status' => TRUE,
      'data' => t('Error: No or wrong POST Data'),
    ));
  }
}