Skip to content

Commit

Permalink
Merge pull request GEWIS#1943 from tomudding/feature/photo-album-search
Browse files Browse the repository at this point in the history
feat: photo album search and improved album layout
  • Loading branch information
tomudding authored Dec 13, 2024
2 parents b422521 + 3e8cf8c commit 39b99cb
Show file tree
Hide file tree
Showing 27 changed files with 532 additions and 225 deletions.
13 changes: 10 additions & 3 deletions module/Activity/view/activity/activity/archive.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ use Laminas\View\Renderer\PhpRenderer;
* @var array $years
* @var ActivityModel[] $activities
*/

if (isset($year)) {
$activeYearIndex = array_search($year, $years);
$isYearInDropdown = $activeYearIndex !== false && $activeYearIndex < count($years) - 5;
} else {
$isYearInDropdown = false;
}
?>
<div class="content-container">
<section class="section">
<div class="container">
<ul class="nav nav-pills head-menu">
<ul class="nav nav-tabs head-menu">
<?php for ($i = count($years) - 1; $i >= max(0, count($years) - 5); $i--): ?>
<li class="<?= isset($year) && $year === $years[$i] ? 'active' : '' ?>">
<a href="<?= $this->url('activity/year', ['year' => $years[$i]]); ?>">
Expand All @@ -24,7 +31,7 @@ use Laminas\View\Renderer\PhpRenderer;
</li>
<?php endfor; ?>
<?php if (7 <= count($years)): ?>
<li role="presentation" class="dropdown">
<li role="presentation" class="dropdown <?= $isYearInDropdown ? 'active' : '' ?>">
<a class="dropdown-toggle"
data-toggle="dropdown"
href="<?= $this->hashUrl() ?>"
Expand All @@ -35,7 +42,7 @@ use Laminas\View\Renderer\PhpRenderer;
</a>
<ul class="dropdown-menu">
<?php for ($i = max(0, count($years) - 6); $i > 0; $i--): ?>
<li>
<li class="<?= isset($year) && $year === $years[$i] ? 'active' : '' ?>">
<a href="<?= $this->url('activity/year', ['year' => $years[$i]]); ?>">
<?= $years[$i] ?>-<?= $years[$i] + 1 ?>
</a>
Expand Down
21 changes: 19 additions & 2 deletions module/Application/language/en.po

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions module/Application/language/gewisweb.pot

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions module/Application/language/nl.po

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions module/Application/src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Application\View\Helper\FileUrl;
use Application\View\Helper\GlideUrl;
use Application\View\Helper\HashUrl;
use Application\View\Helper\HighlightSearch;
use Application\View\Helper\JobCategories;
use Application\View\Helper\Markdown;
use Application\View\Helper\ModuleIsActive;
Expand Down Expand Up @@ -344,6 +345,9 @@ public function getViewHelperConfig(): array

return new HashUrl($serverUrlHelper);
},
'highlightSearch' => static function () {
return new HighlightSearch();
},
'timeDiff' => static function (ContainerInterface $container) {
return new TimeDiff($container->get(MvcTranslator::class));
},
Expand Down
54 changes: 54 additions & 0 deletions module/Application/src/View/Helper/HighlightSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Application\View\Helper;

use Laminas\View\Helper\AbstractHelper;

use function mb_stripos;
use function mb_strlen;
use function mb_substr;
use function sprintf;
use function transliterator_transliterate;

