You are here

function zenophile_create_submit in Zenophile 6

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

Submitor (?) for zenophile_create().

File

./zenophile.module, line 122

Code

function zenophile_create_submit($form, &$form_state) {

  // Does the theme directory exist already for this site?
  $t_dir = "sites/{$form_state['values']['site']}/themes";
  if (!file_exists($t_dir) && !mkdir($t_dir, 0755)) {
    form_set_error(NULL, t('The <em>themes</em> directory for the %site site directory does not exist, and it could not be created automatically. Please create the directory %dir manually and try again.', array(
      '%site' => $site,
      '%dir' => $t_dir,
    )), 'error');
  }
  else {
    $dir = "{$t_dir}/{$form_state['values']['sysname']}";

    // Make the theme directory
    if (file_exists($dir)) {

      // This theoretically should have been caught by the validate function
      // above, but it's possible that there's a directory in this site's
      // themes directory which is not a proper theme… or it's a regular file.
      form_set_error(NULL, t('The subtheme directory %dir could not be created because a file or directory with that name already exists.', array(
        '%dir' => $dir,
      )));
    }
    else {
      if (mkdir($dir)) {

        // Copy over everyting in the STARTERKIT directory except STARTERKIT.info
        // (which we'll get to in just a bit), and template.php and
        // theme-settings.php (which we'll get to in step 6). This is essentially
        // Step 1 of the "by-hand" Zen subtheme creation directions.
        $parent_dir = drupal_get_path('theme', $form_state['values']['parent']);
        $zen_dir = drupal_get_path('theme', 'zen');
        $h = opendir($parent_dir);
        $parent_info = $form_state['values']['parent'] . '.info';
        while (($file = readdir($h)) !== FALSE) {
          $fpath = "{$parent_dir}/{$file}";
          if (is_file($fpath) && $file[0] !== '.' && $file !== $parent_info && $file !== 'template.php' && $file !== 'theme-settings.php') {
            copy($fpath, "{$dir}/{$file}");
          }
        }
        $path_part = "{$dir}/{$form_state['values']['sysname']}";

        // Now take care of STARTERKIT.info. Step 2.
        // Load the info file into a string.
        $info = file_get_contents("{$parent_dir}/{$parent_info}");

        // Reset the $Id$ string
        $info = preg_replace('/^; \\$Id.*\\$$/m', '; $Id$', $info, 1);

        // Build replacement arrays. We definitely want to replace the name and
        // description.
        $from = array(
          "/{$form_state['values']['parent']}/",
          '/^name\\s*=.*/m',
          '/^description\\s*=.*/m',
        );
        $to = array(
          $form_state['values']['sysname'],
          'name        = ' . ($form_state['values']['friendly'] === '' ? $form_state['values']['sysname'] : $form_state['values']['friendly']),
          'description = ' . $form_state['values']['description'],
        );

        // Do we also want to add the fresh stylesheet?
        if ($form_state['values']['fresh']) {
          $from[] = '/^stylesheets\\[all\\]\\[\\]\\s*=\\s*zen\\.css$/m';
          $to[] = "stylesheets[all][]   = zen.css\n\n  ; Specifying a nice clean stylesheet\nstylesheets[all][] = {$form_state['values']['sysname']}-fresh.css";

          // Make the blank stylesheet file
          touch($path_part . '-fresh.css');
        }

        // Do replacement and write the info file
        $info = preg_replace($from, $to, $info);
        file_put_contents($path_part . '.info', $info);

        // Copy the liquid or fixed stylesheet, the print stylesheet, and the
        // Zen stylesheet. Steps 3 through 5. Only do this if the parent is
        // STARTERKIT - otherwise these will have already been copied.
        if ($form_state['values']['parent'] === 'STARTERKIT') {
          copy("{$zen_dir}/layout-{$form_state['values']['layout']}.css", "{$dir}/layout.css");
          copy($zen_dir . '/print.css', $dir . '/print.css');

          // If there is a starter_theme.css file in the directory already,
          // rename it to this_theme.css. Otherwise, copy over zen.css and
          // rename it.
          $parent_css = "{$dir}/{$form_state['values']['parent']}.css";
          if (file_exists($parent_css)) {
            rename($parent_css, "{$dir}/{$form_state['values']['sysname']}.css");
          }
          else {
            copy($zen_dir . '/zen.css', "{$dir}/{$form_state['values']['sysname']}.css");
          }
        }

        // Copy template.php and theme-settings.php and replace STARTERKIT.
        // Kind of Step 1 plus Step 6 mixed together.
        $info = file_get_contents($parent_dir . '/template.php');
        $info = str_replace($form_state['values']['parent'], $form_state['values']['sysname'], $info);
        file_put_contents($dir . '/template.php', $info);
        $info = file_get_contents($parent_dir . '/theme-settings.php');
        $info = str_replace($form_state['values']['parent'], $form_state['values']['sysname'], $info);
        file_put_contents($dir . '/theme-settings.php', $info);
        drupal_set_message(t('A new subtheme was created in %dir.', array(
          '%dir' => $dir,
        )));

        // Flush the cached theme data so the new subtheme appears in the parent
        // theme list
        system_theme_data();
      }
      else {
        drupal_set_message(t('An error occurred while trying to create the subtheme directory %dir.', array(
          '%dir' => $dir,
        )));
      }
    }
  }
}