3 * A helper for web links
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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 WebLinkHelper extends BaseWebHelper implements HelpableTemplate {
28 private $linkName = '';
33 private $linkBase = '';
36 * First parameter seperator
38 const FIRST_PARAMETER_SEPERATOR = '?';
41 * Seperator for more paraemters
43 const EXTRA_PARAMETER_SEPERATOR = '&';
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 createWebLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase = null) {
66 $helperInstance = new WebLinkHelper();
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 $page = $helperInstance->convertDashesToUnderscores($requestInstance->getRequestElement('page'));
89 // Construct config entry
90 $configEntry = $page . '_' . $linkName . '_action_url';
92 // Is the deprecated parameter set?
93 if (!is_null($linkBase)) {
94 // Then output a deprecation message
95 $helperInstance->deprecationWarning(__METHOD__ . ': 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 'page' 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=\"%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("<{$groupCode} id=\"group_%s_%s\">%s",
221 $this->getLinkName(),
226 // Open the new group
227 $this->openGroupByIdContent($groupId, $content, $groupCode);
231 * Adds text (note) to the previously opened group or throws an exception
232 * if no previous group was opened.
234 * @param $groupId Group id to set
235 * @param $groupNote Note to be added to a group
236 * @param $groupCode Code to open and close groups
238 * @throws NoGroupOpenedException If no previous group was opened
240 public function addLinkNote ($groupId, $groupNote, $groupCode = "div") {
241 // Check if a previous group was opened
242 if ($this->ifGroupOpenedPreviously() === false) {
243 // No group was opened before!
244 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
247 // Is a previous sub group open?
248 if ($this->ifSubGroupOpenedPreviously()) {
250 $this->closePreviousSubGroupByContent("</{$groupCode}>");
253 // Generate the group content
254 $content = sprintf("<{$groupCode} id=\"subgroup_%s_%s\">%s",
255 $this->getLinkName(),
260 // Open the sub group
261 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
265 * Adds a link to the previously opened group or throws an exception if no group has been opened
267 * @param $linkAction Action (action=xxx) value for the link
268 * @param $linkText Link text and title (title="xxx") for the link
270 * @throws NoGroupOpenedException If no previous group was opened
272 protected function addActionLink ($linkAction, $linkText, $linkTitle) {
273 // Check if a previous group was opened
274 if ($this->ifGroupOpenedPreviously() === false) {
275 // No group was opened before!
276 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
279 // Default parameter seperator is &
280 $seperator = self::EXTRA_PARAMETER_SEPERATOR;
282 // Is there a question mark in?
283 $linkArray = explode(self::FIRST_PARAMETER_SEPERATOR, $this->getLinkBase());
284 if (count($linkArray) == 0) {
286 $seperator = self::FIRST_PARAMETER_SEPERATOR;
290 $action = sprintf("%saction=%s",
295 // Renders the link content
296 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
298 // Add the content to the previous group
299 $this->addContentToPreviousGroup($linkContent);
303 * Adds a link to the previously opened group with a text from language system
305 * @param $linkAction Action (action=xxx) value for the link
306 * @param $languageId Language id string to use
309 public function addActionLinkById ($linkAction, $languageId) {
310 // Resolve the language string
311 $languageResolvedText = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_text");
313 // Resolve the language string
314 $languageResolvedTitle = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_title");
316 // Add the action link
317 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
321 * Adds a default link (no extra parameters) to the content with specified
322 * language id string.
324 * @param $languageId Language id string to use
327 public function addLinkWithTextById ($languageId) {
328 // Resolve the language string
329 $languageResolvedText = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_text");
331 // Resolve the language string
332 $languageResolvedTitle = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_title");
335 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
337 // Add the content to the previous group
338 $this->addContentToPreviousGroup($linkContent);