Skip to content

Commit

Permalink
#47: Reorganize and rename export configuration routes for consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfranco committed Dec 20, 2024
1 parent 0656722 commit 94d3942
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 151 deletions.
274 changes: 129 additions & 145 deletions src/Controller/AdminExportConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,69 @@ public function listExportConfigs(?int $exportId = null)
return $this->render('admin/export/config_form.html.twig', $data);
}

/**
* Provide interface for creating a new archive configuration.
*/
#[Route('/admin/exports/create', name: 'export_config_create_form', methods: ['GET'])]
public function createFormAction()
{
$data['catalogs'] = [];
$lookupSession = $this->osidRuntime->getCourseManager()->getCourseCatalogLookupSession();
$catalogs = $lookupSession->getCourseCatalogs();
while ($catalogs->hasNext()) {
$data['catalogs'][] = $catalogs->getNextCourseCatalog();
}

return $this->render('admin/export/create_config.html.twig', $data);
}

/**
* Insert a new archive configuration into the database.
*/
#[Route('/admin/exports/create', name: 'export_config_create', methods: ['POST'])]
public function createAction(Request $request)
{
$label = filter_var($request->get('label'), \FILTER_SANITIZE_SPECIAL_CHARS);
$catalogId = filter_var($request->get('catalog_id'), \FILTER_SANITIZE_SPECIAL_CHARS);

$db = $this->entityManager->getConnection();
$query = 'INSERT INTO archive_configurations (id, label, catalog_id) VALUES (NULL,:label,:catalogId)';
$stmt = $db->prepare($query);
$stmt->bindValue('label', $label);
$stmt->bindValue('catalogId', $catalogId);
$stmt->execute();

return $this->redirectToRoute('export_config_form', [
'exportId' => $db->lastInsertId(),
]);
}

/**
* Delete an archive configuration.
*/
#[Route('/admin/exports/{exportId}/delete', name: 'export_config_delete', methods: ['POST'])]
public function deleteconfigAction($exportId)
{
$db = $this->entityManager->getConnection();

// Delete revisions that depend on this config.
$query = 'DELETE FROM archive_configuration_revisions WHERE arch_conf_id = ?';
$stmt = $db->prepare($query);
$stmt->bindValue(1, $exportId);
$stmt->executeQuery();

// Delete this config.
$query = 'DELETE FROM archive_configurations WHERE id = ?';
$stmt = $db->prepare($query);
$stmt->bindValue(1, $exportId);
$stmt->executeQuery();

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

return $response;
}

/**
* Echo JSON data of latest revision for a particular archive configuration.
*/
Expand Down Expand Up @@ -104,91 +167,34 @@ public function revisionhistoryAction(int $exportId)
}

/**
* Display diff between two revisions.
*
* @return void
*
* @since 1/25/18
*/
#[Route('/admin/exports/revisiondiff/{rev1}/{rev2}', name: 'export_config_revision_diff')]
public function revisiondiffAction(int $rev1, int $rev2)
{
$db = $this->entityManager->getConnection();
$query = 'SELECT * FROM archive_configuration_revisions WHERE id = ?';
$stmt = $db->prepare($query);
$stmt->bindValue(1, $rev1);
$result = $stmt->executeQuery();
$data['rev1'] = $result->fetchAssociative();

$stmt->bindValue(1, $rev2);
$result = $stmt->executeQuery();
$data['rev2'] = $result->fetchAssociative();

return $this->render('admin/export/revisiondiff.html.twig', $data);
}

/**
* Display revision JSON data.
*
* @return void
*
* @since 1/25/18
*/
#[Route('/admin/exports/revision/{revisionId}/json', name: 'export_config_revision_json')]
public function viewjsonAction(int $revisionId)
{
$db = $this->entityManager->getConnection();
$query = 'SELECT * FROM archive_configuration_revisions WHERE id = ?';
$stmt = $db->prepare($query);
$stmt->bindValue(1, $revisionId);
$result = $stmt->executeQuery();
$data['rev'] = $result->fetchAssociative();

return $this->render('admin/export/revisionjson.html.twig', $data);
}

