Projet

Général

Profil

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

root / htmltest / sites / all / modules / addressfield / addressfield.devel_generate.inc @ a5572547

1
<?php
2

    
3
/*
4
 * @file
5
 * Devel Generate for addressfield module
6
 *
7
 */
8

    
9
/*
10
 * Implements hook_devel_generate();
11
 */
12
function addressfield_devel_generate($object, $field, $instance, $bundle) {
13
  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
14
    return devel_generate_multiple('_addressfield_devel_generate', $object, $field, $instance, $bundle);
15
  }
16
  else {
17
    return _addressfield_devel_generate($object, $field, $instance, $bundle);
18
  }
19
}
20

    
21
/*
22
 * Helper function to generate a greeky US-based address
23
 */
24
function _addressfield_devel_generate($object, $field, $instance, $bundle) {
25
  // Set default value
26
  $object_field = addressfield_default_values();
27
  $addresses = _addressfield_sample_addresses();
28
  $object_field = array_merge($object_field, $addresses[array_rand($addresses)]);
29
  unset($object_field['data']);
30
  return $object_field;
31
}
32

    
33
/**
34
 * Returns an array of sample addresses.
35
 *
36
 * @return
37
 *   An array of addressfield arrays
38
 */
39
function _addressfield_sample_addresses() {
40
  $fields = &drupal_static(__FUNCTION__);
41
  if (!isset($fields)) {
42
    $filepath = DRUPAL_ROOT . '/' . drupal_get_path('module', 'addressfield');
43
    $fields = array();
44
    if ($handle = @fopen("$filepath/addresses.txt",'r')) {
45
      if (is_resource($handle)) {
46
        $addresses = array();
47
        while (($buffer = fgets($handle)) !== false) {
48
          list($country, $administrative_area, $sub_administrative_area, $locality, $dependent_locality, $postal_code, $thoroughfare, $premise) = explode("\t", $buffer);
49
          $fields[] = array(
50
            'country' => ($country == 'NULL') ? NULL : trim($country),
51
            'administrative_area' => ($administrative_area == 'NULL') ? NULL : trim($administrative_area),
52
            'sub_administrative_area' => ($sub_administrative_area == 'NULL') ? NULL : trim($sub_administrative_area),
53
            'locality' => ($locality == 'NULL') ? NULL : trim($locality),
54
            'dependent_locality' => ($dependent_locality == 'NULL') ? NULL : trim($dependent_locality),
55
            'postal_code' => ($postal_code == 'NULL') ? NULL : trim($postal_code),
56
            'thoroughfare' => ($thoroughfare == 'NULL') ? NULL : trim($thoroughfare),
57
            'premise' => ($premise == 'NULL') ? NULL : trim($premise),
58
          );
59
        }
60
      }
61
      fclose($handle);
62
    }
63
  }
64
  return $fields;
65
}