You are here

function imagepicker_upload_form_submit in Image Picker 5.2

Same name and namespace in other branches
  1. 5 imagepicker.module \imagepicker_upload_form_submit()
  2. 6.2 imagepicker.upload.inc \imagepicker_upload_form_submit()
  3. 7 imagepicker.upload.inc \imagepicker_upload_form_submit()

Submit upload form

File

./imagepicker.module, line 370
Enables permitted roles to upload images for insertion into configured nodes.

Code

function imagepicker_upload_form_submit($form_id, $form_values) {
  if ($form_values['op'] == t('Upload')) {
    if ($form_values['account'] && $form_values['admin']) {
      $user = user_load(array(
        'uid' => $form_values['account'],
      ));
    }
    else {
      global $user;
    }
    $destination = imagepicker_get_path(FALSE, $form_values['admin'] ? array(
      'name' => $user->name,
      'uid' => $user->uid,
    ) : TRUE);
    $thumbsdir = $destination . IMAGEPICKER_THUMBS_DIR;
    $browserdir = $destination . IMAGEPICKER_BROWSER_DIR;
    if (file_check_directory($destination, TRUE) && file_check_directory($thumbsdir, TRUE) && file_check_directory($browserdir, TRUE)) {

      // Add DIRECTORY_SEPARATORS here because drupals' functions remove trailing slashes
      $destination .= DIRECTORY_SEPARATOR;
      $thumbsdir = $thumbsdir . DIRECTORY_SEPARATOR;
      $browserdir = $browserdir . DIRECTORY_SEPARATOR;
      $maxthumbsize = $form_values['thumb'] ? $form_values['thumb'] : 100;
      $scaleto = $form_values['scale'] ? $form_values['scale'] : FALSE;
      if (!$scaleto) {

        // Use $path instead of original $destination variable cause this
        // variable's value will be changed during copying file, so we won't
        // loose it.
        $path = $destination;
        $imagemoved = imagepicker_copy_uploaded_file($path, 'file_upload');
        $file = basename($path);
      }
      else {
        $source = $_FILES['files']['tmp_name']['file_upload'];
        $file = imagepicker_get_uploaded_file_name($destination, 'file_upload');
        $imagescaled = imagepicker_scale_image($source, $destination . $file, $scaleto);
      }
      if (!$scaleto && $imagemoved || $scaleto && $imagescaled) {

        // Source file should still be an uploaded one, as scaled image
        // might have some watermarks etc. from drupal's filters/hooks.
        $source = $_FILES['files']['tmp_name']['file_upload'];
        if (imagepicker_scale_image($source, $thumbsdir . $file, $maxthumbsize)) {
          imagepicker_scale_image($source, $browserdir . $file, variable_get('imagepicker_default_browser_thumbnail_size', 100));
          $nextimgid = db_next_id('{imagepicker}_img_id');
          $title = htmlspecialchars($form_values['title']);
          $description = htmlspecialchars($form_values['description']);
          if (db_query("INSERT INTO {imagepicker} (uid, img_name, img_title, img_description) VALUES ('%d', '%s', '%s', '%s')", array(
            $user->uid,
            $file,
            $title,
            $description,
          ))) {

            // need to get the img_id if there are groups to add to
            if (is_array($form_values['grouplist']) && variable_get('imagepicker_groups_in_upload_enabled', 1)) {
              $result = db_query("SELECT img_id FROM {imagepicker} WHERE uid = '%d' AND img_name = '%s'", array(
                $user->uid,
                $file,
              ));
              $row = db_fetch_array($result);
              $record['img_id'] = $row['img_id'];
              foreach ($form_values['grouplist'] as $gid) {
                $record['gid'] = $gid;
                imagepicker_insert_group_image($record);
              }
            }
            drupal_set_message(t('Image was successfully uploaded.'));
            if ($form_values['admin']) {
              drupal_goto('admin/settings/imagepicker/images/user/' . $user->uid . '/browse');
            }
            elseif ($form_values['account']) {
              drupal_goto('user/' . $user->uid . '/imagepicker/browse');
            }
            else {
              drupal_goto('imagepicker/browse/' . $nextimgid);
            }
          }
          else {
            file_delete($thumbsdir . $file);
            file_delete($browserdir . $file);
            drupal_set_message(t('Error while saving information to database for uploaded image.'), 'error');
          }
        }
        else {
          drupal_set_message(t('Error while creating a thumbnail for uploaded image.'), 'error');
        }
      }
      else {
        if (!$scaleto && !$imagemoved) {
          drupal_set_message(t('Error while moving uploaded file to its destination.'), 'error');
        }
        else {
          drupal_set_message(t('Error while scaling uploaded file.'), 'error');
        }
      }
      file_delete($destination . $file);
    }
    else {
      drupal_set_message(t('Unable to create a directory structure for your images.'), 'error');
    }
  }
  if ($form_values['admin']) {
    drupal_goto('admin/settings/imagepicker/images');
  }
  elseif ($form_values['account']) {
    drupal_goto('user/' . $user->uid . '/imagepicker');
  }
  else {
    drupal_goto('imagepicker');
  }
}