You are here

function library_node_load in Library 7

Implements hook_node_load().

File

./library.module, line 528

Code

function library_node_load($nodes, $types) {
  foreach ($nodes as $node) {
    if (variable_get('library_' . $node->type, LIBRARY_ITEM_NOT_IN_LIBRARY) == LIBRARY_ITEM_IN_LIBRARY) {
      $nids[] = $node->nid;
    }
  }
  if (!isset($nids) || !count($nids)) {
    return;
  }

  // Make sure every eligible node has at least one library item instance.
  $new_to_library = db_select('node', 'n')
    ->fields('n', array(
    'nid',
  ))
    ->where('n.nid IN (:nids) AND n.nid NOT IN (SELECT nid FROM {library})', array(
    ':nids' => $nids,
  ))
    ->execute();
  foreach ($new_to_library as $record) {
    $nid = db_insert('library')
      ->fields(array(
      'nid' => $record->nid,
      'in_circulation' => LIBRARY_CIRCULATION,
      'library_status' => LIBRARY_ITEM_AVAILABLE,
      'created' => REQUEST_TIME,
    ))
      ->execute();
  }

  // Load library items associated with this node.
  $result = db_select('library', 'l')
    ->fields('l', array(
    'id',
    'barcode',
    'nid',
    'in_circulation',
    'library_status',
    'notes',
    'created',
  ))
    ->where('l.nid IN (:nids)', array(
    ':nids' => $nids,
  ))
    ->orderBy('l.nid')
    ->execute();
  $row = 0;
  foreach ($result as $item) {
    $nodes[$item->nid]->library_items[$row] = array(
      'id' => $item->id,
      'barcode' => check_plain($item->barcode),
      'nid' => $item->nid,
      'in_circulation' => $item->in_circulation,
      'library_status' => $item->library_status,
      'notes' => $item->notes,
      'created' => $item->created,
    );
    if ($item->in_circulation == LIBRARY_CIRCULATION && $item->library_status == LIBRARY_ITEM_UNAVAILABLE) {
      $last = library_get_last_transaction_by_item($item, LIBRARY_ACTION_TYPE_UNAVAILABLE);
      if ($last) {
        $nodes[$item->nid]->library_items[$row]['last_patron_uid'] = $last->uid;
        $nodes[$item->nid]->library_items[$row]['last_transaction_id'] = $last->tid;
        $nodes[$item->nid]->library_items[$row]['last_transaction_name'] = $last->action_name;
        if (!empty($last->duedate)) {
          $nodes[$item->nid]->library_items[$row]['last_due_date'] = $last->duedate;
        }
      }
    }
    $row++;
  }
}