You are here

function _invoice_api_invoice_put in Invoice 7

Handles PUT request

Parameters

integer $invoiceId:

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

File

./invoice_api.inc, line 476

Code

function _invoice_api_invoice_put($invoiceId) {
  if (0 === $invoiceId) {
    _invoice_api_http_response_code(400);
    echo json_encode(array(
      'code' => 400,
      'message' => 'Invoice ID invalid or missing',
    ));
    exit;
  }
  else {
    $count = db_query("SELECT COUNT(iid) FROM {invoice_invoices} WHERE iid = :invoiceId", array(
      ':invoiceId' => $invoiceId,
    ))
      ->fetchField();
    if ($count < 1) {
      _invoice_api_http_response_code(404);
      echo json_encode(array(
        'code' => 404,
        'message' => 'Not Found',
      ));
      exit;
    }
  }
  _invoice_api_check_allowed_templates();
  $data = _invoice_api_get_request_data();
  $nid = db_query("SELECT nid FROM {invoice_invoices} WHERE iid = :invoiceId", array(
    ':invoiceId' => $invoiceId,
  ))
    ->fetchField();
  $invoiceModel = new InvoiceRestModel();
  $invoiceModel
    ->exchangeArray($data);
  $transaction = db_transaction();
  try {
    $node = node_load($nid);
    $invoiceModel
      ->mapToNode($node);

    // Set some default values
    node_object_prepare($node);
    if (!empty($node->menu) && isset($node->menu['link']['enabled'])) {

      // Calling menu_node_prepare(), menu_node_save()
      // will cause data loss unless the enabled flag is set.
      $node->menu['link']['enabled'] = (int) (bool) $node->menu['link']['mlid'];
    }
    $node->invoice_number = $invoiceId;
    $node->user_defined_invoice_number = '';

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

    // Save the node.
    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(204);

  // No content
}