Projet

Général

Profil

Paste
Télécharger (13,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / customer / commerce_customer_ui.module @ b858700c

1
<?php
2

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

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

    
13
  // Note: admin/commerce/customer-profiles is defined by a default View.
14

    
15
  // Add a customer profile.
16
  $items['admin/commerce/customer-profiles/add'] = array(
17
    'title' => 'Add a customer profile',
18
    'description' => 'Add a new customer profile.',
19
    'page callback' => 'commerce_customer_ui_customer_profile_add_page',
20
    'access callback' => 'commerce_customer_ui_customer_profile_add_any_access',
21
    'weight' => 10,
22
    'file' => 'includes/commerce_customer_ui.profiles.inc',
23
  );
24
  foreach (commerce_customer_profile_types() as $type => $profile_type) {
25
    $items['admin/commerce/customer-profiles/add/' . strtr($type, array('_' => '-'))] = array(
26
      'title' => 'Create @name',
27
      'title arguments' => array('@name' => $profile_type['name']),
28
      'description' => $profile_type['description'],
29
      'page callback' => 'commerce_customer_ui_customer_profile_form_wrapper',
30
      'page arguments' => array(commerce_customer_profile_new($type)),
31
      'access callback' => 'commerce_customer_profile_access',
32
      'access arguments' => array('create', commerce_customer_profile_new($type)),
33
      'file' => 'includes/commerce_customer_ui.profiles.inc',
34
    );
35
  }
36

    
37
  $items['admin/commerce/customer-profiles/%commerce_customer_profile'] = array(
38
    'title callback' => 'commerce_customer_ui_customer_profile_title',
39
    'title arguments' => array(3),
40
    'page callback' => 'commerce_customer_ui_customer_profile_form_wrapper',
41
    'page arguments' => array(3),
42
    'access callback' => 'commerce_customer_profile_access',
43
    'access arguments' => array('update', 3),
44
    'weight' => 0,
45
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
46
    'file' => 'includes/commerce_customer_ui.profiles.inc',
47
  );
48
  $items['admin/commerce/customer-profiles/%commerce_customer_profile/edit'] = array(
49
    'title' => 'Edit',
50
    'type' => MENU_DEFAULT_LOCAL_TASK,
51
    'weight' => -10,
52
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
53
  );
54
  $items['admin/commerce/customer-profiles/%commerce_customer_profile/delete'] = array(
55
    'title' => 'Delete a customer profile',
56
    'page callback' => 'commerce_customer_ui_customer_profile_delete_form_wrapper',
57
    'page arguments' => array(3),
58
    'access callback' => 'commerce_customer_profile_access',
59
    'access arguments' => array('delete', 3),
60
    'type' => MENU_LOCAL_TASK,
61
    'weight' => 10,
62
    'context' => MENU_CONTEXT_INLINE,
63
    'file' => 'includes/commerce_customer_ui.profiles.inc',
64
  );
65

    
66
  $items['admin/commerce/customer-profiles/types'] = array(
67
    'title' => 'Profile types',
68
    'description' => 'Manage customer profile types for your store.',
69
    'page callback' => 'commerce_customer_ui_customer_profile_types_overview',
70
    'access arguments' => array('administer customer profile types'),
71
    'type' => MENU_LOCAL_TASK,
72
    'weight' => 0,
73
    'file' => 'includes/commerce_customer_ui.profile_types.inc',
74
  );
75

    
76
  foreach (commerce_customer_profile_types() as $type => $profile_type) {
77
    // Convert underscores to hyphens for the menu item argument.
78
    $type_arg = strtr($type, '_', '-');
79

    
80
    $items['admin/commerce/customer-profiles/types/' . $type_arg] = array(
81
      'title' => $profile_type['name'],
82
      'page callback' => 'commerce_customer_ui_profile_type_redirect',
83
      'page arguments' => array($type),
84
      'access arguments' => array('administer customer profile types'),
85
    );
86
  }
87

    
88
  return $items;
89
}
90

    
91
/**
92
 * Menu item title callback: returns the ID of a customer profile for its pages.
93
 *
94
 * @param $profile
95
 *   The customer profile object as loaded via the URL wildcard.
96
 * @return
97
 *   A page title of the format "Profile [profile-id]".
98
 */
