Created
February 24, 2017 11:34
-
-
Save elizoller/01a161c68d0168b7d38b30f42e0036f1 to your computer and use it in GitHub Desktop.
Acquisitions Import Module Snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function acquisitions_import_views_query_alter(&$view, &$query) { | |
if ($view->name == "new_book_list" && ($view->vid == 64 || $view->vid == 65)){ | |
$special_letters = array("A", "C", "D", "J", "K", "L", "M", "P", "S", "U", "V", "Z"); | |
$special_letters_with_children = _get_special_letter_children($special_letters); | |
//if the query filter contains one of the special letters in the filter then loop through all the terms in the taxo list and select all of the terms that start with the special letter | |
if (isset($query->where[1]['conditions'])){ | |
foreach($query->where[1]['conditions'] as $key=>$condition){ | |
if ($condition['field'] == "field_data_field_lc_class_taxonomy.field_lc_class_taxonomy_tid"){ | |
if (is_array($condition['value'])){ | |
//an array of values is set so loop through them | |
foreach($condition['value'] as $val){ | |
$term = taxonomy_term_load($val); | |
$name = $term->name; | |
if (in_array($name, $special_letters)){ | |
unset($condition['value'][$val]); | |
$condition['value'] = array_merge($condition['value'], $special_letters_with_children[$name]); | |
$condition['operator'] = "IN"; | |
} | |
} | |
} else { | |
$term = taxonomy_term_load($condition['value']); | |
$name = $term->name; | |
if (in_array($name, $special_letters)){ | |
$condition['value'] = $special_letters_with_children[$name]; | |
$condition['operator'] = "IN"; | |
} | |
} | |
} | |
$query->where[1]['conditions'][$key]['value'] = $condition['value']; | |
$query->where[1]['conditions'][$key]['operator'] = $condition['operator']; | |
} | |
} | |
} | |
} | |
function _get_special_letter_children($special_letters){ | |
//helper function to load all of the terms in the lc_classification list and map them to the special letters array so each letter contains an array of its child letters | |
$name = 'lc_classification'; | |
$myvoc = taxonomy_vocabulary_machine_name_load($name); | |
$tree = taxonomy_get_tree($myvoc->vid); | |
$special_letters_with_children = array(); | |
foreach($special_letters as $let){ | |
$special_letters_with_children[$let] = array(); | |
} | |
foreach ($tree as $term) { | |
foreach ($special_letters as $let){ | |
if (($term->name[0] == $let) && $term->name != "Unknown"){ | |
array_push($special_letters_with_children[$let], $term->tid); | |
} | |
} | |
} | |
return $special_letters_with_children; | |
} | |
function acquisitions_import_form_alter(&$form, $form_state, $form_id) { | |
if ($form_id == "views_exposed_form" && $form_state['view']->name == 'new_book_list'){ | |
$query = drupal_get_query_parameters(); | |
$special_letters = array("A", "C", "D", "J", "K", "L", "M", "P", "S", "U", "V", "Z"); | |
$extra_special = array("PN", "PQ", "PR", "PS", "PT", "PZ"); | |
if (isset($query['op']) && $query['op'] == 'RSS Feed'){ | |
acquisitions_import_rss_feed_link($form, $form_state); | |
} | |
if (isset($form['field_lc_class_taxonomy_tid_1']['#options'])){ | |
$empty_descs = array(); | |
foreach ($form['field_lc_class_taxonomy_tid_1']['#options'] as $id=>$opt){ | |
$tid = $id; | |
$term = taxonomy_term_load($tid); | |
$name = $term->name; | |
$desc = $term->description; | |
//if the the whole term is one of the special letters then change below to elsif | |
if (in_array($name, $special_letters)){ | |
//its a special letter we better show it | |
$form['field_lc_class_taxonomy_tid_1']['#options'][$id] = $name . " - " . $desc; | |
} elseif (($desc != "" && $desc != "hidden") && !in_array($name[0], $special_letters)){//if the desc is empty or the term doesn't start with one of the special letters then show it | |
$form['field_lc_class_taxonomy_tid_1']['#options'][$id] = $name . " - " . $desc; | |
} elseif ($desc == "hidden"){ | |
unset($form['field_lc_class_taxonomy_tid_1']['#options'][$id]); | |
} elseif (in_array($name, $extra_special)){ //these are some extra super duper special terms which must be shown | |
$form['field_lc_class_taxonomy_tid_1']['#options'][$id] = $name . " - " . $desc; | |
} else { //desc is empty or its a special letter so remove it from the filter | |
$empty_descs[] = array("tid"=>$tid, "name"=>$name); | |
unset($form['field_lc_class_taxonomy_tid_1']['#options'][$id]); | |
} | |
} | |
$message = "The following terms have empty descriptions:<ul>"; | |
foreach($empty_descs as $key=>$term){ | |
$message .= "<li><a href='https://library.northeastern.edu/taxonomy/term/".$term['tid']."/edit'>".$term['tid']."</a> : ".$term['name']."</li>"; | |
} | |
$message .= "</ul>"; | |
watchdog("taxonomy terms", $message); | |
} | |
if (isset($form['field_material_type_tid']['#options'])){ | |
foreach($form['field_material_type_tid']['#options'] as $id=>$opt){ | |
$tid = $id; | |
$term = taxonomy_term_load($tid); | |
$name = $term->name; | |
$desc = $term->description; | |
if ($desc == "hidden"){ | |
unset($form['field_material_type_tid']['#options'][$id]); | |
} | |
} | |
} | |
$form['csv_export'] = array( | |
'#type' => 'submit', | |
'#value' => t('CSV Export'), | |
'#submit' => array('acquisitions_import_csv_export_link'), | |
'#weight' => 10, | |
); | |
$form['rss_feed'] = array( | |
'#type' => 'button', | |
'#value' => t('RSS Feed'), | |
'#submit' => array('acquisitions_import_rss_feed_link'), | |
'#weight' => 10, | |
); | |
} | |
} | |
function acquisitions_import_obj_sort($x, $y){ | |
$xtid = array_keys($x->option)[0]; | |
$ytid = array_keys($y->option)[0]; | |
return strcasecmp($x->option[$xtid], $y->option[$ytid]); | |
} | |
function acquisitions_import_csv_export_link($form, &$form_state){ | |
$query = drupal_get_query_parameters(); | |
unset($query['op']); | |
$options = array('query' => $query); | |
drupal_goto('new-acquisitions-list-export', $options); | |
} | |
function acquisitions_import_rss_feed_link($form, &$form_state){ | |
$query = drupal_get_query_parameters(); | |
unset($query['op']); | |
unset($query['sort_by']); | |
unset($query['sort_order']); | |
unset($query['items_per_page']); | |
$options = array('query' => $query); | |
drupal_goto('new-acquisitions-list-feed', $options); | |
} | |
function acquisitions_import_cron() { | |
$time = date("G"); | |
$day = date("d"); | |
if (($day != 02 || $day != "02")){ | |
watchdog("cron", "Acq import not run due to time restriction"); | |
} elseif ($time > 3){ | |
watchdog("cron", "Acq import not run due to time restriction"); | |
} else { | |
watchdog("cron", "Acq import has been run"); | |
/*ELECTRONIC IMPORT*/ | |
$feed = "new_books"; | |
$source = feeds_source($feed, 0); | |
$token = ""; | |
$finished = "false"; | |
$error = "false"; | |
for ($i=0;$i<=100;$i++){ | |
$config = $source->getConfig(); | |
$url = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/analytics/reports?apikey=l7xxaa6ec3044f5742e5a68d3a8ae584350d&path=%2Fshared%2FNortheastern%20University%2FReports%2FNew%20Book%20List%2FNBL_Electronic&limit=1000"; | |
if ($token != ""){ | |
$url .= "&token=".$token; | |
} | |
$doc = new DOMDocument(); | |
$doc->load($url); | |
$xml = $doc->saveXML(); | |
file_unmanaged_delete("public://import/alma_electronic_".$i.".xml"); | |
file_unmanaged_save_data($xml, "public://import/alma_electronic_".$i.".xml"); | |
$xpath = new DOMXpath($doc); | |
$tokens = $xpath->query("//report/QueryResult/ResumptionToken"); | |
foreach ($tokens as $tok){ | |
$token = $tok->nodeValue; | |
} | |
$is_finished = $xpath->query("//report/QueryResult/IsFinished"); | |
foreach ($is_finished as $fin){ | |
$finished = strval($fin->nodeValue); | |
} | |
$errors = $xpath->query("//*[name()='web_service_result']/*[name()='errorsExist']"); | |
foreach($errors as $err){ | |
$error = "true"; | |
} | |
if ($error == "true" || $doc->textContent == ""){ | |
break; | |
} | |
$url = "http://librarydev.neu.edu/sites/default/files/public/import/alma_electronic_".$i.".xml"; | |
$config['FeedsHTTPFetcher']['source'] = $url; | |
$source->setConfig($config); | |
$source->save(); | |
while (FEEDS_BATCH_COMPLETE != $source->import()); | |
$source->state(FEEDS_FETCH); | |
$source->state(FEEDS_PROCESS); | |
$source->state(FEEDS_PROCESS_CLEAR); | |
if ($finished == "true"){ | |
cache_clear_all('new_book_list:', 'cache_views_data', TRUE); | |
break; | |
} | |
} | |
/*PHYSICAL IMPORT*/ | |
$feed = "new_books_physical"; | |
$source = feeds_source($feed, 0); | |
$token = ""; | |
$finished = "false"; | |
$error = "false"; | |
for ($i=0;$i<=100;$i++){ | |
$config = $source->getConfig(); | |
$url = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/analytics/reports?apikey=l7xxaa6ec3044f5742e5a68d3a8ae584350d&path=%2Fshared%2FNortheastern%20University%2FReports%2FNew%20Book%20List%2FNBL_Physical&limit=1000"; | |
if ($token != ""){ | |
$url .= "&token=".$token; | |
} | |
$doc = new DOMDocument(); | |
$doc->load($url); | |
$xml = $doc->saveXML(); | |
file_unmanaged_delete("public://import/alma_physical_".$i.".xml"); | |
file_unmanaged_save_data($xml, "public://import/alma_physical_".$i.".xml"); | |
$xpath = new DOMXpath($doc); | |
$tokens = $xpath->query("//report/QueryResult/ResumptionToken"); | |
foreach ($tokens as $tok){ | |
$token = $tok->nodeValue; | |
} | |
$is_finished = $xpath->query("//report/QueryResult/IsFinished"); | |
foreach ($is_finished as $fin){ | |
$finished = strval($fin->nodeValue); | |
} | |
$errors = $xpath->query("//*[name()='web_service_result']/*[name()='errorsExist']"); | |
foreach($errors as $err){ | |
$error = "true"; | |
} | |
if ($error == "true" || $doc->textContent == ""){ | |
break; | |
} | |
$url = "http://librarydev.neu.edu/sites/default/files/public/import/alma_physical_".$i.".xml"; | |
$config['FeedsHTTPFetcher']['source'] = $url; | |
$source->setConfig($config); | |
$source->save(); | |
while (FEEDS_BATCH_COMPLETE != $source->import()); | |
$source->state(FEEDS_FETCH); | |
$source->state(FEEDS_PROCESS); | |
$source->state(FEEDS_PROCESS_CLEAR); | |
if ($finished == "true"){ | |
// publish and unpublish everything | |
//query for all nodes of type acquired_item where published = true then loop through and unpublish them | |
$nids = array(); | |
$nids = db_select('node', 'n') | |
->fields('n', array('nid')) | |
->condition('type', 'acquired_item', '=') | |
->condition('status', 1) | |
->execute() | |
->fetchCol(); | |
// Load all nodes in one go for better performance. | |
$nodes = node_load_multiple($nids); | |
foreach ($nodes as $node) { | |
// set status property to 0 | |
$node->status = 0; | |
// re-save the node | |
node_save($node); | |
} | |
//query for all nodes of type acquired_item where date of acquisition has month $month then loop through and publish them | |
$last_month = date('m') - 1; | |
$last_month = sprintf("%02d", $last_month); | |
$year = date('Y'); | |
if ($last_month < 1){ | |
$year = $year-1; | |
$last_month = "12"; | |
} | |
$first_day = date($year."-".$last_month."-01 00:00:00"); | |
$last_day = date($year."-".$last_month."-31 00:00:00"); | |
$query = new EntityFieldQuery; | |
$query->entityCondition('entity_type', 'node') | |
->entityCondition('bundle', 'acquired_item') | |
->fieldCondition('field_date_of_acquisition', 'value', array($first_day,$last_day), 'BETWEEN'); | |
$results = $query->execute(); | |
if (isset($results['node'])){ | |
// Load all nodes in one go for better performance. | |
$last_month_nodes = node_load_multiple(array_keys($results['node'])); | |
foreach ($last_month_nodes as $node) { | |
// set status property to 1 | |
$node->status = 1; | |
// re-save the node | |
node_save($node); | |
} | |
} | |
cache_clear_all('new_book_list:', 'cache_views_data', TRUE); | |
break; | |
} | |
} | |
} | |
} | |
/* automatically set the date filter */ | |
function acquisitions_import_views_pre_build(&$view) { | |
if ($view->name == 'new_book_list') { | |
$query_params = drupal_get_query_parameters(); | |
if (!isset($query_params['month'])) { | |
$view_filters = $view->display_handler->get_option('filters'); | |
} else { | |
$month = explode("-", $query_params['month'])[0]; | |
$year = explode("-", $query_params['month'])[1]; | |
$view_filters = $view->display_handler->get_option('filters'); | |
$view_filters['field_date_of_acquisition_value']['value'] = | |
array("min"=>array("month"=>$month, "day"=>01, "year"=>$year), "max"=>array("month"=>$month, "day"=>31, "year"=>$year)); | |
$view->display_handler->override_option('filters', $view_filters); | |
} | |
} | |
} | |
/* trims that pesky slash at the end of titles */ | |
function acquisitions_import_node_presave($node) { | |
if ($node->type =='acquired_item' && $node->is_new) { | |
$node->field_full_title['und'][0]['value'] = preg_replace("/[ \/]$/", "", trim($node->field_full_title['und'][0]['value'])); | |
$node->title = trim($node->title); | |
$node->title = preg_replace("/^((The\s)|(An\s)|(A\s)|(d'\s)|(Da\s)|(De\s)|(Ye\s)|(D'\s))/", "", $node->title); | |
$node->title = substr($node->title, 0, 254); | |
$node->title = preg_replace("/[ \/]$/", "", trim($node->title)); | |
//trim ", author." string off end of author | |
if (isset($node->field_author['und'][0]['value'])){ | |
$node->field_author['und'][0]['value'] = preg_replace("/(|,)\sauthor.$/", "", $node->field_author['und'][0]['value']); | |
} | |
} | |
} | |
/*for catching newly added material types*/ | |
function acquisitions_import_taxonomy_term_insert($term){ | |
if ($term->vocabulary_machine_name == "material_type"){ | |
watchdog('material type taxonomy', kprint_r($term, TRUE, NULL)); | |
$term->description = "hidden"; | |
taxonomy_term_save($term); | |
$message = "The following material type has been added: ".$term->name; | |
watchdog('material type taxonomy, $message); | |
} | |
} | |
/*the following three functions are for handling caching*/ | |
function acquisitions_import_node_update($node) { | |
if ($node->type == 'acquired_item') { | |
// Act on the unpublishing or publishing of an existing acquired_item. | |
if (($node->original->status == 1 && $node->status == 0) || ($node->original->status == 0 && $node->status == 1)) { | |
cache_clear_all('new_book_list:', 'cache_views_data', TRUE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment