Projet

Général

Profil

Paste
Télécharger (5,86 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / order / commerce_order.tokens.inc @ 9d13637e

1
<?php
2

    
3
/**
4
 * @file
5
 * Builds placeholder replacement tokens for order-related data.
6
 */
7

    
8

    
9
/**
10
 * Implements hook_token_info().
11
 */
12
function commerce_order_token_info() {
13
  $type = array(
14
    'name' => t('Orders', array(), array('context' => 'a drupal commerce order')),
15
    'description' => t('Tokens related to individual orders.'),
16
    'needs-data' => 'commerce-order',
17
  );
18

    
19
  // Tokens for orders.
20
  $order = array();
21

    
22
  $order['order-id'] = array(
23
    'name' => t('Order ID', array(), array('context' => 'a drupal commerce order')),
24
    'description' => t('The unique numeric ID of the order.'),
25
  );
26
  $order['order-number'] = array(
27
    'name' => t('Order number', array(), array('context' => 'a drupal commerce order')),
28
    'description' => t('The order number displayed to the customer.'),
29
  );
30
  $order['revision-id'] = array(
31
    'name' => t('Revision ID'),
32
    'description' => t("The unique ID of the order's latest revision."),
33
  );
34
  $order['type'] = array(
35
    'name' => t('Order type'),
36
    'description' => t('The type of the order.'),
37
  );
38
  $order['type-name'] = array(
39
    'name' => t('Order type name'),
40
    'description' => t('The human-readable name of the order type.'),
41
  );
42
  $order['mail'] = array(
43
    'name' => t('Order e-mail'),
44
    'description' => t('The e-mail address associated with the order.'),
45
  );
46
  $order['status'] = array(
47
    'name' => t('Order status'),
48
    'description' => t('The current status of the order.'),
49
  );
50
  $order['status-title'] = array(
51
    'name' => t('Order status title'),
52
    'description' => t('The human-readable title of the order status.'),
53
  );
54
  $order['state'] = array(
55
    'name' => t('Order state'),
56
    'description' => t('The current state of the order.'),
57
  );
58
  $order['state-title'] = array(
59
    'name' => t('Order state title'),
60
    'description' => t('The human-readable title of the order state.'),
61
  );
62

    
63
  // Chained tokens for orders.
64
  $order['owner'] = array(
65
    'name' => t('Owner'),
66
    'description' => t('The owner of the order.'),
67
    'type' => 'user',
68
  );
69
  $order['created'] = array(
70
    'name' => t('Date created'),
71
    'description' => t('The date the order was created.'),
72
    'type' => 'date',
73
  );
74
  $order['changed'] = array(
75
    'name' => t('Date changed'),
76
    'description' => t('The date the order was last updated.'),
77
    'type' => 'date',
78
  );
79

    
80
  return array(
81
    'types' => array('commerce-order' => $type),
82
    'tokens' => array('commerce-order' => $order),
83
  );
84
}
85

    
86
/**
87
 * Implements hook_tokens().
88
 */
89
function commerce_order_tokens($type, $tokens, array $data = array(), array $options = array()) {
90
  $url_options = array('absolute' => TRUE);
91

    
92
  if (isset($options['language'])) {
93
    $url_options['language'] = $options['language'];
94
    $language_code = $options['language']->language;
95
  }
96
  else {
97
    $language_code = NULL;
98
  }
99

    
100
  $sanitize = !empty($options['sanitize']);
101

    
102
  $replacements = array();
103

    
104
  if ($type == 'commerce-order' && !empty($data['commerce-order'])) {
105
    $order = $data['commerce-order'];
106

    
107
    foreach ($tokens as $name => $original) {
108
      switch ($name) {
109
        // Simple key values on the order.
110
        case 'order-id':
111
          $replacements[$original] = $order->order_id;
112
          break;
113

    
114
        case 'order-number':
115
          $replacements[$original] = $sanitize ? check_plain($order->order_number) : $order->order_number;
116
          break;
117

    
118
        case 'revision_id':
119
          $replacements[$original] = $order->revision_id;
120
          break;
121

    
122
        case 'type':
123
          $replacements[$original] = $sanitize ? check_plain($order->type) : $order->type;
124
          break;
125

    
126
        case 'type-name':
127
          $type_name = commerce_order_type_get_name($order->type);
128
          $replacements[$original] = $sanitize ? check_plain($type_name) : $type_name;
129
          break;
130

    
131
        case 'mail':
132
          $replacements[$original] = $sanitize ? check_plain($order->mail) : $order->mail;
133
          break;
134

    
135
        case 'status':
136
          $replacements[$original] = $sanitize ? check_plain($order->status) : $order->status;
137
          break;
138

    
139
        case 'status-title':
140
          $replacements[$original] = $sanitize ? check_plain(commerce_order_status_get_title($order->status)) : commerce_order_status_get_title($order->status);
141
          break;
142

    
143
        case 'state':
144
          $order_status = commerce_order_status_load($order->status);
145
          $replacements[$original] = $sanitize ? check_plain($order_status['state']) : $order_status['state'];
146
          break;
147

    
148
        case 'state-title':
149
          $order_status = commerce_order_status_load($order->status);
150
          $replacements[$original] = $sanitize ? check_plain(commerce_order_state_get_title($order_status['state'])) : commerce_order_state_get_title($order_status['state']);
151
          break;
152

    
153

    
154
        // Default values for the chained tokens handled below.
155
        case 'owner':
156
          if ($order->uid == 0) {
157
            $name = variable_get('anonymous', t('Anonymous'));
158
          }
159
          else {
160
            $account = user_load($order->uid);
161
            $name = $account->name;
162
          }
163
          $replacements[$original] = $sanitize ? filter_xss($name) : $name;
164
          break;
165
        case 'created':
166
          $replacements[$original] = format_date($order->created, 'medium', '', NULL, $language_code);
167
          break;
168

    
169
        case 'changed':
170
          $replacements[$original] = format_date($order->changed, 'medium', '', NULL, $language_code);
171
          break;
172
      }
173
    }
174

    
175

    
176
    if ($owner_tokens = token_find_with_prefix($tokens, 'owner')) {
177
      $owner = user_load($order->uid);
178
      $replacements += token_generate('user', $owner_tokens, array('user' => $owner), $options);
179
    }
180

    
181
    foreach (array('created', 'changed') as $date) {
182
      if ($created_tokens = token_find_with_prefix($tokens, $date)) {
183
        $replacements += token_generate('date', $created_tokens, array('date' => $order->{$date}), $options);
184
      }
185
    }
186
  }
187

    
188
  return $replacements;
189
}