-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArticleModule.php
110 lines (91 loc) · 3.46 KB
/
ArticleModule.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
<?php
/**
* ArticleModule
*
* @author Putra Sudaryanto <putra@ommu.co>
* @contact (+62)856-299-4114
* @copyright Copyright (c) 2014 Ommu Platform (www.ommu.co)
* @link https://github.com/ommu/mod-article
*
*----------------------------------------------------------------------------------------------------------
*/
class ArticleModule extends CWebModule
{
use ThemeTrait;
public $publicControllers = array();
private $_module = 'article';
public $defaultController = 'site';
// getAssetsUrl()
// return the URL for this module's assets, performing the publish operation
// the first time, and caching the result for subsequent use.
private $_assetsUrl;
public function init() {
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'article.models.*',
'article.components.*',
));
// this method is called before any module controller action is performed
// you may place customized code here
// list public controller in this module
$controllerMap = array();
$controllerPath = 'application.modules.'.$this->_module.'.controllers';
if(!empty($controllerMap))
$controllerMap = array_merge($controllerMap, $this->getController($controllerPath));
else
$controllerMap = $this->getController($controllerPath);
$this->controllerMap = $controllerMap;
$this->publicControllers = array_keys($this->controllerMap);
}
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action))
{
// pake ini untuk set theme per action di controller..
// $currentAction = Yii::app()->controller->id.'/'.$action->id;
if(!in_array(Yii::app()->controller->id, $this->publicControllers) && !Yii::app()->user->isGuest) {
$arrThemes = $this->currentTemplate('admin');
Yii::app()->theme = $arrThemes['folder'];
$this->layout = $arrThemes['layout'];
}
$this->applyCurrentTheme($this);
return true;
}
else
return false;
}
public function getAssetsUrl()
{
if ($this->_assetsUrl === null)
$this->_assetsUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('article.assets'));
return $this->_assetsUrl;
}
public function getController($path, $sub=null)
{
$controllerMap = array();
$controllerPath = Yii::getPathOfAlias($path);
$pathArray = explode('.', $path);
$lastPath = end($pathArray);
foreach (new DirectoryIterator($controllerPath) as $fileInfo) {
if($fileInfo->isDot() && $fileInfo->isDir())
continue;
if($fileInfo->isFile() && !in_array($fileInfo->getFilename(), array('index.php','.DS_Store'))) {
$getFilename = $fileInfo->getFilename();
$controller = strtolower(preg_replace('(Controller.php)', '', $getFilename));
if($lastPath != 'controllers')
$controller = join('', array($lastPath, preg_replace('(Controller.php)', '', $getFilename)));
$controllerClass = preg_replace('(.php)', '', $getFilename);
$controllerMap[$controller] = array(
'class'=>join('.', array($path, $controllerClass)),
);
} else if($fileInfo->isDir()) {
$sub = $fileInfo->getFilename();
$subPath = join('.', array($path, $sub));
$controllerMap = array_merge($controllerMap, $this->getController($subPath, $sub));
}
}
return $controllerMap;
}
}