From 3bf5472504995ce8d09228858fb4bdf3b275ba28 Mon Sep 17 00:00:00 2001 From: Adam Franco Date: Thu, 19 Dec 2024 14:49:23 -0500 Subject: [PATCH] #47: Port export config revert actions. --- assets/export.js | 4 +-- assets/export_revision_history.js | 8 +++-- src/Controller/AdminExports.php | 33 ++++++++++++------- .../admin/export/revisionhistory.html.twig | 2 +- templates/admin/export_config.html.twig | 2 +- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/assets/export.js b/assets/export.js index 5c0c9932..57f1d6e6 100644 --- a/assets/export.js +++ b/assets/export.js @@ -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; @@ -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) { diff --git a/assets/export_revision_history.js b/assets/export_revision_history.js index 53b10215..c54f99d9 100644 --- a/assets/export_revision_history.js +++ b/assets/export_revision_history.js @@ -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 @@ -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')); + }); }); diff --git a/src/Controller/AdminExports.php b/src/Controller/AdminExports.php index d8fdba80..806c26b8 100644 --- a/src/Controller/AdminExports.php +++ b/src/Controller/AdminExports.php @@ -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(); @@ -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`) @@ -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; } /** diff --git a/templates/admin/export/revisionhistory.html.twig b/templates/admin/export/revisionhistory.html.twig index 1ebc5c91..5921ebef 100644 --- a/templates/admin/export/revisionhistory.html.twig +++ b/templates/admin/export/revisionhistory.html.twig @@ -36,7 +36,7 @@ View JSON - + {% endfor %} diff --git a/templates/admin/export_config.html.twig b/templates/admin/export_config.html.twig index c0eb06d4..66168e30 100644 --- a/templates/admin/export_config.html.twig +++ b/templates/admin/export_config.html.twig @@ -23,7 +23,7 @@

- or - Create a new configuration

-
+
{% if selected_config and selected_config.id %}