Skip to content

Commit

Permalink
#47: Port export config revert actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfranco committed Dec 19, 2024
1 parent 2d33158 commit 3bf5472
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions assets/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function generateInputTag(type, value, callback) {

// Run the request and work through our queue when we get the result back.
$.ajax({
url: "../exports/generatecourselist/" + $('#catalogId').val(),
url: $('#config-body').data('courselist-url'),
type: "GET",
error: function(error) {
throw error;
Expand Down Expand Up @@ -190,7 +190,7 @@ function populate() {

loading = true;
$.ajax({
url: "../exports/" + $('#configId').val() + "/latest.json",
url: $('#config-body').data('latest-url'),
type: "GET",
dataType: "JSON",
success: function(data) {
Expand Down
8 changes: 6 additions & 2 deletions assets/export_revision_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ function renderSelected() {
selected.forEach(function(element) { $('#' + element).prop('checked', true); });
}

function revertTo(revId) {
function revertTo(url, revId) {
$.ajax({
url: "../../../export/reverttorevision",
url: url,
type: "POST",
data: {
revId: revId
Expand All @@ -59,4 +59,8 @@ $(document).ready(function() {
$('.compare-revisions-button').on('click', function() {
compare($(this).data('url'));
});

$('.revert-button').on('click', function() {
revertTo($(this).data('url'), $(this).data('rev-id'));
});
});
33 changes: 21 additions & 12 deletions src/Controller/AdminExports.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
/**
* List archive export configurations.
*/
#[Route('/admin/exports/{exportId}', name: 'admin_exports_config')]
#[Route('/admin/exports/config/{exportId}', name: 'admin_exports_config')]
public function listExportConfigs(?int $exportId = null)
{
$db = $this->entityManager->getConnection();
Expand Down Expand Up @@ -356,19 +356,17 @@ public function listrevisionsAction()
*
* @since 1/25/18
*/
public function reverttorevisionAction()
#[Route('/admin/exports/reverttorevision', name: 'admin_exports_config_revert_to_revision', methods: ['POST'])]
public function reverttorevisionAction(Request $request)
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);

$safeRevisionId = filter_input(\INPUT_POST, 'revId', \FILTER_SANITIZE_SPECIAL_CHARS);

$db = Zend_Registry::get('db');
$revId = (int) $request->get('revId');
$db = $this->entityManager->getConnection();
$query = 'SELECT * FROM archive_configuration_revisions WHERE id=:id';
$stmt = $db->prepare($query);
$stmt->execute([':id' => $safeRevisionId]);
$oldRevision = $stmt->fetch();
$note = 'Revert to revision: '.$safeRevisionId.' from '.$oldRevision['last_saved'];
$stmt->bindValue('id', $revId);
$result = $stmt->executeQuery();
$oldRevision = $result->fetchAssociative();
$note = 'Revert to revision #'.$revId.' from '.$oldRevision['last_saved'].' by '.$oldRevision['user_disp_name'];

$query =
'INSERT INTO archive_configuration_revisions (`arch_conf_id`, `note`, `last_saved`, `user_id`, `user_disp_name`, `json_data`)
Expand All @@ -379,8 +377,19 @@ public function reverttorevisionAction()
:userId,
:userDN,
:jsonData)';

$stmt = $db->prepare($query);
$stmt->execute([':configId' => $oldRevision['arch_conf_id'], ':note' => $note, ':userId' => $this->_helper->auth()->getUserId(), ':userDN' => $this->_helper->auth()->getUserDisplayName(), ':jsonData' => $oldRevision['json_data']]);
$stmt->bindValue('configId', $oldRevision['arch_conf_id']);
$stmt->bindValue('note', $note);
$stmt->bindValue('userId', $this->getUser()->getUserIdentifier());
$stmt->bindValue('userDN', $this->getUser()->getName());
$stmt->bindValue('jsonData', $oldRevision['json_data']);
$stmt->executeQuery();

$response = new Response('Success');
$response->headers->set('Content-Type', 'text/plain; charset=utf-8');

return $response;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion templates/admin/export/revisionhistory.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<a class='link-button' href='{{ url('admin_exports_config_revision_json', {revisionId: revision.id}) }}' target="_blank">View JSON</a>
</td>
<td>
<button onclick="revertTo($revision['id'])">Revert to this revision</button>
<button class="revert-button" data-rev-id="{{ revision.id }}" data-url="{{ url('admin_exports_config_revert_to_revision') }}">Revert to this revision</button>
</td>
</tr>
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion templates/admin/export_config.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<p class='inline-p'> - or - <a href="{{ url('admin_exports_create') }}">Create a new configuration</a></p>
</div>

<div id='config-body'>
<div id='config-body' data-latest-url="{{ url('admin_exports_config_latest_revision', {exportId: selected_config.id}) }}" data-courselist-url="{{ url('admin_exports_generate_course_list', {catalogId: selected_config.catalog_id}) }}">
{% if selected_config and selected_config.id %}
<input id='catalogId' type='hidden' value='{{ selected_config.catalog_id }}'></input>
<input id='configId' type='hidden' value='{{ selected_config.id }}'></input>
Expand Down

0 comments on commit 3bf5472

Please sign in to comment.