-
Notifications
You must be signed in to change notification settings - Fork 7
/
CommonBasePresenter.php
114 lines (97 loc) · 3.31 KB
/
CommonBasePresenter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* nPress - opensource cms
*
* @copyright (c) 2012 Pavel Zbytovský (pavel@zby.cz)
* @link http://npress.info/
* @package nPress
*/
/** Base presenter for both Admin and Front module
*
* @author Pavel Zbytovský (pavel@zby.cz)
* @package nPress
*/
abstract class CommonBasePresenter extends Presenter
{
/** @persistent */
public $lang = 'cs'; //TODO from config (https://github.com/nette/nette/pull/445)
public $pages;
public function startup() {
parent::startup();
//lang settings
$this->template->lang = PagesModel::$lang = $this->lang;
$this->template->langs = $this->context->params["langs"];
$this->template->setTranslator(new TranslationsModel($this->lang));
//pages tree
$this->pages = PagesModel::getRoot();
$this->template->pages = $this->pages;
$this->template->crumbs = array();
//configuration
$this->template->config = $this->context->params['npress'];
$this->template->frontjslatte = $this->context->params['appDir'] . '/FrontModule/templates/frontjs.latte';
$this->template->npLayoutFile = $this->context->params['appDir'] . '/FrontModule/templates/@layout.latte';
}
// Link to page mutation, or homepage
public function langSwitch($l){
if(isset($this->page) AND $this->page->lang($l))
return $this->link('this', array('lang'=>$l)); //other lang mutation
return $this->link('Pages:', array('lang'=>$l)); //default page
}
//returns true also for "category" parent
public function isCurrent($link_page_id){
if(!isset($this->page) OR $this->page==false)
return false;
if($link_page_id == $this->page->id)
return true;
if($link_page_id == $this->page->getParent()->id AND $this->page->getParent()->getMeta('.category'))
return true;
return false;
}
//Allow Plugins as direct components of presenter
protected function createComponent($name){
$plugins = $this->context->plugins->getPlugins();
if(in_array($name, $plugins))
return new $name;
return parent::createComponent($name);
}
//Allow to use helpers as a latte macros
public function templatePrepareFilters($template) {
$template->registerFilter($e = new Nette\Latte\Engine());
$s = new Nette\Latte\Macros\MacroSet($e->compiler);
$s->addMacro('helper', 'ob_start()',
function($n) {
$w = new \Nette\Latte\PhpWriter($n->tokenizer, $n->args);
return $w->write('echo %modify(ob_get_clean())');
}
);
}
/** Trigger plugin event as filter supplied value
* @param $eventname
* @param $filter value to supply to filter chain
* @return string resulting value
*/
public function triggerEvent_filter($eventname, $filter){
$triggers = $this->context->plugins->getEventTriggers($eventname);
foreach($triggers as $plugin){
$filter = call_user_func(callback($this[$plugin], $eventname), $filter);
}
return $filter;
}
/** Trigger plugin event, observing returned false
* @param $eventname
* @param [$arg0] argument to filter function
* @param [$arg1] ...
* @return bool true if each event returned true
*/
public function triggerEvent($eventname){
$args = array_slice(func_get_args(), 1);
$triggers = $this->context->plugins->getEventTriggers($eventname);
$ret = true;
foreach($triggers as $plugin){
$r = call_user_func_array(callback($this[$plugin], $eventname), $args);
if($r === false)
$ret = false;
}
return $ret;
}
}