forked from adrien-gueret/Eliya
/
Controller.class.php
100 lines (80 loc) · 2.2 KB
/
Controller.class.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
<?php
namespace Eliya;
abstract class Controller
{
const START_CLASS_NAME = '\\Controller_';
public static $uri = null;
protected static $_init = false;
protected static $_page_name = 'home';
protected $_header_view_path = 'index';
protected $_footer_view_path = 'index';
protected $_breadcrumb = [];
public $request = null;
public $response = null;
final public function __construct(Request &$request)
{
$this->request = $request;
$this->response = $request->response();
if( ! $this->request->isFromAjax())
Tpl::set('default_app_page', static::$_page_name);
}
public function getHeaderViewPath()
{
return $this->_header_view_path;
}
public function getFooterViewPath()
{
return $this->_footer_view_path;
}
public function addBreadcrumbLevel(array $data)
{
foreach($data as $label => $link)
$this->_breadcrumb[] = ['label' => $label, 'link' => $link];
return $this;
}
public function getBreadcrumb()
{
return $this->_breadcrumb;
}
public function setBreadcrumb(array $breadcrumb)
{
$this->_breadcrumb = [];
return $this->addBreadcrumbLevel($breadcrumb);
}
public function getBreadcrumbTpl($appendLinks = true, $separator = ' > ')
{
$links = [];
$total = count($this->_breadcrumb);
$index = 0;
$currentPath = null;
$base_url = Config('main')->ROUTING['BASE_URL'];
foreach($this->_breadcrumb as $level)
{
if(substr($level['link'], 0, 1) === '/')
$link = $base_url.substr($level['link'], 1);
else
$link = $level['link'];
if($appendLinks)
$currentPath .= $link;
else
$currentPath = $link;
if(++$index < $total)
$links[] = '<a href="'.$currentPath.'">' . $level['label'] . '</a>';
else
$links[] = '<span>' . $level['label'] . '</span>';
}
return join($separator, $links);
}
public function getPath()
{
$controller_name = get_called_class();
$segments = explode('_', $controller_name);
//Remove first element (should be 'Controller')
array_shift($segments);
return $this->request->getBaseURL().join('/', $segments);
}
public function __init(){}
public function __before(){}
public function __after(){}
}
?>