Projet

Général

Profil

Paste
Télécharger (12,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date_tools / date_tools.wizard.inc @ 599a39cd

1
<?php
2

    
3
/**
4
 * @file
5
 * The Date Wizard code.
6
 */
7

    
8
/**
9
 * Implements hook_form().
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(
63
      0 => t('No'),
64
      1 => t('Yes'),
65
    ),
66
    '#title' => t('Show repeating date options'),
67
    '#access' => module_exists('date_repeat_field'),
68
  );
69
  $form['field']['advanced'] = array(
70
    '#type' => 'fieldset',
71
    '#collapsible' => TRUE,
72
    '#collapsed' => TRUE,
73
    '#title' => t('Advanced options'),
74
  );
75
  $form['field']['advanced']['todate'] = array(
76
    '#type' => 'select',
77
    '#default_value' => 'optional',
78
    '#options' => array(
79
      '' => t('Never'),
80
      'optional' => t('Optional'),
81
      'required' => t('Required'),
82
    ),
83
    '#title' => t('End Date'),
84
    '#description' => t("Display a matching second date field as a 'End date'."),
85
  );
86
  $form['field']['advanced']['field_type'] = array(
87
    '#type' => 'select',
88
    '#options' => date_tools_wizard_field_types(),
89
    '#default_value' => 'datetime',
90
    '#title' => t('Date field type'),
91
    '#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)."),
92
  );
93
  $form['field']['advanced']['granularity'] = array(
94
    '#type' => 'select',
95
    '#options' => date_granularity_names(),
96
    '#default_value' => array('month', 'day', 'year', 'hour', 'minute'),
97
    '#title' => t('Granularity'),
98
    '#multiple' => TRUE,
99
  );
100
  $form['field']['advanced']['year_range'] = array(
101
    '#type' => 'textfield',
102
    '#default_value' => '-1:+1',
103
    '#title' => t('Year range'),
104
    '#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."),
105
  );
106
  $form['field']['advanced']['tz_handling'] = array(
107
    '#type' => 'select',
108
    '#options' => date_tools_wizard_tz_handling(),
109
    '#default_value' => 'site',
110
    '#title' => t('Date timezone handling'),
111
    '#description' => t("Timezone handling should be set to 'none' for granularity without time elements."),
112
  );
113
  $form['field']['advanced']['weight'] = array(
114
    '#type' => 'textfield',
115
    '#default_value' => '-4',
116
    '#title' => t('Weight'),
117
    '#description' => t("Set the field weight."),
118
  );
119
  $form['calendar'] = array(
120
    '#type' => 'select',
121
    '#default_value' => module_exists('calendar'),
122
    '#options' => array(
123
      0 => t('No'),
124
      1 => t('Yes'),
125
    ),
126
    '#title' => t('Create a calendar for this date field'),
127
    '#access' => module_exists('calendar'),
128
  );
129

    
130
  $form['submit'] = array(
131
    '#type' => 'submit',
132
    '#value' => t('Save'),
133
  );
134
  return $form;
135
}
136

    
137
/**
138
 * Form validation.
139
 */
140
function date_tools_wizard_form_validate(&$form, &$form_state) {
141
  $bundle = $form_state['values']['bundle'];
142
  $field_name = 'field_' . $form_state['values']['field_name'];
143

    
144
  $args = array(
145
    ':field_name' => $field_name,
146
    ':bundle' => $bundle,
147
    ':entity_type' => 'node',
148
  );
149

    
150
  $query = "SELECT type FROM {node_type} WHERE type=:bundle";
151
  $existing_type = db_query($query, array(':bundle' => $args[':bundle']))->fetchField();
152

    
153
  $query = "SELECT field_name FROM {field_config_instance} WHERE field_name=:field_name AND bundle=:bundle AND entity_type=:entity_type";
154
  $existing_instance = db_query($query, $args)->fetchField();
155

    
156
  if ($existing_type) {
157
    drupal_set_message(t('This content type name already exists, adding new field to existing content type.'));
158
  }
159
  if (!preg_match('!^[a-z0-9_]+$!', $bundle)) {
160
    form_set_error('bundle', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
161
  }
162
  if (!empty($form_state['values']['calendar']) && !empty($form_state['values']['blocks']) && strlen($bundle) > 12) {
163
    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.'));
164
  }
165
  if ($existing_instance) {
166
    form_set_error('field_name', t('This field name already exists.'));
167
  }
168
  if (strlen($field_name) > 32) {
169
    form_set_error('field_name', t('The field name must be no more than 26 characters long.'));
170
  }
171
  if (!date_has_time($form_state['values']['granularity']) && $form_state['values']['tz_handling'] != 'none') {
172
    form_set_error('tz_handling', t('Timezone handling must be none for granularity without time.'));
173
  }
174
}
175

    
176
/**
177
 * Form submit.
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
function date_tools_wizard_build($form_values) {
194
  extract($form_values);
195

    
196
  $field_name = 'field_' . $field_name;
197
  $base_table = 'node';
198

    
199
  // Create a node type if it doesn't already exist. This makes it possible to
200
  // add additional date fields to an existing type.
201
  $types = node_type_get_names();
202
  $type_settings = array();
203
  if (!array_key_exists($bundle, $types)) {
204
    date_tools_wizard_create_content_type($name, $bundle, $type_description, $type_settings);
205

    
206
    drupal_set_message(t('Your content type @name has been created.', array('@name' => $name)));
207
  }
208
  else {
209
    $types = node_type_get_types();
210
    $type = $types[$bundle];
211
    if (!empty($type_settings)) {
212
      foreach ($type_settings as $key => $setting) {
213
        $type->$key = $setting;
214
      }
215
      node_type_save($type);
216
    }
217
    $name = $type->name;
218
  }
219

    
220
  $field = array(
221
    'field_name' => $field_name,
222
    'type' => $field_type,
223
    'cardinality' => $repeat ? FIELD_CARDINALITY_UNLIMITED : 1,
224
    'settings' => array(
225
      'granularity' => $granularity,
226
      'tz_handling' => $tz_handling,
227
      'timezone_db' => date_get_timezone_db($tz_handling),
228
      'repeat' => $repeat,
229
      'todate' => !empty($todate) ? $todate : 'optional',
230
    ),
231
  );
232
  $instance = array(
233
    'entity_type' => 'node',
234
    'field_name' => $field_name,
235
    'label' => $label,
236
    'bundle' => $bundle,
237
    // Move the date right below the title.
238
    'weight' => $weight,
239
    'widget' => array(
240
      'type' => $widget_type,
241
      // Increment for minutes and seconds, can be 1, 5, 10, 15, or 30.
242
      'settings' => array(
243
        'increment' => 15,
244
        // The number of years to go back and forward in drop-down year
245
        // selectors.
246
        'year_range' => !empty($year_range) ? $year_range : '-0:+1',
247
        'input_format' => date_default_format($widget_type),
248
        'text_parts' => array(),
249
        'label_position' => 'above',
250
        'repeat_collapsed' => 0,
251
      ),
252
      'weight' => $weight,
253
    ),
254
    'settings' => array(
255
      'default_value' => 'now',
256
      'default_value2' => 'blank',
257
    ),
258
  );
259

    
260
  $instance['display'] = array(
261
    'default' => array(
262
      'label' => 'above',
263
      'type' => 'date_default',
264
      'settings' => array(
265
        'format_type' => 'long',
266
        'show_repeat_rule' => 'show',
267
        'multiple_number' => '',
268
        'multiple_from' => '',
269
        'multiple_to' => '',
270
        'fromto' => 'both',
271
      ),
272
      'module' => 'date',
273
      'weight' => 0 ,
274
    ),
275
    'teaser' => array(
276
      'label' => 'above',
277
      'type' => 'date_default',
278
      'weight' => 0,
279
      'settings' => array(
280
        'format_type' => 'long',
281
        'show_repeat_rule' => 'show',
282
        'multiple_number' => '',
283
        'multiple_from' => '',
284
        'multiple_to' => '',
285
        'fromto' => 'both',
286
      ),
287
      'module' => 'date',
288
    ),
289
  );
290

    
291
  $field = field_create_field($field);
292
  $instance = field_create_instance($instance);
293
  $view_name = 'calendar_node_' . $field_name;
294

    
295
  field_info_cache_clear();
296
  field_cache_clear();
297

    
298
  drupal_set_message(t('Your date field @name has been created.', array('@name' => $label)));
299

    
300
  return $view_name;
301
}
302

    
303
/**
304
 * Includes handler.
305
 */
306
function date_tools_wizard_include() {
307
  module_load_include('inc', 'node', 'content_types');
308
  module_load_include('inc', 'node', 'node.pages');
309
  module_load_include('inc', 'field', 'field.crud');
310
  module_load_include('inc', 'date', 'date_admin');
311
}
312

    
313
/**
314
 * Implements hook_field_types().
315
 */
316
function date_tools_wizard_field_types() {
317
  $field_types = array();
318
  foreach (date_field_info() as $name => $info) {
319
    $field_types[$name] = $info['label'];
320
  }
321
  return $field_types;
322
}
323

    
324
/**
325
 * Implements hook_widget_types().
326
 */
327
function date_tools_wizard_widget_types() {
328
  $widget_types = array();
329
  foreach (date_field_widget_info() as $name => $info) {
330
    if (!strstr($name, '_repeat')) {
331
      $widget_types[$name] = $info['label'];
332
    }
333
  }
334
  return $widget_types;
335
}
336

    
337
/**
338
 * Tz handler.
339
 */
340
function date_tools_wizard_tz_handling() {
341
  include_once drupal_get_path('module', 'date') . '/date_admin.inc';
342
  return date_timezone_handling_options();
343
}
344

    
345
/**
346
 * Create date tools wizard content type.
347
 */
348
function date_tools_wizard_create_content_type($name, $bundle, $description, $type_settings = array()) {
349
  date_tools_wizard_include();
350

    
351
  // Create the content type.
352
  $values  = array(
353
    'name' => $name,
354
    'type' => $bundle,
355
    'description' => $description,
356
    'title_label' => 'Title',
357
    'body_label' => 'Body',
358
    'min_word_count' => '0',
359
    'help' => '',
360
    'node_options' => array(
361
      'status' => 1,
362
      'promote' => 1,
363
      'sticky' => 0,
364
      'revision' => 0,
365
    ),
366
    'language_content_type' => '0',
367
    'old_type' => $bundle,
368
    'orig_type' => '',
369
    'base' => 'node_content',
370
    'custom' => '1',
371
    'modified' => '1',
372
    'locked' => '0',
373
    'url_str' => str_replace('_', '-', $bundle),
374
  );
375

    
376
  // Allow overrides of these values.
377
  foreach ($type_settings as $key => $value) {
378
    $values[$key] = $value;
379
  }
380

    
381
  node_type_save((object) $values);
382

    
383
  // Add the body field.
384
  $trim_length = variable_get('teaser_length', 600);
385

    
386
  $field = field_info_field('body');
387
  $instance = array(
388
    'field_name' => 'body',
389
    'entity_type' => 'node',
390
    'bundle' => $bundle,
391
    'label' => t('Description'),
392
    'widget' => array(
393
      'type' => 'text_textarea_with_summary',
394
      'settings' => array(
395
        'rows' => 20,
396
        'summary_rows' => 5,
397
      ),
398
      'weight' => -4,
399
      'module' => 'text',
400
    ),
401
    'settings' => array(
402
      'display_summary' => TRUE,
403
    ),
404
    'display' => array(
405
      'default' => array(
406
        'label' => 'hidden',
407
        'type' => 'text_default',
408
      ),
409
      'teaser' => array(
410
        'label' => 'hidden',
411
        'type' => 'text_summary_or_trimmed',
412
        'trim_length' => $trim_length,
413
      ),
414
    ),
415
  );
416
  field_create_instance($instance);
417
}