3 namespace CoreFramework\Helper;
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\Registry\Generic\Registry;
8 use CoreFramework\Template\CompileableTemplate;
11 * A helper for web links
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate {
36 private $linkName = '';
41 private $linkBase = '';
44 * First parameter separator
46 const FIRST_PARAMETER_SEPARATOR = '?';
49 * SEPARATOR for more paraemters
51 const EXTRA_PARAMETER_SEPARATOR = '&';
54 * Protected constructor
58 protected function __construct () {
59 // Call parent constructor
60 parent::__construct(__CLASS__);
64 * Creates the helper class
66 * @param $templateInstance An instance of a template engine
67 * @param $linkName Name of the link we shall generate
68 * @param $linkBase Link base for the link. This parameter is deprecated.
69 * @return $helperInstance A prepared instance of this helper
70 * @throws NoConfigEntryException A deprecated exception at this point
72 public static final function createHtmlLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase = NULL) {
74 $helperInstance = new HtmlLinkHelper();
76 // Set template instance
77 $helperInstance->setTemplateInstance($templateInstance);
80 $helperInstance->setLinkName($linkName);
82 // Get the application instance
83 $applicationInstance = Registry::getRegistry()->getInstance('application');
85 // Get the request instance
86 $requestInstance = $applicationInstance->getRequestInstance();
89 if (is_null($requestInstance)) {
90 // Throw an exception here
91 throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
94 // Get page (this will throw an exception if not set)
95 $command = $helperInstance->convertDashesToUnderscores($requestInstance->getRequestElement('command'));
97 // Construct config entry
98 $configEntry = $command . '_' . $linkName . '_action_url';
100 // Is the deprecated parameter set?
101 if (!is_null($linkBase)) {
102 // Then output a deprecation message
103 $helperInstance->deprecationWarning('[' . __METHOD__ . ':' . __LINE__ . ']: linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
106 // Determine link base from config now and 'command' request
108 $newLinkBase = $helperInstance->getConfigInstance()->getConfigEntry($configEntry);
109 $linkBase = $newLinkBase;
110 } catch (NoConfigEntryException $e) {
111 // Is the deprecated linkBase not set?
112 if (is_null($linkBase)) {
113 // Then throw again the exception
114 throw new NoConfigEntryException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
119 $helperInstance->setLinkBase($linkBase);
122 $helperInstance->openGroupByIdContent('main', '', '');
124 // Return the prepared instance
125 return $helperInstance;
129 * Renders the link content (HTML code) with given link text and optional
132 * @param $linkText Link text to set in link
133 * @param $linkTitle Link title to set in link
134 * @param $extraContent Optional extra HTML content
135 * @return $linkContent Rendered text link content
137 private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent='') {
138 // Construct link content
139 $linkContent = sprintf('<a href="{?base_url?}/%s%s" title="%s">%s</a>',
140 $this->getLinkBase(),
151 * Setter for link name
153 * @param $linkName Name of the link we shall generate
156 protected final function setLinkName ($linkName) {
157 $this->linkName = (string) $linkName;
161 * Getter for link name
163 * @return $linkName Name of the link we shall generate
165 public final function getLinkName () {
166 return $this->linkName;
170 * Setter for link base
172 * @param $linkBase Base of the link we shall generate
175 protected final function setLinkBase ($linkBase) {
176 $this->linkBase = (string) $linkBase;
180 * Getter for link base
182 * @return $linkBase Base of the link we shall generate
184 public final function getLinkBase () {
185 return $this->linkBase;
189 * Flush the content out,e g. to a template variable
192 * @todo Completely unimplemented
194 public function flushContent () {
195 // Is a previous opened group still open?
196 if ($this->ifGroupOpenedPreviously()) {
198 $this->closePreviousGroupByContent('');
202 $content = $this->renderContent();
204 // Get template engine
205 $templateInstance = $this->getTemplateInstance();
207 // Add content to variable
208 $templateInstance->assignVariable($this->getLinkName(), $content);
212 * Adds a link group (like the form group is) with some raw language to the
215 * @param $groupId Id string of the group
216 * @param $groupText Text for this group to add
217 * @param $groupCode Code to open and close groups
220 public function addLinkGroup ($groupId, $groupText, $groupCode = 'div') {
221 // Is a group with that name open?
222 if ($this->ifGroupOpenedPreviously()) {
223 // Then close it here
224 $this->closePreviousGroupByContent('');
227 // Generate the group content
228 $content = sprintf('<%s id="group_%s_%s">%s',
230 $this->getLinkName(),
235 // Open the new group
236 $this->openGroupByIdContent($groupId, $content, $groupCode);
240 * Adds text (note) to the previously opened group or throws an exception
241 * if no previous group was opened.
243 * @param $groupId Group id to set
244 * @param $groupNote Note to be added to a group
245 * @param $groupCode Code to open and close groups
247 * @throws NoGroupOpenedException If no previous group was opened
249 public function addLinkNote ($groupId, $groupNote, $groupCode = 'div') {
250 // Check if a previous group was opened
251 if ($this->ifGroupOpenedPreviously() === FALSE) {
252 // No group was opened before!
253 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
256 // Is a previous sub group open?
257 if ($this->ifSubGroupOpenedPreviously()) {
259 $this->closePreviousSubGroupByContent('</' . $groupCode . '>');
262 // Generate the group content
263 $content = sprintf('<%s id="subgroup_%s_%s">%s',
265 $this->getLinkName(),
270 // Open the sub group
271 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
275 * Adds a link to the previously opened group or throws an exception if no group has been opened
277 * @param $linkAction Action (action=xxx) value for the link
278 * @param $linkText Link text and title (title="xxx") for the link
280 * @throws NoGroupOpenedException If no previous group was opened
282 protected function addActionLink ($linkAction, $linkText, $linkTitle) {
283 // Check if a previous group was opened
284 if ($this->ifGroupOpenedPreviously() === FALSE) {
285 // No group was opened before!
286 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
289 // Default parameter SEPARATOR is &
290 $separator = self::EXTRA_PARAMETER_SEPARATOR;
292 // Is there a question mark in?
293 $linkArray = explode(self::FIRST_PARAMETER_SEPARATOR, $this->getLinkBase());
294 if (count($linkArray) == 0) {
296 $separator = self::FIRST_PARAMETER_SEPARATOR;
300 $action = sprintf('%saction=%s',
305 // Renders the link content
306 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
308 // Add the content to the previous group
309 $this->addContentToPreviousGroup($linkContent);
313 * Adds a link to the previously opened group with a text from language system
315 * @param $linkAction Action (action=xxx) value for the link
316 * @param $languageId Language id string to use
319 public function addActionLinkById ($linkAction, $languageId) {
320 // Resolve the language string
321 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
323 // Resolve the language string
324 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
326 // Add the action link
327 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
331 * Adds a default link (no extra parameters) to the content with specified
332 * language id string.
334 * @param $languageId Language id string to use
337 public function addLinkWithTextById ($languageId) {
338 // Resolve the language string
339 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
341 // Resolve the language string
342 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
345 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
347 // Add the content to the previous group
348 $this->addContentToPreviousGroup($linkContent);