diff --git a/ding_availability.field.inc b/ding_availability.field.inc index edcb15b..cef7bee 100644 --- a/ding_availability.field.inc +++ b/ding_availability.field.inc @@ -235,7 +235,7 @@ function ding_availability_field_formatter_view($entity_type, $entity, $field, $ 'link' => array( // The online url can't be used with l(), has it will encode // it. - '#markup' => '' . check_plain($type) . '', + '#markup' => '' . check_plain($type) . '', ), ); } diff --git a/ding_availability.make b/ding_availability.make index 4d3a7c2..95353c3 100644 --- a/ding_availability.make +++ b/ding_availability.make @@ -11,5 +11,5 @@ projects[virtual_field][version] = "1.2" projects[ding_provider][type] = "module" projects[ding_provider][download][type] = "git" projects[ding_provider][download][url] = "git@github.com:ding2/ding_provider.git" -projects[ding_provider][download][branch] = "master" +projects[ding_provider][download][tag] = "v2.0.0" diff --git a/ding_availability.module b/ding_availability.module index 9d655d7..289a036 100644 --- a/ding_availability.module +++ b/ding_availability.module @@ -151,6 +151,7 @@ function ding_availability_items($provider_ids) { */ function ding_availability_holdings($provider_ids) { $items = ding_provider_invoke('availability', 'holdings', $provider_ids); + $items = array_filter($items, function($v) { return $v['availability_information']; }); if (!$items) { return array(); } @@ -184,7 +185,25 @@ function ding_availability_holdings($provider_ids) { ); $rows = array(); + $copies = 0; + $closest_loan = NULL; + $in_acquisition = 0; foreach ($item['holdings'] as $holding) { + // This item is considered to be waited for. + // Next, the number of holdings is compared to reserved ones. + // Also keep the next loan date. + if (empty($item['available']) && !empty($holding['available_from'])) { + $next_loan_time = strtotime($holding['available_from']); + $copies++; + if (empty($closest_loan) || $closest_loan < $next_loan_time) { + $closest_loan = $next_loan_time; + } + } + + if ($holding['status'] == 'inAcquisition') { + $in_acquisition++; + } + $rows[] = ding_availability_holdings_row($holding); } @@ -198,6 +217,8 @@ function ding_availability_holdings($provider_ids) { ), 'total_count' => $item['total_count'], 'reserved_count' => $item['reserved_count'], + 'closest_loan' => (count($item['holdings']) == $copies) ? ding_availability_holding_format_time($closest_loan) : NULL, + 'in_acquisition' => $in_acquisition, )); } } @@ -205,6 +226,39 @@ function ding_availability_holdings($provider_ids) { return $items; } +/** + * Generate a human-readable amount of wait time. + * + * @param int $date + * Loan date timestamp. + * @return string + * Sane display of time to wait (months/weeks/days). + */ +function ding_availability_holding_format_time($date) { + $days = ceil(($date - time()) / 86400); + + $plurals = array('day' => t('days'), 'week' => t('weeks'), 'month' => t('months')); + if ($days > 35) { + $days = ceil($days / 30); + $label = 'month'; + } + elseif ($days > 6) { + $days = ceil($days / 7); + $label = 'week'; + } + else { + $label = 'day'; + } + + if ($days > 1) { + $label = $plurals[$label]; + } + + $text = t('The waiting time is currently: @days @label', array('@days' => $days, '@label' => $label)); + + return $text; +} + /** * @param $holding * diff --git a/js/ding_availability.js b/js/ding_availability.js index a456ba3..f3f10ba 100644 --- a/js/ding_availability.js +++ b/js/ding_availability.js @@ -83,18 +83,18 @@ var available = false; var reservable = false; var is_internet = false; + var element = $('#' + id); + element.removeClass('pending').addClass('processed'); + $.each(entity_ids, function(index, entity_id) { + // Reserve button + var reserver_btn = element.parents('.ting-object:first').find('a[id$=' + entity_id + '].reserve-button'); + if (Drupal.DADB[entity_id]) { available = available || Drupal.DADB[entity_id]['available']; reservable = reservable || Drupal.DADB[entity_id]['reservable']; is_internet = is_internet || Drupal.DADB[entity_id]['is_internet']; - var element = $('#' + id); - element.removeClass('pending').addClass('processed'); - - // Reserve button - var reserver_btn = element.parents('.ting-object:first').find('.reserve-button'); - if (available) { element.addClass('available'); @@ -121,7 +121,7 @@ } } - if (!available && !reservable) { + else { element.addClass('not-reservable'); // Add class to reserve button @@ -130,6 +130,9 @@ } } } + else { + reserver_btn.addClass('not-reservable'); + } }); } @@ -148,6 +151,13 @@ if (Drupal.DADB[entity_id] && (Drupal.DADB[entity_id]['holdings'])) { // Show status for material. $('#' + id).html(Drupal.DADB[entity_id].html); + // Don't show queue time if item not reservable. + if (Drupal.DADB[entity_id].reservable === false) { + $('#' + id + ' span.in-queue').hide(); + } + } + else { + $('div.group-holdings-available').parent().parent().remove(); } } diff --git a/js/ding_availability_labels.js b/js/ding_availability_labels.js index e3f2479..7bb1338 100644 --- a/js/ding_availability_labels.js +++ b/js/ding_availability_labels.js @@ -74,15 +74,17 @@ */ function update_availability(id, entity_ids) { // Default the status to not available and not reservable. - var available = false; - var reservable = false; + var status = { + available: false, + reservable: false + }; // Loop over the entity ids and if one has available or reservable // true save that value. $.each(entity_ids, function(index, entity_id) { if (Drupal.DADB[entity_id]) { - available = available || Drupal.DADB[entity_id]['available']; - reservable = reservable || Drupal.DADB[entity_id]['reservable']; + status.available = status.available || Drupal.DADB[entity_id]['available']; + status.reservable = status.reservable || Drupal.DADB[entity_id]['reservable']; } }); @@ -91,20 +93,9 @@ // Get hold of the reserve button (it hidden as default, so we may need // to show it). - var reserver_btn = element.parents('.ting-object:first').find('[id^=ding-reservation-reserve-form]'); + var reserver_btn = element.parents('.ting-object:first').find('.reserve-button'); - if (available) { - update_availability_elements(element, reserver_btn, 'available'); - } - else if (reservable) { - update_availability_elements(element, reserver_btn, 'reservable'); - } - else if (!available && !reservable) { - update_availability_elements(element, reserver_btn, 'not-reservable'); - } - else { - update_availability_elements(element, reserver_btn, 'unavailable'); - } + update_availability_elements(element, reserver_btn, status); } /** @@ -113,38 +104,38 @@ * * @param element * The target element (material that should be moved). - * @param type - * The type of availability the element has. + * @param status + * Structure with available and reservable state. */ - function update_availability_type(element, type) { - // Select type, if any or create the type. + function update_availability_type(element, status) { var groups_wrapper = element.closest('.search-result--availability'); - var group = $('.js-' + type, groups_wrapper); - if (group.length !== 1) { - // Create group. - if (type === 'available') { - // Add as first line. - group = $('

' + Drupal.t('Available') + ':

'); - groups_wrapper.prepend(group); - } - else if (type === 'unavailable') { - // Add last. - group = $('

' + Drupal.t('Unavailable') + ':

'); - group.insertBefore('.js-pending', groups_wrapper); + var reservable = status['reservable']; + var available = status['available']; + + var group = null; + if ($('.js-online', groups_wrapper).length !== 0) { + group = $('.js-online', groups_wrapper); + } + else if (available) { + group = $('.js-available', groups_wrapper); + + if (group.length === 0) { + group = $('

' + Drupal.t('Available') + ':

'); + groups_wrapper.append(group); } - else if (type === 'reservable') { - // First or after available. - group = $('

' + Drupal.t('Reservable') + ':

'); - if ($('.js-available', groups_wrapper).length) { - group.insertAfter($('.js-available', groups_wrapper)); - } - else { - groups_wrapper.prepend(group); - } + } + else if (reservable) { + group = $('.js-reservable', groups_wrapper); + if (group.length === 0) { + group = $('

' + Drupal.t('Reservable') + ':

'); + groups_wrapper.append(group); } - else { - // Add last to not-reservable. - group = $('

' + Drupal.t('Not reservable') + ':

'); + } + else { + group = $('.js-unavailable', groups_wrapper); + + if (group.length === 0) { + group = $('

' + Drupal.t('Not available') + ':

'); groups_wrapper.append(group); } } @@ -182,16 +173,32 @@ * jQuery availability element to add the class to. * @param btn * Reservation button to add the class to. - * @param class_name - * The class to add to the elements. + * @param status + * Structure with available and reservable state. */ - function update_availability_elements(element, btn, class_name) { - element.addClass(class_name); - if (btn.length) { - btn.addClass(class_name); + function update_availability_elements(element, btn, status) { + var class_name = null; + + for (var i in status) { + if (status[i] === true) { + class_name = i; + } + else { + if (i === 'available') { + class_name = 'un' + i; + } + else if (i === 'reservable') { + class_name = 'not-' + i; + } + } + + element.addClass(class_name); + if (btn.length) { + btn.addClass(class_name); + } } - update_availability_type(element, class_name); + update_availability_type(element, status); } } }; diff --git a/templates/ding-holdings.tpl.php b/templates/ding-holdings.tpl.php index 0efbb9a..0588e00 100644 --- a/templates/ding-holdings.tpl.php +++ b/templates/ding-holdings.tpl.php @@ -9,8 +9,19 @@ * - $reserved_count: Amount of reservations. */ -$total_text = format_plural($total_count, 'We have 1 copy.', 'We have @count copies.', array('@count' => $total_count)); -$reserved_text = format_plural($reserved_count, 'There is 1 user in queue to loan the material.', 'There are @count users in queue to loan the material.'); +$total_text = format_plural($total_count, t('We have 1 copy'), t('We have @count copies'), array('@count' => $total_count)); +$reserved_text = format_plural($reserved_count, t('There is 1 user in queue to loan the material'), t('There are @count users in queue to loan the material')); +$acquisition_text = format_plural($in_acquisition, t('(1 material under way)'), t('(@count materials under way)'), array('@count' => $in_acquisition)); + +if ($in_acquisition > 0) { + $total_text .= ' ' . $acquisition_text; +} + ?> -

+

+ " . $reserved_text . ""; ?> +

+

+ +