<?php
// $Id: views_theme_wizard.module,v 1.1 2006/06/25 03:37:53 merlinofchaos Exp $

function views_theme_wizard_help($section = 'admin/help#views_theme_wizard') {
  $output = '';
  switch ($section) {
    case 'admin/modules#description':
      return t('The views theme wizard helps create stub theming for views.');
  }
}

function views_theme_wizard_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/views/wizard',
      'access' => user_access('administer views'),
      'title' => t('theme wizard'),
      'callback' => 'views_theme_wizard_page',
      'type' => MENU_LOCAL_TASK,
    );
  }
  return $items;
}

function views_theme_wizard_page() {
  $result = db_query("SELECT vid, name FROM {view_view} ORDER BY name");
  while ($v = db_fetch_object($result)) {
    $views[$v->vid] = $v->name;
  }

  $form['markup'] = array(
    '#value' => t('<p>The views theming wizard generates code that you can use in your phptemplate theme to help you customize the output of a view. Simply select a theme and it will generate code for your template.php as well as template files for the individual views.</p><p>At this time this code assumes your view is a <b>list type view</b>! It may not generate effective code for other types of views. Future versions of this program will be smarter, and give more options, but this wizard is still a huge start.</p>'),
    '#weight' => 0,
  );

  $form['vid'] = array(
    '#type' => 'select',
    '#options' => $views,
    '#title' => t('Select a view'),
    '#weight' => 5,
  );

  $form['code1'] = array(
    '#type' => 'markup',
    '#value' => '',
  );

  $form['code2'] = array(
    '#type' => 'markup',
    '#value' => '',
  );

  $form['submit'] = array(
    '#type' => 'button',
    '#value' => t('Generate Theme'),
    '#weight' => 10,
  );

  $form['#after_build'] = array(
    'views_theme_wizard_generate',
  );
  return drupal_get_form('views_theme_wizard_form', $form);
}

function views_theme_wizard_generate($form, $form_values) {
  $view = views_get_view($form_values['vid']);
  if (!$view) {
    return $form;
  }

  $form['code1']['#type'] = 'textarea';
  $form['code1']['#value'] = views_theme_wizard_generate_list_code($view);
  $form['code1']['#title'] = t('This code goes in your template.php file');
  $form['code1']['#rows'] = 20;

  $form['code2']['#type'] = 'textarea';
  $form['code2']['#value'] = views_theme_wizard_generate_list_template_code($view);
  $form['code2']['#title'] = t('This code goes in a file named views-list-%s.tpl.php', array('%s' => $view->name));
  $form['code2']['#rows'] = 20;

  return $form;
}

function views_theme_wizard_generate_list_code($view) {
  $now = format_date(time());
  $code = <<<EOT
/**
 * views template to output a view.
 * This code was generated by the views theming wizard
 * Date: $now
 * View: $view->name
 *
 * This function goes in your template.php file
 */
function phptemplate_views_view_list_{$view->name}(\$view, \$nodes, \$type) {
  \$fields = _views_get_fields();

  \$taken = array();

  // Set up the fields in nicely named chunks.
  foreach (\$view->field as \$id => \$field) {
    \$field_name = \$field['field'];
    if (isset(\$taken[\$field_name])) {
      \$field_name = \$field['queryname'];
    }
    \$taken[\$field_name] = true;
    \$field_names[\$id] = \$field_name;
  }

  // Set up some variables that won't change.
  \$base_vars = array(
    'view' => \$view,
    'view_type' => \$type,
  );

  foreach (\$nodes as \$node) {
    \$vars = \$base_vars;
    foreach (\$view->field as \$id => \$field) {
      \$name = \$field_names[\$id];
      \$vars[\$name] = views_theme_field('views_handle_field', \$field['queryname'], \$fields, \$field, \$node);
      if (isset(\$field['label'])) {
        \$vars[\$name . '_label'] = \$field['label'];
      }
    }
    \$items[] = _phptemplate_callback('views-list-{$view->name}', \$vars);
  }
  if (\$items) {
    return theme('item_list', \$items);
  }
}
EOT;
  return $code;
}

/**
 * generate a template file for a list theme
 */
function views_theme_wizard_generate_list_template_code($view) {
  $header = <<<EOT
<?php 
/**
 * views template to output one 'row' of a view.
 * This code was generated by the views theming wizard
 * Date: $now
 * View: $view->name
 *
 * Variables available:
 * \$view -- the entire view object. Important parts of this object are
 *   $view->name, $view->real_url.
 * \$view_type -- The type of the view. Probably 'page' or 'block' but could
 *   also be 'embed' or other string passed in from a custom view creator.

EOT;

  $fields = _views_get_fields();
  $taken = array();

  // Set up the fields in nicely named chunks.
  foreach ($view->field as $id => $field) {
    $field_name = $field['field'];
    if (isset($taken[$field_name])) {
      $field_name = $field['queryname'];
    }
    $taken[$field_name] = true;
    $output .= <<<EOT
<div class="view-label view-field-{$field_name}">
  <?php print \${$field_name}_label ?>
</div>
<div class="view-field view-data-{$field_name}">
  <?php print \${$field_name}?>
</div>
\n
EOT;
    $fid = $view->field[$id]['id'];
    $header .= ' * $' . $field_name . ' -- ' . $fields[$fid]['help'] . "\n";
    $header .= ' * $' . $field_name . '_label -- The assigned label for $' . $field_name . "\n";
  }

  $header .= <<<EOT
 *
 * This function goes in your views-list-{$view->name}.tpl.php file
 */
?>

EOT;


  return $header .$output;
}

