Skip to content

Instantly share code, notes, and snippets.

@patie
Created April 26, 2012 18:02
Show Gist options
  • Save patie/2501465 to your computer and use it in GitHub Desktop.
Save patie/2501465 to your computer and use it in GitHub Desktop.
<?php
/**
* Base class for all application presenters.
*
* @author John Doe
* @package MyApplication
*/
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
protected function createComponentNavigation($name)
{
$navigacia = new NavigationControl($this, $name);
return $navigacia;
}
}
<?php
/**
* Author: Patrik Gmitter <[email protected]>
* Date: 4/24/12
*/
namespace Model;
use Doctrine\ORM\Mapping as ORM,
Doctrine\ORM\Proxy,
Doctrine\Common\Collections;
/**
* @ORM\Entity
* @ORM\Table(name="blogcategory")
*/
class BlogCategory extends \Nette\Object
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\Column(type="integer", nullable=true)
*/
protected $blogcategory_id;
/**
* @ORM\Column(type="string")
*/
protected $title;
/**
* @ORM\Column(type="string")
*/
protected $slug;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* @ORM\Column(type="integer")
*/
protected $status;
/**
* @ORM\ManyToOne(targetEntity="BlogCategory", inversedBy="id", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="blogcategory_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $parent;
/**
* @ORM\OneToMany(targetEntity="BlogCategory", mappedBy="parent", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"id" = "ASC"})
*/
protected $childrens;
public function __construct($name = "")
{
parent::__construct();
$this->childrens = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getTitle()
{
return $this->title;
}
public function getChildrens()
{
return $this->childrens;
}
}
<?php
/**
* Author: Patrik Gmitter <[email protected]>
* Date: 4/26/12
*/
use Nette\Application\UI\Control,
Nette\Caching\Cache;
class NavigationControl extends Control
{
protected $context;
private $menuTemplate;
public function __construct($parent, $name = NULL)
{
parent::__construct($parent, $name);
$this->context = $parent->context;
}
public function render($position = 'top')
{
$template = $this->template;
$template->setFile(__DIR__ . '/' . $position . '.latte');
$dir = $this->context->params['tempDir'] . '/cache';
$journal = new Nette\Caching\Storages\FileJournal($dir);
$storage = new Nette\Caching\Storages\FileStorage($dir, $journal);
$cache = new Nette\Caching\Cache($storage, 'front');
$navigacia = $cache->load('navigation');
if ($navigacia === NULL) {
$qb = $this->context->database->createQueryBuilder();
$db_result = $qb
->select('bc')
->from('Model\BlogCategory', 'bc')
->where('bc.blogcategory_id is NULL')
->getQuery()->getResult();
$cache->save('navigation', $db_result, array(
Cache::EXPIRE => '+ 1 month'
//Cache::TAGS => array("navigacia""),
));
$template->navigation = $cache->load('navigation');
}
$template->render();
}
}
* kategoria 1
** podkategoria 1.1
** podpodkategoria 1.2
* kategoria 2
* kategoria 3
* kategoria 4
{block #menu}
<ul class="control menu">
<li n:foreach="$navigation as $item">
{if count($item->childrens)>0 }
<span class="active"><strong>{$item->title}</strong></span>
{include #menu, navigation => $item->childrens}
{else}
<span class="active"><strong>{$item->title}</strong></span>
{/if}
</li>
</ul>
{/block}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment