3 namespace Org\Mxchange\CoreFramework\Helper;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Helper\Template\HelpableTemplate;
10 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
11 use Org\Mxchange\CoreFramework\String\Utils\StringUtils;
12 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
15 * A helper for web links
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate {
40 private $linkName = '';
45 private $linkBase = '';
48 * First parameter separator
50 const FIRST_PARAMETER_SEPARATOR = '?';
53 * SEPARATOR for more paraemters
55 const EXTRA_PARAMETER_SEPARATOR = '&';
58 * Protected constructor
62 protected function __construct () {
63 // Call parent constructor
64 parent::__construct(__CLASS__);
68 * Creates the helper class
70 * @param $templateInstance An instance of a template engine
71 * @param $linkName Name of the link we shall generate
72 * @param $linkBase Link base for the link. This parameter is deprecated.
73 * @return $helperInstance A prepared instance of this helper
74 * @throws NoConfigEntryException A deprecated exception at this point
76 public static final function createHtmlLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase = NULL) {
78 $helperInstance = new HtmlLinkHelper();
80 // Set template instance
81 $helperInstance->setTemplateInstance($templateInstance);
84 $helperInstance->setLinkName($linkName);
86 // Get the application instance
87 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
89 // Get the request instance
90 $requestInstance = FrameworkBootstrap::getRequestInstance();
93 if (is_null($requestInstance)) {
94 // Throw an exception here
95 throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
98 // Get page (this will throw an exception if not set)
99 $command = StringUtils::convertDashesToUnderscores($requestInstance->getRequestElement('command'));
101 // Construct config entry
102 $configEntry = $command . '_' . $linkName . '_action_url';
104 // Is the deprecated parameter set?
105 if (!is_null($linkBase)) {
106 // Then output a deprecation message
107 $helperInstance->deprecationWarning('[' . __METHOD__ . ':' . __LINE__ . ']: linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
110 // Determine link base from config now and 'command' request
112 $newLinkBase = $helperInstance->getConfigInstance()->getConfigEntry($configEntry);
113 $linkBase = $newLinkBase;
114 } catch (NoConfigEntryException $e) {
115 // Is the deprecated linkBase not set?
116 if (is_null($linkBase)) {
117 // Then throw again the exception
118 throw new NoConfigEntryException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
123 $helperInstance->setLinkBase($linkBase);
126 $helperInstance->openGroupByIdContent('main', '', '');
128 // Return the prepared instance
129 return $helperInstance;
133 * Renders the link content (HTML code) with given link text and optional
136 * @param $linkText Link text to set in link
137 * @param $linkTitle Link title to set in link
138 * @param $extraContent Optional extra HTML content
139 * @return $linkContent Rendered text link content
141 private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent='') {
142 // Construct link content
143 $linkContent = sprintf('<a href="{?base_url?}/%s%s" title="%s">%s</a>',
144 $this->getLinkBase(),
155 * Setter for link name
157 * @param $linkName Name of the link we shall generate
160 protected final function setLinkName ($linkName) {
161 $this->linkName = (string) $linkName;
165 * Getter for link name
167 * @return $linkName Name of the link we shall generate
169 public final function getLinkName () {
170 return $this->linkName;
174 * Setter for link base
176 * @param $linkBase Base of the link we shall generate
179 protected final function setLinkBase ($linkBase) {
180 $this->linkBase = (string) $linkBase;
184 * Getter for link base
186 * @return $linkBase Base of the link we shall generate
188 public final function getLinkBase () {
189 return $this->linkBase;
193 * Flush the content out,e g. to a template variable
196 * @todo Completely unimplemented
198 public function flushContent () {
199 // Is a previous opened group still open?
200 if ($this->ifGroupOpenedPreviously()) {
202 $this->closePreviousGroupByContent('');
206 $content = $this->renderContent();
208 // Get template engine
209 $templateInstance = $this->getTemplateInstance();
211 // Add content to variable
212 $templateInstance->assignVariable($this->getLinkName(), $content);
216 * Adds a link group (like the form group is) with some raw language to the
219 * @param $groupId Id string of the group
220 * @param $groupText Text for this group to add
221 * @param $groupCode Code to open and close groups
224 public function addLinkGroup ($groupId, $groupText, $groupCode = 'div') {
225 // Is a group with that name open?
226 if ($this->ifGroupOpenedPreviously()) {
227 // Then close it here
228 $this->closePreviousGroupByContent('');
231 // Generate the group content
232 $content = sprintf('<%s id="group_%s_%s">%s',
234 $this->getLinkName(),
239 // Open the new group
240 $this->openGroupByIdContent($groupId, $content, $groupCode);
244 * Adds text (note) to the previously opened group or throws an exception
245 * if no previous group was opened.
247 * @param $groupId Group id to set
248 * @param $groupNote Note to be added to a group
249 * @param $groupCode Code to open and close groups
251 * @throws NoGroupOpenedException If no previous group was opened
253 public function addLinkNote ($groupId, $groupNote, $groupCode = 'div') {
254 // Check if a previous group was opened
255 if ($this->ifGroupOpenedPreviously() === false) {
256 // No group was opened before!
257 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
260 // Is a previous sub group open?
261 if ($this->ifSubGroupOpenedPreviously()) {
263 $this->closePreviousSubGroupByContent('</' . $groupCode . '>');
266 // Generate the group content
267 $content = sprintf('<%s id="subgroup_%s_%s">%s',
269 $this->getLinkName(),
274 // Open the sub group
275 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
279 * Adds a link to the previously opened group or throws an exception if no group has been opened
281 * @param $linkAction Action (action=xxx) value for the link
282 * @param $linkText Link text and title (title="xxx") for the link
284 * @throws NoGroupOpenedException If no previous group was opened
286 protected function addActionLink ($linkAction, $linkText, $linkTitle) {
287 // Check if a previous group was opened
288 if ($this->ifGroupOpenedPreviously() === false) {
289 // No group was opened before!
290 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
293 // Default parameter SEPARATOR is &
294 $separator = self::EXTRA_PARAMETER_SEPARATOR;
296 // Is there a question mark in?
297 $linkArray = explode(self::FIRST_PARAMETER_SEPARATOR, $this->getLinkBase());
298 if (count($linkArray) == 0) {
300 $separator = self::FIRST_PARAMETER_SEPARATOR;
304 $action = sprintf('%saction=%s',
309 // Renders the link content
310 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
312 // Add the content to the previous group
313 $this->addContentToPreviousGroup($linkContent);
317 * Adds a link to the previously opened group with a text from language system
319 * @param $linkAction Action (action=xxx) value for the link
320 * @param $languageId Language id string to use
323 public function addActionLinkById ($linkAction, $languageId) {
324 // Resolve the language string
325 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
327 // Resolve the language string
328 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
330 // Add the action link
331 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
335 * Adds a default link (no extra parameters) to the content with specified
336 * language id string.
338 * @param $languageId Language id string to use
341 public function addLinkWithTextById ($languageId) {
342 // Resolve the language string
343 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
345 // Resolve the language string
346 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
349 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
351 // Add the content to the previous group
352 $this->addContentToPreviousGroup($linkContent);