3 namespace CoreFramework\Helper;
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\Generic\NullPointerException;
8 use CoreFramework\Registry\Registry;
9 use CoreFramework\Template\CompileableTemplate;
12 * A helper for web links
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate {
37 private $linkName = '';
42 private $linkBase = '';
45 * First parameter separator
47 const FIRST_PARAMETER_SEPARATOR = '?';
50 * SEPARATOR for more paraemters
52 const EXTRA_PARAMETER_SEPARATOR = '&';
55 * Protected constructor
59 protected function __construct () {
60 // Call parent constructor
61 parent::__construct(__CLASS__);
65 * Creates the helper class
67 * @param $templateInstance An instance of a template engine
68 * @param $linkName Name of the link we shall generate
69 * @param $linkBase Link base for the link. This parameter is deprecated.
70 * @return $helperInstance A prepared instance of this helper
71 * @throws NoConfigEntryException A deprecated exception at this point
73 public static final function createHtmlLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase = NULL) {
75 $helperInstance = new HtmlLinkHelper();
77 // Set template instance
78 $helperInstance->setTemplateInstance($templateInstance);
81 $helperInstance->setLinkName($linkName);
83 // Get the application instance
84 $applicationInstance = Registry::getRegistry()->getInstance('application');
86 // Get the request instance
87 $requestInstance = $applicationInstance->getRequestInstance();
90 if (is_null($requestInstance)) {
91 // Throw an exception here
92 throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
95 // Get page (this will throw an exception if not set)
96 $command = $helperInstance->convertDashesToUnderscores($requestInstance->getRequestElement('command'));
98 // Construct config entry
99 $configEntry = $command . '_' . $linkName . '_action_url';
101 // Is the deprecated parameter set?
102 if (!is_null($linkBase)) {
103 // Then output a deprecation message
104 $helperInstance->deprecationWarning('[' . __METHOD__ . ':' . __LINE__ . ']: linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
107 // Determine link base from config now and 'command' request
109 $newLinkBase = $helperInstance->getConfigInstance()->getConfigEntry($configEntry);
110 $linkBase = $newLinkBase;
111 } catch (NoConfigEntryException $e) {
112 // Is the deprecated linkBase not set?
113 if (is_null($linkBase)) {
114 // Then throw again the exception
115 throw new NoConfigEntryException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
120 $helperInstance->setLinkBase($linkBase);
123 $helperInstance->openGroupByIdContent('main', '', '');
125 // Return the prepared instance
126 return $helperInstance;
130 * Renders the link content (HTML code) with given link text and optional
133 * @param $linkText Link text to set in link
134 * @param $linkTitle Link title to set in link
135 * @param $extraContent Optional extra HTML content
136 * @return $linkContent Rendered text link content
138 private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent='') {
139 // Construct link content
140 $linkContent = sprintf('<a href="{?base_url?}/%s%s" title="%s">%s</a>',
141 $this->getLinkBase(),
152 * Setter for link name
154 * @param $linkName Name of the link we shall generate
157 protected final function setLinkName ($linkName) {
158 $this->linkName = (string) $linkName;
162 * Getter for link name
164 * @return $linkName Name of the link we shall generate
166 public final function getLinkName () {
167 return $this->linkName;
171 * Setter for link base
173 * @param $linkBase Base of the link we shall generate
176 protected final function setLinkBase ($linkBase) {
177 $this->linkBase = (string) $linkBase;
181 * Getter for link base
183 * @return $linkBase Base of the link we shall generate
185 public final function getLinkBase () {
186 return $this->linkBase;
190 * Flush the content out,e g. to a template variable
193 * @todo Completely unimplemented
195 public function flushContent () {
196 // Is a previous opened group still open?
197 if ($this->ifGroupOpenedPreviously()) {
199 $this->closePreviousGroupByContent('');
203 $content = $this->renderContent();
205 // Get template engine
206 $templateInstance = $this->getTemplateInstance();
208 // Add content to variable
209 $templateInstance->assignVariable($this->getLinkName(), $content);
213 * Adds a link group (like the form group is) with some raw language to the
216 * @param $groupId Id string of the group
217 * @param $groupText Text for this group to add
218 * @param $groupCode Code to open and close groups
221 public function addLinkGroup ($groupId, $groupText, $groupCode = 'div') {
222 // Is a group with that name open?
223 if ($this->ifGroupOpenedPreviously()) {
224 // Then close it here
225 $this->closePreviousGroupByContent('');
228 // Generate the group content
229 $content = sprintf('<%s id="group_%s_%s">%s',
231 $this->getLinkName(),
236 // Open the new group
237 $this->openGroupByIdContent($groupId, $content, $groupCode);
241 * Adds text (note) to the previously opened group or throws an exception
242 * if no previous group was opened.
244 * @param $groupId Group id to set
245 * @param $groupNote Note to be added to a group
246 * @param $groupCode Code to open and close groups
248 * @throws NoGroupOpenedException If no previous group was opened
250 public function addLinkNote ($groupId, $groupNote, $groupCode = 'div') {
251 // Check if a previous group was opened
252 if ($this->ifGroupOpenedPreviously() === FALSE) {
253 // No group was opened before!
254 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
257 // Is a previous sub group open?
258 if ($this->ifSubGroupOpenedPreviously()) {
260 $this->closePreviousSubGroupByContent('</' . $groupCode . '>');
263 // Generate the group content
264 $content = sprintf('<%s id="subgroup_%s_%s">%s',
266 $this->getLinkName(),
271 // Open the sub group
272 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
276 * Adds a link to the previously opened group or throws an exception if no group has been opened
278 * @param $linkAction Action (action=xxx) value for the link
279 * @param $linkText Link text and title (title="xxx") for the link
281 * @throws NoGroupOpenedException If no previous group was opened
283 protected function addActionLink ($linkAction, $linkText, $linkTitle) {
284 // Check if a previous group was opened
285 if ($this->ifGroupOpenedPreviously() === FALSE) {
286 // No group was opened before!
287 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
290 // Default parameter SEPARATOR is &
291 $separator = self::EXTRA_PARAMETER_SEPARATOR;
293 // Is there a question mark in?
294 $linkArray = explode(self::FIRST_PARAMETER_SEPARATOR, $this->getLinkBase());
295 if (count($linkArray) == 0) {
297 $separator = self::FIRST_PARAMETER_SEPARATOR;
301 $action = sprintf('%saction=%s',
306 // Renders the link content
307 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
309 // Add the content to the previous group
310 $this->addContentToPreviousGroup($linkContent);
314 * Adds a link to the previously opened group with a text from language system
316 * @param $linkAction Action (action=xxx) value for the link
317 * @param $languageId Language id string to use
320 public function addActionLinkById ($linkAction, $languageId) {
321 // Resolve the language string
322 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
324 // Resolve the language string
325 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
327 // Add the action link
328 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
332 * Adds a default link (no extra parameters) to the content with specified
333 * language id string.
335 * @param $languageId Language id string to use
338 public function addLinkWithTextById ($languageId) {
339 // Resolve the language string
340 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
342 // Resolve the language string
343 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
346 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
348 // Add the content to the previous group
349 $this->addContentToPreviousGroup($linkContent);