forked from pkerspe/yii2-simple-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrontend.php
More file actions
130 lines (118 loc) · 4.2 KB
/
Frontend.php
File metadata and controls
130 lines (118 loc) · 4.2 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* This file is part of the simple cms project for Yii2
*
* (c) Schallschlucker Agency Paul Kerspe - project homepage <https://github.com/pkerspe/yii2-simple-cms>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace schallschlucker\simplecms;
use Yii;
use yii\base\InvalidConfigException;
/**
* The simple cms frontend module
*
* @author Paul Kerspe
*/
class Frontend extends \yii\base\Module {
const VERSION = '0.2';
public $controllerNamespace = 'schallschlucker\simplecms\controllers\frontend';
public $defaultRoute='show';
public $mediarepositoryPath;
public $languageManager;
public $renderTopMenuNavbar = true;
public $cache;
public $htmlTitlePrefix = '';
public $htmlTitleSuffix = '';
public $assembleBreadcrumbInformation = true; //assemble breadcrumb navigation in ShowController pageAction and store to view parameter $this->params['breadcrumbHierarchyItemPath']
/**
* @var string The prefix for the frontend module URL.
* @See [[GroupUrlRule::prefix]]
*/
public $urlPrefix = 'cms'; //for the url in the forntend to be called
public $routePrefix = 'simplecms_frontend'; //to map to the module id given in the config
public $showBreadcrumbs = false;
public $rootBreadcrumb = 'CMS Home';
/**
*
* @var array The rules to be used in URL management.
*/
public $urlRules = [
'home' => 'show/homepage',
'page/<menuItemId:\d+>' => 'show/page',
'c/<menuItemAlias:[\w-]+>' => 'show/alias',
];
/**
* @inheritdoc
*/
public function __construct($id, $parent = null, $config = []) {
foreach ( $this->getModuleComponents () as $name => $component ) {
if (! isset ( $config ['components'] [$name] )) {
$config ['components'] [$name] = $component;
} elseif (is_array ( $config ['components'] [$name] ) && ! isset ( $config ['components'] [$name] ['class'] )) {
$config ['components'] [$name] ['class'] = $component ['class'];
}
}
if( empty($this->mediarepositoryPath) ){
$this->mediarepositoryPath = Yii::getAlias('@webroot').DIRECTORY_SEPARATOR.'mediarepository';
}
parent::__construct ( $id, $parent, $config );
}
/**
* Returns module components.
*
* @return array
*/
protected function getModuleComponents() {
return [
'languageManager' => [
'class' => 'schallschlucker\simplecms\LanguageManager'
]
];
}
public function getLanguageManager(){
if($this->languageManager == null || $this->languageManager == '' ){
throw new InvalidConfigException("Module is not configured correctly, need to provide name of a configured languageManager compoenent");
}
$configuredLangManager = $this->languageManager;
return Yii::$app->$configuredLangManager;
}
public static function getLanguageManagerStatic(){
//FIXME: currently this function is used in backend and frontend, this is not very clean and should be changed
try {
if(Yii::$app->getModule('simplecms_frontend') != null)
return Yii::$app->getModule('simplecms_frontend')->getLanguageManager();
return Yii::$app->getModule('simplecms_backend')->getLanguageManager();
} catch (\Exception $e){
return Yii::$app->getModule('simplecms_backend')->getLanguageManager();
}
}
/**
* store a value to the cache, if the cache is configured. Just ignores value if cache is not configured
* @param unknown $cacheKey
* @param unknown $value
* @param unknown $caheLivetime
*/
public function setCacheValue($cacheKey, $value, $cacheLivetime){
if($this->cache != null){
Yii::$app->get($this->cache,true)->set($cacheKey, $value, $cacheLivetime);
}
}
/**
* retrieve a value with the given cacheKey from the cache if a cache is configured at all and if the value could be found.
* If no cache is configured the fallbackValue is returned (by default this value is "false" if no parameter is given)
* @param unknown $cacheKey
* @param string $fallbackValue
* @return unknown|string
*/
public function getCachedValue($cacheKey,$fallbackValue = false){
if($this->cache != null){
$value = Yii::$app->get($this->cache,true)->get($cacheKey);
if($value !== false){
return $value;
}
}
return $fallbackValue;
}
}