Skip to content

Commit

Permalink
ISLANDORA-2116 adjusted newspaper display to only load first year iss…
Browse files Browse the repository at this point in the history
…ues, also added ajax handler to populate for any year clicked
  • Loading branch information
wgilling committed Dec 15, 2017
1 parent ff81854 commit c679f4b
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 18 deletions.
7 changes: 7 additions & 0 deletions includes/admin.form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ function islandora_newspaper_admin_settings_form(array $form, array &$form_state
'#size' => 30,
);

$form['islandora_newspaper_use_ajax_loader'] = array(
'#type' => 'checkbox',
'#title' => t('Use AJAX to populate the year of issues for Newspaper view.'),
'#description' => t('When enabled, this should improve performance when a given Newspaper has many issues.'),
'#default_value' => variable_get('islandora_newspaper_use_ajax_loader', 0),
);

module_load_include('inc', 'islandora', 'includes/solution_packs');
$form += islandora_viewers_form('islandora_newspaper_issue_viewers', array('application/pdf'), 'islandora:newspaperIssueCModel');
$form['issue_viewers'] = $form['viewers'];
Expand Down
59 changes: 59 additions & 0 deletions includes/ajax_handler.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* @file
* AJAX handler functions for the Islandora Newspaper Solution pack module.
*/

function islandora_newspaper_ajax_get_year($islandora_object, $year) {
module_load_include('inc', 'islandora_newspaper', 'includes/utilities');
$issues = islandora_newspaper_get_issues($islandora_object);
$grouped_issues = islandora_newspaper_group_issues($issues);
$output = array(
'tabs' => array(
'#type' => 'vertical_tabs',
),
);
ksort($grouped_issues);
$tabs = &$output['tabs'];
$pid = $islandora_object->id;
$months = $grouped_issues[$year];
foreach ($months as $month => $days) {
$month_name = t("@date", array(
"@date" => date("F", mktime(0, 0, 0, $month, 1, 2000)),
));
$tabs[$year][$month] = array(
'#id' => 'y_' . $year . '_' . $month,
'#title' => $month_name,
'#type' => 'fieldset',
'#attributes' => array(
'class' => array('collapsible', 'month'),
),
);
foreach ($days as $day => $issues) {
foreach ($issues as $issue) {
$tabs[$year][$month][$day][] = array(
'#id' => 'y_' . $year . '_' . $month . '_' . $day,
'#theme' => 'link',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#text' => t("@month @day, @year", array(
'@year' => $year,
'@month' => $month_name,
'@day' => $day,
)),
'#path' => "islandora/object/{$issue['pid']}",
'#options' => array(
'attributes' => array(),
'html' => FALSE,
),
);
}
}
ksort($tabs[$year][$month]);
}
$html = theme('islandora_newspaper_single_year_issues', array('tabs' => $tabs));

print $html;
exit(0);
}
28 changes: 28 additions & 0 deletions islandora_newspaper.module
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ function islandora_newspaper_menu() {
'access callback' => 'islandora_newspaper_issue_page_access',
'access arguments' => array(2),
),
// PHP helper function to return page image size for METS editor via AJAX call.
'islandora/object/%islandora_object/newspaper/get_year/%' => array(
'page callback' => 'islandora_newspaper_ajax_get_year',
'page arguments' => array(2, 5),
'file' => 'includes/ajax_handler.inc',
'type' => MENU_CALLBACK,
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 2),
),
);
}

Expand Down Expand Up @@ -165,6 +174,10 @@ function islandora_newspaper_theme($existing, $type, $theme, $path) {
'template' => 'theme/islandora-newspaper-page-img-print',
'variables' => array('islandora_content' => NULL),
),
'islandora_newspaper_single_year_issues' => array(
'template' => 'theme/islandora-newspaper-single-year-issues',
'variables' => array('tabs' => NULL),
),
);
}

Expand Down Expand Up @@ -666,3 +679,18 @@ function islandora_newspaper_islandora_paged_content_content_model_registry() {
),
);
}

