You are here

function xbbcode_highlighter_import_validate in Extensible BBCode 5

File

xbbcode_highlighter/xbbcode_highlighter.module, line 251

Code

function xbbcode_highlighter_import_validate($form_id, &$form) {
  global $xbbcode_highlighter_import_name;
  $filepath = file_directory_path() . '/highlighter';
  file_check_directory($filepath, true);
  if ($_FILES['files']['name']['xml']) {
    $xbbcode_highlighter_import_name = strtolower($_FILES['files']['name']['xml']);
    if (!preg_match('/^.+\\.xml$/', $xbbcode_highlighter_import_name)) {
      return form_set_error('upload', t("This is not the correct file type. It must be an XML file."));
    }

    /* drupal reneges on its promise to replace existing files. thus, make sure to delete. */
    file_delete("{$filepath}/{$xbbcode_highlighter_import_name}");
    $file = file_save_upload('xml', $filepath . '/' . $_FILES['files']['names']['xml'], true);
    if (!$file) {
      form_set_error('upload', t("The upload did not work."));
    }
  }
  else {
    if (!$form['xml']['scan']) {
      return form_set_error('xml', t("You must either upload a file or enter a directory path."));
    }
    $xbbcode_highlighter_import_name = file_scan_directory($form['xml']['scan'], '^.+\\.xml$', array(), '', true, 'name');
    if (!$xbbcode_highlighter_import_name) {
      return form_set_error('scan', t("There are no files in this directory."));
    }
    foreach ($xbbcode_highlighter_import_name as $i => &$file) {
      $name = strtolower($file->basename);

      /* hey, you never know. */
      $file = file_copy($file->filename, "{$filepath}/{$name}", FILE_EXISTS_REPLACE);
      if (!$file) {
        drupal_set_message(t("Import of %file failed.", array(
          '%file' => $name,
        )));
        file_delete("{$filepath}/{$name}");
        unset($xbbcode_highlighter_import_name[$i]);

        // remove it from list.
      }
    }
  }
}