Skip to content

Commit

Permalink
Merge branch 'release-1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
sakren committed May 8, 2014
2 parents f33a6b5 + 7e4bd53 commit 6837280
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 8 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,20 @@ menu:
include: '^Book\:[a-zA-Z]+$'
```

or with array of included targets.

```
menu:
default:
Books settings:
target: Book:default
include:
- Book:add
- Book:edit
```

## Custom templates

```
Expand All @@ -184,6 +198,16 @@ menu:
items: []
```

## Translated titles

```
menu:
default:
translator: true
```

## More menus

Lets say that we want base menu and menu just for users in `admin` role.
Expand Down Expand Up @@ -310,5 +334,9 @@ class BookPresenter extends BasePresenter

## Changelog

* 1.0.1
+ Include option can be array of targets [https://github.com/sakren/nette-menu/issues/1](#1)
+ Added support for translatable titles [https://github.com/sakren/nette-menu/issues/2](#2)

* 1.0.0
+ First version
13 changes: 11 additions & 2 deletions src/DK/Menu/DI/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Extension extends CompilerExtension
private $menuDefaults = array(
'controlClass' => 'DK\Menu\UI\Control',
'controlInterface' => 'DK\Menu\UI\IControlFactory',
'translator' => false,
'template' => array(
'menu' => null, // see constructor
'breadcrumb' => null,
Expand Down Expand Up @@ -64,7 +65,7 @@ public function __construct()

public function loadConfiguration()
{
$config = $this->getConfig($this->defaults);
$config = $this->getConfig($this->defaults) ;
$builder = $this->getContainerBuilder();

$autowired = count($config) === 1;
Expand All @@ -76,11 +77,19 @@ public function loadConfiguration()

$data['items'] = $this->parseItems($data['items']);

$builder->addDefinition($this->prefix($name))
$menu = $builder->addDefinition($this->prefix($name))
->setClass('DK\Menu\Menu')
->setFactory('DK\Menu\DI\Extension::createMenu', array($name, $data['items']))
->setAutowired($autowired);

if (($translator = $data['translator']) !== false) {
if ($translator === true) {
$translator = '@Nette\Localization\ITranslator';
}

$menu->addSetup('setTranslator', array($translator));
}

$builder->addDefinition($this->prefix($name. 'Control'))
->setClass($data['controlClass'], array('@'. $this->prefix($name)))
->addSetup('setMenuTemplate', array($data['template']['menu']))
Expand Down
24 changes: 22 additions & 2 deletions src/DK/Menu/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function getMenu()


/**
* @return \DK\Menu\Item
* @return string
*/
public function getTitle()
{
Expand All @@ -105,6 +105,20 @@ public function setTitle($title)
}


/**
* @return string
*/
public function getTranslatedTitle()
{
$title = $this->getTitle();
if (($translator = $this->getMenu()->getTranslator()) !== null) {
$title = $translator->translate($title);
}

return $title;
}


/**
* @return string
*/
Expand Down Expand Up @@ -462,7 +476,13 @@ public function isActive()
}

if ($this->active === null && $this->hasInclude()) {
if (preg_match('~'. $this->getInclude(). '~', $presenter->getName(). ':'. $presenter->getAction())) {
$include = $this->getInclude();
$name = $presenter->getName(). ':'. $presenter->getAction();

if (is_string($include) && preg_match('~'. $this->getInclude(). '~', $name)) {
$this->active = true;

} elseif (is_array($include) && in_array($name, $include)) {
$this->active = true;
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/DK/Menu/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Nette\Application\Application;
use Nette\Application\UI\Presenter;
use Nette\Security\User;
use Nette\Localization\ITranslator;

/**
*
Expand All @@ -20,6 +21,9 @@ class Menu extends Container
/** @var \Nette\Security\User */
private $user;

/** @var \Nette\Localization\ITranslator */
private $translator;


/**
* @param \Nette\Security\User $user
Expand Down Expand Up @@ -53,6 +57,26 @@ public function init(Application $application)
}


/**
* @return \Nette\Localization\ITranslator
*/
public function getTranslator()
{
return $this->translator;
}


/**
* @param \Nette\Localization\ITranslator $translator
* @return \DK\Menu\Menu
*/
public function setTranslator(ITranslator $translator)
{
$this->translator = $translator;
return $this;
}


/**
* @return \DK\Menu\Menu
*/
Expand Down
2 changes: 1 addition & 1 deletion src/DK/Menu/UI/breadcrumb.latte
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{foreach $menu->getPath() as $item}
<a href="{$item->getLink()}">{$item->getTitle()}</a>{sep} / {/sep}
<a href="{$item->getLink()}">{$item->getTranslatedTitle()}</a>{sep} / {/sep}
{/foreach}
2 changes: 1 addition & 1 deletion src/DK/Menu/UI/menu.latte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<ul n:block="branch" n:if="$menuParent->hasVisualItems()">
<li n:foreach="$menuParent->getItems() as $item" n:if="$item->isVisual() && $item->isAllowed()">
<a href="{$item->getLink()}" n:class="$item->isActive() ? active">{$item->getTitle()}</a>
<a href="{$item->getLink()}" n:class="$item->isActive() ? active">{$item->getTranslatedTitle()}</a>
{include branch, menuParent => $item}
</li>
</ul>
52 changes: 51 additions & 1 deletion tests/DKTests/Menu/Menu.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use Nette\Application\Responses\TextResponse;
use Nette\Application\UI\Presenter;
use Nette\Security\IAuthenticator;
use Nette\Security\Identity;
use Nette\Localization\ITranslator;
use DK\Menu\DI\Extension;
use DK\Menu\Item;

Expand Down Expand Up @@ -161,7 +162,7 @@ class MenuTest extends TestCase
}


public function testCurrentIncluded()
public function testCurrentIncludedRegexp()
{
$container = $this->createContainer('menu');
$presenter = $this->createPresenter($container, 'User');
Expand All @@ -177,6 +178,34 @@ class MenuTest extends TestCase
}


public function testCurrentIncludedArray()
{
$container = $this->createContainer('menu');
$presenter = $this->createPresenter($container, 'About');
$request = $this->createRequest('About', 'terms');

$presenter->run($request);

$menu = $presenter->getMenu();
$current = $menu->getLastCurrentItem();

Assert::same('About us', $current->getTitle());
Assert::same('About:default', $current->getTarget());
}


public function testTranslator()
{
$container = $this->createContainer('translated');
$menu = $container->getByType('DK\Menu\Menu'); /** @var $menu \DK\Menu\Menu */

$item = $menu->getItem('home');

Assert::same('menu.homepage', $item->getTitle());
Assert::same('Homepage', $item->getTranslatedTitle());
}


public function testPath()
{
$container = $this->createContainer('menu');
Expand Down Expand Up @@ -318,4 +347,25 @@ class Authenticator implements IAuthenticator
}


class Translator implements ITranslator
{


/**
* @param string $message
* @param int|null $count
* @return string
*/
public function translate($message, $count = null)
{
if ($message === 'menu.homepage') {
return 'Homepage';
}

return null;
}

}


\run(new MenuTest);
5 changes: 4 additions & 1 deletion tests/DKTests/app/config/menu.neon
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ menu:

about:
title: About us
target: About:default
target: About:default
include:
- About:terms
- About:priceList
14 changes: 14 additions & 0 deletions tests/DKTests/app/config/translated.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:

- DKTests\Menu\Translator

menu:

default:

translator: true

items:
home:
title: menu.homepage
target: Homepage:default
1 change: 1 addition & 0 deletions tests/DKTests/app/templates/About/terms.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{layout ../@layout.latte}

0 comments on commit 6837280

Please sign in to comment.