1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Module\Helper;
13:
14: use Xoops\Core\Kernel\Handlers\XoopsModule;
15:
16: 17: 18: 19: 20: 21:
22:
23: abstract class HelperAbstract
24: {
25: 26: 27:
28: protected $_dirname = '';
29:
30: 31: 32:
33: protected $_module = null;
34:
35: 36: 37:
38: protected $_debug = false;
39:
40: public function init()
41: {
42: }
43:
44: 45: 46:
47: protected function setDirname($dirname)
48: {
49: $this->_dirname = strtolower($dirname);
50: }
51:
52: 53: 54:
55: protected function setDebug($debug)
56: {
57: $this->_debug = (bool)$debug;
58: }
59:
60: 61: 62:
63: static function getInstance()
64: {
65: static $instance = false;
66: $id = $className = get_called_class();
67: if ($className === 'Xoops\Module\Helper\Dummy') {
68: $id = @\Xoops::getInstance()->registry()->get('module_helper_id');
69: }
70: if (!isset($instance[$id])) {
71:
72: $class = new $className();
73: $class->init();
74: $instance[$id] = $class;
75: }
76: return $instance[$id];
77: }
78:
79: 80: 81:
82: public function getModule()
83: {
84: if ($this->_module == null) {
85: $this->_initModule();
86: }
87: return $this->_module;
88: }
89:
90: public function xoops()
91: {
92: return \Xoops::getInstance();
93: }
94:
95: 96: 97: 98: 99:
100: public function getConfig($name)
101: {
102: $name = strtolower($name);
103: $result = $this->xoops()->getModuleConfig($name, $this->_dirname);
104: $this->_addLog("Getting config '{$name}' : " . $result);
105: return $result;
106: }
107:
108: 109: 110: 111: 112:
113: public function getConfigs()
114: {
115: $result = $this->xoops()->getModuleConfigs($this->_dirname);
116: $this->_addLog("Getting configs for {$this->_dirname} module");
117: return $result;
118: }
119:
120: 121: 122: 123: 124:
125: public function getHandler($name)
126: {
127: $name = strtolower($name);
128: $this->_addLog("Getting handler '{$name}'");
129: return $this->xoops()->getModuleHandler($name, $this->_dirname);
130: }
131:
132: public function disableCache()
133: {
134: $this->xoops()->appendConfig('module_cache', array($this->getModule()->getVar('mid') => 0), true, $this->_dirname);
135: $this->_addLog("Disabling module cache");
136: }
137:
138: 139: 140: 141: 142:
143: public function isCurrentModule()
144: {
145: if ($this->xoops()->moduleDirname == $this->_dirname) {
146: return true;
147: }
148: return false;
149: }
150:
151: 152: 153: 154: 155:
156: public function isUserAdmin()
157: {
158: if ($this->xoops()->isUser()) {
159: return $this->xoops()->user->isAdmin($this->getModule()->getVar('mid'));
160: }
161: return false;
162: }
163:
164: 165: 166: 167: 168: 169: 170:
171: public function url($url = '')
172: {
173: return $this->xoops()->url('modules/' . $this->_dirname . '/' . $url);
174: }
175:
176: 177: 178: 179: 180: 181: 182:
183: public function path($path = '')
184: {
185: return $this->xoops()->path('modules/' . $this->_dirname . '/' . $path);
186: }
187:
188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201:
202: public function redirect($url, $time = 3, $message = '', $addredirect = true, $allowExternalLink = false)
203: {
204: $this->xoops()->redirect($this->url($url), $time, $message, $addredirect, $allowExternalLink);
205: }
206:
207: 208: 209: 210: 211:
212: public function loadLanguage($language)
213: {
214: $this->xoops()->loadLanguage($language, $this->_dirname);
215: $this->_addLog("Loading language '{$language}'");
216: }
217:
218: public function loadLocale()
219: {
220: $this->xoops()->loadLocale($this->_dirname);
221: $this->_addLog("Loading locale");
222: }
223:
224: 225: 226: 227: 228: 229:
230: public function getForm($obj, $name)
231: {
232: $name = strtolower($name);
233: $this->_addLog("Loading form '{$name}'");
234: return $this->xoops()->getModuleForm($obj, $name, $this->_dirname);
235: }
236:
237: 238: 239:
240: private function _initModule()
241: {
242: if ($this->isCurrentModule()) {
243: $this->_module = $this->xoops()->module;
244: } else {
245: $this->_module = $this->xoops()->getModuleByDirname($this->_dirname);
246: }
247: if (!$this->_module instanceof XoopsModule) {
248: $this->_module = $this->xoops()->getHandlerModule()->create();
249: }
250: $this->_addLog('Loading module');
251: }
252:
253: 254: 255:
256: protected function _addLog($log)
257: {
258: if ($this->_debug) {
259: $this->xoops()->events()->triggerEvent('core.module.addlog', array(
260: $this->getModule()->getVar('name'),
261: $log
262: ));
263: }
264: }
265: }
266: