You are here

function library_node_update in Library 7

Implements hook_node_update().

As an existing node is being updated, we need to add our own updates.

File

./library.module, line 674

Code

function library_node_update($node) {
  if (variable_get('library_' . $node->type, LIBRARY_ITEM_NOT_IN_LIBRARY) == LIBRARY_ITEM_IN_LIBRARY) {
    $items = $node->library_items;
    if ($items) {
      foreach ($items as $key => $item) {
        if (isset($item['delete']) && $item['delete'] == 1) {
          $nid = db_delete('library_transactions')
            ->condition('item_id', $item['id'])
            ->execute();
          $nid = db_delete('library')
            ->condition('id', $item['id'])
            ->execute();
          unset($node->library_items[$key]);
        }
        else {
          $lid = db_select('library')
            ->fields('library', array(
            'id',
          ))
            ->condition('id', $item['id'], '=')
            ->execute();
          if ($lid
            ->rowCount() == 1) {
            $updated = db_update('library')
              ->fields(array(
              'barcode' => isset($item['barcode']) ? check_plain($item['barcode'] . '') : '',
              'in_circulation' => $item['in_circulation'],
              'notes' => check_plain($item['notes'] . ''),
            ))
              ->condition('id', $item['id'])
              ->execute();
          }
          elseif ($lid
            ->rowCount() == 0) {

            // Notice that we are ignoring any revision information
            // using $node->nid.
            $inserted = db_insert('library')
              ->fields(array(
              'nid' => $node->nid,
              'barcode' => isset($item['barcode']) ? check_plain($item['barcode'] . '') : '',
              'in_circulation' => $item['in_circulation'],
              'library_status' => LIBRARY_ITEM_AVAILABLE,
              'notes' => check_plain($item['notes'] . ''),
              'created' => REQUEST_TIME,
            ))
              ->execute();
          }
          else {
            watchdog('library', 'Database inconsistency.', WATCHDOG_ERROR);
          }
        }
      }
    }
  }
}