Projet

Général

Profil

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

root / drupal7 / sites / all / modules / commerce / modules / order / commerce_order_ui.module @ dbb0c974

1
<?php
2

    
3
/**
4
 * @file
5
 */
6

    
7
/**
8
 * Implements hook_menu().
9
 */
10
function commerce_order_ui_menu() {
11
  $items = array();
12

    
13
  // Note: admin/commerce/orders is defined by a default View.
14

    
15
  // Create an order.
16
  $items['admin/commerce/orders/add'] = array(
17
    'title' => 'Create an order',
18
    'description' => 'Create a new order.',
19
    'page callback' => 'commerce_order_ui_order_form_wrapper',
20
    'page arguments' => array(commerce_order_new()),
21
    'access callback' => 'commerce_order_access',
22
    'access arguments' => array('create'),
23
    'weight' => 10,
24
    'file' => 'includes/commerce_order_ui.orders.inc',
25
  );
26
  $items['admin/commerce/orders/add/%user'] = array(
27
    'title' => 'Create an order',
28
    'description' => 'Create a new order for the specified user.',
29
    'page callback' => 'commerce_order_ui_order_form_wrapper',
30
    'page arguments' => array(commerce_order_new(), 4),
31
    'access callback' => 'commerce_order_access',
32
    'access arguments' => array('create'),
33
    'file' => 'includes/commerce_order_ui.orders.inc',
34
  );
35

    
36
  $items['admin/commerce/orders/%commerce_order'] = array(
37
    'title callback' => 'commerce_order_ui_order_title',
38
    'title arguments' => array(3),
39
    'page callback' => 'commerce_order_ui_order_view',
40
    'page arguments' => array(3),
41
    'access callback' => 'commerce_order_admin_order_view_access',
42
    'access arguments' => array(3),
43
  );
44
  $items['admin/commerce/orders/%commerce_order/view'] = array(
45
    'title' => 'View',
46
    'type' => MENU_DEFAULT_LOCAL_TASK,
47
    'weight' => -10,
48
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
49
  );
50
  $items['admin/commerce/orders/%commerce_order/edit'] = array(
51
    'title' => 'Edit',
52
    'page callback' => 'commerce_order_ui_order_form_wrapper',
53
    'page arguments' => array(3),
54
    'access callback' => 'commerce_order_access',
55
    'access arguments' => array('update', 3),
56
    'type' => MENU_LOCAL_TASK,
57
    'weight' => -5,
58
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
59
    'file' => 'includes/commerce_order_ui.orders.inc',
60
  );
61
  $items['admin/commerce/orders/%commerce_order/delete'] = array(
62
    'title' => 'Delete',
63
    'page callback' => 'commerce_order_ui_order_delete_form_wrapper',
64
    'page arguments' => array(3),
65
    'access callback' => 'commerce_order_access',
66
    'access arguments' => array('delete', 3),
67
    'type' => MENU_LOCAL_TASK,
68
    'weight' => 20,
69
    'context' => MENU_CONTEXT_INLINE,
70
    'file' => 'includes/commerce_order_ui.orders.inc',
71
  );
72

    
73
  $items['admin/commerce/config/order'] = array(
74
    'title' => 'Order settings',
75
    'description' => 'Configure general order settings, fields, and displays.',
76
    'page callback' => 'drupal_get_form',
77
    'page arguments' => array('commerce_order_settings_form'),
78
    'access arguments' => array('configure order settings'),
79
    'file' => 'includes/commerce_order_ui.orders.inc',
80
  );
81
  $items['admin/commerce/config/order/settings'] = array(
82
    'title' => 'Settings',
83
    'type' => MENU_DEFAULT_LOCAL_TASK,
84
    'weight' => -10,
85
  );
86

    
87
  $items['user/%user/orders/%commerce_order'] = array(
88
    'title callback' => 'commerce_order_ui_order_title',
89
    'title arguments' => array(3),
90
    'page callback' => 'commerce_order_ui_order_view',
91
    'page arguments' => array(3, 'customer'),
92
    'access callback' => 'commerce_order_customer_order_view_access',
93
    'access arguments' => array(3),
94
  );
95

    
96
  return $items;
97
}
98

    
99
/**
100
 * Menu item title callback: returns the number of an order for its pages.
101
 *
102
 * @param $order
103
 *   The order object as loaded via the URL wildcard.
104
 * @return
105
 *   A page title of the format "Order ##".
106
 */
