Skip to content

Commit

Permalink
#47: Port the term visibility management actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfranco committed Dec 2, 2024
1 parent 550ad5e commit 378d297
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 111 deletions.
69 changes: 0 additions & 69 deletions application/views/scripts/admin/terms.phtml

This file was deleted.

116 changes: 74 additions & 42 deletions src/Controller/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@

namespace App\Controller;

use App\Service\Osid\IdMap;
use App\Service\Osid\TermHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

class Admin extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
private IdMap $osidIdMap,
private TermHelper $osidTermHelper,
) {
}

/**
* List Admin Screens.
*/
Expand All @@ -19,41 +31,13 @@ public function indexAction()
/**
* Manage term visibility.
*/
#[Route('/admin/terms', name: 'admin_terms')]
public function termsAction()
#[Route('/admin/terms', name: 'admin_terms_list', methods: ['GET'])]
public function termsAction(Request $request)
{
$db = Zend_Registry::get('db');

if ($this->_getParam('change_visibility')) {
// Verify our CSRF key
if (!$this->_getParam('csrf_key') == $this->_helper->csrfKey()) {
throw new PermissionDeniedException('Invalid CSRF Key. Please log in again.');
}

// Verify that this is a valid term.
$catalog = $this->_getParam('catalog');
$term = $this->_getParam('term');
$verifyStmt = $db->prepare('SELECT COUNT(*) FROM STVTERM WHERE STVTERM_CODE = ?');
$verifyStmt->execute([$term]);
$valid = (int) $verifyStmt->fetchColumn();
$verifyStmt->closeCursor();
if (!$valid) {
throw new InvalidArgumentException('Invalid term-code: '.$term);
}

// Disable the term
if ('true' == $this->_getParam('disabled')) {
$visibilityStmt = $db->prepare('INSERT INTO catalog_term_inactive (catalog_id, term_code) VALUES (?, ?);');
}
// Enable the term
else {
$visibilityStmt = $db->prepare('DELETE FROM catalog_term_inactive WHERE catalog_id = ? AND term_code = ?;');
}
$visibilityStmt->execute([$catalog, $term]);
}

$searches = $db->query('SELECT * FROM catalog_term_match')->fetchAll();
$data = [];
$db = $this->entityManager->getConnection();

$searches = $db->executeQuery('SELECT * FROM catalog_term_match')->fetchAll();
$catalogs = [];
$queries = [];
foreach ($searches as $search) {
Expand Down Expand Up @@ -96,18 +80,66 @@ public function termsAction()
catalog ASC, STVTERM_CODE DESC";
$stmt = $db->prepare($query);

$this->view->catalogs = array_unique($catalogs);
$data['catalogs'] = array_unique($catalogs);

// print "<pre>".$query."</pre>";
if ($this->_getParam('catalog') && in_array($this->_getParam('catalog'), $this->view->catalogs)) {
$catalog = $this->_getParam('catalog');
if ($request->get('catalog') && in_array($request->get('catalog'), $data['catalogs'])) {
$catalog = $request->get('catalog');
} else {
$catalog = $this->view->catalogs[0];
$catalog = $data['catalogs'][0];
}
$stmt->bindValue(1, $catalog);
$stmt->bindValue(2, $catalog);
$stmt->bindValue(3, $catalog);
$result = $stmt->executeQuery();
$data['selectedCatalog'] = $catalog;
$data['terms'] = $result->fetchAll();
foreach ($data['terms'] as &$term) {
$term['active'] = intval($term['num_sections']) && !intval($term['manually_disabled']);
}

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

/**
* Manage term visibility.
*/
#[Route('/admin/terms', name: 'admin_terms_update', methods: ['POST'])]
public function termUpdateAction(Request $request)
{
$db = $this->entityManager->getConnection();

if ($request->get('change_visibility')) {
// Verify our CSRF key
if (!$this->isCsrfTokenValid('admin-terms-update', $request->get('csrf_key'))) {
throw new AccessDeniedException('Invalid CSRF key.');
}

// Verify that this is a valid term.
$catalog = $request->get('catalog');
$term = $request->get('term');
$verifyStmt = $db->prepare('SELECT COUNT(*) FROM STVTERM WHERE STVTERM_CODE = ?');
$verifyStmt->bindValue(1, $term);
$result = $verifyStmt->executeQuery();
$valid = (int) $result->fetchOne();
$result->free();
if (!$valid) {
throw new \InvalidArgumentException('Invalid term-code: '.$term);
}

// Disable the term
if ('true' == $request->get('disabled')) {
$visibilityStmt = $db->prepare('INSERT INTO catalog_term_inactive (catalog_id, term_code) VALUES (?, ?);');
}
// Enable the term
else {
$visibilityStmt = $db->prepare('DELETE FROM catalog_term_inactive WHERE catalog_id = ? AND term_code = ?;');
}
$visibilityStmt->bindValue(1, $catalog);
$visibilityStmt->bindValue(2, $term);
$visibilityStmt->executeQuery();
}

$stmt->execute([$catalog, $catalog, $catalog]);
$this->view->catalog = $catalog;
$this->view->terms = $stmt->fetchAll();
return $this->redirect($this->generateUrl('admin_terms_list', ['catalog' => $catalog]));
}

#[Route('/admin/markup', name: 'markup')]
Expand Down Expand Up @@ -142,7 +174,7 @@ public function masqueradeAction()
}

/**
* Manage archive export configurations
* Manage archive export configurations.
*/
#[Route('/admin/export', name: 'admin_archive_config')]
public function exportAction()
Expand Down
63 changes: 63 additions & 0 deletions templates/admin/terms.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{% extends 'base.html.twig' %}

{% block body %}
<h1>Manage Term Visibility</h1>
<div><a href="{{ url('admin_index') }}">&laquo; Back to Administration</a></div>

<form action="{{ url('admin_terms_list') }}" method="GET">
<select name='catalog' onchange="this.form.submit();">
{% for catalog in catalogs %}
<option value="{{ catalog }}" {{ catalog == selectedCatalog ? 'selected="selected"' }}>{{ catalog }}</option>";
{% endfor %}
</select>
</form>

<p>Please Note: Any changes made here will take effect during the next synchronization with Banner</p>

<table class='section_admin'>
<thead>
<tr>
<th>Term Code</th>
<th>Description</th>
<th>Sections</th>
<th>Manual Override</th>
<th>Active?</th>
</tr>
</thead>
<tbody>

{% for term in terms %}
<tr {{ term.active ? 'class="active"' }}>
<td class='code'>{{ term.STVTERM_CODE }}</td>
<td class='desc'>{{ term.STVTERM_DESC }}</td>
<td class='num_sections'>{{ term.num_sections }}</td>

<td class='disable'>
<form action='{{ url('admin_terms_update') }}' method='post'>
<input type='hidden' name='catalog' value='{{ selectedCatalog }}' />
<input type='hidden' name='term' value='{{ term.STVTERM_CODE }}' />
<input type='hidden' name='change_visibility' value='true' />
<input type='hidden' name='csrf_key' value='{{ csrf_token('admin-terms-update') }}' />
<input type='checkbox' name='disabled' value='true'
{{ term.manually_disabled ? ' checked="checked"' }} onchange='if (confirm("{{ term.manually_disabled ? 'Enable':'Disable' }} {{ term.STVTERM_CODE }}?")) {this.form.submit();}'
id="disable-{{ term.STVTERM_CODE }}"
/>
<label for="disable-{{ term.STVTERM_CODE }}">Disabled?</label>
</form>
</td>

<td class='is_active'>
{% if term.active %}
Yes
{% else %}
No:
{{ term.manually_disabled ? 'Manually disabled. ' }}
{{ term.num_sections is empty ? 'Zero sections. ' }}
{% endif %}
</td>
</tr>

{% endfor %}
</tbody>
</table>
{% endblock %}

0 comments on commit 378d297

Please sign in to comment.