You are here

function node_gallery_upload_images in Node Gallery 6.2

Same name and namespace in other branches
  1. 6 node_gallery.pages.inc \node_gallery_upload_images()
1 call to node_gallery_upload_images()
node_gallery_upload_form in ./node_gallery.pages.inc

File

./node_gallery.pages.inc, line 196
Node gallery pages.

Code

function node_gallery_upload_images(&$form, &$form_state) {
  global $user;
  $gallery_config = node_gallery_get_config($form['#node']->type);
  $limits = node_gallery_upload_file_limits($user, $gallery_config);
  $validators = array(
    'file_validate_extensions' => array(
      $limits['extensions'],
    ),
    'file_validate_image_resolution' => array(
      $limits['resolution'],
    ),
    'file_validate_size' => array(
      $limits['file_size'],
      $limits['user_size'],
    ),
  );

  // We need to keep track of the number of files being currently updated so
  // that we can avoid http://drupal.org/node/601186#comment-2951536
  $temp_file_count = 0;

  // Save new file uploads.
  if ($user->uid != 1 || user_access('upload files')) {
    $directory = node_gallery_check_directory($form['#node']);
    foreach ($_FILES['files']['name'] as $id => $name) {
      if (!empty($name)) {

        // An additional check to strip all invalid characters
        $_FILES['files']['name'][$id] = node_gallery_validate_filesystem_path($_FILES['files']['name'][$id]);

        // Make sure we have a filename after scripting bad characters
        if (strlen($_FILES['files']['name'][$id]) < 5) {
          $_FILES['files']['name'][$id] = 'file-' . $_FILES['files']['name'][$id];
        }
        $temp_file_count++;
        $validators['file_validate_number'] = array(
          $limits['max_number'],
          $temp_file_count,
        );
        $file = file_save_upload($id, $validators, $directory);
        if ($file != 0) {
          $file->description = $file->filename;
          $file->weight = 0;
          $file->gid = $form['#node']->nid;
          $form['#node']->images[$file->fid] = $file;
          $form_state['values']['files'][$file->fid] = (array) $file;
        }
      }
    }
  }

  // Order the form according to the set file weight values.
  if (!empty($form_state['values']['files'])) {
    $microweight = 0.001;
    foreach ($form_state['values']['files'] as $fid => $file) {
      if (is_numeric($fid)) {
        $form_state['values']['files'][$fid]['#weight'] = $file['weight'] + $microweight;
        $microweight += 0.001;
      }
    }
    uasort($form_state['values']['files'], 'element_sort');
  }
}