<?php
// $Id: devel_generate.module,v 1.1.2.7 2008/01/15 04:06:56 weitzman Exp $

/**
 * Implementation of hook_menu().
 */
function devel_generate_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/user/generate',
      'title' => t('Generate users'),
      'callback' => 'drupal_get_form',
      'description' => t('Generate a given number of users. Optionally delete current users.'),
      'callback arguments' => array('devel_generate_users_form'),
      'access' => user_access('administer users'),
      'type' => MENU_NORMAL_ITEM
    );
    $items[] = array(
      'path' => 'admin/content/generate',
      'title' => t('Generate content'),
      'description' => t('Generate a given number of nodes and comments. Optionally delete current items.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('devel_generate_content_form'),
      'access' => user_access('administer nodes'),
      'type' => MENU_NORMAL_ITEM
    );
    $items[] = array(
      'path' => 'admin/content/generate_taxonomy',
      'title' => t('Generate categories'),
      'description' => t('Generate a given number of vocabularies and terms. Optionally delete current categories.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('devel_generate_taxonomy_form'),
      'access' => user_access('administer nodes'),
      'type' => MENU_NORMAL_ITEM
    );
  }

  return $items;
}


/**
 * Overview page for admin/generate page
 */
function devel_generate_overview() {

  $menu = menu_get_item(NULL, 'admin/generate');
  $content = system_admin_menu_block($menu);

  $output = theme('admin_block_content', $content);

  return $output;
}

function devel_generate_users_form() {
  $form['num'] = array(
    '#type' => 'textfield',
    '#title' => t('How many users would you like to generate?'),
    '#default_value' => 50,
    '#size' => 10,
  );
  $form['kill_users'] = array(
    '#type' => 'checkbox',
    '#title' => t('Delete all users but user 1 before generating new users.'),
    '#default_value' => FALSE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Do it!'),
  );
  return $form;
}

function devel_generate_users_form_submit($form_id, $form_values) {
  require_once('devel_generate.inc');
  devel_create_users($form_values['num'], $form_values['kill_users']);
}

function devel_generate_content_form() {
  if (module_exists('content')) {
    $types = content_types();
    $warn = '';
    foreach ($types as $type) {
      if (count($type['fields'])) {
        $warn = t('. <strong>Warning</strong>. This type contains CCK fields which won\'t be populated. See !url', array('!url' => l(t('this CCK issue.'), 'http://drupal.org/node/187599')));
      }
      $options[$type['type']] = t($type['name']). $warn;
      unset($warn);
    }
    
  }
  else {
    $types = node_get_types();
    foreach ($types as $type) {
      $options[$type->type] = t($type->name);
    }
  }
  // we cannot currently generate valid polls.
  unset($options['poll']);
  $form['node_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Which node types do you want to create?'),
    '#options' => $options,
    '#default_value' => array_keys($options),
  );
  $form['kill_content'] = array(
    '#type' => 'checkbox',
    '#title' => t('<strong>Delete existing content</strong> before generating new content.'),
    '#default_value' => FALSE,
  );
  
  $form['num_nodes'] = array(
    '#type' => 'textfield',
    '#title' => t('How many nodes would you like to generate?'),
    '#default_value' => 50,
    '#size' => 10,
  );
  $form['num_comments'] = array(
    '#type' => 'textfield',
    '#title' => t('How many comments would you like to generate?'),
    '#default_value' => 0,
    '#size' => 10,
  );
  $form['title_length'] = array(
    '#type' => 'textfield',
    '#title' => t('Max word length of titles'),
    '#default_value' => 8,
    '#size' => 10,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Do it!'),
  );
  return $form;
}

function devel_generate_content_form_submit($form_id, $form_values) {
  require_once('devel_generate.inc');
  devel_generate_content($form_values['num_nodes'], $form_values['num_comments'], $form_values['title_length'], $form_values['kill_content'], array_filter($form_values['node_types']));
}

function devel_generate_taxonomy_form() {
  $form['num_vocab'] = array(
    '#type' => 'textfield',
    '#title' => t('How many vocabularies would you like to generate?'),
    '#default_value' => 3,
    '#size' => 10,
  );
  $form['num_terms'] = array(
    '#type' => 'textfield',
    '#title' => t('How many terms would you like to generate?'),
    '#default_value' => 50,
    '#size' => 10,
  );
  $form['title_length'] = array(
    '#type' => 'textfield',
    '#title' => t('Max word length of term/vocab names'),
    '#default_value' => 12,
    '#size' => 10,
  );
  $form['kill_taxonomy'] = array(
    '#type' => 'checkbox',
    '#title' => t('Delete existing terms and vocabularies before generating new content.'),
    '#default_value' => FALSE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Do it!'),
  );
  return $form;
}

function devel_generate_taxonomy_form_submit($form_id, $form_values) {
  require_once('devel_generate.inc');
  devel_generate_taxonomy_data($form_values['num_vocab'], $form_values['num_terms'], $form_values['title_length'], $form_values['kill_taxonomy']);
}