You are here

function _invoice_api_invoice_post in Invoice 7

Handles POST request

Parameters

string $format Response format:

1 call to _invoice_api_invoice_post()
invoice_api_invoice in ./invoice_api.inc
GET / PUT / POST / DELETE an invoice

File

./invoice_api.inc, line 427

Code

function _invoice_api_invoice_post($format) {
  _invoice_api_check_allowed_templates();
  $data = _invoice_api_get_request_data();
  $invoiceModel = new InvoiceRestModel();
  $invoiceModel
    ->exchangeArray($data);
  $transaction = db_transaction();
  try {
    $node = new stdClass();
    $node->type = 'invoice';
    $node->title = null;
    $node->language = LANGUAGE_NONE;
    $node->invoice_number = null;
    $node->user_defined_invoice_number = '';
    $invoiceModel
      ->mapToNode($node);

    // Set some default values
    node_object_prepare($node);

    // Prepare node for a submit
    $node = node_submit($node);

    // Save the node. A nid property is available after this call.
    node_save($node);
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('invoice_api', $e);
    _invoice_api_http_response_code(500);
    exit;
  }
  _invoice_api_http_response_code(201);
  $uri = _invoice_get_transfer_protocol() . '://' . $_SERVER['HTTP_HOST'] . '/invoice/api/invoice.' . $format . '/' . $node->invoice_number;
  echo json_encode(array(
    '_links' => array(
      'self' => array(
        'href' => $uri,
      ),
    ),
  ));
  drupal_add_http_header('Location', $uri);
}