Projet

Général

Profil

Paste
Télécharger (10,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / payment / commerce_payment_ui.module @ 280fe687

1
<?php
2

    
3
/**
4
 * @file
5
 * Default Payment UI for Drupal Commerce.
6
 */
7

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

    
14
  // Payment tab on orders.
15
  $items['admin/commerce/orders/%commerce_order/payment'] = array(
16
    'title' => 'Payment',
17
    'page callback' => 'commerce_payment_ui_order_tab',
18
    'page arguments' => array(3),
19
    'access callback' => 'commerce_payment_transaction_order_access',
20
    'access arguments' => array('view', 3),
21
    'type' => MENU_LOCAL_TASK,
22
    'weight' => 10,
23
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
24
    'file' => 'includes/commerce_payment_ui.admin.inc',
25
  );
26

    
27
  // Payment transaction operations links.
28
  $items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction'] = array(
29
    'title callback' => 'commerce_payment_ui_payment_transaction_title',
30
    'title arguments' => array(5),
31
    'page callback' => 'commerce_payment_ui_payment_transaction_view',
32
    'page arguments' => array(3, 5, 'administrator'),
33
    'access callback' => 'commerce_payment_transaction_access',
34
    'access arguments' => array('view', 5),
35
    'file' => 'includes/commerce_payment_ui.admin.inc',
36
  );
37

    
38
  $items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/view'] = array(
39
    'title' => 'View',
40
    'type' => MENU_DEFAULT_LOCAL_TASK,
41
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
42
    'weight' => 0,
43
  );
44

    
45
  $items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/delete'] = array(
46
    'title' => 'Delete',
47
    'page callback' => 'commerce_payment_ui_payment_transaction_delete_form_wrapper',
48
    'page arguments' => array(3, 5),
49
    'access callback' => 'commerce_payment_transaction_access',
50
    'access arguments' => array('delete', 5),
51
    'type' => MENU_LOCAL_TASK,
52
    'context' => MENU_CONTEXT_INLINE,
53
    'weight' => 10,
54
    'file' => 'includes/commerce_payment_ui.admin.inc',
55
  );
56

    
57
  // Payment method Rules administration page.
58
  $items['admin/commerce/config/payment-methods'] = array(
59
    'title' => 'Payment methods',
60
    'description' => 'Enable and configure payment method rule configurations.',
61
    'page callback' => 'commerce_payment_ui_admin_page',
62
    'access arguments' => array('administer payment methods'),
63
    'file' => 'includes/commerce_payment_ui.admin.inc',
64
  );
65

    
66
  // Add the menu items for the various Rules forms.
67
  $controller = new RulesUIController();
68
  $items += $controller->config_menu('admin/commerce/config/payment-methods');
69

    
70
  $items['admin/commerce/config/payment-methods/add'] = array(
71
    'title' => 'Add a payment method rule',
72
    'description' => 'Adds an additional payment method rule configuration.',
73
    'page callback' => 'drupal_get_form',
74
    'page arguments' => array('commerce_payment_ui_add_payment_rule_form', 'admin/commerce/config/payment-methods'),
75
    'access arguments' => array('administer payment methods'),
76
    'file path' => drupal_get_path('module', 'rules_admin'),
77
    'file' => 'rules_admin.inc',
78
  );
79

    
80
  return $items;
81
}
82

    
83
/**
84
 * Menu item title callback: returns the transaction ID for its pages.
85
 *
86
 * @param $transaction
87
 *   The transaction object as loaded via the URL wildcard.
88
 * @return
89
 *   A page title of the format "Transaction ##".
90
 */
91
function commerce_payment_ui_payment_transaction_title($transaction) {
92
  return t('Transaction @transaction_id', array('@transaction_id' => $transaction->transaction_id));
93
}
94

    
95
/**
96
 * Implements hook_menu_local_tasks_alter().
97
 */
