You are here

function photos_upload_form_submit in Album Photos 7.3

Same name and namespace in other branches
  1. 6.2 inc/photos.edit.inc \photos_upload_form_submit()

Submit upload form.

1 string reference to 'photos_upload_form_submit'
photos_upload_form in inc/photos.edit.inc
Upload form.

File

inc/photos.edit.inc, line 158
Handles uploading and editing images.

Code

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

  // $limits = _upload_file_limits($user); // @todo set limits for maximum upload size etc.
  $validators = array(
    'file_validate_is_image' => array(),
  );
  $files_uploaded = array();
  if (variable_get('photos_plupload_status', 0)) {
    $nid = isset($form_state['values']['nid']) ? $form_state['values']['nid'] : $form_state['values']['pid'];
    $album_uid = db_query("SELECT uid FROM {node} WHERE nid = :nid", array(
      ':nid' => $nid,
    ))
      ->fetchField();
    $account = user_load($album_uid);
    foreach ($form_state['values']['plupload'] as $uploaded_file) {
      if ($uploaded_file['status'] == 'done') {

        // Check for zip files.
        $ext = drupal_substr($uploaded_file['name'], -3);
        if ($ext != 'zip' && $ext != 'ZIP') {
          $file = new stdClass();
          $photos_path = photos_check_path('default', '', $account);
          $photos_name = _photos_rename($uploaded_file['name']);
          $file->uri = file_destination($photos_path . '/' . $photos_name, FILE_EXISTS_RENAME);
          if (file_unmanaged_move($uploaded_file['tmppath'], $file->uri)) {
            $info = image_get_info($file->uri);
            if ($info['extension'] && $info['width']) {

              // @todo add limits?
              // $limits = _upload_file_limits($ac);

              /* $validators = array(
                   // 'file_validate_image_resolution' => array($limits['resolution']),
                   // '_file_validate_size' => array($limits['file_size'], $limits['user_size'], $ac)
                 ); */
              $file->pid = $form_state['values']['pid'];
              $file->nid = isset($form_state['values']['nid']) ? $form_state['values']['nid'] : $form_state['values']['pid'];
              $file->uid = $account->uid;
              $file->filename = $photos_name;
              $file->filesize = $info['file_size'];
              $file->filemime = $info['mime_type'];
              if ($file->fid = _photos_save_data($file)) {
                $files_uploaded[] = photos_image_date($file);
              }
            }
            else {
              file_delete($file->uri);
              watchdog('photos_swfu', 'Wrong file type');
            }
          }
          else {
            watchdog('photos_swfu', 'Upload error. Could not move temp file.');
          }
        }
        else {
          if (!variable_get('photos_upzip', 0)) {
            drupal_set_message(t('Please set Album photos to open zip uploads.'), 'error');
          }
          $directory = photos_check_path();
          file_prepare_directory($directory);
          $zip = file_destination($directory . '/' . $uploaded_file['name'], FILE_EXISTS_RENAME);
          if (file_unmanaged_move($uploaded_file['tmppath'], $zip)) {
            $value = new stdClass();
            $value->pid = $form_state['values']['pid'];
            $value->nid = isset($form_state['values']['nid']) ? $form_state['values']['nid'] : $form_state['values']['pid'];
            $value->title = $uploaded_file['name'];
            $value->des = '';
            if (!($msg = _photos_unzip($zip, $value))) {
              drupal_set_message(t('Zip upload failed.'), 'error');
            }
          }
        }
      }
      else {
        drupal_set_message(t('Error uploading some photos.'), 'error');
      }
    }
  }
  else {
    $photos_num = variable_get('photos_num', 5);
    for ($i = 0; $i < $photos_num; ++$i) {
      _photos_rename();
      if ($_FILES['files']['name']['images_' . $i]) {
        $value = new stdClass();
        $value->pid = $form_state['values']['pid'];
        $value->nid = isset($form_state['values']['nid']) ? $form_state['values']['nid'] : $form_state['values']['pid'];
        $value->des = $form_state['values']['des_' . $i];
        $value->title = $form_state['values']['title_' . $i];
        $ext = drupal_substr($_FILES['files']['name']['images_' . $i], -3);
        if ($ext != 'zip' && $ext != 'ZIP') {
          if ($file = file_save_upload('images_' . $i, $validators, photos_check_path())) {
            $files_uploaded[] = photos_image_date((object) array_merge((array) $file, (array) $value), 1);
          }
        }
        else {
          if (!variable_get('photos_upzip', 0)) {
            return form_set_error('error', t('Please set Album photos to open zip upload'));
          }
          $directory = photos_check_path();
          file_prepare_directory($directory);
          $zip = file_destination($directory . '/' . trim(basename($_FILES['files']['name']['images_' . $i])), FILE_EXISTS_RENAME);
          if (move_uploaded_file($_FILES['files']['tmp_name']['images_' . $i], $zip)) {
            if (!($msg = _photos_unzip($zip, $value))) {
              $msg = t('Failed to upload');
            }
          }
        }
      }
    }
  }
  if (!empty($files_uploaded)) {
    drupal_set_message(t('%count image(s) uploaded successfully.', array(
      '%count' => count($files_uploaded),
    )));
  }
  if (isset($msg)) {
    drupal_set_message($msg);
  }
}