Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ding_availability.field.inc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function ding_availability_field_formatter_view($entity_type, $entity, $field, $
$element[$delta] = array(
'#markup' => '<div id="' . $id . '" class="holdings holdings-' . $entity->localId . '"></div>',
'#attached' => $attached,
);
);
break;
case 'ding_availability_item':
$element[$delta] = array(
Expand Down
74 changes: 71 additions & 3 deletions ding_availability.module
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ function ding_availability_menu() {
'title' => 'Availability status',
'page callback' => 'ding_availability_js',
'access arguments' => array('access content'),
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
);

$items['ding_availability/holdings'] = array(
'title' => 'Availability and holding status',
'page callback' => 'ding_availability_holdings_js',
'access arguments' => array('access content'),
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
);

Expand All @@ -44,14 +46,62 @@ function ding_availability_ding_provider_user() {
*
*/
function ding_availability_js($provider_ids) {
$data = $_POST['availability'];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $data variable is not used ?

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'];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the $_POST data sanitized ?

if(is_array($availability) && is_array($holdings))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If statements missing {}

$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) {
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);
}
}

return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}

/**
Expand Down Expand Up @@ -102,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']) {
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']) {
Expand Down Expand Up @@ -180,3 +233,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',
),
);
}
101 changes: 101 additions & 0 deletions ding_availability.theme.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php


/**
* Preprocess ding_availability_holdings
*/
function template_preprocess_ding_availability_holdings(&$variables) {
$variables['content'] = array();

$holdings = $variables['holdings'];
$variables['content']['title'] = array(
'#type' => 'markup',
'#markup' => '<h2>' . t('Position') . '</h2>',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a tpl should be used to make it easier to override the markup ?

);
$items = array();
if(isset($holdings['location'])) {
foreach($holdings['location']['holdings'] as $count => $location) {
$items[] = join(' → ', $location);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note: Would it be possible to have the separate char as input parameter to the theme function ?

}
$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'),
),
);
$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'],
'#header' => $holdings['holdings']['header'],
'#rows' => $holdings['holdings']['rows'],
);
}
$variables['classes'] = '';
$variables['classes_array'] = array();
$variables['classes_array'][] = 'ding_availability_holdings';
}

/**
* Theme ding_availability_holdings
*/
function theme_ding_availability_holdings(&$variables) {
return '<div class="' . $variables['classes'] . '">' . render($variables['content']) . '</div>';
}

/**
* Preprocess ding_availability_status
*/
function template_preprocess_ding_availability_status(&$variables) {
$availability = $variables['availability'];
$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';
}

/**
* Theme ding_availability_holdings
*/
function theme_ding_availability_status(&$variables) {
return '<li' . drupal_attributes($variables['attributes']) . '>' . $variables['data'] . "</li>";
}
45 changes: 39 additions & 6 deletions js/ding_availability.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@

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': {} };
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() {
$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.trigger('click');
}
/*return;
var ids = [];
var html_ids = [];
$.each(settings.ding_availability, function(id, entity_ids) {
Expand Down Expand Up @@ -76,17 +109,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) {
Expand Down Expand Up @@ -117,9 +151,8 @@
}
}
}

*/
}
};

})(jQuery);