<?php
// $Id: ec_devel.module,v 1.2.2.1.2.5 2007/02/17 10:18:57 sammys Exp $

/**
 * Implementation of hook_help()
 */
function ec_devel_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('<b>EC:</b> Add additional debug features to E-Commerce');
      break;
  }
}

/**
 * Implementation of hook_menu()
 */
function ec_devel_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'cart/review/checkout',
      'title' => t('checkout'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
    );
    $items[] = array(
      'path' => 'cart/review/object',
      'title' => t('object structure'),
      'callback' => 'ec_devel_checkout',
      'access' => user_access('access devel information'),
      'type' => MENU_LOCAL_TASK,
    );

    if (module_exist('ec_recurring')) {
      $items[] = array(
        'path' => 'admin/store/settings/ec_recurring_debug',
        'title' => t('ec_recurring debugging'),
        'callback' => 'ec_recurring_debugging',
        'access' => user_access('administer expiry schedules'),
      );
      $items[] = array(
        'path' => 'admin/store/settings/ec_recurring_debug/expirations',
        'title' => t('debug ec_recurring expirations'),
        'callback' => 'ec_recurring_debug_expirations',
        'access' => user_access('administer expiry schedules'),
        'type' => MENU_CALLBACK,
      );
      
    }
  }
  return $items;
}

/**
 * Implementation of hook_invoiceapi()
 */
function ec_devel_invoiceapi($invoice, $op) {
  if (is_string($invoice) && $op == 'form') {
    if (user_access('create invoice') && user_access('access devel information')) {
      return 'object structure';
    }
    else {
      return;
    }
  }

  switch ($op) {
    case 'form':
      $form[] = array('#value' => ec_devel_object($invoice));
      return $form;
      break;
  }
}

/**
 * ec_devel_checkout()
 */
function ec_devel_checkout() {
  $txn = ec_checkout_get_data();
  return ec_devel_object($txn);
}

/**
 * ec_devel_object()
 */
function ec_devel_object($object) {

  foreach ($object as $field => $value) {
    if (is_null($value)) {
      $printed_value = 'NULL';
    }
    else if (is_array($value) || is_object($value)) {
      ob_start();
      print_r($value);
      $printed_value = ob_get_clean();
      $printed_value = '<pre>'. check_plain($printed_value) .'</pre>';
    }
    else {
      $printed_value = check_plain($value);
    }

    $output .= theme('box', $field, $printed_value);
  }

  return $output;
}