98
function commerce_payment_ui_menu_local_tasks_alter(&$data, $router_item, $root_path) {
99
  // Add action link 'admin/commerce/config/payment-methods/add' on
100
  // 'admin/commerce/config/payment-methods'.
101
  if ($root_path == 'admin/commerce/config/payment-methods') {
102
    $item = menu_get_item('admin/commerce/config/payment-methods/add');
103
    if ($item['access']) {
104
      $data['actions']['output'][] = array(
105
        '#theme' => 'menu_local_action',
106
        '#link' => $item,
107
      );
108
    }
109
  }
110
}
111

    
112
/**
113
 * Implements hook_help().
114
 */
115
function commerce_payment_ui_help($path, $arg) {
116
  switch ($path) {
117
    case 'admin/commerce/config/payment-methods':
118
      return t("Payment methods are enabled for use by the rule configurations listed below. An enabled payment rule can specify a payment method to enable using one of the available <em>Enable payment method</em> actions. The action's settings form will contain any necessary settings for the payment method that must be configured before it may be used.");
119

    
120
    case 'admin/commerce/config/payment-methods/add':
121
      return t("Submitting this form will create a new rule configuration that enables the selected payment method. You will need to edit the action on the rule to configure the payment method settings. You can also add any conditions that must be met for the payment method to be available on the checkout form, such as a comparison against the customer's country or the order total.");
122
  }
123
}
124
/**
125
 * Implements hook_entity_info_alter().
126
 */
127
function commerce_payment_ui_entity_info_alter(&$entity_info) {
128
  // Add a URI callback to the profile entity.
129
  $entity_info['commerce_payment_transaction']['uri callback'] = 'commerce_payment_ui_payment_transaction_uri';
130
}
131

    
132
/**
133
 * Entity uri callback: points to the admin view page of the given payment
134
 * transaction.
135
 */
136
function commerce_payment_ui_payment_transaction_uri($transaction) {
137
  // First look for a return value in the default entity uri callback.
138
  $uri = commerce_payment_transaction_uri($transaction);
139

    
140
  // If a value was found, return it now.
141
  if (!empty($uri)) {
142
    return $uri;
143
  }
144

    
145
  // Only return a value if the transaction references an order and the user has
146
  // permission to view the payment transaction.
147
  if ($order = commerce_order_load($transaction->order_id)) {
148
    if (commerce_payment_transaction_access('view', $transaction)) {
149
      return array(
150
        'path' => 'admin/commerce/orders/' . $order->order_id . '/payment/' . $transaction->transaction_id . '/view',
151
      );
152
    }
153
  }
154

    
155
  return NULL;
156
}
157

    
158
/**
159
 * Implements hook_views_api().
160
 */
161
function commerce_payment_ui_views_api() {
162
  return array(
163
    'api' => 3,
164
    'path' => drupal_get_path('module', 'commerce_payment_ui') . '/includes/views',
165
  );
166
}
167

    
168
/**
169
 * Implements hook_form_FORM_ID_alter().
170
 *
171
 * Adds a Cancel link to the Save button for the payment terminal form that is
172
 * part of the View used as the order Payment tab.
173
 */
174
function commerce_payment_ui_form_commerce_payment_order_transaction_add_form_alter(&$form, &$form_state) {
175
  if (!empty($form_state['payment_method'])) {
176
    $form['actions']['submit']['#suffix'] = l(t('Cancel'), 'admin/commerce/orders/' . $form_state['order']->order_id . '/payment');
177
  }
178
}
179

    
180
/**
181
 * Implements hook_forms().
182
 */
