XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
Legacy_Utils.class.php
1<?php
11
12/***
13 * @internal
14 * @public
15 * @brief The collection of static utility functions for Legacy.
16 */
18{
19 /***
20 * Checks whether must modules have been installed.
21 * @static
22 * @return mixed Returns hashmap including the list of uninstalled,
23 * disabled and recommended modules, basically. But, if there is no problem,
24 * returns true.
25 */
26 public static function checkSystemModules()
27 {
28 $root=&XCube_Root::getSingleton();
29 $systemModules = array_map('trim', explode(',', $root->getSiteConfig('Cube', 'SystemModules')));
30 $recommendedModules = array_map('trim', explode(',', $root->getSiteConfig('Cube', 'RecommendedModules')));
31 $moduleHandler =& xoops_gethandler('module');
32 $uninstalledModules = [];
33 $disabledModules = [];
34 foreach ($systemModules as $systemModule) {
35 if (!empty($systemModule)) {
36 if (!($moduleObject =& $moduleHandler->getByDirname($systemModule))) {
37 $uninstalledModules[] = $systemModule;
38 } elseif (!$moduleObject->get('isactive')) {
39 $disabledModules[] = $systemModule;
40 }
41 }
42 }
43 if (0 == count($uninstalledModules) && 0 == count($disabledModules)) {
44 return true;
45 } else {
46 return ['uninstalled' =>$uninstalledModules, 'disabled' =>$disabledModules, 'recommended' =>$recommendedModules];
47 }
48 }
49
50 /***
51 * Creates a instance of the module with the generating convention. And,
52 * returns it.
53 * @param XoopsModule $module
54 * @param bool $loadConfig
55 * @return Legacy_AbstractModule \Legacy_Module
56 */
57 public static function &createModule(XoopsModule $module, bool $loadConfig=true)
58 {
59 $instance = null;
60
61 //
62 // TODO need cache here?
63 //
64 XCube_DelegateUtils::call('Legacy_Utils.CreateModule', new XCube_Ref($instance), $module, $loadConfig);
65
66 if (is_object($instance) && $instance instanceof \Legacy_AbstractModule) {
67 return $instance;
68 }
69
70 $dirname = $module->get('dirname');
71
72 //
73 // IMPORTANT CONVENTION
74 //
75 $className = ucfirst($dirname) . '_Module';
76 if (!XC_CLASS_EXISTS($className)) {
77 $filePath = XOOPS_ROOT_PATH . "/modules/{$dirname}/class/Module.class.php";
78 if (file_exists($filePath)) {
79 require_once $filePath;
80 }
81 }
82
83 if (XC_CLASS_EXISTS($className)) {
84 $instance = new $className($module, $loadConfig);
85 } else {
86 $instance = new Legacy_ModuleAdapter($module, $loadConfig);
87 }
88
89 return $instance;
90 }
91
92 /***
93 * Creates a instance of the block procedure with the generating convention.
94 * And, returns it.
95 * @static
96 * @param $block
97 * @return Legacy_BlockProcedure
98 */
99 public static function &createBlockProcedure(&$block)
100 {
101 //
102 // IMPORTANT CONVENTION
103 //
104 $retBlock = null;
105
106 //
107 // TODO need cache here?
108 //
109 XCube_DelegateUtils::call('Legacy_Utils.CreateBlockProcedure', new XCube_Ref($retBlock), $block);
110
111 if (is_object($retBlock) && $retBlock instanceof \Legacy_AbstractBlockProcedure) {
112 return $retBlock;
113 }
114
115 $func = $block->get('show_func');
116 if ('cl::' == substr($func, 0, 4)) {
117 $className = ucfirst($block->get('dirname')) . '_' . substr($func, 4);
118 if (!XC_CLASS_EXISTS($className)) {
119 $filePath = XOOPS_ROOT_PATH . '/modules/' . $block->get('dirname') . '/blocks/' . $block->get('func_file');
120 if (!file_exists($filePath)) {
121 $retBlock = new Legacy_BlockProcedureAdapter($block);
122 return $retBlock;
123 }
124
125 require_once $filePath;
126
127 if (!XC_CLASS_EXISTS($className)) {
128 $retBlock = new Legacy_BlockProcedureAdapter($block);
129 return $retBlock;
130 }
131 }
132
133 $retBlock = new $className($block);
134 } else {
135 $retBlock = new Legacy_BlockProcedureAdapter($block);
136 }
137
138 return $retBlock;
139 }
140
141 /***
142 * Calls user control event.
143 */
144 public static function raiseUserControlEvent()
145 {
146 $root =& XCube_Root::getSingleton();
147 foreach (array_keys($_REQUEST) as $key) {
148 if (0 === strpos($key, 'Legacy_Event_User_')) {
149 $eventName = substr($key, 18);
150 XCube_DelegateUtils::call('Legacy.Event.User.' . $eventName);
151 $root->mContext->mAttributes['userEvent'][$eventName] = true;
152 }
153 }
154 }
155
156 /***
157 * Converts the version of the module from $modversion value to integer
158 * number.
159 * @param string $version
160 * @return int
161 */
162 public static function convertVersionFromModinfoToInt($version)
163 {
164 return round(100 * (float)$version);
165 }
166
167 /***
168 * Converts the version of the module from DB value to float.
169 * @param int $version
170 * @return float
171 */
172 public static function convertVersionIntToFloat($version)
173 {
174 return round((float)( (int) $version / 100), 2);
175 }
176
184 public static function getUid()
185 {
186 $root = XCube_Root::getSingleton();
187 return ($root->mContext->mUser->isInRole('Site.RegisteredUser')) ? $root->mContext->mXoopsUser->get('uid') : 0;
188 }
189
197 public static function getUserName(/*** int ***/ $uid)
198 {
199 $name = null;
200 XCube_DelegateUtils::call('Legacy_User.GetUserName', new XCube_Ref($name), $uid);
201 if (! $name) {
202 $handler =& xoops_gethandler('member');
203 $user =& $handler->getUser((int)$uid);
204 if ($user) {
205 $name = $user->getShow('uname');
206 }
207 }
208 return $name;
209 }
210
218 public static function getDirnameListByTrustDirname(/*** string ***/ $trustDirname)
219 {
220 $list = [];
221 $cri = new CriteriaCompo();
222 $cri->add(new Criteria('isactive', 0, '>'));
223 $cri->add(new Criteria('trust_dirname', $trustDirname));
224 $cri->addSort('weight', 'ASC');
225 $cri->addSort('mid', 'ASC');
226 foreach (xoops_gethandler('module')->getObjects($cri) as $module) {
227 $list[] = $module->get('dirname');
228 }
229 return $list;
230 }
231
239 public static function getTrustDirnameByDirname(/*** string ***/ $dirname)
240 {
241 $handler =& xoops_gethandler('module');
242 $module =& $handler->getByDirname($dirname);
243 if ($module) {
244 return $module->get('trust_dirname') ?: null;
245 }
246 }
247
257 public static function formatPagetitle(/*** string ***/ $modulename, /*** string ***/ $pagetitle, /*** string ***/ $action)
258 {
259 $handler = xoops_gethandler('config');
260 $configArr = $handler->getConfigsByDirname('legacyRender');
261
262 $replace = [$modulename, $pagetitle, $action];
263 $search = ['{modulename}', '{pagetitle}', '{action}'];
264 $ret = str_replace($search, $replace, $configArr['pagetitle']);
265
266 $ret = (! $modulename) ? preg_replace("/\[modulename\](.*)\[\/modulename\]/U", '', $ret) : preg_replace("/\[modulename\](.*)\[\/modulename\]/U", '$1', $ret);
267 $ret = (! $pagetitle) ? preg_replace("/\[pagetitle\](.*)\[\/pagetitle\]/U", '', $ret) : preg_replace("/\[pagetitle\](.*)\[\/pagetitle\]/U", '$1', $ret);
268 $ret = (! $action) ? preg_replace("/\[action\](.*)\[\/action\]/U", '', $ret) : preg_replace("/\[action\](.*)\[\/action\]/U", '$1', $ret);
269
270 return $ret;
271 }
272
281 public static function getModuleHandler(/*** string ***/ $name, /*** string ***/ $dirname)
282 {
283 $trustDirname = self::getTrustDirnameByDirname($dirname);
284 if (isset($trustDirname)) {
285 $path = XOOPS_TRUST_PATH. '/modules/'. $trustDirname .'/class/handler/' . ucfirst($name) . '.class.php';
286 $className = ucfirst($trustDirname) . '_' . ucfirst($name) . 'Handler';
287 self::_loadClassFile($path, $className);
288
289 $root =& XCube_Root::getSingleton();
290 $instance = new $className($root->mController->getDB(), $dirname);
291 return $instance;
292 } else {
293 return xoops_getmodulehandler($name, $dirname);
294 }
295 }
296
308 public static function renderUri(/*** string ***/ $dirname, /*** string ***/ $dataname=null, /*** int ***/ $data_id=0, /*** string ***/ $action=null, /*** string ***/ $query=null)
309 {
310 $uri = null;
311 if (true == XCube_Root::getSingleton()->mContext->getXoopsConfig('cool_uri')) {
312 if (isset($dataname)) {
313 if ($data_id>0) {
314 if (isset($action)) {
315 $uri = sprintf('/%s/%s/%d/%s', $dirname, $dataname, $data_id, $action);
316 } else {
317 $uri = sprintf('/%s/%s/%d', $dirname, $dataname, $data_id);
318 }
319 } else {
320 if (isset($action)) {
321 $uri = sprintf('/%s/%s/%s', $dirname, $dataname, $action);
322 } else {
323 $uri = sprintf('/%s/%s', $dirname, $dataname);
324 }
325 }
326 } else {
327 if ($data_id>0) {
328 if (isset($action)) {
329 die();
330 } else {
331 $uri = sprintf('/%s/%d', $dirname, $data_id);
332 }
333 } else {
334 if (isset($action)) {
335 die();
336 } else {
337 $uri = '/'.$dirname;
338 }
339 }
340 }
341 $uri = (isset($query)) ? XOOPS_URL.$uri.'?'.$query : XOOPS_URL. $uri;
342 } else {
343 XCube_DelegateUtils::call('Module.'.$dirname.'.Global.Event.GetNormalUri', new XCube_Ref($uri), $dirname, $dataname, $data_id, $action, $query);
344
345 $uri = XOOPS_MODULE_URL. $uri;
346 }
347
348 return $uri;
349 }
350
358 public static function getCommonModuleList(/*** string ***/ $role)
359 {
360 $list = [];
361 $cri = new CriteriaCompo();
362 $cri->add(new Criteria('isactive', 0, '>'));
363 $cri->add(new Criteria('role', $role));
364 $cri->addSort('weight', 'ASC');
365 $cri->addSort('mid', 'ASC');
366 foreach (xoops_gethandler('module')->getObjects($cri) as $module) {
367 $list[] = $module->get('dirname');
368 }
369 return $list;
370 }
371
380 private static function _loadClassFile(/*** string ***/ $path, /*** string ***/ $class)
381 {
382 if (!file_exists($path)) {
383 return false;
384 }
385 require_once $path;
386
387 return class_exists($class);
388 }
389}
[Abstract] Represents modules and used for Legacy_Controller
Used for adapting $xoopsModule to imitate XOOPS2 responses.
static getTrustDirnameByDirname( $dirname)
static getUserName( $uid)
static getModuleHandler( $name, $dirname)
static getCommonModuleList( $role)
static renderUri( $dirname, $dataname=null, $data_id=0, $action=null, $query=null)
static getDirnameListByTrustDirname( $trustDirname)
static formatPagetitle( $modulename, $pagetitle, $action)