class HighlightSearch extends AbstractHelper
{
/**
* Insert `<mark>` around a search prompt in the content of a returned result.
*/
public function __invoke(
string $query,
string $content,
): string {
// Convert the decision to something that is easily searchable (i.e. it MUST contain only Latin-ASCII characters).

Check warning on line 24 in module/Application/src/View/Helper/HighlightSearch.php

View workflow job for this annotation

GitHub Actions / php-codesniffer / PHP_CodeSniffer (8.3)

Line exceeds 120 characters; contains 122 characters
$transliteratedDecision = transliterator_transliterate('Any-Latin; Latin-ASCII', $content);
// Do the same for the search prompt, as otherwise searches WITH non-ASCII characters will not work.
$query = transliterator_transliterate('Any-Latin; Latin-ASCII', $query);

$offset = 0;
$output = '';
$length = mb_strlen($query);

// There is a very important assumption here; the transliterated version of the decision MUST be exactly as long as

Check warning on line 33 in module/Application/src/View/Helper/HighlightSearch.php

View workflow job for this annotation

GitHub Actions / php-codesniffer / PHP_CodeSniffer (8.3)

Line exceeds 120 characters; contains 123 characters
// the original version. Otherwise, the insertion is done with an incorrect offset. As such, using `iconv` is NOT

Check warning on line 34 in module/Application/src/View/Helper/HighlightSearch.php

View workflow job for this annotation

GitHub Actions / php-codesniffer / PHP_CodeSniffer (8.3)

Line exceeds 120 characters; contains 121 characters
// good as it will either extend (e.g. `€` becomes `EUR`) or completely remove characters (`//IGNORE` option).
while (false !== ($position = mb_stripos($transliteratedDecision, $query, $offset, 'UTF-8'))) {
// Progressively insert markers into the original decision.
$output .= sprintf(
'%s%s%s%s',
mb_substr($content, $offset, $position - $offset, 'UTF-8'),
'<mark>',
mb_substr($content, $position, $length, 'UTF-8'),
'</mark>',
);

$offset = $position + $length;
}

// Add the final part of the decision back.
$output .= mb_substr($content, $offset, null, 'UTF-8');

return $output;
}
}
1 change: 1 addition & 0 deletions module/Application/src/View/HelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @method string fileUrl(string $path)
* @method GlideUrl glideUrl()
* @method HashUrl hashUrl()
* @method string highlightSearch(string $query, string $content)
* @method JobCategoryModel[] jobCategories()
* @method string localisedTextElement(ElementInterface $element)
* @method string localiseText(LocalisedTextModel $localisedText)
Expand Down
41 changes: 2 additions & 39 deletions module/Decision/view/decision/decision/search.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,6 @@ use Laminas\View\Renderer\PhpRenderer;
*/

$this->headTitle($this->translate('Search for decision'));

/**
* Insert `<mark>` around a search prompt in the content of a decision.
*/
function highlightSearch(
string $decision,
string $search,
): string {
// Convert the decision to something that is easily searchable (i.e. it MUST contain only Latin-ASCII characters).
$transliteratedDecision = transliterator_transliterate('Any-Latin; Latin-ASCII', $decision);
// Do the same for the search prompt, as otherwise searches WITH non-ASCII characters will not work.
$search = transliterator_transliterate('Any-Latin; Latin-ASCII', $search);

$offset = 0;
$output = '';
$length = mb_strlen($search);

// There is a very important assumption here; the transliterated version of the decision MUST be exactly as long as
// the original version. Otherwise, the insertion is done with an incorrect offset. As such, using `iconv` is NOT
// good as it will either extend (e.g. `€` becomes `EUR`) or completely remove characters (`//IGNORE` option).
while (false !== ($position = mb_stripos($transliteratedDecision, $search, $offset, 'UTF-8'))) {
// Progressively insert markers into the original decision.
$output .= sprintf('%s%s%s%s',
mb_substr($decision, $offset, $position - $offset, 'UTF-8'),
'<mark>',
mb_substr($decision, $position, $length, 'UTF-8'),
'</mark>',
);

$offset = $position + $length;
}

// Add the final part of the decision back.
$output .= mb_substr($decision, $offset, null, 'UTF-8');

return $output;
}
?>
<section class="section">
<div class="container">
Expand Down Expand Up @@ -119,9 +82,9 @@ function highlightSearch(
</a>
</strong>
<span class="decision-content">
<?= highlightSearch(
$this->escapeHtml($decision->getContent()),
<?= $this->highlightSearch(
$this->escapeHtml($prompt),
$this->escapeHtml($decision->getContent()),
) ?>
</span>
</li>
Expand Down
9 changes: 9 additions & 0 deletions module/Photo/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@
],
],
],
'search' => [
'type' => Literal::class,
'options' => [
'route' => '/search',
'defaults' => [
'action' => 'search',
],
],
],
'weekly' => [
'type' => Literal::class,
'options' => [
Expand Down
Loading

0 comments on commit 39b99cb

Please sign in to comment.