-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThemeTrait.php
105 lines (95 loc) · 2.64 KB
/
ThemeTrait.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
<?php
/**
* ThemeTrait
*
* @author Putra Sudaryanto <[email protected]>
* @contact (+62)856-299-4114
* @copyright Copyright (c) 2018 Ommu Platform (www.ommu.co)
* @created date 5 July 2018, 15:48 WIB
* @link https://github.com/ommu/yii-traits
*
* Contains many function that most used :
* currentTemplate
* applyCurrentTheme
* applyViewPath
* themeInfo
*
*/
trait ThemeTrait
{
/**
* Return setting template with tipe of theme
*
* @param string $typePage Type of theme. example: admin, public, maintenance
*/
public function currentTemplate($typeTheme)
{
$model = OmmuThemes::model()->find(array(
'select' => 'folder, layout',
'condition' => 'group_page = :group AND default_theme = :default',
'params' => array(
':group' => $typeTheme,
':default' => 1,
)
));
if($model != null)
return array('folder' => $model->folder, 'layout' => $model->layout);
}
/**
* Refer layout path to current applied theme.
*
* @param object $module that currently active [optional]
* @return void
*/
public function applyCurrentTheme($module=null)
{
$theme = Yii::app()->theme->name;
Yii::app()->theme = $theme;
if($module !== null) {
$themePath = Yii::getPathOfAlias('webroot.themes.'.$theme.'.views.layouts');
$module->setLayoutPath($themePath);
}
}
/**
* @return string the root directory of view files. Defaults to 'moduleDir/views' where
* moduleDir is the directory containing the module class.
*/
public function applyViewPath($path, $core=true)
{
$module = strtolower(Yii::app()->controller->module->id);
$basePath = Yii::app()->basePath;
$modulePath = Yii::app()->modulePath;
$viewPath = Yii::app()->controller->module->viewPath;
if($module == null)
$viewPath = Yii::app()->viewPath;
$path = preg_replace('(controllers)', 'views', $path);
$viewPathSlashes = addcslashes($viewPath, '/');
if(!preg_match("/$viewPathSlashes/", $path)) {
if($module == null) {
if($core == true)
Yii::app()->viewPath = join('/', [$basePath, 'views']);
else
Yii::app()->viewPath = $path;
} else
Yii::app()->controller->module->viewPath = join('/', [$modulePath, $module, 'views']);
}
}
/**
* Return setting template with typePage: public, admin_sweeto or back_office
*/
public function themeInfo($theme, $type=null)
{
define('DS', DIRECTORY_SEPARATOR);
Yii::import('mustangostang.spyc.Spyc');
if($theme == null)
return false;
$themePath = Yii::getPathOfAlias('webroot.themes.'.$theme).DS.$theme.'.yaml';
$themeYML = Spyc::YAMLLoad($themePath);
if(empty($themeYML))
return false;
if($type == null)
return $themeYML;
else
return $themeYML[$type];
}
}