You are here

function local_fonts_add_form_validate in @font-your-face 7.2

Same name and namespace in other branches
  1. 7 modules/local_fonts/local_fonts.module \local_fonts_add_form_validate()

Implements hook_form_validate().

File

modules/local_fonts/local_fonts.module, line 178

Code

function local_fonts_add_form_validate($form, &$form_state) {

  // Ensure a font family with the same results does not exist in the database.
  $values = $form_state['values'];
  $font_path = strtr(preg_replace('#[^a-zA-Z0-9]+#', ' ', $values['css_family']), ' ', '_') . '-' . $values['css_style'] . '-' . $values['css_weight'];
  $font_style = $values['css_style'];
  $font_weight = $values['css_weight'];
  $local_fonts_directory = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . file_stream_wrapper_get_instance_by_scheme('public')
    ->getDirectoryPath() . '/fontyourface/local_fonts';
  $destination_directory = $local_fonts_directory . '/' . $font_path;
  $file_uploaded = FALSE;
  if (file_prepare_directory($local_fonts_directory, FILE_CREATE_DIRECTORY) && file_prepare_directory($destination_directory, FILE_CREATE_DIRECTORY)) {

    // Validate the files against their extension
    $files = $_FILES['files']['name'];
    foreach ($files as $key => $filename) {
      $file_object = new stdClass();
      $file_object->filename = $filename;
      if (!empty($file_object->filename)) {
        $validation = file_validate_extensions($file_object, $key);
        if (is_array($validation) && count($validation) > 0) {
          form_set_error($key, implode(', ', $validation));
        }
        else {
          $file_uploaded = TRUE;
        }

        // else
      }

      // if
    }

    // foreach
  }
  else {
    form_set_error('files', t('Error creating file directory.'));
  }

  // else
  if (!$file_uploaded) {
    form_set_error('files', t('Upload at least one font file to go with the font family.'));
  }

  // if
}