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