From 1a81da3e02bf62d8356e3c5c19c6621b340f9cae Mon Sep 17 00:00:00 2001 From: Martin Dahl Date: Fri, 21 Dec 2012 16:02:40 +0100 Subject: [PATCH 1/2] Picks fra inlead samt fkb behov --- ding_availability.field.inc | 2 +- ding_availability.module | 52 ++++++++++++++++++++-- ding_availability.theme.inc | 88 +++++++++++++++++++++++++++++++++++++ js/ding_availability.js | 32 +++++++++++--- 4 files changed, 164 insertions(+), 10 deletions(-) create mode 100644 ding_availability.theme.inc diff --git a/ding_availability.field.inc b/ding_availability.field.inc index 5b8672b..0c05539 100644 --- a/ding_availability.field.inc +++ b/ding_availability.field.inc @@ -106,7 +106,7 @@ function ding_availability_field_formatter_view($entity_type, $entity, $field, $ $element[$delta] = array( '#markup' => '
', '#attached' => $attached, - ); + ); break; case 'ding_availability_item': $element[$delta] = array( diff --git a/ding_availability.module b/ding_availability.module index 90e5f3e..3c43f08 100644 --- a/ding_availability.module +++ b/ding_availability.module @@ -15,6 +15,7 @@ function ding_availability_menu() { 'title' => 'Availability status', 'page callback' => 'ding_availability_js', 'access arguments' => array('access content'), + 'delivery callback' => 'ajax_deliver', 'type' => MENU_CALLBACK, ); @@ -22,6 +23,7 @@ function ding_availability_menu() { 'title' => 'Availability and holding status', 'page callback' => 'ding_availability_holdings_js', 'access arguments' => array('access content'), + 'delivery callback' => 'ajax_deliver', 'type' => MENU_CALLBACK, ); @@ -44,14 +46,43 @@ function ding_availability_ding_provider_user() { * */ function ding_availability_js($provider_ids) { + $data = $_POST['availability']; drupal_json_output(ding_availability_items(explode(',', $provider_ids))); } /** * */ -function ding_availability_holdings_js($provider_ids) { - drupal_json_output(ding_availability_holdings(explode(',', $provider_ids))); +function ding_availability_holdings_js($provider_ids = NULL) { + $commands = array(); + $availability = $_POST['availability']; + + $holdings = $_POST['holdings']; + if(is_array($availability) && is_array($holdings)) + $ids = array_merge($availability, $holdings); + else + $ids = is_array($availability) ? $availability : $holdings; + $data = ding_availability_holdings($ids); + + if(is_array($availability)) { + // Set availability + foreach($availability as $html_id => $avid) { + $return = theme('ding_availability_status', array('availability' => $data[$avid])); + $commands[] = ajax_command_replace('#' . $html_id, $return); + } + } + if(is_array($holdings)) { + // Set holdings + foreach($holdings as $html_id => $avid) { + $return = theme('ding_availability_holdings', array('holdings' => $data[$avid])); + $commands[] = ajax_command_html('#' . $html_id, $return); + } + } + + return array( + '#type' => 'ajax', + '#commands' => $commands, + ); } /** @@ -102,7 +133,7 @@ function ding_availability_holdings($provider_ids) { * Adds the human readable status text of an item. */ function _ding_availability_text(&$item) { - if ($item['available'] && $item['reservable']) { + if (($item['available'] && $item['reservable']) || $item['is_internet']) { $item['status'] = t('available'); } elseif (!$item['available'] && $item['reservable']) { @@ -180,3 +211,18 @@ function ding_availability_preprocess_ting_object_entities(&$variables) { } } +/** + * Implements hook_theme + */ +function ding_availability_theme($existing, $type, $theme, $path) { + return array( + 'ding_availability_holdings' => array( + 'variables' => array('holdings' => array()), + 'file' => 'ding_availability.theme.inc', + ), + 'ding_availability_status' => array( + 'variables' => array('availability' => NULL), + 'file' => 'ding_availability.theme.inc', + ), + ); +} \ No newline at end of file diff --git a/ding_availability.theme.inc b/ding_availability.theme.inc new file mode 100644 index 0000000..c85e8e7 --- /dev/null +++ b/ding_availability.theme.inc @@ -0,0 +1,88 @@ + 'markup', + '#markup' => '

' . t('Position') . '

', + ); + $items = array(); + if(isset($holdings['location'])) { + foreach($holdings['location']['holdings'] as $count => $location) { + $items[] = join(' → ', $location); + } + $variables['content']['location'] = array( + '#theme' => 'item_list', + '#items' => $items, + '#title' => '', + '#type' => 'ul', + '#attributes' => array( + 'class' => array(drupal_html_class('ding_availability_location')), + ), + ); + } + if(isset($holdings['holdings'])) { + $variables['content']['holdings'] = array( + '#type' => 'fieldset', + '#title' => t('View all holdings'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + '#attached' => array( + 'js' => array( + 'misc/form.js', + 'misc/collapse.js', + ), + ), + '#attributes' => array( + 'class' => array('collapsible', 'collapsed'), + ), + ); + $variables['content']['holdings']['table'] = array( + '#theme' => 'table', + '#attributes' => $holdings['holdings']['attributes'], + '#header' => $holdings['holdings']['header'], + '#rows' => $holdings['holdings']['rows'], + ); + } + $variables['classes_array'][] = 'ding_availability_holdings'; +} + +/** + * Theme ding_availability_holdings + */ +function theme_ding_availability_holdings(&$variables) { + return '
' . render($variables['content']) . '
'; +} + +/** + * Preprocess ding_availability_status + */ +function template_preprocess_ding_availability_status(&$variables) { + $availability = $variables['availability']; + /*$variables['content']['availabitity'] = array( + '#theme' => 'item_list', + '#items' => array( + 0 => array('data' => ucfirst($availability['status']), 'title' => $availability['status']), + ), + '#type' => 'ul', + '#attributes' => array( + 'class' => array(drupal_html_class('ding_availability_status')), + ), + );*/ + $variables['attributes']['title'] = $availability['status']; + $variables['data'] = t(ucfirst($availability['status'])); + $variables['classes_array'][] = 'ding_availability_status'; +} + +/** + * Theme ding_availability_holdings + */ +function theme_ding_availability_status(&$variables) { + return '' . $variables['data'] . ""; +} \ No newline at end of file diff --git a/js/ding_availability.js b/js/ding_availability.js index 5d8d35e..4f104ad 100644 --- a/js/ding_availability.js +++ b/js/ding_availability.js @@ -10,6 +10,26 @@ Drupal.behaviors.dingAvailabilityAttach = { attach: function(context, settings) { + if($('.availability:not(.processed), .holdings:not(.processed)', context).size()) { + var $url = settings.basePath + settings.pathPrefix + 'ding_availability/holdings'; + var element_settings = { 'url': $url, 'event': 'click', 'progress': { 'type': 'throbber' }, 'submit': {} }; + + var $ids = {}; + $('.availability:not(.processed)', context).addClass('processed').each(function() { + $ids[$(this).attr('id')] = settings.ding_availability[$(this).attr('id')][0]; + }); + element_settings.submit.availability = $ids; + $ids = {}; + $('.holdings:not(.processed)', context).addClass('processed').each(function() { + $ids[$(this).attr('id')] = settings.ding_availability[$(this).attr('id')][0]; + }); + element_settings.submit.holdings = $ids; + + var base = $('.availability.processed:first'); + Drupal.ajax[base.attr('id')] = new Drupal.ajax(base.attr('id'), base, element_settings); + base.click(); + } + /*return; var ids = []; var html_ids = []; $.each(settings.ding_availability, function(id, entity_ids) { @@ -76,17 +96,18 @@ } if (available && reservable) { - $('#' + id).attr('title', Drupal.t('available')); + $('#' + id).attr('title', Drupal.t('Available')); } else if (!available && reservable) { - $('#' + id).attr('title', Drupal.t('on loan')); + $('#' + id).attr('title', Drupal.t('On loan')); } else if (available && ! reservable) { - $('#' + id).attr('title', Drupal.t('not reservable')); + $('#' + id).attr('title', Drupal.t('Not reservable')); } else if (!available && ! reservable) { - $('#' + id).attr('title', Drupal.t('unavailable')); + $('#' + id).attr('title', Drupal.t('Unavailable')); } + $('#' + id).text($('#' + id).attr('title')); } function updateHoldings(id, entity_ids) { @@ -117,9 +138,8 @@ } } } - +*/ } }; })(jQuery); - From e2e452eeabd180136ca5c49820c16385e9986868 Mon Sep 17 00:00:00 2001 From: Martin Dahl Date: Wed, 10 Jul 2013 10:01:34 +0200 Subject: [PATCH 2/2] Bug fixes --- ding_availability.module | 28 +++++++++++++++++++++++++--- ding_availability.theme.inc | 33 +++++++++++++++++++++++---------- js/ding_availability.js | 15 ++++++++++++++- 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/ding_availability.module b/ding_availability.module index 3c43f08..bdf4e89 100644 --- a/ding_availability.module +++ b/ding_availability.module @@ -67,13 +67,32 @@ function ding_availability_holdings_js($provider_ids = NULL) { if(is_array($availability)) { // Set availability foreach($availability as $html_id => $avid) { - $return = theme('ding_availability_status', array('availability' => $data[$avid])); - $commands[] = ajax_command_replace('#' . $html_id, $return); + if(isset($data[$avid])) { + $return = theme('ding_availability_status', array('availability' => $data[$avid])); + $commands[] = ajax_command_replace('#' . $html_id, $return); + } else { + $default_data = array( + 'local_id' => $avid, + 'available' => 0, + 'reservable' => 0, + 'show_reservation_button' => 0, + 'reserved_count' => 0, + 'deferred_period' => FALSE, + 'is_periodical' => 0, + 'is_internet' => TRUE, + 'status' => 'unavailable', + ); + $return = theme('ding_availability_status', array('availability' => $default_data)); + $commands[] = ajax_command_replace('#' . $html_id, $return); + } } } if(is_array($holdings)) { // Set holdings foreach($holdings as $html_id => $avid) { + if(empty($data[$avid]) || !isset($data[$avid])) { + $data[$avid]['location']['holdings'][][] = t('Not provided'); + } $return = theme('ding_availability_holdings', array('holdings' => $data[$avid])); $commands[] = ajax_command_html('#' . $html_id, $return); } @@ -133,7 +152,10 @@ function ding_availability_holdings($provider_ids) { * Adds the human readable status text of an item. */ function _ding_availability_text(&$item) { - if (($item['available'] && $item['reservable']) || $item['is_internet']) { + if (isset($item['status']) && $item['status'] == 'inAcquisition') { + $item['status'] = t('in acquisition'); + } + elseif (($item['available'] && $item['reservable']) || $item['is_internet']) { $item['status'] = t('available'); } elseif (!$item['available'] && $item['reservable']) { diff --git a/ding_availability.theme.inc b/ding_availability.theme.inc index c85e8e7..f90657f 100644 --- a/ding_availability.theme.inc +++ b/ding_availability.theme.inc @@ -43,6 +43,15 @@ function template_preprocess_ding_availability_holdings(&$variables) { 'class' => array('collapsible', 'collapsed'), ), ); + $row = array(); + $row['data']['Library'] = t('Reservations'); + $row['data']['Copies'] = $variables['holdings']['reserved_count']; + $row['data']['Home'] = ''; + $row['data']['Reference'] = ''; + $row['data']['Checked_out'] = ''; + $row['class'] = array(drupal_html_class('availability_holdings_last_row')); + $holdings['holdings']['rows'][] = $row; + $variables['content']['holdings']['table'] = array( '#theme' => 'table', '#attributes' => $holdings['holdings']['attributes'], @@ -50,6 +59,8 @@ function template_preprocess_ding_availability_holdings(&$variables) { '#rows' => $holdings['holdings']['rows'], ); } + $variables['classes'] = ''; + $variables['classes_array'] = array(); $variables['classes_array'][] = 'ding_availability_holdings'; } @@ -65,17 +76,19 @@ function theme_ding_availability_holdings(&$variables) { */ function template_preprocess_ding_availability_status(&$variables) { $availability = $variables['availability']; - /*$variables['content']['availabitity'] = array( - '#theme' => 'item_list', - '#items' => array( - 0 => array('data' => ucfirst($availability['status']), 'title' => $availability['status']), - ), - '#type' => 'ul', - '#attributes' => array( - 'class' => array(drupal_html_class('ding_availability_status')), - ), - );*/ $variables['attributes']['title'] = $availability['status']; + if($availability['available']) { + $variables['attributes']['class'][] = 'available'; + } + if($availability['reservable']) { + $variables['attributes']['class'][] = 'reservable'; + } + if($availability['is_internet']) { + $variables['attributes']['class'][] = drupal_html_class('is_internet'); + } + if($availability['is_periodical']) { + $variables['attributes']['class'][] = drupal_html_class('is_periodical'); + } $variables['data'] = t(ucfirst($availability['status'])); $variables['classes_array'][] = 'ding_availability_status'; } diff --git a/js/ding_availability.js b/js/ding_availability.js index 4f104ad..efbc48e 100644 --- a/js/ding_availability.js +++ b/js/ding_availability.js @@ -13,6 +13,19 @@ if($('.availability:not(.processed), .holdings:not(.processed)', context).size()) { var $url = settings.basePath + settings.pathPrefix + 'ding_availability/holdings'; var element_settings = { 'url': $url, 'event': 'click', 'progress': { 'type': 'throbber' }, 'submit': {} }; + if($.browser.msie && ($.browser.version == '8.0' || $.browser.version == '7.0')) { + element_settings.success = function (response, status) { + for (var i in response) { + if(response[i].selector) { + $(response[i].selector)[response[i].method](response[i].data); + if(response[i].selector.indexOf('holdings-') != -1) { + var settings = response.settings || Drupal.ajax.settings || Drupal.settings; + Drupal.attachBehaviors($(response[i].selector), settings); + } + } + } + } + } var $ids = {}; $('.availability:not(.processed)', context).addClass('processed').each(function() { @@ -27,7 +40,7 @@ var base = $('.availability.processed:first'); Drupal.ajax[base.attr('id')] = new Drupal.ajax(base.attr('id'), base, element_settings); - base.click(); + base.trigger('click'); } /*return; var ids = [];