You are here

commerce_recurring_ui.module in Commerce Recurring Framework 7.2

File

commerce_recurring_ui/commerce_recurring_ui.module
View source
<?php

/**
 * Implements hook_menu().
 */
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) {

    // Convert underscores to hyphens for the menu item argument.
    $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;
}

/**
 * Implements hook_menu_alter().
 */
function commerce_recurring_ui_menu_alter(&$items) {

  // Transform the field UI tabs into contextual links.
  foreach (commerce_recurring_types() as $type) {

    // Convert underscores to hyphens for the menu item argument.
    $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;
  }
}

/**
 * Implements hook_permission().
 */
function commerce_recurring_ui_permission() {
  $permissions = array();
  $permissions['cancel own subscription renewals'] = array(
    'title' => t('Cancel Own Subscription Renewals'),
  );
  return $permissions;
}

/**
 * Implements hook_entity_info_alter().
 */
function commerce_recurring_ui_entity_info_alter(&$entity_info) {

  // Expose the admin UI for commerce_recurring fields.
  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',
    );
  }
}

/**
 * Implements hook_views_api().
 */
function commerce_recurring_ui_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'commerce_recurring_ui') . '/includes/views',
  );
}

/**
 * Cancel a single recurring subscription form
 * @param $form
 * @param $form_state
 * @param $id
 * @return mixed
 */
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);
    }

    // If we get to here, we're allowed to cancel the subscription.
    $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'));
  }
}

/**
 * Does the user have access to cancel the recurring entity
 * @param $permission
 * @param $recurring_entity
 * @return bool
 */
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;
}

/**
 * Cancel subscription submit handler
 * @param $form
 * @param $form_state
 */
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';
}