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

Implement AJAX Summary Displays. #1462

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/conf/assets.conf
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ packages = {
floorplan = ca.floorplan.js,
prompts = ca.promptmanager.js,
mediaupload = ca.mediaupload.js,
querytranslator = ca.querytranslator.js
querytranslator = ca.querytranslator.js,
ajaxSummaryDisplay = ca.ajaxsummarydisplay.js
},
# -----------------------
google = {
Expand Down Expand Up @@ -352,5 +353,9 @@ loadSets = {

querybuilder = [
ca/querytranslator, jquery/dot, jquery/extendext, jquery/querybuilder, jquery/querybuildercss
]
],

ajaxSummaryDisplay = [
ca/ajaxSummaryDisplay, jquery/ui
]
}
184 changes: 126 additions & 58 deletions app/lib/BaseEditorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,32 +689,24 @@ protected function redirectAfterDelete($t_subject) {
}
# -------------------------------------------------------
/**
* Generates display summary of record data based upon a bundle display for screen (HTML)
* Render Summary
*
* @param array $pa_options Array of options passed through to _initView
* @return bool
*
* @throws ApplicationException
*/
public function Summary($pa_options=null) {
AssetLoadManager::register('tableList');
list($vn_subject_id, $t_subject) = $this->_initView($pa_options);



if (!$this->_checkAccess($t_subject)) { throw new ApplicationException(_t('Access denied')); }

if((defined('__CA_ENABLE_DEBUG_OUTPUT__') && __CA_ENABLE_DEBUG_OUTPUT__) || (bool)$this->request->config->get('display_template_debugger') || ($this->request->user->getPreference('show_template_debugger') !== 'hide')) {
$this->render('../template_test_html.php');
}

$vb_ajax_load_displays = $t_subject->getAppConfig()->get('enable_ajax_summary_displays');
session_write_close();
$this->SummaryInfo($pa_options, !$vb_ajax_load_displays);

$t_display = new ca_bundle_displays();
$va_displays = caExtractValuesByUserLocale($t_display->getBundleDisplays(array('table' => $t_subject->tableNum(), 'user_id' => $this->request->getUserID(), 'access' => __CA_BUNDLE_DISPLAY_READ_ACCESS__, 'restrictToTypes' => array($t_subject->getTypeID()))));

if ((!($vn_display_id = $this->request->getParameter('display_id', pInteger))) || !isset($va_displays[$vn_display_id])) {
$vn_display_id = $this->request->user->getVar($t_subject->tableName().'_summary_display_id');
}
if (!isset($va_displays[$vn_display_id]) || (is_array($va_displays[$vn_display_id]['settings']['show_only_in']) && sizeof($va_displays[$vn_display_id]['settings']['show_only_in']) && !in_array('editor_summary', $va_displays[$vn_display_id]['settings']['show_only_in']))) {
$va_tmp = array_filter($va_displays, function($v) { return !isset($v['settings']['show_only_in']) || !is_array($v['settings']['show_only_in']) || in_array('editor_summary', $v['settings']['show_only_in']); });
$vn_display_id = sizeof($va_tmp) > 0 ? array_shift(array_keys($va_tmp)) : 0;
if((defined('__CA_ENABLE_DEBUG_OUTPUT__') && __CA_ENABLE_DEBUG_OUTPUT__) || (bool)$this->request->config->get('display_template_debugger') || ($this->request->user->getPreference('show_template_debugger') !== 'hide')) {
$this->renderGeneric('template_test_html.php');
}

// save where we are in session, for "Save and return" button
Expand All @@ -729,57 +721,133 @@ public function Summary($pa_options=null) {

Session::setVar('save_and_return_locations', caPushToStack($va_save, $va_save_and_return, __CA_SAVE_AND_RETURN_STACK_SIZE__));

$this->view->setVar($t_subject->tableName().'_summary_last_settings', Session::getVar($t_subject->tableName().'_summary_last_settings'));

$this->opo_app_plugin_manager->hookSummarizeItem(
[
'id' => $vn_subject_id,
'table_num' => $t_subject->tableNum(),
'table_name' => $t_subject->tableName(),
'instance' => $t_subject,
'request' => $this->request
]
);

$this->view->setVar('ajax_display', $vb_ajax_load_displays);
$this->render('summary_html.php');
}
# -------------------------------------------------------
/**
* Generates display for summary of record data
*
* @param array $pa_options Array of options passed through to _initView
*
* @throws ApplicationException
*/
public function SummaryDisplay($pa_options=null) {
$this->SummaryInfo($pa_options, TRUE);
$this->renderGeneric('summary_ajax_display_html.php');
}
# -------------------------------------------------------

/**
* Generates summary of record data based upon a bundle display for screen (HTML)
*
* @param array $pa_options Array of options passed through to _initView
*
* @throws ApplicationException
*/
public function SummaryData($pa_options=null) {
$this->SummaryInfo($pa_options, TRUE);

$ajax_placement_id = $this->getRequest()->getParameter('placement_id', pInteger);
if (isset($ajax_placement_id)) {
$this->view->setVar('ajax_item', $ajax_placement_id);
}
$this->renderGeneric('summary_ajax_data_html.php');
}
# -------------------------------------------------------
/**
* Render a "generic" template.
*
* @param string $ps_view path to view file
*/
public function renderGeneric($ps_view) {
$oldViewsPaths = $this->getViewPaths();
$this->setViewPath('generic');
$this->render($ps_view);
$this->setViewPath($oldViewsPaths);
}
# -------------------------------------------------------
/**
* Generates summary of record data based upon a bundle display for screen (HTML)
*
* @param array $pa_options Array of options passed through to _initView
* @param bool $pb_load_placement_info Load placement information
*
* @throws ApplicationException
*/
public function SummaryInfo($pa_options=null, $pb_load_placement_info=TRUE) {
ini_set('display_errors', 0);
list($vn_subject_id, $t_subject) = $this->_initView($pa_options);

if (!$this->_checkAccess($t_subject)) { throw new ApplicationException(_t('Access denied')); }

$t_display = new ca_bundle_displays();
$va_displays = caExtractValuesByUserLocale($t_display->getBundleDisplays(array('table' => $t_subject->tableNum(), 'user_id' => $this->request->getUserID(), 'access' => __CA_BUNDLE_DISPLAY_READ_ACCESS__, 'restrictToTypes' => array($t_subject->getTypeID()))));

if ((!($vn_display_id = $this->request->getParameter('display_id', pInteger))) || !isset($va_displays[$vn_display_id])) {
$vn_display_id = $this->request->user->getVar($t_subject->tableName().'_summary_display_id');
}
if (!isset($va_displays[$vn_display_id]) || (is_array($va_displays[$vn_display_id]['settings']['show_only_in']) && sizeof($va_displays[$vn_display_id]['settings']['show_only_in']) && !in_array('editor_summary', $va_displays[$vn_display_id]['settings']['show_only_in']))) {
$va_tmp = array_filter($va_displays, function($v) { return !isset($v['settings']['show_only_in']) || !is_array($v['settings']['show_only_in']) || in_array('editor_summary', $v['settings']['show_only_in']); });
$vn_display_id = sizeof($va_tmp) > 0 ? array_shift(array_keys($va_tmp)) : 0;
}

$this->view->setVar('bundle_displays', $va_displays);
$this->view->setVar('t_display', $t_display);

// Check validity and access of specified display
if ($t_display->load($vn_display_id) && ($t_display->haveAccessToDisplay($this->request->getUserID(), __CA_BUNDLE_DISPLAY_READ_ACCESS__))) {
$this->view->setVar('display_id', $vn_display_id);
if ($pb_load_placement_info) {
if ($this->request->isAjax()) {
session_write_close();
}
// Check validity and access of specified display
if ($t_display->load($vn_display_id) && ($t_display->haveAccessToDisplay($this->request->getUserID(), __CA_BUNDLE_DISPLAY_READ_ACCESS__))) {
$this->view->setVar('display_id', $vn_display_id);

$va_placements = $t_display->getPlacements(array('returnAllAvailableIfEmpty' => true, 'table' => $t_subject->tableNum(), 'user_id' => $this->request->getUserID(), 'access' => __CA_BUNDLE_DISPLAY_READ_ACCESS__, 'no_tooltips' => true, 'format' => 'simple', 'settingsOnly' => true, 'omitEditingInfo' => true));

$va_display_list = array();
foreach($va_placements as $vn_placement_id => $va_display_item) {
$va_settings = caUnserializeForDatabase($va_display_item['settings']);
$va_placements = $t_display->getPlacements(array('returnAllAvailableIfEmpty' => true, 'table' => $t_subject->tableNum(), 'user_id' => $this->request->getUserID(), 'access' => __CA_BUNDLE_DISPLAY_READ_ACCESS__, 'no_tooltips' => true, 'format' => 'simple', 'settingsOnly' => true, 'omitEditingInfo' => true));

// get column header text
$vs_header = $va_display_item['display'];
if (isset($va_settings['label']) && is_array($va_settings['label'])) {
$va_tmp = caExtractValuesByUserLocale(array($va_settings['label']));
if ($vs_tmp = array_shift($va_tmp)) { $vs_header = $vs_tmp; }
$va_display_list = array();
foreach ($va_placements as $vn_placement_id => $va_display_item) {
$va_settings = caUnserializeForDatabase($va_display_item['settings']);

// get column header text
$vs_header = $va_display_item['display'];
if (isset($va_settings['label']) && is_array($va_settings['label'])) {
$va_tmp = caExtractValuesByUserLocale(array($va_settings['label']));
if ($vs_tmp = array_shift($va_tmp)) {
$vs_header = $vs_tmp;
}
}

$va_display_list[$vn_placement_id] = array(
'placement_id' => $vn_placement_id,
'bundle_name' => $va_display_item['bundle_name'],
'display' => $vs_header,
'settings' => $va_settings
);
}

$va_display_list[$vn_placement_id] = array(
'placement_id' => $vn_placement_id,
'bundle_name' => $va_display_item['bundle_name'],
'display' => $vs_header,
'settings' => $va_settings
);
}
$this->view->setVar('placements', $va_display_list);

$this->view->setVar('placements', $va_display_list);
$this->request->user->setVar($t_subject->tableName() . '_summary_display_id', $vn_display_id);
} else {
$va_display_list = $t_display->getDisplayListForResultsEditor($t_subject->tableName(), ['user_id' => $this->request->getUserID()]);

$this->request->user->setVar($t_subject->tableName().'_summary_display_id', $vn_display_id);
} else {
$va_display_list = $t_display->getDisplayListForResultsEditor($t_subject->tableName(), ['user_id' => $this->request->getUserID()]);

$this->view->setVar('display_id', 0);
$this->view->setVar('placements', $va_display_list['displayList']);

$this->view->setVar('display_id', 0);
$this->view->setVar('placements', $va_display_list['displayList']);
}
}
$this->view->setVar($t_subject->tableName().'_summary_last_settings', Session::getVar($t_subject->tableName().'_summary_last_settings'));

$this->opo_app_plugin_manager->hookSummarizeItem(
[
'id' => $vn_subject_id,
'table_num' => $t_subject->tableNum(),
'table_name' => $t_subject->tableName(),
'instance' => $t_subject,
'request' => $this->request
]
);

$this->render('summary_html.php');
}
# -------------------------------------------------------
/**
Expand Down
96 changes: 96 additions & 0 deletions assets/ca/ca.ajaxsummarydisplay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* ----------------------------------------------------------------------
* js/ca.ajaxsummarydisplay.js
* ----------------------------------------------------------------------
* CollectiveAccess
* Open-source collections management software
* ----------------------------------------------------------------------
*
* Software by Whirl-i-Gig (http://www.whirl-i-gig.com)
* Copyright 2009-2023 Whirl-i-Gig
*
* For more information visit http://www.CollectiveAccess.org
*
* This program is free software; you may redistribute it and/or modify it under
* the terms of the provided license as published by Whirl-i-Gig
*
* CollectiveAccess is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTIES whatsoever, including any implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* This source code is free and modifiable under the terms of
* GNU General Public License. (http://www.gnu.org/copyleft/gpl.html). See
* the "license.txt" file for details, or visit the CollectiveAccess web site at
* http://www.CollectiveAccess.org
*
* ----------------------------------------------------------------------
*/

var caUI = caUI || {};

(function ($) {
caUI.initAjaxSummaryDisplay = function(options) {
// --------------------------------------------------------------------------------
// setup options
var that = jQuery.extend({
controllerUrl: ''
}, options);

// --------------------------------------------------------------------------------
// Define methods
// --------------------------------------------------------------------------------
// Load the display 'template' of sorts. ?>
that.loadDisplay = function() {
$.ajax({
type: 'POST',
url: that.controllerUrl + '/SummaryDisplay',
data: $('#caSummaryDisplaySelectorForm').serialize(),
error: (jqXHR, textStatus, errorThrown) => {
$('#summary-html-data-page ._error').html('Error: ' + textStatus);
},
success: (data) => {
$('#summary-html-data-page .content_wrapper').empty();
$('#summary-html-data-page .content_wrapper').html(data);

that.loadData();
},
});
};
// Load the actual data for the display 'template'.
that.loadData = function() {
$('#summary-html-data-page ._content').each(
function() {
let id = $(this).attr('placementId');
$.ajax({
url: that.controllerUrl + '/summaryData',
data: $('#caSummaryDisplaySelectorForm').serialize() + '&placement_id=' + id,
error: (jqXHR, textStatus, errorThrown) => {
$('#summary-html-data-page ._error').html('Error: ' + textStatus);
},
success: (data) => {
$('#summary-html-data-page ._content[placementid="' + id + '"]').html(data);
},
});
}
);
};

$(document).ready(function () {
// Show/hide the loading spinner div. ?>
$(document).ajaxStart(() => {
$('#summary-html-data-page ._error').empty();
$('#summary-html-data-page ._indicator').show();
});
$(document).ajaxStop(() => $('#summary-html-data-page ._indicator').hide());

that.loadDisplay();
});

$("#caSummaryDisplaySelectorForm").submit(function(e){
e.preventDefault();
that.loadDisplay();
return false;
});
return that;
};

})(jQuery);
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
$va_bundle_displays = $this->getVar('bundle_displays');
$t_display = $this->getVar('t_display');
$va_placements = $this->getVar("placements");
$ajax_display = $this->getVar('ajax_display');
?>
<div id="summary" style="clear: both;">
<?php
Expand Down Expand Up @@ -60,6 +61,16 @@
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td valign="top" align="left" style="padding-right:10px;">
<?php if ($ajax_display): ?>
<div id="summary-html-data-page">
<div class="_error"></div>
<div class="_indicator">
<img src='<?php print $this->request->getUrlPathForThemeFile('/graphics/icons/indicator.gif'); ?>'/>
Loading...
</div>
<div class="content_wrapper"></div>
</div>
<?php else: ?>
<?php
foreach($va_placements as $vn_placement_id => $va_info) {
$vs_class = "";
Expand All @@ -71,6 +82,7 @@
print "<div class=\"unit".$vs_class."\"><span class=\"heading".$vs_class."\">".$va_info['display'].":</span> ".$vs_display_value."</div>\n";
}
?>
<?php endif; ?>
</td>
</td>
</tr>
Expand All @@ -79,3 +91,13 @@
<?php
TooltipManager::add('#printButton', _t("Download Summary as PDF"));
TooltipManager::add('a.downloadMediaContainer', _t("Download Media"));
?>

<?php if ($ajax_display): ?>
<?php AssetLoadManager::register("ajaxSummaryDisplay"); ?>
<script type="text/javascript">
var caAjaxSummaryDisplay = caUI.initAjaxSummaryDisplay({
controllerUrl: '<?php print $this->request->getControllerUrl();?>',
});
</script>
<?php endif; ?>