/**
* Provide interface for creating a new archive configuration.
*
* @return void
*
* @since 1/23/18
* Insert new archive configuration revision to the DB.
*/
#[Route('/admin/exports/create', name: 'export_config_create')]
public function newconfigAction()
#[Route('/admin/exports/{exportId}/insertrevision', name: 'export_config_insert_revision', methods: ['POST'])]
public function insertrevisionAction(Request $request, int $exportId)
{
$data['catalogs'] = [];
$lookupSession = $this->osidRuntime->getCourseManager()->getCourseCatalogLookupSession();
$catalogs = $lookupSession->getCourseCatalogs();
while ($catalogs->hasNext()) {
$data['catalogs'][] = $catalogs->getNextCourseCatalog();
$safeNote = filter_var($request->get('note'), \FILTER_SANITIZE_SPECIAL_CHARS);
$jsonArray = json_decode($request->get('jsonData'));
foreach ($jsonArray as $key => $value) {
$value = filter_var($value, \FILTER_SANITIZE_SPECIAL_CHARS);
}
$safeJsonData = json_encode($jsonArray, \JSON_PRETTY_PRINT);

return $this->render('admin/export/create_config.html.twig', $data);
}

/**
* Delete an archive configuration.
*
* @return void
*
* @since 1/23/18
*/
#[Route('/admin/exports/{exportId}/delete', name: 'export_config_delete_config', methods: ['POST'])]
public function deleteconfigAction($exportId)
{
$db = $this->entityManager->getConnection();

// Delete revisions that depend on this config.
$query = 'DELETE FROM archive_configuration_revisions WHERE arch_conf_id = ?';
$stmt = $db->prepare($query);
$stmt->bindValue(1, $exportId);
$stmt->executeQuery();

// Delete this config.
$query = 'DELETE FROM archive_configurations WHERE id = ?';
$query =
'INSERT INTO archive_configuration_revisions (`arch_conf_id`, `note`, `last_saved`, `user_id`, `user_disp_name`, `json_data`)
VALUES (
:configId,
:note,
CURRENT_TIMESTAMP,
:userId,
:userDN,
:jsonData)';
$stmt = $db->prepare($query);
$stmt->bindValue(1, $exportId);
$stmt->bindValue('configId', $exportId);
$stmt->bindValue('note', $safeNote);
$stmt->bindValue('userId', $this->getUser()->getUserIdentifier());
$stmt->bindValue('userDN', $this->getUser()->getName());
$stmt->bindValue('jsonData', $safeJsonData);
$stmt->executeQuery();

$response = new Response('Success');
Expand All @@ -197,45 +203,6 @@ public function deleteconfigAction($exportId)
return $response;
}

/**
* Insert a new archive configuration into the database.
*/
#[Route('/admin/exports/insert', name: 'export_config_insert_config', methods: ['POST'])]
public function insertconfigAction(Request $request)
{
$label = filter_var($request->get('label'), \FILTER_SANITIZE_SPECIAL_CHARS);
$catalogId = filter_var($request->get('catalog_id'), \FILTER_SANITIZE_SPECIAL_CHARS);

$db = $this->entityManager->getConnection();
$query = 'INSERT INTO archive_configurations (id, label, catalog_id) VALUES (NULL,:label,:catalogId)';
$stmt = $db->prepare($query);
$stmt->bindValue('label', $label);
$stmt->bindValue('catalogId', $catalogId);
$stmt->execute();

return $this->redirectToRoute('export_config_form', [
'exportId' => $db->lastInsertId(),
]);
}

/**
* Echo JSON data of all archive configuration revisions.
*
* @return void
*
* @since 1/23/18
*/
public function listrevisionsAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);

$db = Zend_Registry::get('db');
$revisions = $db->query('SELECT * FROM archive_configuration_revisions ORDER BY last_saved DESC')->fetchAll();

echo json_encode($revisions);
}

/**
* Revert to an older revision by re-inserting it with new timestamp.
*
Expand Down Expand Up @@ -280,40 +247,57 @@ public function reverttorevisionAction(Request $request)
}

/**
* Insert new archive configuration revision to the DB.
* Display diff between two revisions.
*/
#[Route('/admin/exports/{exportId}/insertrevision', name: 'export_config_insert_revision', methods: ['POST'])]
public function insertrevisionAction(Request $request, int $exportId)
#[Route('/admin/exports/revisiondiff/{rev1}/{rev2}', name: 'export_config_revision_diff')]
public function revisiondiffAction(int $rev1, int $rev2)
{
$safeNote = filter_var($request->get('note'), \FILTER_SANITIZE_SPECIAL_CHARS);
$jsonArray = json_decode($request->get('jsonData'));
foreach ($jsonArray as $key => $value) {
$value = filter_var($value, \FILTER_SANITIZE_SPECIAL_CHARS);
}
$safeJsonData = json_encode($jsonArray, \JSON_PRETTY_PRINT);
$db = $this->entityManager->getConnection();
$query = 'SELECT * FROM archive_configuration_revisions WHERE id = ?';
$stmt = $db->prepare($query);
$stmt->bindValue(1, $rev1);
$result = $stmt->executeQuery();
$data['rev1'] = $result->fetchAssociative();

$stmt->bindValue(1, $rev2);
$result = $stmt->executeQuery();
$data['rev2'] = $result->fetchAssociative();

return $this->render('admin/export/revisiondiff.html.twig', $data);
}

