1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xmf\Module;
13:
14: use Xmf\Language;
15:
16: /**
17: * Xmf\Module\Admin provides helpful methods for module administration
18: * uses.
19: *
20: * Xmf\Module\Admin provides a method compatible subset of the Xoops\Module\Admin class
21: * (introduced in 2.6) and other convenience methods useful in transition
22: *
23: * @category Xmf\Module\Admin
24: * @package Xmf
25: * @author Richard Griffith <richard@geekwright.com>
26: * @copyright 2011-2016 XOOPS Project (http://xoops.org)
27: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
28: * @link http://xoops.org
29: */
30: class Admin
31: {
32:
33: /**
34: * The real ModuleAdmin object
35: *
36: * @var object
37: */
38: protected static $ModuleAdmin = null;
39: protected $lastInfoBoxTitle = null;
40: protected static $paypal = '';
41:
42: /**
43: * Constructor
44: */
45: protected function __construct()
46: {
47: }
48:
49: /**
50: * Retrieve a module admin instance
51: *
52: * If we are on a next generation system this will be the a native Xoops\Module\Admin instance.
53: * Older systems with the Frameworks based admin class will get an instance of this class which
54: * provides compatible methods built from the old Frameworks version.
55: *
56: * @return object a ModuleAdmin or Xoops\Module\Admin instance.
57: */
58: public static function getInstance()
59: {
60:
61: static $instance;
62:
63: if ($instance === null) {
64: if (class_exists('\Xoops\Module\Admin', true)) {
65: $instance = new \Xoops\Module\Admin;
66: static::$ModuleAdmin = $instance;
67: } else {
68: include_once $GLOBALS['xoops']->path('Frameworks/moduleclasses/moduleadmin/moduleadmin.php');
69: static::$ModuleAdmin = new \ModuleAdmin;
70: Language::load('xmf');
71: $instance = new static();
72: }
73: }
74:
75: return $instance;
76: }
77:
78: /**
79: * Add config line
80: *
81: * @param string $value message to include in config box
82: * @param string $type type of line to add
83: * minimal set of acceptable types and value expectation
84: * 'default' - value is message displayed directly (also used for unknown types)
85: * 'folder' - value is directory name, will display accept if exists, error if not
86: * 'chmod' - value is array(directory, permission) accept if exists with permission,
87: * else error
88: * 'module' - value is string module name, or array(module name, errortype)
89: * If module is active, an accept line displays, otherwise, a warning
90: * (if value is array(module, "warning") or an error displays.
91: *
92: * @return bool
93: */
94: public function addConfigBoxLine($value = '', $type = 'default')
95: {
96: if ($type === 'module') {
97: $mod = (is_array($value)) ? $value[0] : $value;
98: if (xoops_isActiveModule($mod)) {
99: return $this->addConfigAccept(sprintf(_AM_XMF_MODULE_INSTALLED, $mod));
100: } else {
101: $nomod = (is_array($value)) ? $value[1] : 'error';
102: $line = sprintf(_AM_XMF_MODULE_NOT_INSTALLED, $mod);
103: if ($nomod === 'warning') {
104: return $this->addConfigWarning($line);
105: } else {
106: return $this->addConfigError($line);
107: }
108: }
109: }
110: return static::$ModuleAdmin->addConfigBoxLine($value, $type);
111: }
112:
113: /**
114: * Add Info box
115: *
116: * @param string $title info box title
117: * @param string $type for compatibility only
118: * @param string $extra for compatibility only
119: *
120: * @return bool
121: */
122: public function addInfoBox($title, $type = 'default', $extra = '')
123: {
124: $this->lastInfoBoxTitle = $title;
125:
126: return static::$ModuleAdmin->addInfoBox($title);
127: }
128:
129: /**
130: * Add line to the info box
131: *
132: * @param string $text text to add to info box
133: * @param string $type type of infobox line
134: * @param string $color color for infobox line
135: *
136: * @return bool
137: */
138: public function addInfoBoxLine($text = '', $type = 'default', $color = 'inherit')
139: {
140: return static::$ModuleAdmin->addInfoBoxLine(
141: $this->lastInfoBoxTitle,
142: $text,
143: '',
144: $color,
145: $type
146: );
147: }
148:
149: /**
150: * Add Item button
151: *
152: * @param string $title title of button
153: * @param string $link link for button
154: * @param string $icon icon for button
155: * @param string $extra extra
156: *
157: * @return bool
158: */
159: public function addItemButton($title, $link, $icon = 'add', $extra = '')
160: {
161: return static::$ModuleAdmin->addItemButton($title, $link, $icon, $extra);
162: }
163:
164: /**
165: * Render all items buttons
166: *
167: * @param string $position button position (left, right)
168: * @param string $delimiter delimiter between buttons
169: *
170: * @return string
171: */
172: public function renderButton($position = null, $delimiter = " ")
173: {
174: if (null === $position) {
175: $position = 'right';
176: }
177:
178: return static::$ModuleAdmin->renderButton($position, $delimiter);
179: }
180:
181: /**
182: * Display all item buttons
183: *
184: * @param string $position button position (left, right)
185: * @param string $delimiter delimiter between buttons
186: *
187: * @return void
188: */
189: public function displayButton($position = null, $delimiter = " ")
190: {
191: echo $this->renderButton($position, $delimiter);
192: }
193:
194: /**
195: * Render InfoBox
196: *
197: * @return string HTML rendered info box
198: */
199: public function renderInfoBox()
200: {
201: return static::$ModuleAdmin->renderInfoBox();
202: }
203:
204: /**
205: * Display InfoBox
206: *
207: * @return void
208: */
209: public function displayInfoBox()
210: {
211: echo $this->renderInfoBox();
212: }
213:
214: /**
215: * Render index page for admin
216: *
217: * @return string HTML rendered info box
218: */
219: public function renderIndex()
220: {
221: return static::$ModuleAdmin->renderIndex();
222: }
223:
224: /**
225: * Display index page for admin
226: *
227: * @return void
228: */
229: public function displayIndex()
230: {
231: echo $this->renderIndex();
232: }
233:
234: /**
235: * Display the navigation menu
236: *
237: * @param string $menu menu key (script name, i.e. index.php)
238: *
239: * @return void
240: */
241: public function displayNavigation($menu = '')
242: {
243: echo static::$ModuleAdmin->addNavigation($menu);
244: }
245:
246: /**
247: * Render about page
248: *
249: * @param bool $logo_xoops display XOOPS logo
250: *
251: * @return bool|mixed|string
252: */
253: public function renderAbout($logo_xoops = true)
254: {
255: return static::$ModuleAdmin->renderAbout(static::$paypal, $logo_xoops);
256: }
257:
258: /**
259: * Display about page
260: *
261: * @param bool $logo_xoops display XOOPS logo
262: *
263: * @return void
264: */
265: public function displayAbout($logo_xoops = true)
266: {
267: echo $this->renderAbout($logo_xoops);
268: }
269:
270: /**
271: * Add error to config box
272: *
273: * @param string $value the error message
274: *
275: * @return bool
276: */
277: public function addConfigError($value = '')
278: {
279: $path = XOOPS_URL . '/Frameworks/moduleclasses/icons/16/';
280: $line = "";
281: $line .= "<span style='color : red; font-weight : bold;'>";
282: $line .= "<img src='" . $path . "0.png' >";
283: $line .= $value;
284: $line .= "</span>";
285: $value = $line;
286: $type = 'default';
287:
288: return static::$ModuleAdmin->addConfigBoxLine($value, $type);
289: }
290:
291: /**
292: * Add accept (OK) message to config box
293: *
294: * @param string $value the OK message
295: *
296: * @return bool
297: */
298: public function addConfigAccept($value = '')
299: {
300: $path = XOOPS_URL . '/Frameworks/moduleclasses/icons/16/';
301: $line = "";
302: $line .= "<span style='color : green;'>";
303: $line .= "<img src='" . $path . "1.png' >";
304: $line .= $value;
305: $line .= "</span>";
306: $value = $line;
307: $type = 'default';
308:
309: return static::$ModuleAdmin->addConfigBoxLine($value, $type);
310: }
311:
312: /**
313: * Add warning to config box
314: *
315: * @param string $value the warning message
316: *
317: * @return bool
318: */
319: public function addConfigWarning($value = '')
320: {
321: $path = XOOPS_URL . '/Frameworks/moduleclasses/icons/16/';
322: $line = "";
323: $line .= "<span style='color : orange; font-weight : bold;'>";
324: $line .= "<img src='" . $path . "warning.png' >";
325: $line .= $value;
326: $line .= "</span>";
327: $value = $line;
328: $type = 'default';
329:
330: return static::$ModuleAdmin->addConfigBoxLine($value, $type);
331: }
332:
333:
334: /**
335: * Check for installed module and version and do addConfigBoxLine()
336: *
337: * @param string $moddir - module directory name
338: * @param integer $minversion - minimum acceptable module version (100 = V1.00)
339: *
340: * @return bool true if requested version of the module is available
341: */
342: public function addConfigModuleVersion($moddir, $minversion)
343: {
344: $return = false;
345: $helper = Helper::getHelper($moddir);
346: if (is_object($helper) && is_object($helper->getModule())) {
347: $mod_modversion = $helper->getModule()->getVar('version');
348: $mod_version_f = $mod_modversion / 100;
349: $min_version_f = $minversion / 100;
350: $value = sprintf(
351: _AM_XMF_MODULE_VERSION,
352: strtoupper($moddir),
353: $min_version_f,
354: $mod_version_f
355: );
356: if ($mod_modversion >= $minversion) {
357: $this->addConfigAccept($value);
358: $return = true;
359: } else {
360: $this->addConfigError($value);
361: }
362: } else {
363: $value = sprintf(
364: _AM_XMF_MODULE_NOTFOUND,
365: strtoupper($moddir),
366: $minversion / 100
367: );
368: $this->addConfigError($value);
369: }
370:
371: return $return;
372: }
373:
374: // the following not part of next generation Xoops\Module\Admin
375:
376: /**
377: * Are we in a next generation environment?
378: *
379: * not part of next generation Xoops\Module\Admin
380: *
381: * @return bool true if we are in a post XOOPS 2.5.x environment
382: */
383: protected static function isXng()
384: {
385: return class_exists('\Xoops', false);
386: }
387:
388: /**
389: * Get an appropriate imagePath for menu.php use.
390: *
391: * just to help with other admin things than ModuleAdmin
392: *
393: * not part of next generation Xoops\Module\Admin
394: *
395: * @param string $image icon name to prepend with path
396: *
397: * @return string the icon path
398: */
399: public static function menuIconPath($image)
400: {
401: if (static::isXng()) {
402: return($image);
403: } else {
404: $path = '../../Frameworks/moduleclasses/icons/32/';
405:
406: return($path . $image);
407: }
408: }
409:
410: /**
411: * Get an appropriate URL for system provided icons.
412: *
413: * Things which were in Frameworks in 2.5 are in media in later versions,
414: * making it harder to use and rely on the standard icons.
415: *
416: * not part of next generation Xoops\Module\Admin
417: *
418: * @param string $name the image name to provide URL for, or blank
419: * to just get the URL path.
420: * @param string $size the icon size (directory). Valid values are
421: * 16, 32 or /. A '/' slash will simply set the
422: * path to the icon directory and append $image.
423: *
424: * @return string path to icons
425: */
426: public static function iconUrl($name = '', $size = '32')
427: {
428: switch ($size) {
429: case '16':
430: $path = '16/';
431: break;
432: case '/':
433: $path = '';
434: break;
435: case '32':
436: default:
437: $path = '32/';
438: break;
439: }
440:
441: if (static::isXng()) {
442: $path = '/media/xoops/images/icons/' . $path;
443: } else {
444: $path = '/Frameworks/moduleclasses/icons/' . $path;
445: }
446:
447: return(XOOPS_URL . $path . $name);
448: }
449:
450: /**
451: * set paypal for 2.5.x renderAbout
452: *
453: * not part of next generation Xoops\Module\Admin
454: *
455: * @param string $paypal PayPal identifier for donate button
456: *
457: * @return void
458: */
459: public static function setPaypal($paypal = '')
460: {
461: static::$paypal = $paypal;
462: }
463: }
464: