Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISLANDORA-2116 adjusted newspaper display to only load first year iss… #157

Open
wants to merge 3 commits into
base: 7.x
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions includes/admin.form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ function islandora_newspaper_admin_settings_form(array $form, array &$form_state
);
$form['#validate'][] = 'islandora_newspaper_admin_settings_form_validate';

$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
67 changes: 67 additions & 0 deletions includes/ajax_handler.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

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

/**
* Helper to display the issues for a given year for a newspaper.
*
* @param abstractObject $islandora_object
* The fedora newspaper model object.
* @param string $year
* The year for which to render the markup.
*/
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);
}
2 changes: 1 addition & 1 deletion islandora_newspaper.info
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description = "A default Islandora module to handle newspapers"
dependencies[] = islandora
dependencies[] = islandora_paged_content
package = Islandora Solution Packs
version = 7.x-dev
version = 7.x-1.12
core = 7.x
configure = admin/islandora/solution_pack_config/newspaper
stylesheets[all][] = css/islandora_newspaper.base.css
Expand Down
27 changes: 27 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),
),
// AJAX handler to render a year of issues per newspaper.
'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 @@ -668,6 +681,20 @@ 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 {
}
}
}
* Implements hook_islandora_solution_pack_child_relationships().
*/
function islandora_newspaper_islandora_solution_pack_child_relationships($cmodels) {
Expand Down
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