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\Template\HelpableTemplate;
11 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
12 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
13 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
16 * A helper for web links
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate {
41 private $linkName = '';
46 private $linkBase = '';
49 * First parameter separator
51 const FIRST_PARAMETER_SEPARATOR = '?';
54 * SEPARATOR for more paraemters
56 const EXTRA_PARAMETER_SEPARATOR = '&';
59 * Protected constructor
63 protected function __construct () {
64 // Call parent constructor
65 parent::__construct(__CLASS__);
69 * Creates the helper class
71 * @param $templateInstance An instance of a template engine
72 * @param $linkName Name of the link we shall generate
73 * @param $linkBase Link base for the link. This parameter is deprecated.
74 * @return $helperInstance A prepared instance of this helper
75 * @throws NoConfigEntryException A deprecated exception at this point
77 public static final function createHtmlLinkHelper (CompileableTemplate $templateInstance, string $linkName, $linkBase = NULL) {
79 $helperInstance = new HtmlLinkHelper();
81 // Set template instance
82 $helperInstance->setTemplateInstance($templateInstance);
85 $helperInstance->setLinkName($linkName);
87 // Get the application instance
88 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
90 // Get the request instance
91 $requestInstance = FrameworkBootstrap::getRequestInstance();
94 if (is_null($requestInstance)) {
95 // Throw an exception here
96 throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
99 // Get page (this will throw an exception if not set)
100 $command = StringUtils::convertDashesToUnderscores($requestInstance->getRequestElement('command'));
102 // Construct config entry
103 $configEntry = $command . '_' . $linkName . '_action_url';
105 // Is the deprecated parameter set?
106 if (!is_null($linkBase)) {
107 // Then output a deprecation message
108 $helperInstance->deprecationWarning('[' . __METHOD__ . ':' . __LINE__ . ']: linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
111 // Determine link base from config now and 'command' request
113 $newLinkBase = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
114 $linkBase = $newLinkBase;
115 } catch (NoConfigEntryException $e) {
116 // Is the deprecated linkBase not set?
117 if (is_null($linkBase)) {
118 // Then throw again the exception
119 throw new NoConfigEntryException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
124 $helperInstance->setLinkBase($linkBase);
127 $helperInstance->openGroupByIdContent('main', '', '');
129 // Return the prepared instance
130 return $helperInstance;
134 * Renders the link content (HTML code) with given link text and optional
137 * @param $linkText Link text to set in link
138 * @param $linkTitle Link title to set in link
139 * @param $extraContent Optional extra HTML content
140 * @return $linkContent Rendered text link content
142 private function renderLinkContentWithTextExtraContent (string $linkText, string $linkTitle, string $extraContent = '') {
143 // Construct link content
144 $linkContent = sprintf('<a href="{?base_url?}/%s%s" title="%s">%s</a>',
145 $this->getLinkBase(),
156 * Setter for link name
158 * @param $linkName Name of the link we shall generate
161 protected final function setLinkName (string $linkName) {
162 $this->linkName = $linkName;
166 * Getter for link name
168 * @return $linkName Name of the link we shall generate
170 public final function getLinkName () {
171 return $this->linkName;
175 * Setter for link base
177 * @param $linkBase Base of the link we shall generate
180 protected final function setLinkBase (string $linkBase) {
181 $this->linkBase = $linkBase;
185 * Getter for link base
187 * @return $linkBase Base of the link we shall generate
189 public final function getLinkBase () {
190 return $this->linkBase;
194 * Flush the content out,e g. to a template variable
197 * @todo Completely unimplemented
199 public function flushContent () {
200 // Is a previous opened group still open?
201 if ($this->ifGroupOpenedPreviously()) {
203 $this->closePreviousGroupByContent('');
207 $content = $this->renderContent();
209 // Get template engine
210 $templateInstance = $this->getTemplateInstance();
212 // Add content to variable
213 $templateInstance->assignVariable($this->getLinkName(), $content);
217 * Adds a link group (like the form group is) with some raw language to the
220 * @param $groupId Id string of the group
221 * @param $groupText Text for this group to add
222 * @param $groupCode Code to open and close groups
225 public function addLinkGroup (string $groupId, string $groupText, string $groupCode = 'div') {
226 // Is a group with that name open?
227 if ($this->ifGroupOpenedPreviously()) {
228 // Then close it here
229 $this->closePreviousGroupByContent('');
232 // Generate the group content
233 $content = sprintf('<%s id="group_%s_%s">%s',
235 $this->getLinkName(),
240 // Open the new group
241 $this->openGroupByIdContent($groupId, $content, $groupCode);
245 * Adds text (note) to the previously opened group or throws an exception
246 * if no previous group was opened.
248 * @param $groupId Group id to set
249 * @param $groupNote Note to be added to a group
250 * @param $groupCode Code to open and close groups
252 * @throws NoGroupOpenedException If no previous group was opened
254 public function addLinkNote (string $groupId, string $groupNote, string $groupCode = 'div') {
255 // Check if a previous group was opened
256 if ($this->ifGroupOpenedPreviously() === false) {
257 // No group was opened before!
258 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
261 // Is a previous sub group open?
262 if ($this->ifSubGroupOpenedPreviously()) {
264 $this->closePreviousSubGroupByContent('</' . $groupCode . '>');
267 // Generate the group content
268 $content = sprintf('<%s id="subgroup_%s_%s">%s',
270 $this->getLinkName(),
275 // Open the sub group
276 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
280 * Adds a link to the previously opened group or throws an exception if no group has been opened
282 * @param $linkAction Action (action=xxx) value for the link
283 * @param $linkText Link text and title (title="xxx") for the link
285 * @throws NoGroupOpenedException If no previous group was opened
287 protected function addActionLink (string $linkAction, string $linkText, string $linkTitle) {
288 // Check if a previous group was opened
289 if ($this->ifGroupOpenedPreviously() === false) {
290 // No group was opened before!
291 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
294 // Default parameter SEPARATOR is &
295 $separator = self::EXTRA_PARAMETER_SEPARATOR;
297 // Is there a question mark in?
298 $linkArray = explode(self::FIRST_PARAMETER_SEPARATOR, $this->getLinkBase());
299 if (count($linkArray) == 0) {
301 $separator = self::FIRST_PARAMETER_SEPARATOR;
305 $action = sprintf('%saction=%s',
310 // Renders the link content
311 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
313 // Add the content to the previous group
314 $this->addContentToPreviousGroup($linkContent);
318 * Adds a link to the previously opened group with a text from language system
320 * @param $linkAction Action (action=xxx) value for the link
321 * @param $languageId Language id string to use
324 public function addActionLinkById (string $linkAction, string $languageId) {
325 // Resolve the language string
326 $languageResolvedText = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_text');
328 // Resolve the language string
329 $languageResolvedTitle = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_title');
331 // Add the action link
332 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
336 * Adds a default link (no extra parameters) to the content with specified
337 * language id string.
339 * @param $languageId Language id string to use
342 public function addLinkWithTextById (string $languageId) {
343 // Resolve the language string
344 $languageResolvedText = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_text');
346 // Resolve the language string
347 $languageResolvedTitle = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_title');
350 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
352 // Add the content to the previous group
353 $this->addContentToPreviousGroup($linkContent);