summaryrefslogtreecommitdiffstats
path: root/devel_generate.module
blob: 75fa1c5f548cd58fea8ac55be371be55810d6bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
// $Id$

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

  // Admin user pages
  $items['admin/generate'] = array(
    'title' => 'Generate items',
    'description' => "Populate your database with dummy items.",
    'position' => 'left',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array('administer site configuration'),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );
  $items['admin/generate/user'] = array(
    'title' => t('Generate users'),
    'description' => t('Generate a given number of users. Optionally delete current users.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('devel_generate_users_form'),
    'access arguments' => array('administer users')
  );
  $items['admin/generate/content'] = array(
    'title' => t('Generate content'),
    'description' => t('Generate a given number of nodes and comments. Optionally delete current items.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('devel_generate_content_form'),
    'access arguments' => array('administer nodes')
  );
  if (module_exists('taxonomy')) {
    $items['admin/generate/taxonomy'] = array(
      'title' => t('Generate categories'),
      'description' => t('Generate a given number of vocabularies and terms. Optionally delete current categories.'),
      'page callback' => 'drupal_get_form',
      'page arguments' => array('devel_generate_taxonomy_form'),
      'access arguments' => array('administer nodes'),
    );
  }

  return $items;
}

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 (except user id 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_state) {
  require_once('devel_generate.inc');
  devel_create_users($form_state['values']['num'], $form_state['values']['kill_users']);
}

function devel_generate_content_form() {
  if (module_exists('content')) {
    $types = content_types();
    foreach ($types as $type) {
      $warn = '';
      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;
    }
  }
  else {
    $types = node_get_types();
    foreach ($types as $type) {
      $options[$type->type] = t($type->name);
    }
  }
  $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 all content</strong> in these node types 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' => module_exists('comment') ? 'textfield' : 'value',
    '#title' => t('How many comments per node would you like to generate?'),
    '#description' => t('You must also enable comments for the node types you are generating.'),
    '#default_value' => 0,
    '#size' => 3,
    '#access' => module_exists('comment'),
  );
  $form['title_length'] = array(
    '#type' => 'textfield',
    '#title' => t('Max word length of titles'),
    '#default_value' => 8,
    '#size' => 10,
  );
  $form['add_upload'] = array(
    '#type' => module_exists('upload') ? 'checkbox' : 'value',
    '#title' => t('Add an upload to each generated node'),
    '#default_value' => FALSE,
  );
  $form['add_terms'] = array(
    '#type' => module_exists('taxonomy') ? 'checkbox' : 'value',
    '#title' => t('Add taxonomy terms to each node.'),
    '#default_value' => FALSE,
    '#access' => module_exists('taxonomy'),
  );
  // TODO: restore this. see devel_create_content()
  $form['add_alias'] = array(
    '#type' => 'value',
    '#title' => t('Add an url alias for each node.'),
    '#default_value' => FALSE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Do it!'),
  );

  return $form;
}

function devel_generate_content_form_submit($form_id, &$form_state) {
  require_once('devel_generate.inc');
  devel_generate_content($form_state['values']['num_nodes'], $form_state['values']['num_comments'], $form_state['values']['title_length'], $form_state['values']['kill_content'], $form_state['values']['add_upload'], $form_state['values']['add_terms'], $form_state['values']['add_alias'], array_filter($form_state['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_state) {
  require_once('devel_generate.inc');
  devel_generate_taxonomy_data($form_state['values']['num_vocab'], $form_state['values']['num_terms'], $form_state['values']['title_length'], $form_state['values']['kill_taxonomy']);
}