107
function commerce_order_ui_order_title($order) {
108
  return t('Order @number', array('@number' => $order->order_number));
109
}
110

    
111
/**
112
 * Menu item access callback: prevent view access to the admin order display
113
 * for customers who have 'view' access to the order but not administration pages.
114
 *
115
 * @param $order
116
 *   The order object as loaded via the menu item wildcard.
117
 *
118
 * @return
119
 *   Boolean indicating the user's access to view the page.
120
 */
121
function commerce_order_admin_order_view_access($order) {
122
  return user_access('access administration pages') && commerce_order_access('view', $order);
123
}
124

    
125
/**
126
 * Menu item access callback: prevent view access to the customer order display
127
 * for orders in a 'cart' status and then perform a normal order access check.
128
 *
129
 * @param $order
130
 *   The order object as loaded via the menu item wildcard.
131
 *
132
 * @return
133
 *   Boolean indicating the user's access to view the page.
134
 */
135
function commerce_order_customer_order_view_access($order) {
136
  // If the order is in a shopping cart order status...
137
  if (in_array($order->status, array_keys(commerce_order_statuses(array('cart' => TRUE))))) {
138
    // Do not allow the customer to see the page.
139
    return FALSE;
140
  }
141

    
142
  // Otherwise fallback on normal order access.
143
  return commerce_order_access('view', $order);
144
}
145

    
146
/**
147
 * Implements hook_menu_local_tasks_alter().
148
 */
149
function commerce_order_ui_menu_local_tasks_alter(&$data, $router_item, $root_path) {
150
  // Add action link 'admin/commerce/orders/add' on 'admin/commerce/orders'.
151
  if ($root_path == 'admin/commerce/orders') {
152
    $item = menu_get_item('admin/commerce/orders/add');
153
    if ($item['access']) {
154
      $data['actions']['output'][] = array(
155
        '#theme' => 'menu_local_action',
156
        '#link' => $item,
157
      );
158
    }
159
  }
160

    
161
  // Add an action link to the order edit page from the user order page.
162
  if ($root_path == 'user/%/orders/%') {
163
    // Extract the order ID from the current router item and fetch the admin
164
    // update menu item.
165
    $order_id = $router_item['original_map'][3];
166
    $item = menu_get_item('admin/commerce/orders/' . $order_id . '/edit');
167

    
168
    if ($item['access']) {
169
      // Override the title.
170
      $item['title'] = t('Edit this order');
171
      $data['actions']['output'][] = array(
172
        '#theme' => 'menu_local_action',
173
        '#link' => $item,
174
      );
175
    }
176
  }
177
}
178

    
179
/**
180
 * Implements hook_i18n_string_list().
181
 */
182
function commerce_order_ui_i18n_string_list($group) {
183
  if ($group == 'commerce') {
184
    // Allow the order creation help text to be translated.
185
    $help = variable_get('commerce_order_help_text', '');
186

    
187
    if (!empty($help)) {
188
      $strings['commerce']['order']['help']['create'] = $help;
189
      return $strings;
190
    }
191
  }
192
}
193

    
194
/**
195
 * Implements hook_help().
196
 */
197
function commerce_order_ui_help($path, $arg) {
198
  // Display a user configurable help text on the order add page.
199
  if (strpos($path, 'admin/commerce/orders/add') === 0) {
200
    $help = variable_get('commerce_order_help_text', '');
201

    
202
    if (!empty($help)) {
203
      $help = commerce_i18n_string('commerce:order:help:create', $help, array('sanitize' => FALSE));
204
      return '<p>' . filter_xss_admin($help) . '</p>';
205
    }
206
  }
207
}
208

    
209
/**
210
 * Implements hook_entity_info_alter().
211
 */
212
function commerce_order_ui_entity_info_alter(&$entity_info) {
213
  // Add a URI callback to the order entity.
214
  $entity_info['commerce_order']['uri callback'] = 'commerce_order_ui_order_uri';
215

    
216
  // Expose the order UI for order fields.
217
  $entity_info['commerce_order']['bundles']['commerce_order']['admin'] = array(
218
    'path' => 'admin/commerce/config/order',
219
    'real path' => 'admin/commerce/config/order',
220
    'access arguments' => array('configure order settings'),
221
  );
222
}
223

    
224
/**
225
 * Entity uri callback: points to the admin view page of the given order.
226
 */
227
function commerce_order_ui_order_uri($order) {
228
  // First look for a return value in the default entity uri callback.
229
  $uri = commerce_order_uri($order);
230

    
231
  // If a value was found, return it now.
232
  if (!empty($uri)) {
233
    return $uri;
234
  }
235

    
236
  // Only return a value if the user has permission to view the order.
237
  if (commerce_order_access('view', $order)) {
238
    return array(
239
      'path' => 'admin/commerce/orders/' . $order->order_id,
240
    );
241
  }
242

    
243
  return NULL;
244
}
245

    
246
/**
247
 * Implements hook_forms().
248
 */
