You are here

function zenophile_create_validate in Zenophile 6.2

Same name and namespace in other branches
  1. 6 zenophile.module \zenophile_create_validate()
  2. 7 zenophile.module \zenophile_create_validate()

Validate function for zenophile_create().

File

./zenophile.module, line 200
Creates Zen subthemes quickly and easily.

Code

function zenophile_create_validate($form, &$form_state) {

  // Check that the system name of the theme is valid
  if (in_array($form_state['values']['sysname'], array(
    'layout',
    'print',
    'sidebars',
  ))) {

    // drupal6-reference and html-elements should also be excluded, but the
    // preg_match() regex below will catch those since they have hyphens.
    form_set_error('sysname', t('That <em>System name</em> value cannot be used. Zenophile will need to create %sysname.css to continue, but that filename is reserved for another important Zen CSS file. Please choose a different <em>System name</em> value.', array(
      '@sysname' => $form_state['values']['sysname'],
    )));
  }
  elseif ($exists = drupal_get_path('theme', $form_state['values']['sysname'])) {
    form_set_error('sysname', t('A theme with this <em>System name</em> already exists at %exists. Please chose a different one.', array(
      '%exists' => $exists,
    )));
  }
  elseif (!preg_match('/^[abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz0-9_]*$/', $form_state['values']['sysname'])) {

    // Zen's documentations say that no digits should be used in theme system
    // names, but that restriction seems to be arbitrary - in actuality, digits
    // can be anywhere except first character (because function names will be
    // named with the theme name as a prefix, and function names cannot begin
    // with a digit in PHP). So even though the form element #description says
    // digits can't be used, we're actually going to allow them so long as
    // they're not in the first character. See this issue:
    // http://drupal.org/node/606574
    // As for why the pattern above doesn't use [a-z], see:
    // http://stackoverflow.com/questions/1930487/will-a-z-ever-match-accented-characters-in-preg-pcre
    form_set_error('sysname', t('The <em>System name</em> may only consist of lowercase letters and the underscore character.'));
  }
  elseif (count(form_get_errors()) === 0) {

    // We only want to continue if all required form elements were filled out -
    // http://drupal.org/node/631002
    // Test if we can make these directories. It's pretty dumb to be actually
    // modifying the disk in a validate hook, but I don't know of any better way
    // to test if a directory can be made than going ahead and trying to make
    // it, and I think crashing out with an error in the submit hook is worse,
    // because it won't take the user back to the form with the previous values
    // already filled in, among other reasons.
    $site_dir = 'sites/' . $form_state['values']['site'];
    $themes_dir = $site_dir . '/themes';
    if (!file_exists($themes_dir) && !mkdir($themes_dir, 0755)) {
      form_set_error('site', t('The <em>themes</em> directory for the %site site directory does not exist, and it could not be created automatically. This is likely a permissions problem. Check that the web server has permissions to write to the %site directory, or create the %themes directory manually and try again.', array(
        '%site' => $site_dir,
        '%themes' => $themes_dir,
      )), 'error');
    }
    else {
      $dir = "{$themes_dir}/{$form_state['values']['sysname']}";
      if (file_exists($dir)) {
        form_set_error('sysname', t('That <em>System name</em> value cannot be used with that <em>Site directory</em> value. Zenophile wants to create and use the directory %dir, but a file or directory with that name already exists.', array(
          '%dir' => $dir,
        )));
      }
      elseif (ZENOPHILE_DEBUG) {
        drupal_set_message(t('Zenophile is in DEBUG mode. Despite what it may say below, your theme will not actually be created. Set ZENOPHILE_DEBUG to FALSE in zenophile.module to take Zenophile out of debug mode.'), 'error');
      }
      else {

        /*         $chmod = intval($form_state['values']['chmod']); */
        if (!mkdir($dir)) {
          form_set_error('sysname', t('The directory %dir could not be created. This is likely a permissions problem. Check that the web server has permissions to write to the %themes directory.', array(
            '%dir' => $dir,
            '%themes' => $themes_dir,
          )));
        }
      }
    }
  }
}