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\Configuration\NoConfigEntryException;
9 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
10 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
11 use Org\Mxchange\CoreFramework\Helper\Template\HelpableTemplate;
12 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
13 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
14 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
17 * A helper for web links
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate {
42 private $linkName = '';
47 private $linkBase = '';
50 * First parameter separator
52 const FIRST_PARAMETER_SEPARATOR = '?';
55 * SEPARATOR for more paraemters
57 const EXTRA_PARAMETER_SEPARATOR = '&';
60 * Protected constructor
64 private function __construct () {
65 // Call parent constructor
66 parent::__construct(__CLASS__);
70 * Creates the helper class
72 * @param $templateInstance An instance of a template engine
73 * @param $linkName Name of the link we shall generate
74 * @param $linkBase Link base for the link. This parameter is deprecated.
75 * @return $helperInstance A prepared instance of this helper
76 * @throws NoConfigEntryException A deprecated exception at this point
78 public static final function createHtmlLinkHelper (CompileableTemplate $templateInstance, string $linkName, $linkBase = NULL) {
80 $helperInstance = new HtmlLinkHelper();
82 // Set template instance
83 $helperInstance->setTemplateInstance($templateInstance);
86 $helperInstance->setLinkName($linkName);
88 // Get the application instance
89 $applicationInstance = ApplicationHelper::getSelfInstance();
91 // Get the request instance
92 $requestInstance = FrameworkBootstrap::getRequestInstance();
95 if (is_null($requestInstance)) {
96 // Throw an exception here
97 throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
100 // Get page (this will throw an exception if not set)
101 $command = StringUtils::convertDashesToUnderscores($requestInstance->getRequestElement('command'));
103 // Construct config entry
104 $configEntry = $command . '_' . $linkName . '_action_url';
106 // Is the deprecated parameter set?
107 if (!is_null($linkBase)) {
108 // Then output a deprecation message
109 $helperInstance->deprecationWarning('[' . __METHOD__ . ':' . __LINE__ . ']: linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
112 // Determine link base from config now and 'command' request
114 $newLinkBase = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
115 $linkBase = $newLinkBase;
116 } catch (NoConfigEntryException $e) {
117 // Is the deprecated linkBase not set?
118 if (is_null($linkBase)) {
119 // Then throw again the exception
120 throw new NoConfigEntryException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
125 $helperInstance->setLinkBase($linkBase);
128 $helperInstance->openGroupByIdContent('main', '', '');
130 // Return the prepared instance
131 return $helperInstance;
135 * Renders the link content (HTML code) with given link text and optional
138 * @param $linkText Link text to set in link
139 * @param $linkTitle Link title to set in link
140 * @param $extraContent Optional extra HTML content
141 * @return $linkContent Rendered text link content
143 private function renderLinkContentWithTextExtraContent (string $linkText, string $linkTitle, string $extraContent = '') {
144 // Construct link content
145 $linkContent = sprintf('<a href="{?base_url?}/%s%s" title="%s">%s</a>',
146 $this->getLinkBase(),
157 * Setter for link name
159 * @param $linkName Name of the link we shall generate
162 protected final function setLinkName (string $linkName) {
163 $this->linkName = $linkName;
167 * Getter for link name
169 * @return $linkName Name of the link we shall generate
171 public final function getLinkName () {
172 return $this->linkName;
176 * Setter for link base
178 * @param $linkBase Base of the link we shall generate
181 protected final function setLinkBase (string $linkBase) {
182 $this->linkBase = $linkBase;
186 * Getter for link base
188 * @return $linkBase Base of the link we shall generate
190 public final function getLinkBase () {
191 return $this->linkBase;
195 * Flush the content out,e g. to a template variable
198 * @todo Completely unimplemented
200 public function flushContent () {
201 // Is a previous opened group still open?
202 if ($this->ifGroupOpenedPreviously()) {
204 $this->closePreviousGroupByContent('');
208 $content = $this->renderContent();
210 // Get template engine
211 $templateInstance = $this->getTemplateInstance();
213 // Add content to variable
214 $templateInstance->assignVariable($this->getLinkName(), $content);
218 * Adds a link group (like the form group is) with some raw language to the
221 * @param $groupId Id string of the group
222 * @param $groupText Text for this group to add
223 * @param $groupCode Code to open and close groups
226 public function addLinkGroup (string $groupId, string $groupText, string $groupCode = 'div') {
227 // Is a group with that name open?
228 if ($this->ifGroupOpenedPreviously()) {
229 // Then close it here
230 $this->closePreviousGroupByContent('');
233 // Generate the group content
234 $content = sprintf('<%s id="group_%s_%s">%s',
236 $this->getLinkName(),
241 // Open the new group
242 $this->openGroupByIdContent($groupId, $content, $groupCode);
246 * Adds text (note) to the previously opened group or throws an exception
247 * if no previous group was opened.
249 * @param $groupId Group id to set
250 * @param $groupNote Note to be added to a group
251 * @param $groupCode Code to open and close groups
253 * @throws NoGroupOpenedException If no previous group was opened
255 public function addLinkNote (string $groupId, string $groupNote, string $groupCode = 'div') {
256 // Check if a previous group was opened
257 if ($this->ifGroupOpenedPreviously() === false) {
258 // No group was opened before!
259 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
262 // Is a previous sub group open?
263 if ($this->ifSubGroupOpenedPreviously()) {
265 $this->closePreviousSubGroupByContent('</' . $groupCode . '>');
268 // Generate the group content
269 $content = sprintf('<%s id="subgroup_%s_%s">%s',
271 $this->getLinkName(),
276 // Open the sub group
277 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
281 * Adds a link to the previously opened group or throws an exception if no group has been opened
283 * @param $linkAction Action (action=xxx) value for the link
284 * @param $linkText Link text and title (title="xxx") for the link
286 * @throws NoGroupOpenedException If no previous group was opened
288 protected function addActionLink (string $linkAction, string $linkText, string $linkTitle) {
289 // Check if a previous group was opened
290 if ($this->ifGroupOpenedPreviously() === false) {
291 // No group was opened before!
292 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
295 // Default parameter SEPARATOR is &
296 $separator = self::EXTRA_PARAMETER_SEPARATOR;
298 // Is there a question mark in?
299 $linkArray = explode(self::FIRST_PARAMETER_SEPARATOR, $this->getLinkBase());
300 if (count($linkArray) == 0) {
302 $separator = self::FIRST_PARAMETER_SEPARATOR;
306 $action = sprintf('%saction=%s',
311 // Renders the link content
312 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
314 // Add the content to the previous group
315 $this->addContentToPreviousGroup($linkContent);
319 * Adds a link to the previously opened group with a text from language system
321 * @param $linkAction Action (action=xxx) value for the link
322 * @param $languageId Language id string to use
325 public function addActionLinkById (string $linkAction, string $languageId) {
326 // Resolve the language string
327 $languageResolvedText = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_text');
329 // Resolve the language string
330 $languageResolvedTitle = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_title');
332 // Add the action link
333 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
337 * Adds a default link (no extra parameters) to the content with specified
338 * language id string.
340 * @param $languageId Language id string to use
343 public function addLinkWithTextById (string $languageId) {
344 // Resolve the language string
345 $languageResolvedText = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_text');
347 // Resolve the language string
348 $languageResolvedTitle = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_title');
351 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
353 // Add the content to the previous group
354 $this->addContentToPreviousGroup($linkContent);