You are here

public function CmisBrowserDocumentUploadForm::submitForm in CMIS API 8

Same name and namespace in other branches
  1. 8.2 src/Form/CmisBrowserDocumentUploadForm.php \Drupal\cmis\Form\CmisBrowserDocumentUploadForm::submitForm()
  2. 3.0.x src/Form/CmisBrowserDocumentUploadForm.php \Drupal\cmis\Form\CmisBrowserDocumentUploadForm::submitForm()

Form submission 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 FormInterface::submitForm

File

src/Form/CmisBrowserDocumentUploadForm.php, line 66

Class

CmisBrowserDocumentUploadForm
Class CmisBrowserDocumentUploadForm.

Namespace

Drupal\cmis\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $form_state
    ->setRedirect('cmis.cmis_repository_controller_browser', [
    'config' => $values['config'],
    'folder_id' => $values['folder_id'],
  ]);
  $directory = file_directory_temp();
  $filename = $directory . '/' . $_FILES['files']['name']['local_file'];
  if (!is_uploaded_file($_FILES['files']['tmp_name']['local_file']) || !copy($_FILES['files']['tmp_name']['local_file'], $filename)) {

    // Can't create file.
    drupal_set_message($this
      ->t("File can't uploaded."), 'warning');
    return;
  }

  // Open repository.
  if ($repository = new \Drupal\cmis\Controller\CmisRepositoryController($values['config'], $values['folder_id'])) {
    if (!empty($repository
      ->getBrowser()
      ->getConnection()
      ->validObjectName($_FILES['files']['name']['local_file'], 'cmis:document', $values['folder_id']))) {

      // Document exists. Delete file from local.
      unlink($filename);
      drupal_set_message($this
        ->t("The document name @name exists in folder.", [
        '@name' => $_FILES['files']['name']['local_file'],
      ]), 'warning');
      return;
    }
    $session = $repository
      ->getBrowser()
      ->getConnection()
      ->getSession();
    $properties = array(
      \Dkd\PhpCmis\PropertyIds::OBJECT_TYPE_ID => 'cmis:document',
      \Dkd\PhpCmis\PropertyIds::NAME => $_FILES['files']['name']['local_file'],
    );
    if (!empty($values['description'])) {
      $properties[\Dkd\PhpCmis\PropertyIds::DESCRIPTION] = $values['description'];
    }

    // Create document.
    try {
      $document = $session
        ->createDocument($properties, $session
        ->createObjectId($values['folder_id']), \GuzzleHttp\Stream\Stream::factory(fopen($filename, 'r')));

      // Delete file from local.
      unlink($filename);
      drupal_set_message($this
        ->t("Document name @name has been created.", [
        '@name' => $_FILES['files']['name']['local_file'],
      ]));
    } catch (Exception $exc) {
      drupal_set_message($this
        ->t("Document name @name couldn't create.", [
        '@name' => $_FILES['files']['name']['local_file'],
      ]), 'warning');
    }
  }
}