View source
<?php
function commerce_recurring_ui_menu() {
$items = array();
$items['admin/commerce/recurring-entities/types'] = array(
'title' => 'Recurring entity types',
'description' => 'Manage recurring entity types for your store.',
'page callback' => 'commerce_recurring_ui_types_overview',
'access arguments' => array(
'administer commerce_recurring entities',
),
'type' => MENU_LOCAL_TASK,
'weight' => 0,
'file' => 'commerce_recurring_ui.admin.inc',
);
foreach (commerce_recurring_types() as $type) {
$type_arg = strtr($type, '_', '-');
$title = ucfirst(strtr($type, '_', ' '));
$items['admin/commerce/recurring-entities/types/' . $type_arg] = array(
'title' => $title,
'page callback' => 'commerce_recurring_ui_entity_type_redirect',
'page arguments' => array(
$type,
),
'access arguments' => array(
'administer commerce_recurring entities',
),
'file' => 'commerce_recurring_ui.admin.inc',
);
}
$items['admin/commerce/recurring-entities/%commerce_recurring/edit'] = array(
'title' => 'Edit',
'page callback' => 'entity_ui_get_form',
'page arguments' => array(
'commerce_recurring',
3,
),
'access callback' => 'commerce_recurring_access',
'access arguments' => array(
'update',
3,
),
'type' => MENU_LOCAL_TASK,
'weight' => -5,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'commerce_recurring_ui.admin.inc',
);
$items['admin/commerce/recurring-entities/%commerce_recurring/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_recurring_ui_delete_confirm_form',
3,
),
'access callback' => 'commerce_recurring_access',
'access arguments' => array(
'delete',
3,
),
'type' => MENU_LOCAL_TASK,
'weight' => 20,
'context' => MENU_CONTEXT_INLINE,
'file' => 'commerce_recurring_ui.admin.inc',
);
$items['user/%user/recurring/%commerce_recurring/cancel/%'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_recurring_ui_cancel_renewals_form',
1,
3,
5,
),
'access callback' => 'commerce_recurring_ui_cancel_access',
'access arguments' => array(
'cancel own subscription renewals',
3,
),
'description' => 'Cancel subscription renewals',
'type' => MENU_CALLBACK,
);
return $items;
}
function commerce_recurring_ui_menu_alter(&$items) {
foreach (commerce_recurring_types() as $type) {
$type_arg = strtr($type, '_', '-');
$items['admin/commerce/recurring-entities/types/' . $type_arg . '/fields']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
$items['admin/commerce/recurring-entities/types/' . $type_arg . '/display']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
}
}
function commerce_recurring_ui_permission() {
$permissions = array();
$permissions['cancel own subscription renewals'] = array(
'title' => t('Cancel Own Subscription Renewals'),
);
return $permissions;
}
function commerce_recurring_ui_entity_info_alter(&$entity_info) {
foreach ($entity_info['commerce_recurring']['bundles'] as $type => &$bundle) {
$bundle['admin'] = array(
'path' => 'admin/commerce/recurring-entities/types/' . strtr($type, '_', '-'),
'real path' => 'admin/commerce/recurring-entities/types/' . strtr($type, '_', '-'),
'access arguments' => array(
'administer commerce_recurring entities',
),
);
$entity_info['commerce_recurring']['admin ui'] = array(
'path' => 'admin/commerce/recurring-entities',
'file' => 'commerce_recurring_ui.admin.inc',
);
}
}
function commerce_recurring_ui_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'commerce_recurring_ui') . '/includes/views',
);
}
function commerce_recurring_ui_cancel_renewals_form($form, &$form_state, $user, $recurring_entity, $token = '') {
if (drupal_valid_token($token, $recurring_entity->id)) {
$my_subscriptions_path = 'user/' . $user->uid . '/user-recurring-entities';
if (!$recurring_entity->status) {
drupal_set_message(t("Subscription @id has already been cancelled. You will not be charged again for this subscription.", array(
"@id" => $recurring_entity->id,
)));
drupal_goto($my_subscriptions_path);
}
$form['id'] = array(
'#value' => $recurring_entity->id,
);
$confirm_question = t('Are you sure you want to cancel your subscription renewal?');
$confirm_button = t('Cancel subscription renewal');
return confirm_form($form, $confirm_question, $my_subscriptions_path, t('This action cannot be undone.'), $confirm_button, t('Return to My Subscriptions'));
}
}
function commerce_recurring_ui_cancel_access($permission, $recurring_entity) {
global $user;
return user_access('edit any commerce_recurring entity', $user) || user_access($permission, $user) && $recurring_entity->uid == $user->uid;
}
function commerce_recurring_ui_cancel_renewals_form_submit($form, &$form_state) {
$recurring_entity_id = $form['id']['#value'];
$recurring_entity = commerce_recurring_load($recurring_entity_id);
if ($form_state['values']['confirm']) {
commerce_recurring_stop_recurring($recurring_entity);
drupal_set_message(t('The subscription @id was cancelled, you will not be charged again for this subscription.', array(
'@id' => $recurring_entity_id,
)));
}
$form_state['redirect'] = 'user/' . $recurring_entity->uid . '/user-recurring-entities';
}