Skip to content

Commit

Permalink
last fixes and readme update
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Dec 6, 2017
1 parent b330dbb commit 3a41c70
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/composer.lock
/vendor
57 changes: 56 additions & 1 deletion README.md
Expand Up @@ -2,7 +2,13 @@

[![Latest Stable Version](https://poser.pugx.org/heimrichhannot/contao-newsnavigation-bundle/v/stable)](https://packagist.org/packages/heimrichhannot/contao-newsnavigation-bundle)

A [contao](https://contao.org/de/) bundle to provide a simple navigation between news articles. It add template variables got go from one news article to the next or the previous article. News article order is calculated by time property.
A [contao](https://contao.org/de/) bundle to provide a simple navigation between news articles. It add template variables to go from one news article to the next or the previous article. News article order is calculated by time property.

## Features

* add Template variables to NewsReaderModule to jump between news articles
* option to respect news archives set in NewsReaderModule
* add custom filters via service

## Requirements

Expand All @@ -18,6 +24,8 @@ composer require heimrichhannot/contao-newsnavigation-bundle

## Usage

### Template variables

To use this extension, you need to output the template variables in your custom news template.

Example:
Expand All @@ -29,6 +37,10 @@ Example:
<?php endif; ?>
```

### Newsarchives

To let the navigation respect only news archives set in NewsReaderModule, you find an option in the module backend.

## Developers

### Template variables
Expand All @@ -43,3 +55,46 @@ nextArticleLabel | Next article label ("Next article")
previousArticleLabel | Previous article label ("Previous article")

If you want the news title instead the next/previous article label, see [#1](https://github.com/heimrichhannot/contao-newsnavigation-bundle/issues/1) how to do this.

### Custom filters

The module allow a simple mechanic to add custom filters for the news navigation. You need to set additional filters to the `huh.newsnavigation.newsfilter` before the `parseArticles` Hook (from this module) of the NewsReaderModel is called. You can use following methods:

Examples:
```
// add additional database conditions
System::getContainer()->get('huh.newsnavigation.newsfilter')->addFilter('tl_news.newsCategory=?', 'contao4bundles');
// add additions options
System::getContainer()->get('huh.newsnavigation.newsfilter')->setOption('limit', '5');
```

### Custom filter use

You can use the filter in custom way:

```
$filter = System::getContainer()->get('huh.newsnavigation.newsfilter');
// Creates a new filter object which contains all filters
$newFilter = $filter->createCopy();
// Create a new empty newsfilter instance
$newFilter = new HeimrichHannot\NewsNavigationBundle\NewsFilter\NewsFilter();
// Return the filter rules
$filter->getColumns()
$filter->getValues()
$filter->getOptions()
// Override filter rules
$filter->setColumns();
$filter->setValues();
$filter->setOptions();
// Change or return model
$filter->setModel();
$filter->getModel();
```


4 changes: 2 additions & 2 deletions src/EventListener/HookListener.php
Expand Up @@ -20,12 +20,12 @@ class HookListener
* @param array $article
* @param ModuleNewsReader$module
*/
public function addNewsNavigationLinks($template, $article, $module)
public function addNewsNavigationLinks(FrontendTemplate $template, array $article, $module)
{
$filter = System::getContainer()->get('huh.newsnavigation.newsfilter');
$filter->filterOnlyPublished();
if ($module->newsnavigationRespectArchive) {
$filter->filterByArchive($article['pid']);
$filter->filterByPids($module->news_archives);
}

$olderArticle = $filter->createCopy()
Expand Down
33 changes: 20 additions & 13 deletions src/NewsFilter/NewsFilter.php
Expand Up @@ -16,16 +16,22 @@ class NewsFilter
* @var NewsModel
*/
private $model;
private $table;
private $columns = [];
private $values = [];
private $options = [];

/**
* NewsArticle constructor.
*
* @param NewsModel|null $model
*/
public function __construct()
public function __construct($model = null)
{
$this->setModel(new NewsModel());
if (null === $model) {
$model = new NewsModel();
}
$this->setModel($model);
}

/**
Expand All @@ -52,17 +58,14 @@ public function filterOnlyPublished()
/**
* Filter by a single archive.
*
* @param int $pid
* @param array $pids
*
* @return $this
*/
public function filterByArchive($pid)
public function filterByPids(array $pids)
{
$t = $this->table;
if ($pid && is_numeric($pid)) {
$this->columns[] = 'pid = ?';
$this->values[] = $pid;
}
$this->columns[] = "$t.pid IN(".implode(',', array_map('intval', $pids)).')';

return $this;
}
Expand Down Expand Up @@ -136,26 +139,30 @@ public function setModel(NewsModel $model)
*
* @return $this
*/
public function addFilter(string $column, string $value)
public function addFilter(string $column, string $value = '')
{
$this->columns[] = $column;
$this->values[] = $value;
if (!empty($value)) {
$this->values[] = $value;
}

return $this;
}

/**
* Add additions options to the filter. If option is already set, it will be appended.
* Add additions options to the filter.
* Some options will be appended instead of overwritten by default ("order"),.
*
* @param string $key
* @param string $value
* @param bool $forceOverwrite if true, option will be overwritten, if already exist
* @param bool $forceOverwrite if true, option will always be overwritten
*
* @return $this
*/
public function setOption(string $key, string $value, bool $forceOverwrite = false)
{
if (!$this->options[$key] || true === $forceOverwrite) {
$append = ['order'];
if (!$this->options[$key] || true === $forceOverwrite || !in_array($key, $append, true)) {
$this->options[$key] = $value;
} else {
$this->options[$key] .= ", $value";
Expand Down
7 changes: 6 additions & 1 deletion src/Resources/contao/dca/tl_module.php
Expand Up @@ -12,9 +12,14 @@

$dca['palettes']['newsreader'] = str_replace('news_archives', 'news_archives,newsnavigationRespectArchive', $dca['palettes']['newsreader']);

$translator = System::getContainer()->get('translator');

$fields = [
'newsnavigationRespectArchive' => [
'label' => &$GLOBALS['TL_LANG']['tl_module']['newsnavigationRespectArchive'],
'label' =>[
$translator->trans('huh.newsnavigation.tl_module.newsnavigationRespectArchive.0'),
$translator->trans('huh.newsnavigation.tl_module.newsnavigationRespectArchive.1'),
],
'inputType' => 'checkbox',
'exclude' => true,
'sql' => "char(1) NOT NULL default ''"
Expand Down
7 changes: 6 additions & 1 deletion src/Resources/translations/messages.de.yml
@@ -1,4 +1,9 @@
huh.newsnavigation:
article:
next: Nächster Artikel
previous: Vorheriger Artikel
previous: Vorheriger Artikel

tl_module:
newsnavigationRespectArchive:
0: Neugkeitennavigation nur im aktuellen Archiv
1: Wenn gesetzt, werden die die Link zum nächsten und vorherigen Neuigkeitenartikel nur Neugkeiten aus dem Archiv des aktuell betrachteten Neuigkeitenartikels enthalten.

0 comments on commit 3a41c70

Please sign in to comment.