99
function commerce_customer_ui_customer_profile_title($profile) {
100
  return t('Customer profile @profile_id', array('@profile_id' => $profile->profile_id));
101
}
102

    
103
/**
104
 * Access callback: determine if the user can create any type of profile.
105
 */
106
function commerce_customer_ui_customer_profile_add_any_access() {
107
  // Grant automatic access to users with administer customer profiles permission.
108
  if (user_access('administer commerce_customer_profile entities')) {
109
    return TRUE;
110
  }
111

    
112
  // Check the user's access on a profile type basis.
113
  foreach (commerce_customer_profile_types() as $type => $profile_type) {
114
    if (commerce_customer_profile_access('create', commerce_customer_profile_new($type))) {
115
      return TRUE;
116
    }
117
  }
118

    
119
  return FALSE;
120
}
121

    
122
/**
123
 * Redirects a customer profile type URL to its fields management page.
124
 */
125
function commerce_customer_ui_profile_type_redirect($type) {
126
  drupal_goto('admin/commerce/customer-profiles/types/' . strtr($type, '_', '-') . '/fields');
127
}
128

    
129
/**
130
 * Implements hook_menu_alter().
131
 */
132
function commerce_customer_ui_menu_alter(&$items) {
133
  // Transform the field UI tabs into contextual links.
134
  foreach (commerce_customer_profile_types() as $type => $profile_type) {
135
    // Convert underscores to hyphens for the menu item argument.
136
    $type_arg = strtr($type, '_', '-');
137

    
138
    // Transform the field UI tabs into contextual links.
139
    $items['admin/commerce/customer-profiles/types/' . $type_arg . '/fields']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
140
    $items['admin/commerce/customer-profiles/types/' . $type_arg . '/display']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
141
  }
142
}
143

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

    
161
/**
162
 * Implements hook_admin_menu_map().
163
 */
164
function commerce_customer_ui_admin_menu_map() {
165
  // Add awareness to the administration menu of the various profile types so
166
  // they are included in the dropdown menu.
167
  $map['admin/commerce/customer-profiles/types/%'] = array(
168
    'parent' => 'admin/commerce/customer-profiles/types',
169
    'arguments' => array(
170
      array('%' => array_keys(commerce_customer_profile_types())),
171
    ),
172
  );
173

    
174
  return $map;
175
}
176

    
177
/**
178
 * Implements hook_help().
179
 */
180
function commerce_customer_ui_help($path, $arg) {
181
  switch ($path) {
182
    case 'admin/commerce/customer-profiles/types/add':
183
      return '<p>' . t('Individual customer profile types can have different fields assigned to them.') . '</p>';
184
  }
185

    
186
  // Return the user defined help text per customer profile type when adding profiles.
187
  if ($arg[1] == 'commerce' && $arg[2] == 'customer-profiles' && $arg[3] == 'add' && $arg[4]) {
188
    $profile_type = commerce_customer_profile_type_load($arg[4]);
189
    return (!empty($profile_type['help']) ? '<p>' . filter_xss_admin($profile_type['help']) . '</p>' : '');
190
  }
191
}
192

    
193
/**
194
 * Implements hook_theme().
195
 */
