You are here

function invoice_api_invoice in Invoice 7

GET / PUT / POST / DELETE an invoice

Parameters

string $requestMethod:

string $format Response format:

integer $invoiceId:

1 string reference to 'invoice_api_invoice'
invoice_menu in ./invoice.module
Implements hook_menu()

File

./invoice_api.inc, line 210

Code

function invoice_api_invoice($requestMethod, $format, $invoiceId = null) {
  if ($requestMethod != 'GET') {

    // Check anti-CSRF token header
    if (!isset($_SERVER['HTTP_X_CSRF_TOKEN']) || !drupal_valid_token($_SERVER['HTTP_X_CSRF_TOKEN'], '_invoice_api_session_token')) {
      _invoice_api_http_response_code(406);
      echo json_encode(array(
        'code' => 406,
        'message' => 'Token validation failed',
      ));
      exit;
    }
  }
  switch ($requestMethod) {
    case 'GET':
      if ($invoiceId > 0) {
        _invoice_api_invoice_get((int) $invoiceId, $format);
      }
      else {
        _invoice_api_invoice_get_list();
      }
      break;
    case 'POST':
      _invoice_api_invoice_post($format);
      break;
    case 'PUT':
      _invoice_api_invoice_put((int) $invoiceId);
      break;
    case 'DELETE':
      _invoice_api_http_response_code(501);
      echo json_encode(array(
        'code' => 501,
        'message' => 'Invoices may never be deleted,' . ' create a credit invoice instead.',
      ));
      exit;
      break;
    default:

      // Method not allowed
      _invoice_api_http_response_code(405);
      echo json_encode(array(
        'code' => 405,
        'message' => 'Method Not Allowed',
      ));
      exit;
  }
}