You are here

public function AddExternalPackageForm::validateForm in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Form/AddExternalPackageForm.php \Drupal\opigno_module\Form\AddExternalPackageForm::validateForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

src/Form/AddExternalPackageForm.php, line 77

Class

AddExternalPackageForm
Add External package form.

Namespace

Drupal\opigno_module\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Validation is optional.
  $file_field = "package";
  $storage = $form_state
    ->getStorage();
  $is_ppt = isset($storage['mode']) && $storage['mode'] == 'ppt' ? TRUE : FALSE;
  if (empty($_FILES['files']['name'][$file_field])) {

    // Only need to validate if the field actually has a file.
    $form_state
      ->setError($form['package'], $this
      ->t("The file was not uploaded."));
  }

  // Prepare folder.
  $temporary_file_path = !$is_ppt ? 'public://external_packages' : 'public://' . ExternalPackageController::getPptConversionDir();
  \Drupal::service('file_system')
    ->prepareDirectory($temporary_file_path, FileSystemInterface::MODIFY_PERMISSIONS | FileSystemInterface::CREATE_DIRECTORY);

  // Prepare file validators.
  $extensions = !$is_ppt ? [
    'h5p zip',
  ] : [
    'ppt pptx',
  ];
  $validators = [
    'file_validate_extensions' => $extensions,
  ];

  // Validate file.
  if ($is_ppt) {
    $ppt_dir = ExternalPackageController::getPptConversionDir();
    $file_default_scheme = \Drupal::config('system.file')
      ->get('default_scheme');
    $public_files_real_path = \Drupal::service('file_system')
      ->realpath($file_default_scheme . "://");
    $ppt_dir_real_path = $public_files_real_path . '/' . $ppt_dir;
    $file = file_save_upload($file_field, $validators, $temporary_file_path, NULL, FileSystemInterface::EXISTS_REPLACE);

    // Rename uploaded file - remove special chars.
    $file_new = $file[0];
    $filename = $file_new
      ->getFilename();
    $filename_new = preg_replace('/[^a-zA-Z0-9-_\\.]/', '-', $filename);
    $file_new
      ->setFilename($filename_new);
    $file_new
      ->setFileUri($temporary_file_path . '/' . $filename_new);
    $file_new
      ->save();
    rename($ppt_dir_real_path . '/' . $filename, $ppt_dir_real_path . '/' . $filename_new);
    if (!empty($file_new)) {

      // Actions on ppt(x) file upload.
      $ppt_dir = ExternalPackageController::getPptConversionDir();
      $file_default_scheme = \Drupal::config('system.file')
        ->get('default_scheme');
      $public_files_real_path = \Drupal::service('file_system')
        ->realpath($file_default_scheme . "://");
      $ppt_dir_real_path = $public_files_real_path . '/' . $ppt_dir;
      $this
        ->logger('ppt_converter')
        ->notice('$ppt_dir_real_path: ' . $ppt_dir_real_path);
      $images = ExternalPackageController::convertPptSlidesToImages($file_new, $ppt_dir_real_path);
      if ($images) {
        \Drupal::logger('ppt_converter')
          ->notice('$images: <pre><code>' . print_r($images, TRUE) . '</code></pre>');

        // Create H5P package in 'sites/default/files/external_packages_ppt'.
        ExternalPackageController::createH5pCoursePresentationPackage($images, $ppt_dir_real_path, $form_state
          ->getValue('name'));
      }
      if (file_exists($temporary_file_path . '/ppt-content-import.h5p')) {

        // Replace form uploaded file with converted h5p content file.
        $file_new = File::load($file[0]
          ->id());
        $file_new
          ->setFilename('ppt-content-import.h5p');
        $file_new
          ->setFileUri($temporary_file_path . '/ppt-content-import.h5p');
        $file_new
          ->setMimeType('application/octet-stream');
        $file_new
          ->save();
        $file[0] = $file_new;
      }
    }
  }
  else {
    $file = file_save_upload($file_field, $validators, $temporary_file_path, NULL, FileSystemInterface::EXISTS_REPLACE);
  }
  if (!$file[0]) {
    return $form_state
      ->setRebuild();
  }

  // Validate Scorm and Tincan packages.
  $this
    ->validateZipPackage($form, $form_state, $file[0]);

  // Set file id in form state for loading on submit.
  $form_state
    ->set('package', $file[0]
    ->id());
}