183
function commerce_payment_ui_forms($form_id, $args) {
184
  $forms = array();
185

    
186
  // Define a wrapper ID for the payment transaction delete confirmation form.
187
  $forms['commerce_payment_ui_payment_transaction_delete_form'] = array(
188
    'callback' => 'commerce_payment_payment_transaction_delete_form',
189
  );
190

    
191
  $forms['commerce_payment_ui_add_payment_rule_form'] = array(
192
    'callback' => 'rules_admin_add_reaction_rule',
193
  );
194

    
195
  return $forms;
196
}
197

    
198
/**
199
 * Implements hook_form_FORM_ID_alter().
200
 *
201
 * The Payment UI module instantiates the payment transaction delete form at a
202
 * particular path in the Commerce IA. It uses its own form ID to do so and
203
 * alters the form here to add in appropriate redirection.
204
 *
205
 * @see commerce_payment_ui_payment_transaction_delete_form()
206
 */
207
function commerce_payment_ui_form_commerce_payment_ui_payment_transaction_delete_form_alter(&$form, &$form_state) {
208
  $form['actions']['cancel']['#href'] = 'admin/commerce/orders/' . $form_state['order']->order_id . '/payment';
209
  $form['#submit'][] = 'commerce_payment_ui_payment_transaction_delete_form_submit';
210
}
211

    
212
/**
213
 * Submit callback for commerce_payment_ui_payment_transaction_delete_form().
214
 *
215
 * @see commerce_payment_ui_form_commerce_payment_ui_payment_transaction_delete_form_alter()
216
 */
217
function commerce_payment_ui_payment_transaction_delete_form_submit($form, &$form_state) {
218
  $form_state['redirect'] = 'admin/commerce/orders/' . $form_state['order']->order_id . '/payment';
219
}
220

    
221
/**
222
 * Implements hook_form_FORM_ID_alter().
223
 *
224
 * The Payment UI module instantiates the Rules Admin rule configuration add
225
 * form at a particular path in the Commerce IA. It uses its own form ID to do
226
 * so and alters the form here to select the necessary Rules event. It also lets
227
 * the user specify which payment method to enable through the Rule.
228
 *
229
 * @see commerce_payment_ui_add_payment_rule_form_submit()
230
 * @see rules_admin_add_reaction_rule()
231
 */
232
function commerce_payment_ui_form_commerce_payment_ui_add_payment_rule_form_alter(&$form, &$form_state) {
233
  unset($form['settings']['help']);
234
  $form['settings']['event']['#type'] = 'value';
235
  $form['settings']['event']['#value'] = 'commerce_payment_methods';
236

    
237
  $form['method_id'] = array(
238
    '#type' => 'select',
239
    '#title' => t('Payment method'),
240
    '#options' => commerce_payment_method_get_title(),
241
    '#required' => TRUE,
242
  );
243

    
244
  // Add function call to the beginning of the submit callback array
245
  array_unshift($form['#submit'],'commerce_payment_ui_add_payment_rule_form_submit');
246

    
247
  $form['submit']['#suffix'] = l(t('Cancel'), 'admin/commerce/config/payment-methods');
248
}
249

    
250
/**
251
 * Submit callback for commerce_payment_ui_form_commerce_payment_ui_add_payment_rule_form_alter().
252
 */
253
function commerce_payment_ui_add_payment_rule_form_submit($form, &$form_state) {
254
  // Enable the selected payment method on the Rule.
255
  $method_id = $form_state['values']['method_id'];
256

    
257
  $form_state['rules_config']
258
    ->action('commerce_payment_enable_' . $method_id, array(
259
      'commerce_order:select' => 'commerce-order',
260
      'payment_method' => $method_id,
261
    ));
262
}
263

    
264
/**
265
 * Sets the breadcrumb for transaction pages.
266
 *
267
 * @param $order
268
 *   The order object the transaction is for.
269
 * @param $view_mode
270
 *   The view mode for the current order page, 'administrator' only for now.
271
 *
272
 * @deprecated since 7.x-1.4
273
 */
274
function commerce_payment_ui_set_order_breadcrumb($order, $view_mode = 'administrator') {
275
  // This function used to manually set a breadcrumb that is now properly
276
  // generated by Drupal itself.
277
}