/**
* Display revision JSON data.
*/
#[Route('/admin/exports/revision/{revisionId}/json', name: 'export_config_revision_json')]
public function viewjsonAction(int $revisionId)
{
$db = $this->entityManager->getConnection();
$query =
'INSERT INTO archive_configuration_revisions (`arch_conf_id`, `note`, `last_saved`, `user_id`, `user_disp_name`, `json_data`)
VALUES (
:configId,
:note,
CURRENT_TIMESTAMP,
:userId,
:userDN,
:jsonData)';
$query = 'SELECT * FROM archive_configuration_revisions WHERE id = ?';
$stmt = $db->prepare($query);
$stmt->bindValue('configId', $exportId);
$stmt->bindValue('note', $safeNote);
$stmt->bindValue('userId', $this->getUser()->getUserIdentifier());
$stmt->bindValue('userDN', $this->getUser()->getName());
$stmt->bindValue('jsonData', $safeJsonData);
$stmt->executeQuery();
$stmt->bindValue(1, $revisionId);
$result = $stmt->executeQuery();
$data['rev'] = $result->fetchAssociative();

$response = new Response('Success');
$response->headers->set('Content-Type', 'text/plain; charset=utf-8');
return $this->render('admin/export/revisionjson.html.twig', $data);
}

return $response;
/**
* Echo JSON data of all archive configuration revisions.
*
* @return void
*
* @since 1/23/18
*/
public function listrevisionsAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);

$db = Zend_Registry::get('db');
$revisions = $db->query('SELECT * FROM archive_configuration_revisions ORDER BY last_saved DESC')->fetchAll();

echo json_encode($revisions);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Controller/AdminExportScheduling.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
use App\Service\Osid\Runtime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class AdminExportScheduling extends AbstractController
Expand Down
4 changes: 2 additions & 2 deletions templates/admin/export/config_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
<option value="{{ url('export_config_form', {exportId: config.id}) }}" {{ selected_config and selected_config.id == config.id ? 'selected="selected"' }}>{{ config.label }}</option>
{% endfor %}
</select>
<p class='inline-p'> - or - <a href="{{ url('export_config_create') }}">Create a new configuration</a></p>
<p class='inline-p'> - or - <a href="{{ url('export_config_create_form') }}">Create a new configuration</a></p>
</div>

<div id='config-body' data-latest-url="{{ selected_config ? url('export_config_latest_revision', {exportId: selected_config.id}) }}" data-courselist-url="{{ selected_config ? url('export_config_generate_course_list', {catalogId: selected_config.catalog_id}) }}" data-insert-revision-url="{{ selected_config ? url('export_config_insert_revision', {exportId: selected_config.id}) }}" data-delete-url="{{ selected_config ? url('export_config_delete_config', {exportId: selected_config.id}) }}">
<div id='config-body' data-latest-url="{{ selected_config ? url('export_config_latest_revision', {exportId: selected_config.id}) }}" data-courselist-url="{{ selected_config ? url('export_config_generate_course_list', {catalogId: selected_config.catalog_id}) }}" data-insert-revision-url="{{ selected_config ? url('export_config_insert_revision', {exportId: selected_config.id}) }}" data-delete-url="{{ selected_config ? url('export_config_delete', {exportId: selected_config.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
2 changes: 1 addition & 1 deletion templates/admin/export/create_config.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<h1>Create New Catalog Export Configuration</h1>
<div class='admin-menu'><a href="{{ url('export_config_form') }}">&laquo; Back to Archive Export Configuration</a></div>

<form class='config-create-form' action="{{ url('export_config_insert_config') }}" method="post">
<form class='config-create-form' action="{{ url('export_config_create') }}" method="post">
<label for='label'>Label:</label><input name='label'></input><br>
<label for='catalog_id'>Catalog:</label>
<select name='catalog_id'>
Expand Down

0 comments on commit 94d3942

Please sign in to comment.