196
function commerce_customer_ui_theme() {
197
  return array(
198
    'customer_profile_add_list' => array(
199
      'variables' => array('content' => array()),
200
      'file' => 'includes/commerce_customer_ui.profiles.inc',
201
    ),
202
    'customer_profile_type_admin_overview' => array(
203
      'variables' => array('type' => NULL),
204
      'file' => 'includes/commerce_customer_ui.profile_types.inc',
205
    ),
206
    'commerce_customer_profile_status' => array(
207
      'variables' => array('status' => NULL, 'label' => NULL, 'profile' => NULL),
208
      'path' => drupal_get_path('module', 'commerce_customer_ui') . '/theme',
209
      'template' => 'commerce-customer-profile-status',
210
    ),
211
  );
212
}
213

    
214
/**
215
 * Implements hook_entity_info_alter().
216
 */
217
function commerce_customer_ui_entity_info_alter(&$entity_info) {
218
  // Add a URI callback to the profile entity.
219
  $entity_info['commerce_customer_profile']['uri callback'] = 'commerce_customer_ui_customer_profile_uri';
220

    
221
  // Expose the admin UI for profile fields.
222
  foreach ($entity_info['commerce_customer_profile']['bundles'] as $type => &$bundle) {
223
    $bundle['admin'] = array(
224
      'path' => 'admin/commerce/customer-profiles/types/' . strtr($type, '_', '-'),
225
      'real path' => 'admin/commerce/customer-profiles/types/' . strtr($type, '_', '-'),
226
      'access arguments' => array('administer customer profile types'),
227
    );
228
  }
229
}
230

    
231
/**
232
 * Entity uri callback: points to the edit form of the given profile.
233
 */
234
function commerce_customer_ui_customer_profile_uri($profile) {
235
  // First look for a return value in the default entity uri callback.
236
  $uri = commerce_customer_profile_uri($profile);
237

    
238
  // If a value was found, return it now.
239
  if (!empty($uri)) {
240
    return $uri;
241
  }
242

    
243
  // Only return a value if the user has permission to view the profile.
244
  if (commerce_customer_profile_access('view', $profile)) {
245
    return array(
246
      'path' => 'admin/commerce/customer-profiles/' . $profile->profile_id,
247
    );
248
  }
249

    
250
  return NULL;
251
}
252

    
253
/**
254
 * Implements hook_forms().
255
 */
256
function commerce_customer_ui_forms($form_id, $args) {
257
  $forms = array();
258

    
259
  // Define a wrapper ID for the customer profile add / edit form.
260
  $forms['commerce_customer_ui_customer_profile_form'] = array(
261
    'callback' => 'commerce_customer_customer_profile_form',
262
  );
263

    
264
  // Define a wrapper ID for the customer profile delete confirmation form.
265
  $forms['commerce_customer_ui_customer_profile_delete_form'] = array(
266
    'callback' => 'commerce_customer_customer_profile_delete_form',
267
  );
268

    
269
  return $forms;
270
}
271

    
272
/**
273
 * Implements hook_form_alter().
274
 */
275
function commerce_customer_ui_form_alter(&$form, &$form_state, $form_id) {
276
  // On field administration forms for customer profile types add a breadcrumb.
277
  if (in_array($form_id, array('field_ui_field_overview_form', 'field_ui_display_overview_form'))) {
278
    if ($form['#entity_type'] == 'commerce_customer_profile') {
279
      // Load the customer profile type being modified for this form.
280
      $profile_type = commerce_customer_profile_type_load($form['#bundle']);
281
      drupal_set_title($profile_type['name']);
282
    }
283
  }
284
}
285

    
286
/**
287
 * Implements hook_form_FORM_ID_alter().
288
 *
289
 * The Customer UI module instantiates the Profile add/edit form at particular
290
 * paths in the Commerce IA. It uses its own form ID to do so and alters the
291
 * form here to add in appropriate redirection and an additional button.
292
 *
293
 * @see commerce_customer_ui_customer_profile_form()
294
 */
