You are here

public function UploadForm::submitForm in Pack & Upload 8

Same name and namespace in other branches
  1. 2.0.x lib/Drupal/pack_upload/Form/UploadForm.php \Drupal\pack_upload\Form\UploadForm::submitForm()

Overrides FormInterface::submitForm

See also

\Drupal\Core\Form\FormInterface::submitForm()

File

lib/Drupal/pack_upload/Form/UploadForm.php, line 54
This file contains the code for upload form of zip.

Class

UploadForm

Namespace

Drupal\pack_upload\Form

Code

public function submitForm(array &$form, array &$form_state) {
  $file = file_save_upload('file', $form_state, array(
    'file_validate_extensions' => array(
      'zip tar tar.gz',
    ),
  ));
  $uri = file_build_uri($this
    ->config('pack_upload.settings')
    ->get('path'));

  // Created a directory if not available.
  if (!is_dir($uri)) {
    drupal_mkdir($uri, FILE_CHMOD_DIRECTORY);
  }
  if ($file) {
    $file = is_array($file) ? array_shift($file) : $file;
    if ($path = file_unmanaged_move($file
      ->getFileUri(), $uri, FILE_EXISTS_RENAME)) {
      $form_state['values']['file'] = $file;
      $realpath = drupal_realpath($path);
      $zip = new \ZipArchive();
      $zip
        ->open($realpath);
      $result = $zip
        ->extractTo(drupal_realpath($uri));
      if ($result === TRUE) {
        drupal_set_message(t('All media have been extracted to %path', array(
          '%path' => drupal_realpath($uri),
        )));
      }
      else {
        watchdog('error', 'There is some problem related to extraction of the file. Please upload and try again.', array(), WATCHDOG_ERROR, NULL);
        drupal_set_message(t('There is some problem related to extraction of the file. Please upload and try again.'), 'error', FALSE);
      }
      $zip
        ->close();
    }
    else {
      $this
        ->setFormError('file', $form_state, t("Failed to write the uploaded file the file folder."));
    }
  }
  else {
    $this
      ->setFormError('file', $form_state, t('No file was uploaded.'));
  }
}