<?php

/**
 * @file
 * Install, update and uninstall functions for the dropzonejs module.
 */

/**
 * Implements hook_requirements().
 */
function dropzonejs_requirements($phase) {

  $requirements = [];

  $path = DRUPAL_ROOT . '/libraries/dropzone/dist/min/dropzone.min.js';
  if (\Drupal::moduleHandler()->moduleExists('libraries')) {
    $path = libraries_get_path('dropzone') . '/dist/min/dropzone.min.js';
  }

  // Is the library found in the root libraries path.
  $library_found = file_exists($path);

  // If library is not found, then look in the current profile libraries path.
  if (!$library_found) {
    $profile_path = drupal_get_path('profile', drupal_get_profile());
    $profile_path .= '/libraries/dropzone/dist/min/dropzone.min.js';
    // Is the library found in the current profile libraries path.
    $library_found = file_exists($profile_path);
  }

  if (!$library_found) {
    $requirements['dropzonejs_library'] = array(
      'title' => t('Dropzone library missing'),
      'description' => t('Dropzonejs requires the dropzone.min.js library.
        Download it (https://github.com/enyo/dropzone) and place it in the
        libraries folder (/libraries)'),
      'severity' => REQUIREMENT_ERROR,
    );
  }

  return $requirements;
}

/**
 * The upload handler should use stream wrappers.
 */
function dropzonejs_update_8001() {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory->getEditable('dropzonejs.settings');
  $config->clear('tmp_dir');
  $config->set('tmp_upload_scheme', 'temporary');
  $config->save(TRUE);
}
