3 * A helper for web links
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate {
28 private $linkName = '';
33 private $linkBase = '';
36 * First parameter separator
38 const FIRST_PARAMETER_SEPARATOR = '?';
41 * SEPARATOR for more paraemters
43 const EXTRA_PARAMETER_SEPARATOR = '&';
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Creates the helper class
58 * @param $templateInstance An instance of a template engine
59 * @param $linkName Name of the link we shall generate
60 * @param $linkBase Link base for the link. This parameter is deprecated.
61 * @return $helperInstance A prepared instance of this helper
62 * @throws NoConfigEntryException A deprecated exception at this point
64 public static final function createHtmlLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase = NULL) {
66 $helperInstance = new HtmlLinkHelper();
68 // Set template instance
69 $helperInstance->setTemplateInstance($templateInstance);
72 $helperInstance->setLinkName($linkName);
74 // Get the application instance
75 $applicationInstance = Registry::getRegistry()->getInstance('application');
77 // Get the request instance
78 $requestInstance = $applicationInstance->getRequestInstance();
81 if (is_null($requestInstance)) {
82 // Throw an exception here
83 throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
86 // Get page (this will throw an exception if not set)
87 $command = $helperInstance->convertDashesToUnderscores($requestInstance->getRequestElement('command'));
89 // Construct config entry
90 $configEntry = $command . '_' . $linkName . '_action_url';
92 // Is the deprecated parameter set?
93 if (!is_null($linkBase)) {
94 // Then output a deprecation message
95 $helperInstance->deprecationWarning('[' . __METHOD__ . ':' . __LINE__ . ']: linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
98 // Determine link base from config now and 'command' request
100 $newLinkBase = $helperInstance->getConfigInstance()->getConfigEntry($configEntry);
101 $linkBase = $newLinkBase;
102 } catch (NoConfigEntryException $e) {
103 // Is the deprecated linkBase not set?
104 if (is_null($linkBase)) {
105 // Then throw again the exception
106 throw new NoConfigEntryException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
111 $helperInstance->setLinkBase($linkBase);
114 $helperInstance->openGroupByIdContent('main', '', '');
116 // Return the prepared instance
117 return $helperInstance;
121 * Renders the link content (HTML code) with given link text and optional
124 * @param $linkText Link text to set in link
125 * @param $linkTitle Link title to set in link
126 * @param $extraContent Optional extra HTML content
127 * @return $linkContent Rendered text link content
129 private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent='') {
130 // Construct link content
131 $linkContent = sprintf('<a href="{?base_url?}/%s%s" title="%s">%s</a>',
132 $this->getLinkBase(),
143 * Setter for link name
145 * @param $linkName Name of the link we shall generate
148 protected final function setLinkName ($linkName) {
149 $this->linkName = (string) $linkName;
153 * Getter for link name
155 * @return $linkName Name of the link we shall generate
157 public final function getLinkName () {
158 return $this->linkName;
162 * Setter for link base
164 * @param $linkBase Base of the link we shall generate
167 protected final function setLinkBase ($linkBase) {
168 $this->linkBase = (string) $linkBase;
172 * Getter for link base
174 * @return $linkBase Base of the link we shall generate
176 public final function getLinkBase () {
177 return $this->linkBase;
181 * Flush the content out,e g. to a template variable
184 * @todo Completely unimplemented
186 public function flushContent () {
187 // Is a previous opened group still open?
188 if ($this->ifGroupOpenedPreviously()) {
190 $this->closePreviousGroupByContent('');
194 $content = $this->renderContent();
196 // Get template engine
197 $templateInstance = $this->getTemplateInstance();
199 // Add content to variable
200 $templateInstance->assignVariable($this->getLinkName(), $content);
204 * Adds a link group (like the form group is) with some raw language to the
207 * @param $groupId Id string of the group
208 * @param $groupText Text for this group to add
209 * @param $groupCode Code to open and close groups
212 public function addLinkGroup ($groupId, $groupText, $groupCode = 'div') {
213 // Is a group with that name open?
214 if ($this->ifGroupOpenedPreviously()) {
215 // Then close it here
216 $this->closePreviousGroupByContent('');
219 // Generate the group content
220 $content = sprintf('<%s id="group_%s_%s">%s',
222 $this->getLinkName(),
227 // Open the new group
228 $this->openGroupByIdContent($groupId, $content, $groupCode);
232 * Adds text (note) to the previously opened group or throws an exception
233 * if no previous group was opened.
235 * @param $groupId Group id to set
236 * @param $groupNote Note to be added to a group
237 * @param $groupCode Code to open and close groups
239 * @throws NoGroupOpenedException If no previous group was opened
241 public function addLinkNote ($groupId, $groupNote, $groupCode = 'div') {
242 // Check if a previous group was opened
243 if ($this->ifGroupOpenedPreviously() === FALSE) {
244 // No group was opened before!
245 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
248 // Is a previous sub group open?
249 if ($this->ifSubGroupOpenedPreviously()) {
251 $this->closePreviousSubGroupByContent('</' . $groupCode . '>');
254 // Generate the group content
255 $content = sprintf('<%s id="subgroup_%s_%s">%s',
257 $this->getLinkName(),
262 // Open the sub group
263 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
267 * Adds a link to the previously opened group or throws an exception if no group has been opened
269 * @param $linkAction Action (action=xxx) value for the link
270 * @param $linkText Link text and title (title="xxx") for the link
272 * @throws NoGroupOpenedException If no previous group was opened
274 protected function addActionLink ($linkAction, $linkText, $linkTitle) {
275 // Check if a previous group was opened
276 if ($this->ifGroupOpenedPreviously() === FALSE) {
277 // No group was opened before!
278 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
281 // Default parameter SEPARATOR is &
282 $separator = self::EXTRA_PARAMETER_SEPARATOR;
284 // Is there a question mark in?
285 $linkArray = explode(self::FIRST_PARAMETER_SEPARATOR, $this->getLinkBase());
286 if (count($linkArray) == 0) {
288 $separator = self::FIRST_PARAMETER_SEPARATOR;
292 $action = sprintf('%saction=%s',
297 // Renders the link content
298 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
300 // Add the content to the previous group
301 $this->addContentToPreviousGroup($linkContent);
305 * Adds a link to the previously opened group with a text from language system
307 * @param $linkAction Action (action=xxx) value for the link
308 * @param $languageId Language id string to use
311 public function addActionLinkById ($linkAction, $languageId) {
312 // Resolve the language string
313 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
315 // Resolve the language string
316 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
318 // Add the action link
319 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
323 * Adds a default link (no extra parameters) to the content with specified
324 * language id string.
326 * @param $languageId Language id string to use
329 public function addLinkWithTextById ($languageId) {
330 // Resolve the language string
331 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
333 // Resolve the language string
334 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
337 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
339 // Add the content to the previous group
340 $this->addContentToPreviousGroup($linkContent);