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

Feature/block list of sites #1305

Merged
merged 2 commits into from
Oct 16, 2018
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
1 change: 1 addition & 0 deletions application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@
'media' => Site\BlockLayout\Media::class,
'browsePreview' => Site\BlockLayout\BrowsePreview::class,
'itemShowCase' => Site\BlockLayout\ItemShowcase::class,
'listOfSites' => Site\BlockLayout\ListOfSites::class,
'tableOfContents' => Site\BlockLayout\TableOfContents::class,
'lineBreak' => Site\BlockLayout\LineBreak::class,
'itemWithMetadata' => Site\BlockLayout\ItemWithMetadata::class,
Expand Down
88 changes: 88 additions & 0 deletions application/src/Site/BlockLayout/ListOfSites.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
namespace Omeka\Site\BlockLayout;

use Omeka\Api\Representation\SiteRepresentation;
use Omeka\Api\Representation\SitePageRepresentation;
use Omeka\Api\Representation\SitePageBlockRepresentation;
use Zend\Form\Element;
use Zend\Form\Form;
use Zend\View\Renderer\PhpRenderer;

class ListOfSites extends AbstractBlockLayout
{
protected $defaults = [
'limit' => null,
'pagination' => false,
];

public function getLabel()
{
return 'List of sites'; // @translate
}

public function form(PhpRenderer $view, SiteRepresentation $site,
SitePageRepresentation $page = null, SitePageBlockRepresentation $block = null
) {
$data = $block ? $block->data() + $this->defaults : $this->defaults;

$form = new Form();
$form->add([
'name' => 'o:block[__blockIndex__][o:data][limit]',
'type' => Element\Number::class,
'options' => [
'label' => 'Max number of sites', // @translate
'info' => 'An empty value means no limit.', // @translate
],
'attributes' => [
'id' => 'list-of-sites-limit',
'placeholder' => '100', // @translate
],
]);
$form->add([
'name' => 'o:block[__blockIndex__][o:data][pagination]',
'type' => Element\Checkbox::class,
'options' => [
'label' => 'Add pagination in case of a limit', // @translate
],
'attributes' => [
'id' => 'list-of-sites-pagination',
],
]);

$form->setData([
'o:block[__blockIndex__][o:data][limit]' => $data['limit'],
'o:block[__blockIndex__][o:data][pagination]' => $data['pagination'],
]);

return $view->formCollection($form);
}

public function render(PhpRenderer $view, SitePageBlockRepresentation $block)
{
$limit = $block->dataValue('limit', $this->defaults['limit']);
$pagination = $limit && $block->dataValue('pagination', $this->defaults['pagination']);

$data = [];
if ($pagination) {
$currentPage = $view->params()->fromQuery('page', 1);
$data['page'] = $currentPage;
$data['per_page'] = $limit;
} elseif ($limit) {
$data['limit'] = $limit;
}

$response = $view->api()->search('sites', $data);

if ($pagination) {
$totalCount = $response->getTotalResults();
$view->pagination(null, $totalCount, $currentPage, $limit);
}

$sites = $response->getContent();

return $view->partial('common/block-layout/list-of-sites', [
'sites' => $sites,
'pagination' => $pagination,
]);
}
}
13 changes: 13 additions & 0 deletions application/view/common/block-layout/list-of-sites.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
$hyperlink = $this->plugin('hyperlink');
?>
<div class="list-of-sites">
<?php if ($pagination): ?>
<?php echo $this->pagination(); ?>
<?php endif; ?>
<ul>
<?php foreach ($sites as $site): ?>
<li><?php echo $hyperlink($site->title(), $site->siteUrl()); ?></li>
<?php endforeach; ?>
</ul>
</div>