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\Registry;
11 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
14 * A helper for web links
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 <<<<<<< HEAD:framework/main/classes/helper/html/links/class_HtmlLinkHelper.php
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
22 >>>>>>> Some updates::inc/main/classes/helper/html/links/class_HtmlLinkHelper.php
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate {
43 private $linkName = '';
48 private $linkBase = '';
51 * First parameter separator
53 const FIRST_PARAMETER_SEPARATOR = '?';
56 * SEPARATOR for more paraemters
58 const EXTRA_PARAMETER_SEPARATOR = '&';
61 * Protected constructor
65 protected function __construct () {
66 // Call parent constructor
67 parent::__construct(__CLASS__);
71 * Creates the helper class
73 * @param $templateInstance An instance of a template engine
74 * @param $linkName Name of the link we shall generate
75 * @param $linkBase Link base for the link. This parameter is deprecated.
76 * @return $helperInstance A prepared instance of this helper
77 * @throws NoConfigEntryException A deprecated exception at this point
79 public static final function createHtmlLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase = NULL) {
81 $helperInstance = new HtmlLinkHelper();
83 // Set template instance
84 $helperInstance->setTemplateInstance($templateInstance);
87 $helperInstance->setLinkName($linkName);
89 // Get the application instance
90 $applicationInstance = Registry::getRegistry()->getInstance('application');
92 // Get the request instance
93 $requestInstance = FrameworkBootstrap::getRequestInstance();
96 if (is_null($requestInstance)) {
97 // Throw an exception here
98 throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
101 // Get page (this will throw an exception if not set)
102 $command = $helperInstance->convertDashesToUnderscores($requestInstance->getRequestElement('command'));
104 // Construct config entry
105 $configEntry = $command . '_' . $linkName . '_action_url';
107 // Is the deprecated parameter set?
108 if (!is_null($linkBase)) {
109 // Then output a deprecation message
110 $helperInstance->deprecationWarning('[' . __METHOD__ . ':' . __LINE__ . ']: linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
113 // Determine link base from config now and 'command' request
115 $newLinkBase = $helperInstance->getConfigInstance()->getConfigEntry($configEntry);
116 $linkBase = $newLinkBase;
117 } catch (NoConfigEntryException $e) {
118 // Is the deprecated linkBase not set?
119 if (is_null($linkBase)) {
120 // Then throw again the exception
121 throw new NoConfigEntryException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
126 $helperInstance->setLinkBase($linkBase);
129 $helperInstance->openGroupByIdContent('main', '', '');
131 // Return the prepared instance
132 return $helperInstance;
136 * Renders the link content (HTML code) with given link text and optional
139 * @param $linkText Link text to set in link
140 * @param $linkTitle Link title to set in link
141 * @param $extraContent Optional extra HTML content
142 * @return $linkContent Rendered text link content
144 private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent='') {
145 // Construct link content
146 $linkContent = sprintf('<a href="{?base_url?}/%s%s" title="%s">%s</a>',
147 $this->getLinkBase(),
158 * Setter for link name
160 * @param $linkName Name of the link we shall generate
163 protected final function setLinkName ($linkName) {
164 $this->linkName = (string) $linkName;
168 * Getter for link name
170 * @return $linkName Name of the link we shall generate
172 public final function getLinkName () {
173 return $this->linkName;
177 * Setter for link base
179 * @param $linkBase Base of the link we shall generate
182 protected final function setLinkBase ($linkBase) {
183 $this->linkBase = (string) $linkBase;
187 * Getter for link base
189 * @return $linkBase Base of the link we shall generate
191 public final function getLinkBase () {
192 return $this->linkBase;
196 * Flush the content out,e g. to a template variable
199 * @todo Completely unimplemented
201 public function flushContent () {
202 // Is a previous opened group still open?
203 if ($this->ifGroupOpenedPreviously()) {
205 $this->closePreviousGroupByContent('');
209 $content = $this->renderContent();
211 // Get template engine
212 $templateInstance = $this->getTemplateInstance();
214 // Add content to variable
215 $templateInstance->assignVariable($this->getLinkName(), $content);
219 * Adds a link group (like the form group is) with some raw language to the
222 * @param $groupId Id string of the group
223 * @param $groupText Text for this group to add
224 * @param $groupCode Code to open and close groups
227 public function addLinkGroup ($groupId, $groupText, $groupCode = 'div') {
228 // Is a group with that name open?
229 if ($this->ifGroupOpenedPreviously()) {
230 // Then close it here
231 $this->closePreviousGroupByContent('');
234 // Generate the group content
235 $content = sprintf('<%s id="group_%s_%s">%s',
237 $this->getLinkName(),
242 // Open the new group
243 $this->openGroupByIdContent($groupId, $content, $groupCode);
247 * Adds text (note) to the previously opened group or throws an exception
248 * if no previous group was opened.
250 * @param $groupId Group id to set
251 * @param $groupNote Note to be added to a group
252 * @param $groupCode Code to open and close groups
254 * @throws NoGroupOpenedException If no previous group was opened
256 public function addLinkNote ($groupId, $groupNote, $groupCode = 'div') {
257 // Check if a previous group was opened
258 if ($this->ifGroupOpenedPreviously() === false) {
259 // No group was opened before!
260 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
263 // Is a previous sub group open?
264 if ($this->ifSubGroupOpenedPreviously()) {
266 $this->closePreviousSubGroupByContent('</' . $groupCode . '>');
269 // Generate the group content
270 $content = sprintf('<%s id="subgroup_%s_%s">%s',
272 $this->getLinkName(),
277 // Open the sub group
278 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
282 * Adds a link to the previously opened group or throws an exception if no group has been opened
284 * @param $linkAction Action (action=xxx) value for the link
285 * @param $linkText Link text and title (title="xxx") for the link
287 * @throws NoGroupOpenedException If no previous group was opened
289 protected function addActionLink ($linkAction, $linkText, $linkTitle) {
290 // Check if a previous group was opened
291 if ($this->ifGroupOpenedPreviously() === false) {
292 // No group was opened before!
293 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
296 // Default parameter SEPARATOR is &
297 $separator = self::EXTRA_PARAMETER_SEPARATOR;
299 // Is there a question mark in?
300 $linkArray = explode(self::FIRST_PARAMETER_SEPARATOR, $this->getLinkBase());
301 if (count($linkArray) == 0) {
303 $separator = self::FIRST_PARAMETER_SEPARATOR;
307 $action = sprintf('%saction=%s',
312 // Renders the link content
313 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
315 // Add the content to the previous group
316 $this->addContentToPreviousGroup($linkContent);
320 * Adds a link to the previously opened group with a text from language system
322 * @param $linkAction Action (action=xxx) value for the link
323 * @param $languageId Language id string to use
326 public function addActionLinkById ($linkAction, $languageId) {
327 // Resolve the language string
328 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
330 // Resolve the language string
331 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
333 // Add the action link
334 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
338 * Adds a default link (no extra parameters) to the content with specified
339 * language id string.
341 * @param $languageId Language id string to use
344 public function addLinkWithTextById ($languageId) {
345 // Resolve the language string
346 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
348 // Resolve the language string
349 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
352 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
354 // Add the content to the previous group
355 $this->addContentToPreviousGroup($linkContent);