Move packages to templating

This commit is contained in:
Peter Kokot
2019-02-11 03:50:47 +01:00
parent df7659a780
commit e548fbd90e
5 changed files with 227 additions and 234 deletions

View File

@@ -133,7 +133,7 @@ if (isset($_POST['submit'])) {
$rest = $container->get(Rest::class);
$rest->savePackage($_POST['name']);
$rest->savePackagesCategory($packageRepository->find($_POST['name'], 'category'));
$content .= '<b>Package information successfully updated.</b><br><br>';
$content .= 'Package information successfully updated.';
} elseif (isset($_GET['action']) && 'release_remove' === $_GET['action']) {
if (!isset($_GET['release'])) {
echo $template->render('error.php', [
@@ -144,7 +144,7 @@ if (isset($_POST['submit'])) {
}
if ($container->get(Release::class)->remove($_GET['id'], $_GET['release'])) {
$content .= '<b>Release successfully deleted.</b><br><br>';
$content .= 'Release successfully deleted.';
} else {
echo $template->render('error.php', [
'errors' => ['An error occured while deleting the release!'],

View File

@@ -27,7 +27,9 @@
use App\Utils\Breadcrumbs;
use App\Utils\Pagination;
$script_name = htmlspecialchars($_SERVER['SCRIPT_NAME'], ENT_QUOTES);
require_once __DIR__.'/../include/pear-prepend.php';
$scriptName = htmlspecialchars($_SERVER['SCRIPT_NAME'], ENT_QUOTES);
/**
* Returns an appropriate query string for a self referencing link
@@ -35,30 +37,29 @@ $script_name = htmlspecialchars($_SERVER['SCRIPT_NAME'], ENT_QUOTES);
function getQueryString($catpid, $catname, $showempty = false, $moreinfo = false)
{
$querystring = [];
$entries_cnt = 0;
$count = 0;
if ($catpid) {
$querystring[] = 'catpid='.(int)$catpid;
$entries_cnt++;
$count++;
}
if ($catname) {
$querystring[] = 'catname='.urlencode($catname);
$entries_cnt++;
$count++;
}
if ($showempty) {
$querystring[] = 'showempty='.(int)$showempty;
$entries_cnt++;
$count++;
}
if ($moreinfo) {
$querystring[] = 'moreinfo='.(int)$moreinfo;
$entries_cnt++;
$count++;
}
if ($entries_cnt) {
if ($count) {
return '?'.implode('&amp;', $querystring);
} else {
return '';
@@ -66,15 +67,15 @@ function getQueryString($catpid, $catname, $showempty = false, $moreinfo = false
}
// Check input variables. Expected url vars: catpid (category parent id), catname, showempty
$moreinfo = isset($_GET['moreinfo']) ? (int)$_GET['moreinfo'] : false;
$catpid = isset($_GET['catpid']) ? (int)$_GET['catpid'] : null;
$showempty = isset($_GET['showempty']) ? (bool)$_GET['showempty'] : false;
$moreinfo = isset($_GET['moreinfo']) ? (int) $_GET['moreinfo'] : false;
$catpid = isset($_GET['catpid']) ? (int) $_GET['catpid'] : null;
$showempty = isset($_GET['showempty']) ? (bool) $_GET['showempty'] : false;
if (empty($catpid)) {
$category_where = "IS NULL";
$catname = "Top Level";
$categoryWhere = 'IS NULL';
$catname = 'Top Level';
} else {
$category_where = "= " . $catpid;
$categoryWhere = '= '.$catpid;
if (isset($_GET['catname']) && preg_match('/^[0-9a-z_ ]{1,80}$/i', $_GET['catname'])) {
$catname = $_GET['catname'];
@@ -85,63 +86,59 @@ if (empty($catpid)) {
// The user is already at the top level
if (empty($catpid)) {
$showempty_link = 'Top Level';
$showEmptyLink = 'Top Level';
} else {
$showempty_link = '<a href="'. $script_name . getQueryString($catpid, $catname, !$showempty, $moreinfo) . '">' . ($showempty ? 'Hide empty' : 'Show empty').'</a>';
$showEmptyLink = '<a href="'.$scriptName.getQueryString($catpid, $catname, !$showempty, $moreinfo).'">'.($showempty ? 'Hide empty' : 'Show empty').'</a>';
}
// Main part of script
if ($catpid) {
$catname = $database->run('SELECT name FROM categories WHERE id=:id', [':id' => $catpid])->fetch()['name'];
$category_title = "Package Browser :: " . htmlspecialchars($catname, ENT_QUOTES);
$categoryTitle = "Package Browser :: " . htmlspecialchars($catname, ENT_QUOTES);
} else {
$category_title = 'Package Browser :: Top Level';
$categoryTitle = 'Package Browser :: Top Level';
}
response_header($category_title);
// 1) Show categories of this level
$statement = $database->run("SELECT c.*, COUNT(p.id) AS npackages
FROM categories c
LEFT JOIN packages p ON p.category = c.id
WHERE p.package_type = 'pecl'
AND c.parent ".$category_where."
AND c.parent $categoryWhere
GROUP BY c.id ORDER BY name");
// Get names of sub-categories
$subcats = $database->run("SELECT p.id AS pid, c.id AS id, c.name AS name, c.summary AS summary".
" FROM categories c, categories p ".
" WHERE p.parent $category_where ".
" AND c.parent = p.id ORDER BY c.name")->fetchAll();
$subcats = $database->run("SELECT p.id AS pid, c.id AS id, c.name AS name, c.summary AS summary
FROM categories c, categories p
WHERE p.parent $categoryWhere
AND c.parent = p.id ORDER BY c.name")->fetchAll();
// Get names of sub-packages
$subpkgs = $database->run("SELECT p.category, p.id AS id, p.name AS name, p.summary AS summary".
" FROM packages p, categories c".
" WHERE c.parent $category_where ".
" AND p.package_type = 'pecl' ".
" AND p.category = c.id ORDER BY p.name")->fetchAll();
$subpkgs = $database->run("SELECT p.category, p.id AS id, p.name AS name, p.summary AS summary
FROM packages p, categories c
WHERE c.parent $categoryWhere
AND p.package_type = 'pecl'
AND p.category = c.id ORDER BY p.name")->fetchAll();
$max_sub_links = 4;
$totalpackages = 0;
$maxSubLinks = 4;
$totalPackages = 0;
$categories = [];
foreach ($statement->fetchAll() as $row) {
extract($row);
// Show only categories with packages
if (!$showempty AND $row['npackages'] < 1) {
if (!$showempty && $row['npackages'] < 1) {
continue;
}
$sub_links = [];
$subLinks = [];
foreach ($subcats as $subcat) {
if ($subcat['pid'] === $row['id']) {
$sub_links[] = '<b><a href="'.$script_name.'?catpid='.$subcat['id'].'&amp;catname='
$subLinks[] = '<b><a href="'.$scriptName.'?catpid='.$subcat['id'].'&amp;catname='
. urlencode($subcat['name'])
. '" title="'.htmlspecialchars($subcat['summary'], ENT_QUOTES).'">'
. $subcat['name'].'</a></b>';
if (count($sub_links) >= $max_sub_links) {
if (count($subLinks) >= $maxSubLinks) {
break;
}
}
@@ -149,28 +146,26 @@ foreach ($statement->fetchAll() as $row) {
foreach ($subpkgs as $subpkg) {
if ($subpkg['category'] === $row['id']) {
$sub_links[] = '<a href="/package/'.$subpkg['name'].'" title="'
$subLinks[] = '<a href="/package/'.$subpkg['name'].'" title="'
. htmlspecialchars($subpkg['summary'], ENT_QUOTES).'">'.$subpkg['name'].'</a>';
if (count($sub_links) >= $max_sub_links) {
if (count($subLinks) >= $maxSubLinks) {
break;
}
}
}
if (count($sub_links) >= $max_sub_links) {
$sub_links = implode(', ', $sub_links).' <img src="/img/caret-r.gif" alt="[more]">';
if (count($subLinks) >= $maxSubLinks) {
$subLinks = implode(', ', $subLinks).' <img src="/img/caret-r.gif" alt="[more]">';
} else {
$sub_links = implode(', ', $sub_links);
$subLinks = implode(', ', $subLinks);
}
settype($npackages, 'string');
$data = '<font size="+1"><b><a href="'. $script_name .'?catpid='.$id.'&amp;catname='.urlencode($name).'">'.$name.'</a></b></font> ('.$npackages.')<br />';
$data .= $sub_links.'<br />';
settype($row['npackages'], 'string');
$data = '<font size="+1"><b><a href="'.$scriptName.'?catpid='.$row['id'].'&amp;catname='.urlencode($row['name']).'">'.$row['name'].'</a></b></font> ('.$row['npackages'].')<br>';
$data .= $subLinks.'<br>';
$categories[] = $data;
$totalpackages += $npackages;
$totalPackages += $row['npackages'];
}
// Begin code for showing packages if we aren't at the top level.
@@ -187,7 +182,7 @@ if (!empty($catpid)) {
if (count($subcats) > 0) {
foreach ($subcats as $subcat) {
$subCategories[] = sprintf('<b><a href="%s?catpid=%d&catname=%s" title="%s">%s</a></b>',
$script_name,
$scriptName,
$subcat['id'],
urlencode($subcat['name']),
htmlspecialchars($subcat['summary'], ENT_QUOTES),
@@ -201,7 +196,7 @@ if (!empty($catpid)) {
$total = $database->run("SELECT count(*) FROM packages WHERE category = ? AND package_type='pecl'", [$catpid])->fetchColumn();
$pagination = new Pagination();
$pagination->setNumberOfItems($total);
$currentPage = isset($_GET['pageID']) ? (int)$_GET['pageID'] : 1;
$currentPage = isset($_GET['pageID']) ? (int) $_GET['pageID'] : 1;
$pagination->setCurrentPage($currentPage);
$from = $pagination->getFrom();
$to = $pagination->getTo();
@@ -228,7 +223,7 @@ if (!empty($catpid)) {
}
$link .= 'pageID='.$previousPage;
$prev = '<a href="'.$link.'"><img src="img/prev.gif" width="10" height="10" border="0" alt="&lt;&lt;" />Back</a>';
$prev = '<a href="'.$link.'"><img src="/img/prev.gif" width="10" height="10" border="0" alt="&lt;&lt;" />Back</a>';
}
$next = '';
@@ -241,7 +236,7 @@ if (!empty($catpid)) {
}
$link .= 'pageID='.$nextPage;
$next = '<a href="'.$link.'">Next<img src="img/next.gif" width="10" height="10" border="0" alt="&gt;&gt;" /></a>';
$next = '<a href="'.$link.'">Next<img src="/img/next.gif" width="10" height="10" border="0" alt="&gt;&gt;" /></a>';
}
foreach ($packages as $key => $pkg) {
@@ -279,7 +274,22 @@ if ($moreinfo) {
$hideMoreInfoLink = '#';
}
$breadcrumbs = $container->get(Breadcrumbs::class)->getBreadcrumbs($catpid, false);
// Template
include __DIR__.'/../templates/packages.php';
echo $template->render('pages/packages.php', [
'title' => $categoryTitle,
'breadcrumbs' => $container->get(Breadcrumbs::class)->getBreadcrumbs($catpid, false),
'showEmptyLink' => $showEmptyLink,
'categories' => $categories,
'catpid' => $catpid,
'packages' => isset($packages) ? $packages : null,
'subCategories' => isset($subCategories) ? $subCategories : null,
'hideMoreInfoLink' => $hideMoreInfoLink,
'showMoreInfoLink' => $showMoreInfoLink,
'prev' => isset($prev) ? $prev : null,
'from' => isset($from) ? $from : null,
'to' => isset($to) ? $to : null,
'total' => isset($total) ? $total : null,
'next' => isset($next) ? $next : null,
'defaultMoreInfoVis' => isset($defaultMoreInfoVis) ? $defaultMoreInfoVis : null,
'totalPackages' => $totalPackages,
'catname' => $catname,
]);

View File

@@ -1,175 +0,0 @@
<?php
/*
+----------------------------------------------------------------------+
| The PECL website |
+----------------------------------------------------------------------+
| Copyright (c) 1999-2019 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Richard Heyes <richard@php.net> |
| Martin Jansen <mj@php.net> |
| Pierre Joye <pierre@php.net> |
| Peter Kokot <petk@php.net> |
+----------------------------------------------------------------------+
*/
?>
<script>
function toggleMoreInfo(packageName, packageID)
{
if (!document.getElementById) {
location.href = '/package/' + packageName;
return;
}
var rowLayer = document.getElementById('moreInfo_' + packageID);
var rowSpacerLayer = document.getElementById('moreInfo_' + packageID + '_spacer');
newDisplay = rowLayer.style.display == 'none' ? 'inline' : 'none';
rowLayer.style.display = newDisplay;
rowSpacerLayer.style.display = newDisplay;
}
</script>
<table border="0" width="100%">
<tr>
<th valign="top" align="left">
Contents of :: <?= $breadcrumbs ?>
</th>
<td valign="top" align="right">
<?php echo $showempty_link; ?>
</td>
</tr>
</table>
<?php if (count($categories) > 0): ?>
<table border="0" cellpadding="6" cellspacing="2" width="100%">
<?php for ($i = 0; $i < ceil(count($categories)/2); $i++): ?>
<tr>
<td width="50%">
<?php if (!empty($categories[2 * $i])): ?>
<?= $categories[2 * $i]; ?>
<?php endif; ?>
</td>
<td width="50%">
<?php if (!empty($categories[2 * $i + 1])): ?>
<?= $categories[2 * $i + 1]; ?>
<?php endif; ?>
</td>
</tr>
<?php endfor; ?>
</table>
<?php endif; ?>
<?php if($catpid and $packages) { ?>
<br />
<?php if (isset($subCategories)) { ?>
Sub-categories: <?php echo $subCategories; ?><br /><br />
<?php } ?>
<a href="<?php echo $hideMoreInfoLink; ?>" title="Hide all more info"><img src="img/moreinfo-no.gif" width="17" height="17" border="0" vspace="3" /></a>
<a href="<?php echo $showMoreInfoLink; ?>" title="Show all more info"><img src="img/moreinfo-yes.gif" width="17" height="17" border="0" vspace="3" /></a>
<table border="0" style="border: solid 1px black" cellspacing="0" cellpadding="0">
<tr>
<td>
<table border="0" width="100%" cellspacing="0">
<tr class="tableHeader">
<td class="tableHeader" width="50" align="left">&nbsp;<?php echo $prev; ?></td>
<td class="tableHeader" align="center">Packages (<?php echo $from; ?> - <?php echo $to; ?> of <?php echo $total; ?>)</td>
<td class="tableHeader" width="50" align="right"><?php echo $next; ?>&nbsp;</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table border="0" id="packageList">
<tr>
<th class="pkgListHeader">#</td>
<th class="pkgListHeader"><nobr>Package name</nobr></td>
<th class="pkgListHeader">Description</td>
<td>&nbsp;</td>
</tr>
<?php foreach($packages as $p) { ?>
<tr>
<td valign="top"><?php echo ($from++); ?></td>
<td valign="top"><a href="/package/<?php echo $p['name']; ?>"><strong><?php echo $p['name']; ?></strong></td>
<td valign="top"><?php echo $p['summary']; ?></td>
<td valign="top">
<a href="#" onclick="toggleMoreInfo(<?php echo "'".$p['name']."'"; ?>,<?php echo $p['id']; ?>); return false" onmouseover="window.status = 'View more info about <?php echo $p['name']; ?>'; return true" onmouseout="window.status = ''" title="View more info about <?php echo $p['name']; ?>">
<img src="img/moreinfo.gif" border="0" />
</a>
</td>
</tr>
<tr <?php if($defaultMoreInfoVis == 'none') { ?> style="display: <?php echo $defaultMoreInfoVis; ?>" <?php } ?> id="moreInfo_<?php echo $p['id']; ?>">
<td>&nbsp;</td>
<td colspan="3" class="moreInfo">
<table border="0" class="moreInfoHeader" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td><span class="moreInfoText">More information</span></td>
<td align="right">
<a href="javascript: toggleMoreInfo(<?php echo "'".$p['name']."'"; ?>,<?php echo $p['id']; ?>)"><img class="closeButton" src="img/close.gif" border="0" /></a>
</td>
</tr>
</table>
<table border="0">
<tr>
<td class="eInfo_label"><strong>Number of releases:</strong></td>
<td><?php echo $p['eInfo']['numReleases']; ?></td>
<td class="eInfo_label"><strong>License type:</strong></td>
<td><?php echo $p['eInfo']['license']; ?></td>
</tr>
<tr>
<td class="eInfo_label"><strong>Status:</strong></td>
<td><?php echo $p['eInfo']['status']; ?></td>
</tr>
</table>
</td>
</tr>
<tr <?php if($defaultMoreInfoVis == 'none') { ?> style="display: <?php echo $defaultMoreInfoVis; ?>" <?php } ?> id="moreInfo_<?php echo $p['id']; ?>_spacer"><td colspan="5">&nbsp;</td></tr>
<?php } ?>
</table>
</td>
</tr>
</table>
<?php } elseif ($catpid) { ?>
<p align="center">
No packages found in this category
</p>
<?php } ?>
<br />
<p align="center">
<?php if(!$catpid) {?>
Total number of packages: <?php echo $totalpackages; ?><br />
<a href="/package-stats.php">View package statistics</a>
<?php } else { ?>
<a href="/package-stats.php?cid=<?php echo $catpid; ?>">Statistics for category "<?php echo $catname; ?>"</a>
<?php } ?>
</p>
<?php response_footer();

View File

@@ -12,6 +12,8 @@ function confirmed_goto(url, message) {
<h1>Edit package</h1>
<p><b><?= $this->e($content) ?></b></p>
<table cellpadding="0" cellspacing="1" style="width: 90%; border: 0px;">
<tr>
<td bgcolor="#000000">

View File

@@ -0,0 +1,156 @@
<?php $this->extend('layout.php', ['title' => $title]) ?>
<?php $this->start('content') ?>
<script>
function toggleMoreInfo(packageName, packageID)
{
if (!document.getElementById) {
location.href = '/package/' + packageName;
return;
}
var rowLayer = document.getElementById('moreInfo_' + packageID);
var rowSpacerLayer = document.getElementById('moreInfo_' + packageID + '_spacer');
newDisplay = rowLayer.style.display == 'none' ? 'inline' : 'none';
rowLayer.style.display = newDisplay;
rowSpacerLayer.style.display = newDisplay;
}
</script>
<table border="0" width="100%">
<tr>
<th valign="top" align="left">
Contents of :: <?= $breadcrumbs ?>
</th>
<td valign="top" align="right">
<?= $showEmptyLink ?>
</td>
</tr>
</table>
<?php if (count($categories) > 0): ?>
<table border="0" cellpadding="6" cellspacing="2" width="100%">
<?php for ($i = 0; $i < ceil(count($categories)/2); $i++): ?>
<tr>
<td width="50%">
<?php if (!empty($categories[2 * $i])): ?>
<?= $categories[2 * $i] ?>
<?php endif ?>
</td>
<td width="50%">
<?php if (!empty($categories[2 * $i + 1])): ?>
<?= $categories[2 * $i + 1] ?>
<?php endif ?>
</td>
</tr>
<?php endfor ?>
</table>
<?php endif ?>
<?php if($catpid && $packages): ?>
<br>
<?php if (isset($subCategories)): ?>
Sub-categories: <?= $subCategories ?><br><br>
<?php endif ?>
<a href="<?= $hideMoreInfoLink ?>" title="Hide all more info">
<img src="/img/moreinfo-no.gif" width="17" height="17" border="0" vspace="3" />
</a>
<a href="<?= $showMoreInfoLink ?>" title="Show all more info">
<img src="/img/moreinfo-yes.gif" width="17" height="17" border="0" vspace="3" />
</a>
<table border="0" style="border: solid 1px black" cellspacing="0" cellpadding="0">
<tr>
<td>
<table border="0" width="100%" cellspacing="0">
<tr class="tableHeader">
<td class="tableHeader" width="50" align="left">&nbsp;<?= $prev ?></td>
<td class="tableHeader" align="center">Packages (<?= $from ?> - <?= $to ?> of <?= $total ?>)</td>
<td class="tableHeader" width="50" align="right"><?= $next ?>&nbsp;</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table border="0" id="packageList">
<tr>
<th class="pkgListHeader">#</td>
<th class="pkgListHeader">Package name</td>
<th class="pkgListHeader">Description</td>
<td>&nbsp;</td>
</tr>
<?php foreach($packages as $p): ?>
<tr>
<td valign="top"><?= $from++ ?></td>
<td valign="top"><a href="/package/<?= $this->e($p['name']) ?>"><strong><?= $this->e($p['name']) ?></strong></td>
<td valign="top"><?= $this->e($p['summary']) ?></td>
<td valign="top">
<a href="#" onclick="toggleMoreInfo('<?= $this->e($p['name']) ?>',<?= $this->e($p['id']) ?>); return false" onmouseover="window.status = 'View more info about <?= $this->e($p['name']) ?>'; return true" onmouseout="window.status = ''" title="View more info about <?= $this->e($p['name']) ?>">
<img src="/img/moreinfo.gif" border="0" />
</a>
</td>
</tr>
<tr <?= ('none' === $defaultMoreInfoVis) ? 'style="display:'.$defaultMoreInfoVis.'"' : '' ?> id="moreInfo_<?= $p['id'] ?>">
<td>&nbsp;</td>
<td colspan="3" class="moreInfo">
<table border="0" class="moreInfoHeader" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td><span class="moreInfoText">More information</span></td>
<td align="right">
<a href="javascript: toggleMoreInfo('<?= $this->e($p['name']) ?>',<?= $this->e($p['id']) ?>)"><img class="closeButton" src="/img/close.gif" border="0" /></a>
</td>
</tr>
</table>
<table border="0">
<tr>
<td class="eInfo_label"><strong>Number of releases:</strong></td>
<td><?= $this->e($p['eInfo']['numReleases']) ?></td>
<td class="eInfo_label"><strong>License type:</strong></td>
<td><?= $this->e($p['eInfo']['license']) ?></td>
</tr>
<tr>
<td class="eInfo_label"><strong>Status:</strong></td>
<td><?= $p['eInfo']['status'] ?></td>
</tr>
</table>
</td>
</tr>
<tr <?= 'none' === $defaultMoreInfoVis ? 'style="display:'.$defaultMoreInfoVis.'"' : '' ?> id="moreInfo_<?= $this->noHtml($p['id']) ?>_spacer">
<td colspan="5">&nbsp;</td>
</tr>
<?php endforeach ?>
</table>
</td>
</tr>
</table>
<?php elseif ($catpid): ?>
<p align="center">No packages found in this category</p>
<?php endif ?>
<br>
<p align="center">
<?php if(!$catpid): ?>
Total number of packages: <?= $this->e($totalPackages) ?><br>
<a href="/package-stats.php">View package statistics</a>
<?php else: ?>
<a href="/package-stats.php?cid=<?= $this->e($catpid) ?>">Statistics for category "<?= $this->e($catname) ?>"</a>
<?php endif ?>
</p>
<?php $this->end('content') ?>