249
function commerce_order_ui_forms($form_id, $args) {
250
  $forms = array();
251

    
252
  // Define a wrapper ID for the order add / edit form.
253
  $forms['commerce_order_ui_order_form'] = array(
254
    'callback' => 'commerce_order_order_form',
255
  );
256

    
257
  // Define a wrapper ID for the order delete confirmation form.
258
  $forms['commerce_order_ui_order_delete_form'] = array(
259
    'callback' => 'commerce_order_order_delete_form',
260
  );
261

    
262
  return $forms;
263
}
264

    
265
/**
266
 * Implements hook_form_FORM_ID_alter().
267
 *
268
 * The Order UI module instantiates the Order add/edit form at particular paths
269
 * in the Commerce IA. It uses its own form ID to do so and alters the form
270
 * here to add in appropriate redirection.
271
 *
272
 * @see commerce_order_ui_order_form()
273
 */
274
function commerce_order_ui_form_commerce_order_ui_order_form_alter(&$form, &$form_state) {
275
  // Add a submit handler to the save button to add a redirect.
276
  $form['actions']['submit']['#submit'][] = 'commerce_order_ui_order_form_submit';
277
  $form['actions']['submit']['#suffix'] = l(t('Cancel'), 'admin/commerce/orders');
278
}
279

    
280
/**
281
 * Submit callback for commerce_order_ui_order_form().
282
 *
283
 * @see commerce_order_ui_form_commerce_order_ui_order_form_alter()
284
 */
285
function commerce_order_ui_order_form_submit($form, &$form_state) {
286
  // Apply the redirect based on the clicked button.
287
  if ($form_state['triggering_element']['#value'] == t('Save order', array(), array('context' => 'a drupal commerce order'))) {
288
    drupal_set_message(t('Order saved.'));
289

    
290
    $form_state['redirect'] = 'admin/commerce/orders/' . $form_state['commerce_order']->order_id . '/edit';
291
  }
292
}
293

    
294
/**
295
 * Implements hook_form_FORM_ID_alter().
296
 *
297
 * The Order UI module instantiates the Order delete form at a particular path
298
 * in the Commerce IA. It uses its own form ID to do so and alters the form
299
 * here to add in appropriate redirection.
300
 *
301
 * @see commerce_order_ui_order_delete_form()
302
 */
303
function commerce_order_ui_form_commerce_order_ui_order_delete_form_alter(&$form, &$form_state) {
304
  $form['actions']['cancel']['#href'] = 'admin/commerce/orders';
305
  $form['#submit'][] = 'commerce_order_ui_order_delete_form_submit';
306
}
307

    
308
/**
309
 * Submit callback for commerce_order_ui_order_delete_form().
310
 *
311
 * @see commerce_order_ui_form_commerce_order_ui_order_delete_form_alter()
312
 */
313
function commerce_order_ui_order_delete_form_submit($form, &$form_state) {
314
  $form_state['redirect'] = 'admin/commerce/orders';
315
}
316

    
317
/**
318
 * Implements hook_views_api().
319
 */
320
function commerce_order_ui_views_api() {
321
  return array(
322
    'api' => 3,
323
    'path' => drupal_get_path('module', 'commerce_order_ui') . '/includes/views',
324
  );
325
}
326

    
327
/**
328
 * Sets the breadcrumb for order pages.
329
 *
330
 * @param $view_mode
331
 *   The view mode for the current order page, 'administrator' or 'customer'.
332
 *
333
 * @deprecated since 7.x-1.4
334
 */
335
function commerce_order_ui_set_breadcrumb($view_mode = 'administrator') {
336
  // This function used to manually set a breadcrumb that is now properly
337
  // generated by Drupal itself.
338
}
339

    
340
/**
341
 * Generate an array for rendering the given order.
342
 *
343
 * @param $order
344
 *   A fully loaded order object.
345
 * @param $view_mode
346
 *   The view mode for displaying the order, 'administrator' or 'customer'.
347
 *
348
 * @return
349
 *   An array as expected by drupal_render().
350
 */
351
function commerce_order_ui_order_view($order, $view_mode = 'administrator') {
352
  drupal_add_css(drupal_get_path('module', 'commerce_order') . '/theme/commerce_order.theme.css');
353
  return entity_view('commerce_order', array($order->order_id => $order), $view_mode, NULL, TRUE);
354
}
355

    
356
/**
357
 * Implements hook_form_FORM_ID_alter().
358
 */