295
function commerce_customer_ui_form_commerce_customer_ui_customer_profile_form_alter(&$form, &$form_state) {
296
  // Add a submit handler to the save button to add a redirect.
297
  $form['actions']['submit']['#submit'][] = 'commerce_customer_ui_customer_profile_form_submit';
298

    
299
  // Add the save and continue button for new profiles.
300
  if (empty($form_state['customer_profile']->profile_id)) {
301
    $form['actions']['save_continue'] = array(
302
      '#type' => 'submit',
303
      '#value' => t('Save and add another'),
304
      '#submit' => $form['actions']['submit']['#submit'],
305
      '#suffix' => l(t('Cancel'), 'admin/commerce/customer-profiles'),
306
      '#weight' => 45,
307
    );
308
  }
309
  else {
310
    $form['actions']['submit']['#suffix'] = l(t('Cancel'), 'admin/commerce/customer-profiles');
311
  }
312
}
313

    
314
/**
315
 * Submit callback for commerce_customer_ui_customer_profile_form().
316
 *
317
 * @see commerce_customer_ui_form_commerce_customer_ui_customer_profile_form_alter()
318
 */
319
function commerce_customer_ui_customer_profile_form_submit($form, &$form_state) {
320
  // Set the redirect based on the button clicked.
321
  if ($form_state['triggering_element']['#parents'][0] == 'save_continue') {
322
    $form_state['redirect'] = 'admin/commerce/customer-profiles/add/' . strtr($form_state['customer_profile']->type, array('_' => '-'));
323
  }
324
  elseif (arg(2) == 'customer-profiles' && arg(3) == 'add') {
325
    $form_state['redirect'] = 'admin/commerce/customer-profiles';
326
  }
327
}
328

    
329
/**
330
 * Implements hook_form_FORM_ID_alter().
331
 *
332
 * The Customer UI module instantiates the Profile delete form at a particular
333
 * path in the Commerce IA. It uses its own form ID to do so and alters the
334
 * form here to add in appropriate redirection.
335
 *
336
 * @see commerce_customer_ui_customer_profile_delete_form()
337
 */
338
function commerce_customer_ui_form_commerce_customer_ui_customer_profile_delete_form_alter(&$form, &$form_state) {
339
  $form['actions']['cancel']['#href'] = 'admin/commerce/customer-profiles';
340
  $form['#submit'][] = 'commerce_customer_ui_customer_profile_delete_form_submit';
341
}
342

    
343
/**
344
 * Submit callback for commerce_customer_ui_customer_profile_delete_form().
345
 *
346
 * @see commerce_customer_ui_form_commerce_customer_ui_customer_profile_delete_form_alter()
347
 */
348
function commerce_customer_ui_customer_profile_delete_form_submit($form, &$form_state) {
349
  $form_state['redirect'] = 'admin/commerce/customer-profiles';
350
}
351

    
352
/**
353
 * Implements hook_views_api().
354
 */
355
function commerce_customer_ui_views_api() {
356
  return array(
357
    'api' => 3,
358
    'path' => drupal_get_path('module', 'commerce_customer_ui') . '/includes/views',
359
  );
360
}
361

    
362
/**
363
 * Sets the breadcrumb for administrative customer pages.
364
 *
365
 * @param $profiles
366
 *   TRUE or FALSE indicating whether or not the breadcrumb should include the
367
 *     profiles overview page.
368
 * @param $profile_types
369
 *   TRUE or FALSE indicating whether or not the breadcrumb should include the
370
 *     profile types administrative page.
371
 *
372
 * @deprecated since 7.x-1.4
373
 */
374
function commerce_customer_ui_set_breadcrumb($profile_types = FALSE) {
375
  // This function used to manually set a breadcrumb that is now properly
376
  // generated by Drupal itself.
377
}
378

    
379
/**
380
 * Implements hook_form_FORM_ID_alter().
381
 */
382
function commerce_customer_ui_form_entity_translation_admin_form_alter(&$form, &$form_state, $form_id) {
383
  // Hide the commerce_customer_profile option from entity translation.
384
  unset($form['entity_translation_entity_types']['#options']['commerce_customer_profile']);
385
}