1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* The Date Wizard code.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_form().
|
10
|
*
|
11
|
* @todo.
|
12
|
*/
|
13
|
function date_tools_wizard_form() {
|
14
|
$form = array();
|
15
|
$form['type'] = array(
|
16
|
'#type' => 'fieldset',
|
17
|
'#title' => t('Content type'),
|
18
|
);
|
19
|
$form['type']['bundle'] = array(
|
20
|
'#type' => 'textfield',
|
21
|
'#default_value' => 'date',
|
22
|
'#title' => t('Content type name'),
|
23
|
'#description' => t('Machine-readable name. Allowed values: (a-z, 0-9, _). If this is not an existing content type, the content type will be created.'),
|
24
|
);
|
25
|
$form['type']['name'] = array(
|
26
|
'#type' => 'textfield',
|
27
|
'#default_value' => t('Date'),
|
28
|
'#title' => t('Content type label'),
|
29
|
'#description' => t('The human-readable name for this content type. Only needed when creating a new content type.'),
|
30
|
);
|
31
|
$form['type']['type_description'] = array(
|
32
|
'#type' => 'textarea',
|
33
|
'#default_value' => t('A date content type that is linked to a Views calendar.'),
|
34
|
'#title' => t('Content type description'),
|
35
|
'#description' => t('A description for the content type. Only needed when creating a new content type.'),
|
36
|
);
|
37
|
$form['field'] = array(
|
38
|
'#type' => 'fieldset',
|
39
|
'#title' => t('Date field'),
|
40
|
);
|
41
|
$form['field']['field_name'] = array(
|
42
|
'#type' => 'textfield',
|
43
|
'#default_value' => 'date',
|
44
|
'#field_prefix' => 'field_',
|
45
|
'#title' => t('Date field name'),
|
46
|
'#description' => t('Machine-readable name. Allowed values: (a-z, 0-9, _) Must not be an existing field name.'),
|
47
|
);
|
48
|
$form['field']['label'] = array(
|
49
|
'#tree' => TRUE,
|
50
|
'#type' => 'textfield',
|
51
|
'#default_value' => t('Date'),
|
52
|
'#title' => t('Date field label'),
|
53
|
'#description' => t('The human-readable label for this field.'),
|
54
|
);
|
55
|
$form['field']['widget_type'] = array(
|
56
|
'#type' => 'select',
|
57
|
'#options' => date_tools_wizard_widget_types(),
|
58
|
'#default_value' => 'date_select',
|
59
|
'#title' => t('Date widget type'),
|
60
|
);
|
61
|
$form['field']['repeat'] = array(
|
62
|
'#type' => 'select',
|
63
|
'#default_value' => 0,
|
64
|
'#options' => array(
|
65
|
0 => t('No'),
|
66
|
1 => t('Yes'),
|
67
|
),
|
68
|
'#title' => t('Show repeating date options'),
|
69
|
'#access' => module_exists('date_repeat_field'),
|
70
|
);
|
71
|
$form['field']['advanced'] = array(
|
72
|
'#type' => 'fieldset',
|
73
|
'#collapsible' => TRUE,
|
74
|
'#collapsed' => TRUE,
|
75
|
'#title' => t('Advanced options'),
|
76
|
);
|
77
|
$form['field']['advanced']['todate'] = array(
|
78
|
'#type' => 'select',
|
79
|
'#default_value' => 'optional',
|
80
|
'#options' => array(
|
81
|
'' => t('Never'),
|
82
|
'optional' => t('Optional'),
|
83
|
'required' => t('Required'),
|
84
|
),
|
85
|
'#title' => t('End Date'),
|
86
|
'#description' => t("Display a matching second date field as a 'End date'."),
|
87
|
);
|
88
|
$form['field']['advanced']['field_type'] = array(
|
89
|
'#type' => 'select',
|
90
|
'#options' => date_tools_wizard_field_types(),
|
91
|
'#default_value' => 'datetime',
|
92
|
'#title' => t('Date field type'),
|
93
|
'#description' => t("The recommend type is Datetime, except for historical dates or dates with only year or month granularity. Older or incomplete dates should use the Date type (an ISO date)."),
|
94
|
);
|
95
|
$form['field']['advanced']['granularity'] = array(
|
96
|
'#type' => 'select',
|
97
|
'#options' => date_granularity_names(),
|
98
|
'#default_value' => array('month', 'day', 'year', 'hour', 'minute'),
|
99
|
'#title' => t('Granularity'),
|
100
|
'#multiple' => TRUE,
|
101
|
);
|
102
|
$form['field']['advanced']['year_range'] = array(
|
103
|
'#type' => 'textfield',
|
104
|
'#default_value' => '-1:+1',
|
105
|
'#title' => t('Year range'),
|
106
|
'#description' => t("Range of allowed years, oldest to newest. '-1:+1 means oldest date is one year back, newest is one year forward from current year."),
|
107
|
);
|
108
|
$form['field']['advanced']['tz_handling'] = array(
|
109
|
'#type' => 'select',
|
110
|
'#options' => date_tools_wizard_tz_handling(),
|
111
|
'#default_value' => 'site',
|
112
|
'#title' => t('Date timezone handling'),
|
113
|
'#description' => t("Timezone handling should be set to 'none' for granularity without time elements."),
|
114
|
);
|
115
|
$form['calendar'] = array(
|
116
|
'#type' => 'select',
|
117
|
'#default_value' => module_exists('calendar'),
|
118
|
'#options' => array(
|
119
|
0 => t('No'),
|
120
|
1 => t('Yes'),
|
121
|
),
|
122
|
'#title' => t('Create a calendar for this date field'),
|
123
|
'#access' => module_exists('calendar'),
|
124
|
);
|
125
|
|
126
|
$form['submit'] = array(
|
127
|
'#type' => 'submit',
|
128
|
'#value' => t('Save'),
|
129
|
);
|
130
|
return $form;
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Form validate.
|
135
|
*
|
136
|
* @todo.
|
137
|
*/
|
138
|
function date_tools_wizard_form_validate(&$form, &$form_state) {
|
139
|
$bundle = $form_state['values']['bundle'];
|
140
|
$field_name = 'field_' . $form_state['values']['field_name'];
|
141
|
|
142
|
$args = array(
|
143
|
':field_name' => $field_name,
|
144
|
':bundle' => $bundle,
|
145
|
':entity_type' => 'node',
|
146
|
);
|
147
|
|
148
|
$query = "SELECT type FROM {node_type} WHERE type=:bundle";
|
149
|
$existing_type = db_query($query, array(':bundle' => $args[':bundle']))->fetchField();
|
150
|
|
151
|
$query = "SELECT field_name FROM {field_config_instance} WHERE field_name=:field_name AND bundle=:bundle AND entity_type=:entity_type";
|
152
|
$existing_instance = db_query($query, $args)->fetchField();
|
153
|
|
154
|
if ($existing_type) {
|
155
|
drupal_set_message(t('This content type name already exists, adding new field to existing content type.'));
|
156
|
}
|
157
|
if (!preg_match('!^[a-z0-9_]+$!', $bundle)) {
|
158
|
form_set_error('bundle', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
|
159
|
}
|
160
|
if (!empty($form_state['values']['calendar']) && !empty($form_state['values']['blocks']) && strlen($bundle) > 12) {
|
161
|
form_set_error('bundle', t('The content type name must be no more than 12 characters long when using it to create a calendar and blocks.'));
|
162
|
}
|
163
|
if ($existing_instance) {
|
164
|
form_set_error('field_name', t('This field name already exists.'));
|
165
|
}
|
166
|
if (strlen($field_name) > 32) {
|
167
|
form_set_error('field_name', t('The field name must be no more than 26 characters long.'));
|
168
|
}
|
169
|
if (!date_has_time($form_state['values']['granularity']) && $form_state['values']['tz_handling'] != 'none') {
|
170
|
form_set_error('tz_handling', t('Timezone handling must be none for granularity without time.'));
|
171
|
}
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Form submit.
|
176
|
*
|
177
|
* @todo.
|
178
|
*/
|
179
|
function date_tools_wizard_form_submit(&$form, &$form_state) {
|
180
|
$view_name = date_tools_wizard_build($form_state['values']);
|
181
|
menu_rebuild();
|
182
|
if (!empty($form_state['values']['calendar']) && !empty($view_name)) {
|
183
|
$form_state['redirect'] = 'admin/structure/views/template/' . $view_name . '/add';
|
184
|
}
|
185
|
else {
|
186
|
$form_state['redirect'] = 'admin/structure/types/manage/' . str_replace('_', '-', $form_state['values']['bundle']) . '/fields';
|
187
|
}
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* Wizard build.
|
192
|
*
|
193
|
* @todo.
|
194
|
*/
|
195
|
function date_tools_wizard_build($form_values) {
|
196
|
extract($form_values);
|
197
|
|
198
|
$field_name = 'field_' . $field_name;
|
199
|
$base_table = 'node';
|
200
|
|
201
|
// Create a node type if it doesn't already exist.
|
202
|
// This makes it possible to add additional date fields to
|
203
|
// an existing type.
|
204
|
$types = node_type_get_names();
|
205
|
$type_settings = array();
|
206
|
if (!array_key_exists($bundle, $types)) {
|
207
|
date_tools_wizard_create_content_type($name, $bundle, $type_description, $type_settings);
|
208
|
|
209
|
drupal_set_message(t('Your content type @name has been created.', array('@name' => $name)));
|
210
|
}
|
211
|
else {
|
212
|
$types = node_type_get_types();
|
213
|
$type = $types[$bundle];
|
214
|
if (!empty($type_settings)) {
|
215
|
foreach ($type_settings as $key => $setting) {
|
216
|
$type->$key = $setting;
|
217
|
}
|
218
|
node_type_save($type);
|
219
|
}
|
220
|
$name = $type->name;
|
221
|
}
|
222
|
|
223
|
$field = array(
|
224
|
'field_name' => $field_name,
|
225
|
'type' => $field_type,
|
226
|
'cardinality' => $repeat ? FIELD_CARDINALITY_UNLIMITED : 1,
|
227
|
'settings' => array(
|
228
|
'granularity' => $granularity,
|
229
|
'tz_handling' => $tz_handling,
|
230
|
'timezone_db' => date_get_timezone_db($tz_handling),
|
231
|
'repeat' => $repeat,
|
232
|
'todate' => !empty($todate) ? $todate : 'optional',
|
233
|
),
|
234
|
);
|
235
|
$instance = array(
|
236
|
'entity_type' => 'node',
|
237
|
'field_name' => $field_name,
|
238
|
'label' => $label,
|
239
|
'bundle' => $bundle,
|
240
|
// Move the date right below the title.
|
241
|
'weight' => -4,
|
242
|
'widget' => array(
|
243
|
'type' => $widget_type,
|
244
|
// Increment for minutes and seconds, can be 1, 5, 10, 15, or 30.
|
245
|
'settings' => array(
|
246
|
'increment' => 15,
|
247
|
// The number of years to go back and forward in drop-down year
|
248
|
// selectors.
|
249
|
'year_range' => !empty($year_range) ? $year_range : '-0:+1',
|
250
|
'input_format' => date_default_format($widget_type),
|
251
|
'text_parts' => array(),
|
252
|
'label_position' => 'above',
|
253
|
'repeat_collapsed' => 0,
|
254
|
),
|
255
|
'weight' => -4,
|
256
|
),
|
257
|
'settings' => array(
|
258
|
'default_value' => 'now',
|
259
|
'default_value2' => 'blank',
|
260
|
),
|
261
|
);
|
262
|
|
263
|
$instance['display'] = array(
|
264
|
'default' => array(
|
265
|
'label' => 'above',
|
266
|
'type' => 'date_default',
|
267
|
'settings' => array(
|
268
|
'format_type' => 'long',
|
269
|
'show_repeat_rule' => 'show',
|
270
|
'multiple_number' => '',
|
271
|
'multiple_from' => '',
|
272
|
'multiple_to' => '',
|
273
|
'fromto' => 'both',
|
274
|
),
|
275
|
'module' => 'date',
|
276
|
'weight' => 0 ,
|
277
|
),
|
278
|
'teaser' => array(
|
279
|
'label' => 'above',
|
280
|
'type' => 'date_default',
|
281
|
'weight' => 0,
|
282
|
'settings' => array(
|
283
|
'format_type' => 'long',
|
284
|
'show_repeat_rule' => 'show',
|
285
|
'multiple_number' => '',
|
286
|
'multiple_from' => '',
|
287
|
'multiple_to' => '',
|
288
|
'fromto' => 'both',
|
289
|
),
|
290
|
'module' => 'date',
|
291
|
),
|
292
|
);
|
293
|
|
294
|
$field = field_create_field($field);
|
295
|
$instance = field_create_instance($instance);
|
296
|
$view_name = 'calendar_node_' . $field_name;
|
297
|
|
298
|
field_info_cache_clear(TRUE);
|
299
|
field_cache_clear(TRUE);
|
300
|
|
301
|
drupal_set_message(t('Your date field @name has been created.', array('@name' => $label)));
|
302
|
|
303
|
return $view_name;
|
304
|
}
|
305
|
|
306
|
/**
|
307
|
* Includes handler.
|
308
|
*
|
309
|
* @todo.
|
310
|
*/
|
311
|
function date_tools_wizard_include() {
|
312
|
module_load_include('inc', 'node', 'content_types');
|
313
|
module_load_include('inc', 'node', 'node.pages');
|
314
|
module_load_include('inc', 'field', 'field.crud');
|
315
|
module_load_include('inc', 'date', 'date_admin');
|
316
|
}
|
317
|
|
318
|
/**
|
319
|
* Implements hook_field_types().
|
320
|
*
|
321
|
* @todo.
|
322
|
*/
|
323
|
function date_tools_wizard_field_types() {
|
324
|
$field_types = array();
|
325
|
foreach (date_field_info() as $name => $info) {
|
326
|
$field_types[$name] = $info['label'];
|
327
|
}
|
328
|
return $field_types;
|
329
|
}
|
330
|
|
331
|
/**
|
332
|
* Implements hook_widget_types().
|
333
|
* @todo.
|
334
|
*/
|
335
|
function date_tools_wizard_widget_types() {
|
336
|
$widget_types = array();
|
337
|
foreach (date_field_widget_info() as $name => $info) {
|
338
|
if (!strstr($name, '_repeat')) {
|
339
|
$widget_types[$name] = $info['label'];
|
340
|
}
|
341
|
}
|
342
|
return $widget_types;
|
343
|
}
|
344
|
|
345
|
/**
|
346
|
* Tz handler.
|
347
|
*
|
348
|
* @todo.
|
349
|
*/
|
350
|
function date_tools_wizard_tz_handling() {
|
351
|
include_once drupal_get_path('module', 'date') . '/date_admin.inc';
|
352
|
return date_timezone_handling_options();
|
353
|
}
|
354
|
|
355
|
/**
|
356
|
* Create date tools wizard content type.
|
357
|
*
|
358
|
* @todo.
|
359
|
*/
|
360
|
function date_tools_wizard_create_content_type($name, $bundle, $description, $type_settings = array()) {
|
361
|
|
362
|
date_tools_wizard_include();
|
363
|
|
364
|
// Create the content type.
|
365
|
$values = array(
|
366
|
'name' => $name,
|
367
|
'type' => $bundle,
|
368
|
'description' => $description,
|
369
|
'title_label' => 'Title',
|
370
|
'body_label' => 'Body',
|
371
|
'min_word_count' => '0',
|
372
|
'help' => '',
|
373
|
'node_options' => array(
|
374
|
'status' => 1,
|
375
|
'promote' => 1,
|
376
|
'sticky' => 0,
|
377
|
'revision' => 0,
|
378
|
),
|
379
|
'language_content_type' => '0',
|
380
|
'old_type' => $bundle,
|
381
|
'orig_type' => '',
|
382
|
'base' => 'node_content',
|
383
|
'custom' => '1',
|
384
|
'modified' => '1',
|
385
|
'locked' => '0',
|
386
|
'url_str' => str_replace('_', '-', $bundle),
|
387
|
);
|
388
|
|
389
|
// Allow overrides of these values.
|
390
|
foreach ($type_settings as $key => $value) {
|
391
|
$values[$key] = $value;
|
392
|
}
|
393
|
|
394
|
node_type_save((object) $values);
|
395
|
|
396
|
// Add the body field.
|
397
|
$trim_length = variable_get('teaser_length', 600);
|
398
|
|
399
|
$field = field_info_field('body');
|
400
|
$instance = array(
|
401
|
'field_name' => 'body',
|
402
|
'entity_type' => 'node',
|
403
|
'bundle' => $bundle,
|
404
|
'label' => t('Description'),
|
405
|
'widget' => array(
|
406
|
'type' => 'text_textarea_with_summary',
|
407
|
'settings' => array(
|
408
|
'rows' => 20,
|
409
|
'summary_rows' => 5,
|
410
|
),
|
411
|
'weight' => -4,
|
412
|
'module' => 'text',
|
413
|
),
|
414
|
'settings' => array(
|
415
|
'display_summary' => TRUE,
|
416
|
),
|
417
|
'display' => array(
|
418
|
'default' => array(
|
419
|
'label' => 'hidden',
|
420
|
'type' => 'text_default',
|
421
|
),
|
422
|
'teaser' => array(
|
423
|
'label' => 'hidden',
|
424
|
'type' => 'text_summary_or_trimmed',
|
425
|
'trim_length' => $trim_length,
|
426
|
),
|
427
|
),
|
428
|
);
|
429
|
field_create_instance($instance);
|
430
|
|
431
|
}
|