359
function commerce_order_ui_form_entity_translation_admin_form_alter(&$form, &$form_state, $form_id) {
360
  // Hide the commerce_order option from entity translation.
361
  unset($form['entity_translation_entity_types']['#options']['commerce_order']);
362
}
363

    
364
/**
365
 * Builds a form to redirect to an order's view page.
366
 *
367
 * @param $redirect_page
368
 *   The page to redirect to, either 'admin', 'customer', or 'select' to add a
369
 *   widget to the form so the user can specify which page they want.
370
 * @param $identifier
371
 *   The identifier to use to determine which order should be viewed; either
372
 *   'order_number' (the default), 'order_id', or 'select'.
373
 */
374
function commerce_order_ui_redirect_form($form, &$form_state, $redirect_page = 'admin', $identifier = 'order_number') {
375
  $form['#attached']['css'][] = drupal_get_path('module', 'commerce_order') . '/theme/commerce_order.admin.css';
376

    
377
  if ($identifier == 'select') {
378
    $form['identifier'] = array(
379
      '#type' => 'select',
380
      '#title' => t('Specify order by', array(), array('context' => 'a drupal commerce order')),
381
      '#options' => array(
382
        'order_number' => t('Order number', array(), array('context' => 'a drupal commerce order')),
383
        'order_id' => t('Order ID', array(), array('context' => 'a drupal commerce order')),
384
      ),
385
      '#default_value' => 'order_number',
386
    );
387

    
388
    $order_title = t('Order', array(), array('context' => 'a drupal commerce order'));
389
  }
390
  else {
391
    $form['identifier'] = array(
392
       '#type' => 'value',
393
       '#value' => $identifier,
394
    );
395

    
396
    if ($identifier == 'order_number') {
397
      $order_title = t('Order number', array(), array('context' => 'a drupal commerce order'));
398
    }
399
    else {
400
      $order_title = t('Order ID', array(), array('context' => 'a drupal commerce order'));
401
    }
402
  }
403

    
404
  $form['order_identifier'] = array(
405
    '#type' => 'textfield',
406
    '#title' => $order_title,
407
    '#size' => 16,
408
  );
409

    
410
  $form['order'] = array(
411
    '#type' => 'value',
412
    '#value' => NULL,
413
  );
414

    
415
  if ($redirect_page == 'select') {
416
    $form['redirect_page'] = array(
417
      '#type' => 'select',
418
      '#title' => t('Redirect page'),
419
      '#options' => array(
420
        'admin' => t('Admin view page'),
421
        'customer' => t('Customer view page'),
422
      ),
423
      '#default_value' => 'admin',
424
    );
425
  }
426
  else {
427
    $form['redirect_page'] = array(
428
      '#type' => 'value',
429
      '#value' => $redirect_page,
430
    );
431
  }
432

    
433
  $form['submit'] = array(
434
    '#type' => 'submit',
435
    '#value' => t('View order'),
436
  );
437

    
438
  return $form;
439
}
440

    
441
/**
442
 * Validate callback: ensure a valid order was specified for viewing.
443
 */
444
function commerce_order_ui_redirect_form_validate($form, &$form_state) {
445
  $order = FALSE;
446

    
447
  // Attempt to load the specified order.
448
  if ($form_state['values']['identifier'] == 'order_number') {
449
    $order = commerce_order_load_by_number($form_state['values']['order_identifier']);
450
  }
451
  elseif ($form_state['values']['identifier'] == 'order_id') {
452
    $order = commerce_order_load($form_state['values']['order_identifier']);
453
  }
454

    
455
  // If the order could not be loaded by ID or number or the user does not have
456
  // view access for the order, throw an error.
457
  if (empty($order) || !commerce_order_access('view', $order)) {
458
    form_set_error('order', t('You have specified an invalid order.'));
459
  }
460
  else {
461
    // If all's clear, store the order in the form state.
462
    form_set_value($form['order'], $order, $form_state);
463
  }
464
}
465

    
466
/**
467
 * Submit callback: redirect to the appropriate page for the specified order.
468
 */
469
function commerce_order_ui_redirect_form_submit($form, &$form_state) {
470
  // Extract the order from the form state.
471
  $order = $form_state['values']['order'];
472

    
473
  // Redirect to either the admin or customer view page as specified.
474
  if ($form_state['values']['redirect_page'] == 'admin') {
475
    $form_state['redirect'] = 'admin/commerce/orders/' . $order->order_id;
476
  }
477
  elseif ($form_state['values']['redirect_page'] == 'customer') {
478
    $form_state['redirect'] = 'user/' . $order->uid . '/orders/' . $order->order_id;
479
  }
480
}