/[drupal]/contributions/modules/devel/devel_generate.module
ViewVC logotype

Contents of /contributions/modules/devel/devel_generate.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.36 - (show annotations) (download) (as text)
Wed Oct 14 14:44:48 2009 UTC (6 weeks, 1 day ago) by weitzman
Branch: MAIN
CVS Tags: HEAD
Changes since 1.35: +11 -2 lines
File MIME type: text/x-php
When generating users, allow admin to specify their roles.
1 <?php
2 // $Id: devel_generate.module,v 1.35 2009/08/29 20:23:44 weitzman Exp $
3
4 /**
5 * Implementation of hook_menu().
6 */
7 function devel_generate_menu() {
8 $items = array();
9
10 $items['admin/config/development/generate/user'] = array(
11 'title' => 'Generate users',
12 'description' => 'Generate a given number of users. Optionally delete current users.',
13 'page callback' => 'drupal_get_form',
14 'page arguments' => array('devel_generate_users_form'),
15 'access arguments' => array('administer users'),
16 );
17 $items['admin/config/development/generate/content'] = array(
18 'title' => 'Generate content',
19 'description' => 'Generate a given number of nodes and comments. Optionally delete current items.',
20 'page callback' => 'drupal_get_form',
21 'page arguments' => array('devel_generate_content_form'),
22 'access arguments' => array('administer nodes'),
23 );
24 if (module_exists('taxonomy')) {
25 $items['admin/config/development/generate/taxonomy'] = array(
26 'title' => 'Generate taxonomy',
27 'description' => 'Generate a given number of vocabularies and terms. Optionally delete current taxonomies.',
28 'page callback' => 'drupal_get_form',
29 'page arguments' => array('devel_generate_taxonomy_form'),
30 'access arguments' => array('administer nodes'),
31 );
32 }
33
34 return $items;
35 }
36
37 function devel_generate_users_form() {
38 $form['num'] = array(
39 '#type' => 'textfield',
40 '#title' => t('How many users would you like to generate?'),
41 '#default_value' => 50,
42 '#size' => 10,
43 );
44 $form['kill_users'] = array(
45 '#type' => 'checkbox',
46 '#title' => t('Delete all users (except user id 1) before generating new users.'),
47 '#default_value' => FALSE,
48 );
49 $options = user_roles(TRUE);
50 unset($options[DRUPAL_AUTHENTICATED_RID]);
51 $form['roles'] = array(
52 '#type' => 'checkboxes',
53 '#title' => t('Which roles should the users receive?'),
54 '#description' => t('Users always receive the <em>authenticated user</em> role.'),
55 '#options' => $options,
56 );
57
58 $options = array(1 => t('Now'));
59 foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
60 $options[$interval] = format_interval($interval, 1) . ' ' . t('ago');
61 }
62 $form['time_range'] = array(
63 '#type' => 'select',
64 '#title' => t('How old should user accounts be?'),
65 '#description' => t('User ages will be distributed randomly from the current time, back to the selected time.'),
66 '#options' => $options,
67 '#default_value' => 604800,
68 );
69
70 $form['submit'] = array(
71 '#type' => 'submit',
72 '#value' => t('Do it!'),
73 );
74 return $form;
75 }
76
77 function devel_generate_users_form_submit($form_id, &$form_state) {
78 module_load_include('inc', 'devel_generate');
79 $values = $form_state['values'];
80 devel_create_users($values['num'], $values['kill_users'], $values['time_range'], $values['roles']);
81 }
82
83 function devel_generate_content_form() {
84 $options = array();
85
86 if (module_exists('content')) {
87 $types = content_types();
88 foreach ($types as $type) {
89 $warn = '';
90 if (count($type['fields'])) {
91 $warn = t('. This type contains CCK fields which will only be populated by fields that implement the content_generate hook.');
92 }
93 $options[$type['type']] = t($type['name']). $warn;
94 }
95 }
96 else {
97 $types = node_type_get_types();
98 foreach ($types as $type) {
99 $options[$type->type] = t($type->name);
100 }
101 }
102 // we cannot currently generate valid polls.
103 unset($options['poll']);
104
105 if (empty($options)) {
106 drupal_set_message(t('You do not have any content types that can be generated. <a href="@create-type">Go create a new content type</a> already!</a>', array('@create-type' => url('admin/structure/types/add'))), 'error', FALSE);
107 return;
108 }
109
110 $form['node_types'] = array(
111 '#type' => 'checkboxes',
112 '#title' => t('Which node types do you want to create?'),
113 '#options' => $options,
114 '#default_value' => array_keys($options),
115 );
116 $form['kill_content'] = array(
117 '#type' => 'checkbox',
118 '#title' => t('<strong>Delete all content</strong> in these node types before generating new content.'),
119 '#default_value' => FALSE,
120 );
121 $form['num_nodes'] = array(
122 '#type' => 'textfield',
123 '#title' => t('How many nodes would you like to generate?'),
124 '#default_value' => 50,
125 '#size' => 10,
126 );
127
128 $options = array(1 => t('Now'));
129 foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
130 $options[$interval] = format_interval($interval, 1) . ' ' . t('ago');
131 }
132 $form['time_range'] = array(
133 '#type' => 'select',
134 '#title' => t('How far back in time should the nodes be dated?'),
135 '#description' => t('Node creation dates will be distributed randomly from the current time, back to the selected time.'),
136 '#options' => $options,
137 '#default_value' => 604800,
138 );
139
140 $form['num_comments'] = array(
141 '#type' => module_exists('comment') ? 'textfield' : 'value',
142 '#title' => t('How many comments per node would you like to generate?'),
143 '#description' => t('You must also enable comments for the node types you are generating.'),
144 '#default_value' => 0,
145 '#size' => 3,
146 '#access' => module_exists('comment'),
147 );
148 $form['title_length'] = array(
149 '#type' => 'textfield',
150 '#title' => t('Max word length of titles'),
151 '#default_value' => 4,
152 '#size' => 10,
153 );
154 $form['add_upload'] = array(
155 '#type' => 'checkbox',
156 '#disabled' => !module_exists('upload'),
157 '#description' => t('Requires upload.module'),
158 '#title' => t('Add an upload to each node'),
159 '#default_value' => FALSE,
160 );
161 $form['add_terms'] = array(
162 '#disabled' => !module_exists('taxonomy'),
163 '#description' => t('Requires taxonomy.module'),
164 '#type' => 'checkbox',
165 '#title' => t('Add taxonomy terms to each node.'),
166 '#default_value' => FALSE,
167 );
168 $form['add_alias'] = array(
169 '#type' => 'checkbox',
170 '#disabled' => !module_exists('path'),
171 '#description' => t('Requires path.module'),
172 '#title' => t('Add an url alias for each node.'),
173 '#default_value' => FALSE,
174 );
175 $form['add_statistics'] = array(
176 '#type' => 'checkbox',
177 '#title' => t('Add statistics for each node (node_counter table).'),
178 '#default_value' => TRUE,
179 '#access' => module_exists('statistics'),
180 );
181
182 $form['submit'] = array(
183 '#type' => 'submit',
184 '#value' => t('Do it!'),
185 );
186 $form['#redirect'] = FALSE;
187
188 return $form;
189 }
190
191 function devel_generate_content_form_submit($form_id, &$form_state) {
192 module_load_include('inc', 'devel_generate', 'devel_generate');
193 $form_state['values']['node_types'] = array_filter($form_state['values']['node_types']);
194 if ($form_state['values']['num_nodes'] <= 50 && $form_state['values']['num_comments'] <= 10) {
195 module_load_include('inc', 'devel_generate');
196 devel_generate_content($form_state);
197 }
198 else {
199 module_load_include('inc', 'devel_generate', 'devel_generate_batch');
200 devel_generate_batch_content($form_state);
201 }
202 }
203
204 function devel_generate_taxonomy_form() {
205 $form['num_vocab'] = array(
206 '#type' => 'textfield',
207 '#title' => t('How many vocabularies would you like to generate?'),
208 '#default_value' => 3,
209 '#size' => 10,
210 );
211 $form['num_terms'] = array(
212 '#type' => 'textfield',
213 '#title' => t('How many terms would you like to generate?'),
214 '#default_value' => 50,
215 '#size' => 10,
216 );
217 $form['title_length'] = array(
218 '#type' => 'textfield',
219 '#title' => t('Max word length of term/vocab names'),
220 '#default_value' => 12,
221 '#size' => 10,
222 );
223 $form['kill_taxonomy'] = array(
224 '#type' => 'checkbox',
225 '#title' => t('Delete existing terms and vocabularies before generating new content.'),
226 '#default_value' => FALSE,
227 );
228 $form['submit'] = array(
229 '#type' => 'submit',
230 '#value' => t('Do it!'),
231 );
232 return $form;
233 }
234
235 function devel_generate_taxonomy_form_submit($form_id, &$form_state) {
236 module_load_include('inc', 'devel_generate');
237 devel_generate_taxonomy_data($form_state['values']['num_vocab'], $form_state['values']['num_terms'], $form_state['values']['title_length'], $form_state['values']['kill_taxonomy']);
238 }
239
240 // Modules that want to affect generated nodes may implement hook_node('presave'). See OG module or CCK.
241 // A few implementations live here because core doesn't do bulk node generation.
242 function devel_generate_node_presave(&$node) {
243 if (isset($node->devel_generate)) {
244 $results = $node->devel_generate;
245 if (!empty($results['add_upload'])) {
246 devel_generate_add_upload($node);
247 }
248 if (!empty($results['add_terms'])) {
249 devel_generate_add_terms($node);
250 }
251 }
252 }
253
254 function devel_generate_node_insert(&$node) {
255 if (isset($node->devel_generate)) {
256 $results = $node->devel_generate;
257
258 if (!empty($results['num_comments'])) {
259 devel_generate_add_comments($node, $results['users'], $results['num_comments'], $results['title_length']);
260 }
261
262
263 // Add an url alias. Cannot happen before save because we don't know the nid.
264 if (!empty($results['add_alias'])) {
265 path_set_alias("node/$node->nid", "node-$node->nid-$node->type");
266 }
267
268 // Add node statistics.
269 if (!empty($results['add_statistics']) && module_exists('statistics')) {
270 devel_generate_add_statistics($node);
271 }
272 }
273 }
274

  ViewVC Help
Powered by ViewVC 1.1.2