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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
<?php
/* --------------------------------------------------------------
ModuleTitleProvider.inc.php 2017-04-11
Gambio GmbH
http://www.gambio.de
Copyright (c) 2017 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
/**
* Class ModuleTitleProvider
*
* @category System
* @package Shared
*/
abstract class ModuleTitleProvider
{
/**
* @var \LanguageTextManager
*/
protected static $language;
/**
* @var string
*/
protected static $type;
/**
* Returns the module title value.
* The value gets manipulated by the strip_tags and trim functions.
*
* @param string $methodName Name of module method.
* @param string $languageDirectory Path to language directory.
*
* @return string Stripped module title.
*/
public static function getStrippedTagsTitle($methodName, $languageDirectory = '')
{
$title = trim(strip_tags(static::getTitle($methodName, $languageDirectory)));
return $title === '' ? $methodName : $title;
}
public function title($methodName, $languageDirectory = '')
{
return static::getTitle($methodName, $languageDirectory);
}
/**
* Returns the title of a module module.
*
* @param string $methodName Name of module method.
* @param string $languageDirectory Path to language directory.
*
* @return string Module title.
*/
public static function getTitle($methodName, $languageDirectory = '')
{
static::_validateMethodNameArgument($methodName);
static::_validateLanguageDirectoryArgument($languageDirectory);
static::_getLanguageTextManager()->init_from_lang_file('lang/' . $languageDirectory . '/modules/'
. static::$type . '/' . $methodName . '.php');
$titleConstant = 'MODULE_' . strtoupper(static::$type) . '_' . strtoupper($methodName) . '_TEXT_TITLE';
if(defined($titleConstant))
{
return constant($titleConstant);
}
// dynamic includes are only testable with a lot of pain, so we just ignore the following part
// @codeCoverageIgnoreStart
elseif(file_exists(DIR_FS_CATALOG . 'includes/modules/' . static::$type . '/' . basename($methodName) . '.php'))
{
include_once DIR_FS_CATALOG . 'includes/modules/' . static::$type . '/' . basename($methodName) . '.php';
if(defined($titleConstant))
{
return constant($titleConstant);
}
if(class_exists($methodName))
{
$module = MainFactory::create($methodName);
if(isset($module->title) && trim($module->title) !== '')
{
return trim($module->title);
}
}
}
// @codeCoverageIgnoreEnd
return $methodName;
}
/**
* Validates the method name argument.
* The referenced value gets manipulated by the basename and trim functions.
*
* @param string $methodName Name of module method.
*/
protected static function _validateMethodNameArgument(&$methodName)
{
if(!is_string($methodName) || trim($methodName) === '')
{
throw new InvalidArgumentException('$methodName (' . gettype($methodName)
. ') is not a string or is empty');
}
$methodName = trim($methodName);
}
/**
* Validates the language directory argument.
* The referenced value gets manipulated by the basename and trim functions.
*
* @param string $languageDirectory Path of language directory.
*/
protected static function _validateLanguageDirectoryArgument(&$languageDirectory)
{
if(!is_string($languageDirectory))
{
throw new InvalidArgumentException('$languageDirectory (' . gettype($languageDirectory)
. ') is not a string');
}
$languageDirectory = basename(trim($languageDirectory));
if($languageDirectory === '')
{
$languageDirectory = $_SESSION['language'];
}
}
/**
* Returns the language text manager instance.
*
* @return LanguageTextManager
*/
protected static function _getLanguageTextManager()
{
if(null === static::$language)
{
static::$language = MainFactory::create_object('LanguageTextManager', array(), true);
}
return static::$language;
}
}