Projet

Général

Profil

Paste
Télécharger (2,77 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date.devel_generate.inc @ ba09eb79

1
<?php
2

    
3
/**
4
 * @file
5
 * Date Devel Generate code.
6
 */
7

    
8
/**
9
 * Implements hook_devel_generate().
10
 *
11
 * Included only when needed.
12
 */
13
function date_devel_generate($entity, $field, $instance, $bundle) {
14

    
15
  $entity_field = array();
16
  if (isset($instance['widget']['settings']['year_range'])) {
17
    $split = explode(':', $instance['widget']['settings']['year_range']);
18
    // Determine how much to go back and forward depending on whether a relative
19
    // number of years (with - or + sign) or an absolute year is given.
20
    $back = strpos($split[0], '-') === 0
21
      ? str_replace('-', '', $split[0])
22
      : date_format(date_now(), 'Y') - $split[0];
23
    $forward = strpos($split[1], '+') === 0
24
      ? str_replace('+', '', $split[1])
25
      : $split[1] - date_format(date_now(), 'Y');
26
  }
27
  else {
28
    $back = 2;
29
    $forward = 2;
30
  }
31
  // Pick a random year within the time range,
32
  // and a random second within that year.
33
  $year = date_format(date_now(), 'Y') - $back + mt_rand(0, ($forward + $back));
34
  $start = new DateObject($year . '-01-01 00:00:00', date_get_timezone_db($field['settings']['tz_handling']));
35
  $leap = date_format($start, 'L');
36
  $max_days = $leap ? 366 : 365;
37
  $seconds = mt_rand(0, ($max_days * 86400));
38
  date_modify($start, "+$seconds seconds");
39
  $increment = $instance['widget']['settings']['increment'];
40
  date_increment_round($start, $increment);
41

    
42
  // Modify End date by 1 hour to 3 days, shorter for repeating dates
43
  // longer for others.
44
  $start2 = clone($start);
45
  $max = !empty($field['settings']['repeat']) ? 720 : 4320;
46
  $max = 240;
47
  date_modify($start2, '+' . mt_rand(60, $max) . ' minutes');
48
  date_increment_round($start2, $increment);
49

    
50
  if ($field['settings']['tz_handling'] == 'date') {
51
    // Choose a random timezone.
52
    // Not all keys exist, so we have to check.
53
    $timezones = array_keys(date_timezone_names(TRUE));
54
    $key = mt_rand(0, count($timezones) - 1);
55
    if (!array_key_exists($key, $timezones)) {
56
      $timezone = date_default_timezone();
57
    }
58
    else {
59
      $timezone = $timezones[$key];
60
    }
61
  }
62
  else {
63
    $timezone = date_get_timezone($field['settings']['tz_handling']);
64
  }
65

    
66
  switch ($field['type']) {
67
    case 'date':
68
      $format = DATE_FORMAT_ISO;
69
      break;
70

    
71
    case 'datestamp':
72
      $format = DATE_FORMAT_UNIX;
73
      break;
74

    
75
    case 'datetime':
76
      $format = DATE_FORMAT_DATETIME;
77
      break;
78
  }
79
  $entity_field['value'] = date_format($start, $format);
80
  if ($field['settings']['todate']) {
81
    $entity_field['value2'] = date_format($start2, $format);
82
  }
83
  date_timezone_set($start, timezone_open($timezone));
84
  $entity_field['timezone'] = $timezone;
85
  $entity_field['offset'] = date_offset_get($start);
86
  date_timezone_set($start2, timezone_open($timezone));
87
  $entity_field['offset2'] = date_offset_get($start2);
88
  return $entity_field;
89

    
90
}