* @version 0.0.0
* @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.shipsimu.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate {
/**
* Name of the link
*/
private $linkName = '';
/**
* Base of the link
*/
private $linkBase = '';
/**
* First parameter separator
*/
const FIRST_PARAMETER_SEPARATOR = '?';
/**
* SEPARATOR for more paraemters
*/
const EXTRA_PARAMETER_SEPARATOR = '&';
/**
* Protected constructor
*
* @return void
*/
protected function __construct () {
// Call parent constructor
parent::__construct(__CLASS__);
}
/**
* Creates the helper class
*
* @param $templateInstance An instance of a template engine
* @param $linkName Name of the link we shall generate
* @param $linkBase Link base for the link. This parameter is deprecated.
* @return $helperInstance A prepared instance of this helper
* @throws NoConfigEntryException A deprecated exception at this point
*/
public static final function createHtmlLinkHelper (CompileableTemplate $templateInstance, string $linkName, $linkBase = NULL) {
// Get new instance
$helperInstance = new HtmlLinkHelper();
// Set template instance
$helperInstance->setTemplateInstance($templateInstance);
// Set link name
$helperInstance->setLinkName($linkName);
// Get the application instance
$applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
// Get the request instance
$requestInstance = FrameworkBootstrap::getRequestInstance();
// Sanity-check on it
if (is_null($requestInstance)) {
// Throw an exception here
throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
} // END - if
// Get page (this will throw an exception if not set)
$command = StringUtils::convertDashesToUnderscores($requestInstance->getRequestElement('command'));
// Construct config entry
$configEntry = $command . '_' . $linkName . '_action_url';
// Is the deprecated parameter set?
if (!is_null($linkBase)) {
// Then output a deprecation message
$helperInstance->deprecationWarning('[' . __METHOD__ . ':' . __LINE__ . ']: linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
} // END - if
// Determine link base from config now and 'command' request
try {
$newLinkBase = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
$linkBase = $newLinkBase;
} catch (NoConfigEntryException $e) {
// Is the deprecated linkBase not set?
if (is_null($linkBase)) {
// Then throw again the exception
throw new NoConfigEntryException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
} // END - if
}
// Set link base
$helperInstance->setLinkBase($linkBase);
// Add default group
$helperInstance->openGroupByIdContent('main', '', '');
// Return the prepared instance
return $helperInstance;
}
/**
* Renders the link content (HTML code) with given link text and optional
* extra content
*
* @param $linkText Link text to set in link
* @param $linkTitle Link title to set in link
* @param $extraContent Optional extra HTML content
* @return $linkContent Rendered text link content
*/
private function renderLinkContentWithTextExtraContent (string $linkText, string $linkTitle, string $extraContent = '') {
// Construct link content
$linkContent = sprintf('%s',
$this->getLinkBase(),
$extraContent,
$linkTitle,
$linkText
);
// Return it
return $linkContent;
}
/**
* Setter for link name
*
* @param $linkName Name of the link we shall generate
* @return void
*/
protected final function setLinkName (string $linkName) {
$this->linkName = $linkName;
}
/**
* Getter for link name
*
* @return $linkName Name of the link we shall generate
*/
public final function getLinkName () {
return $this->linkName;
}
/**
* Setter for link base
*
* @param $linkBase Base of the link we shall generate
* @return void
*/
protected final function setLinkBase (string $linkBase) {
$this->linkBase = $linkBase;
}
/**
* Getter for link base
*
* @return $linkBase Base of the link we shall generate
*/
public final function getLinkBase () {
return $this->linkBase;
}
/**
* Flush the content out,e g. to a template variable
*
* @return void
* @todo Completely unimplemented
*/
public function flushContent () {
// Is a previous opened group still open?
if ($this->ifGroupOpenedPreviously()) {
// Then close it
$this->closePreviousGroupByContent('');
} // END - if
// Get the content
$content = $this->renderContent();
// Get template engine
$templateInstance = $this->getTemplateInstance();
// Add content to variable
$templateInstance->assignVariable($this->getLinkName(), $content);
}
/**
* Adds a link group (like the form group is) with some raw language to the
* helper.
*
* @param $groupId Id string of the group
* @param $groupText Text for this group to add
* @param $groupCode Code to open and close groups
* @return void
*/
public function addLinkGroup (string $groupId, string $groupText, string $groupCode = 'div') {
// Is a group with that name open?
if ($this->ifGroupOpenedPreviously()) {
// Then close it here
$this->closePreviousGroupByContent('');
} // END - if
// Generate the group content
$content = sprintf('<%s id="group_%s_%s">%s',
$groupCode,
$this->getLinkName(),
$groupId,
$groupText
);
// Open the new group
$this->openGroupByIdContent($groupId, $content, $groupCode);
}
/**
* Adds text (note) to the previously opened group or throws an exception
* if no previous group was opened.
*
* @param $groupId Group id to set
* @param $groupNote Note to be added to a group
* @param $groupCode Code to open and close groups
* @return void
* @throws NoGroupOpenedException If no previous group was opened
*/
public function addLinkNote (string $groupId, string $groupNote, string $groupCode = 'div') {
// Check if a previous group was opened
if ($this->ifGroupOpenedPreviously() === false) {
// No group was opened before!
throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
} // END - if
// Is a previous sub group open?
if ($this->ifSubGroupOpenedPreviously()) {
// Then close it
$this->closePreviousSubGroupByContent('' . $groupCode . '>');
} // END - if
// Generate the group content
$content = sprintf('<%s id="subgroup_%s_%s">%s',
$groupCode,
$this->getLinkName(),
$groupId,
$groupNote
);
// Open the sub group
$this->openSubGroupByIdContent($groupId, $content, $groupCode);
}
/**
* Adds a link to the previously opened group or throws an exception if no group has been opened
*
* @param $linkAction Action (action=xxx) value for the link
* @param $linkText Link text and title (title="xxx") for the link
* @return void
* @throws NoGroupOpenedException If no previous group was opened
*/
protected function addActionLink (string $linkAction, string $linkText, string $linkTitle) {
// Check if a previous group was opened
if ($this->ifGroupOpenedPreviously() === false) {
// No group was opened before!
throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
} // END - if
// Default parameter SEPARATOR is &
$separator = self::EXTRA_PARAMETER_SEPARATOR;
// Is there a question mark in?
$linkArray = explode(self::FIRST_PARAMETER_SEPARATOR, $this->getLinkBase());
if (count($linkArray) == 0) {
// No question mark
$separator = self::FIRST_PARAMETER_SEPARATOR;
} // END - if
// Prepare action
$action = sprintf('%saction=%s',
$separator,
$linkAction
);
// Renders the link content
$linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
// Add the content to the previous group
$this->addContentToPreviousGroup($linkContent);
}
/**
* Adds a link to the previously opened group with a text from language system
*
* @param $linkAction Action (action=xxx) value for the link
* @param $languageId Language id string to use
* @return void
*/
public function addActionLinkById (string $linkAction, string $languageId) {
// Resolve the language string
$languageResolvedText = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_text');
// Resolve the language string
$languageResolvedTitle = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_title');
// Add the action link
$this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
}
/**
* Adds a default link (no extra parameters) to the content with specified
* language id string.
*
* @param $languageId Language id string to use
* @return void
*/
public function addLinkWithTextById (string $languageId) {
// Resolve the language string
$languageResolvedText = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_text');
// Resolve the language string
$languageResolvedTitle = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_title');
// Now add the link
$linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
// Add the content to the previous group
$this->addContentToPreviousGroup($linkContent);
}
}