/**
* If this is on the Newspaper object page, add the javascript for ajax loading.
*/
function islandora_newspaper_preprocess_page(&$vars) {
$islandora_object = menu_get_object('islandora_object', 2);
if (is_object($islandora_object)) {
if (!(array_search('islandora:newspaperCModel', $islandora_object->models) === FALSE)) {
$path = drupal_get_path('module', 'islandora_newspaper');
drupal_add_js($path . '/js/ajax_newspaper_loader.js');
}
else {
}
}
}
56 changes: 56 additions & 0 deletions js/ajax_newspaper_loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
(function($) {
Drupal.behaviors.loadNewspaperBehavior = {
attach: function (context, settings) {

$(".islandora-newspaper-content ul .vertical-tab-button a").click(function() {
// From the current link, get the year value from the text.
var a = $(this);
var year = a.first()[0].innerText
year = year.replace('(active tab)', '').trim();

// Also, need to grab the hidden PID field value.
var pid = $('#hidden-pid').text().trim();

if (year && pid) {
newspaper_year_callback(pid, year);
}
});

}
};
})(jQuery);


function newspaper_year_callback(pid, year) {
if (pid && year) {
var url = window.location.protocol + "//" + window.location.host + "/islandora/object/" + pid + "/newspaper/get_year/" + year;
ajax=islandora_newspaper_AjaxCaller();
ajax.open("GET", url, true);
ajax.onreadystatechange=function(){
if(ajax.readyState==4){
if(ajax.status==200){
jQuery("#y_" + year).html(ajax.responseText);
}
}
}
ajax.send(null);
}
}

function islandora_newspaper_AjaxCaller(){
var xmlhttp=false;
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(E){
xmlhttp = false;
}
}

if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
15 changes: 15 additions & 0 deletions theme/islandora-newspaper-single-year-issues.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* @file
* islandora-newspaper-single-year-issues.tpl.php
*
* This is the template file to render the months / issues for signle year of
* a newspaper.
*
* Available variables:
* - $tabs: The form elements.
*
*/
?>
<?php print drupal_render($tabs); ?>
46 changes: 28 additions & 18 deletions theme/theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ function template_preprocess_islandora_newspaper(array &$variables) {
$object = $variables['object'];
$issues = islandora_newspaper_get_issues($object);
$grouped_issues = islandora_newspaper_group_issues($issues);
$hidden_pid = (variable_get('islandora_newspaper_use_ajax_loader', 0) > 0 ?
'<span id="hidden-pid" style="display:none">' .
$object->id . '</span>' : '');
$output = array(
'controls' => array(
'#theme' => 'links',
'#prefix' => $hidden_pid,
'#attributes' => array(
'class' => array('links', 'inline'),
),
Expand Down Expand Up @@ -54,9 +58,13 @@ function template_preprocess_islandora_newspaper(array &$variables) {
),
);
$tabs = &$output['tabs'];
$first_pane_done = FALSE;
$load_all = (variable_get('islandora_newspaper_use_ajax_loader', 0) < 1);
ksort($grouped_issues);
foreach ($grouped_issues as $year => $months) {
$tabs[$year] = array(
'#title' => $year,
'#id' => 'y_' . $year,
'#type' => 'fieldset',
);
foreach ($months as $month => $days) {
Expand All @@ -70,30 +78,32 @@ function template_preprocess_islandora_newspaper(array &$variables) {
'class' => array('collapsible', 'collapsed', 'month'),
),
);
foreach ($days as $day => $issues) {
foreach ($issues as $issue) {
$tabs[$year][$month][$day][] = array(
'#theme' => 'link',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#text' => t("@month @day, @year", array(
'@year' => $year,
'@month' => $month_name,
'@day' => $day,
)),
'#path' => "islandora/object/{$issue['pid']}",
'#options' => array(
'attributes' => array(),
'html' => FALSE,
),
);
if (!$first_pane_done || $load_all) {
foreach ($days as $day => $issues) {
foreach ($issues as $issue) {
$tabs[$year][$month][$day][] = array(
'#theme' => 'link',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#text' => t("@month @day, @year", array(
'@year' => $year,
'@month' => $month_name,
'@day' => $day,
)),
'#path' => "islandora/object/{$issue['pid']}",
'#options' => array(
'attributes' => array(),
'html' => FALSE,
),
);
}
}
$first_pane_done = TRUE;
}
ksort($tabs[$year][$month]);
}
ksort($tabs[$year]);
}
ksort($tabs);

$variables['islandora_content_render_array'] = $output;
$variables['parent_collections'] = islandora_get_parents_from_rels_ext($object);
Expand Down

0 comments on commit c679f4b

Please sign in to comment.