Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.27 KB | None | 0 0
  1. /**
  2.  * Main function that imports, updates or deletes item or bibliographic records.
  3.  * It can optionally take in a MARC record to bypass harvesting from the WebOPAC.
  4.  * @param $data
  5.  *   An associative array of data for a record, with the following keys:
  6.  *     - 'bib_recnum' (optional)
  7.  *       Millennium record number (b123456) for the record to process.
  8.  *     - 'item_recnum' (optional)
  9.  *       A Millennium record number (i123456) for the record to process.
  10.  *       If bib_recnum is also given, it is assumed it is the bib record
  11.  *       associated with this item.
  12.  *     - 'marc' (optional)
  13.  *       MARC text as it is exported from the WebOPAC.
  14.  * @param bool $force_update (Optional) States if the function should always update existing data.
  15.  */
  16. function millennium_process_record(&$data, $force_update = false) {
  17.   // Set defaults
  18.   if (!isset($data['marc'])) {
  19.     $data['marc'] = null;
  20.   }
  21.   if (!isset($data['item_recnum'])) {
  22.     $data['item_recnum'] = null;
  23.   }
  24.   if (!isset($data['bib_recnum'])) {
  25.     $data['bib_recnum'] = null;
  26.   }
  27.   #drupal_set_message("millennium_process_record():");
  28.  #dpm($data);
  29.  
  30.   $type = "";
  31.   // If only bib_recnum
  32.   if ($data['bib_recnum'] && !$data['item_recnum']) {
  33.     $type = "bib";
  34.   }
  35.   // If only item_recnum set, or if both record numbers set:
  36.   if (
  37.     (!$data['bib_recnum'] && $data['item_recnum']) ||
  38.     ($data['bib_recnum'] && $data['item_recnum'])) {
  39.     $type = "item";
  40.   }
  41.  
  42.   switch($type) {
  43.     case "bib":
  44.       $result = millennium_process_bib_record(
  45.         $data,
  46.         $force_update
  47.       );
  48.       break;
  49.     case "item":
  50.       $result = millennium_process_item_record(
  51.         $data,
  52.         $force_update
  53.       );
  54.       break;
  55.     default:
  56.       watchdog("Millennium", "millennium_process_record() unknown record type: @data", array('@data' => var_export($data, TRUE)));
  57.       return array("success" => false, "error" => "Could not determine function to call.");
  58.   }
  59.  
  60.   if ($result["node"]) {
  61.     $data["node"] = $result["node"];
  62.   }
  63.   #dpm($result);
  64.  // Log errors
  65.   if ($result['success'] !== true) {
  66.     watchdog("Millennium", "millennium_process_record() failed with error @error", array('@error' => $result['error']));
  67.   }
  68.  
  69.   return array("success" => $result['result'], 'status' => $result['status'], 'error' => $result['error']);
  70. }
  71.  
  72. /**
  73.  * Imports or updates Millennium item records in Drupal
  74.  * @param array $data Millennium record information
  75.  * @param bool $force_update (Optional) States if an existing node for that item record number should be updated.
  76.  */
  77. function millennium_process_item_record($data, $force_update = true) {
  78.   #drupal_set_message("millennium_process_item_record($item_recnum, $force_update, $marc_text, $bib_recnum)");
  79.  
  80.   if ($data['base_url']) {
  81.     $base_url = $data['base_url'];
  82.   } else {
  83.     return array("success" => false, "error" => "No base_url in item #" . $data['item_recnum']);
  84.   }
  85.  
  86.   // Had we already imported this item record?
  87.   $item_import_history = db_fetch_object(db_query(
  88.     "SELECT * FROM {millennium_bib_item} WHERE item_recnum='%s' AND base_url = '%s'",
  89.     $data['item_recnum'],
  90.     $base_url
  91.     ));
  92.   if ($item_import_history->nid) {
  93.     #drupal_set_message("Item $item_recnum was imported before");
  94.    // Item was imported before.
  95.     $status = millennium_process_item_record_prev_imported($data, $item_import_history, $force_update = true);
  96.     $node = node_load($item_import_history->nid);
  97.     return array(
  98.       "success" => true,
  99.       "status" => $status['result'],
  100.       "node" => $node,
  101.       #"error" => t("This item #@item_number had already been imported under nid @nid", array("@item_number" => $item_recnum, "@nid" => $item_import_history->nid))
  102.    );
  103.   } else {
  104.     // Item was not imported before.
  105.     // If not given as the function's argument, get the item's bib record number.
  106.     if ($bib_recnum == null) {
  107.       $result = millennium_fetch_recordpage($data['item_recnum'], $base_url, 'plain');
  108.       $record_html = $result->data;
  109.       $ok = preg_match('/\/record=(b[0-9]+)/si', $record_html, $matches);
  110.       if ($ok) {
  111.         $bib_recnum = $matches[1];
  112.       }
  113.     }
  114.   }
  115.  
  116.   if (!$bib_recnum) {
  117.     return array("success" => false, "error" => "Could not get bib_recnum for item #" . $data['item_recnum']);
  118.   }
  119.  
  120.   // We have a bib_number by now.
  121.   // Is there an existing node with this bib_number?
  122.   $bib_import_history = db_fetch_object(db_query(
  123.     "SELECT * FROM {millennium_node_bib} WHERE bib_recnum='%s' AND base_url = '%s'",
  124.     $bib_recnum,
  125.     $base_url
  126.     ));
  127.   if (!$bib_import_history->nid) {
  128.     // New item, new bib.
  129.     // Must the node for the bib before the item can be imported.
  130.     $data = array('bib_recnum' => $bib_recnum, 'marc' => $marc_text);
  131.     $result = millennium_process_bib_record($data, $force_update);
  132.     if ($result["success"] != true) {
  133.       return array("success" => false, "error" => "Could not create a new node for item: " . $data['item_recnum'] . " and bib: " . $data['bib_recnum']);
  134.     } else {
  135.       $nid = $result["node"]->nid;
  136.     }
  137.   } else {
  138.     $nid = $bib_import_history->nid;
  139.   }
  140.  
  141.   // Do actual import of new item.
  142.   $ok = millennium_process_item_record_new_import($data, $nid);
  143.   if ($ok) {
  144.     return array('success' => true, 'status' => 'item_created');
  145.   } else {
  146.     return array('success' => false, 'error' => "Could not create a new item for item $item_recnum");
  147.   }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement