Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: photo album search and improved album layout #1943

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -33,13 +33,14 @@
* @method Acl acl(string $factory)
* @method string bootstrapElementError(ElementInterface $element)
* @method Breadcrumbs breadcrumbs()
* @method Breadcrumbs addBreadcrumb(string $breadcrumb = '', bool $active = true, string $url = '', string|null $setType = null)

Check warning on line 36 in module/Application/src/View/HelperTrait.php

View workflow job for this annotation

GitHub Actions / php-codesniffer / PHP_CodeSniffer (8.3)

Line exceeds 120 characters; contains 129 characters
* @method CompanyUserModel|null companyIdentity()
* @method string diff(string|null $old, string|null $new, string $renderer = Diff::DIFF_RENDER_COMBINED, array $rendererOverwrites = [], array $differOverwrites = [])

Check warning on line 38 in module/Application/src/View/HelperTrait.php

View workflow job for this annotation

GitHub Actions / php-codesniffer / PHP_CodeSniffer (8.3)

Line exceeds 120 characters; contains 167 characters
* @method CompanyFeaturedPackageModel|null featuredCompanyPackage()
* @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
Loading