3 * A helper for web links
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 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 private $firstParameter = '?';
41 * Seperator for more paraemters
43 private $parameterSeperator = '&';
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 NullPointerException Thrown if an instance is null
63 * @throws ConfigEntryNotFoundException A deprecated exception at this point
65 public final static function createWebLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase = null) {
67 $helperInstance = new WebLinkHelper();
69 // Set template instance
70 $helperInstance->setTemplateInstance($templateInstance);
73 $helperInstance->setLinkName($linkName);
75 // Get the application instance
76 $applicationInstance = Registry::getRegistry()->getInstance('application');
79 if (is_null($applicationInstance)) {
80 // Throw an exception here
81 throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
84 // Get the request instance
85 $requestInstance = $applicationInstance->getRequestInstance();
88 if (is_null($requestInstance)) {
89 // Throw an exception here
90 throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
93 // Get page (this will throw an exception if not set)
94 $page = $helperInstance->convertDashesToUnderscores($requestInstance->getRequestElement('page'));
96 // Construct config entry
97 $configEntry = $page . '_' . $linkName;
99 // Is the deprecated parameter set?
100 if (!is_null($linkBase)) {
101 // Then output a deprecation message
102 $helperInstance->deprecationWarning(__METHOD__ . ': linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
105 // Determine link base from config now and 'page' request
107 $newLinkBase = $helperInstance->getConfigInstance()->getConfigEntry($configEntry);
108 $linkBase = $newLinkBase;
109 } catch (ConfigEntryNotFoundException $e) {
110 // Is the deprecated linkBase not set?
111 if (is_null($linkBase)) {
112 // Then throw again the exception
113 throw new ConfigEntryNotFoundException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
118 $helperInstance->setLinkBase($linkBase);
121 $helperInstance->openGroupByIdContent('main', '', '');
123 // Return the prepared instance
124 return $helperInstance;
128 * Renders the link content (HTML code) with given link text and optional
131 * @param $linkText Link text to set in link
132 * @param $linkTitle Link title to set in link
133 * @param $extraContent Optional extra HTML content
134 * @return $linkContent Rendered text link content
136 private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent='') {
137 // Construct link content
138 $linkContent = sprintf("<a href=\"%s%s\" title=\"%s\">%s</a>",
139 $this->getLinkBase(),
150 * Setter for link name
152 * @param $linkName Name of the link we shall generate
155 protected final function setLinkName ($linkName) {
156 $this->linkName = (string) $linkName;
160 * Getter for link name
162 * @return $linkName Name of the link we shall generate
164 public final function getLinkName () {
165 return $this->linkName;
169 * Setter for link base
171 * @param $linkBase Base of the link we shall generate
174 protected final function setLinkBase ($linkBase) {
175 $this->linkBase = (string) $linkBase;
179 * Getter for link base
181 * @return $linkBase Base of the link we shall generate
183 public final function getLinkBase () {
184 return $this->linkBase;
188 * Flush the content out,e g. to a template variable
191 * @todo Completely unimplemented
193 public function flushContent () {
194 // Is a previous opened group still open?
195 if ($this->ifGroupOpenedPreviously()) {
197 $this->closePreviousGroupByContent('');
201 $content = $this->renderContent();
203 // Get template engine
204 $templateInstance = $this->getTemplateInstance();
206 // Add content to variable
207 $templateInstance->assignVariable($this->getLinkName(), $content);
211 * Adds a link group (like the form group is) with some raw language to the
214 * @param $groupId Id string of the group
215 * @param $groupText Text for this group to add
216 * @param $groupCode Code to open and close groups
219 public function addLinkGroup ($groupId, $groupText, $groupCode = "div") {
220 // Is a group with that name open?
221 if ($this->ifGroupOpenedPreviously()) {
222 // Then close it here
223 $this->closePreviousGroupByContent('');
226 // Generate the group content
227 $content = sprintf("<{$groupCode} id=\"group_%s_%s\">%s",
228 $this->getLinkName(),
233 // Open the new group
234 $this->openGroupByIdContent($groupId, $content, $groupCode);
238 * Adds text (note) to the previously opened group or throws an exception
239 * if no previous group was opened.
241 * @param $groupId Group id to set
242 * @param $groupNote Note to be added to a group
243 * @param $groupCode Code to open and close groups
245 * @throws NoGroupOpenedException If no previous group was opened
247 public function addLinkNote ($groupId, $groupNote, $groupCode = "div") {
248 // Check if a previous group was opened
249 if ($this->ifGroupOpenedPreviously() === false) {
250 // No group was opened before!
251 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
254 // Is a previous sub group open?
255 if ($this->ifSubGroupOpenedPreviously()) {
257 $this->closePreviousSubGroupByContent("</{$groupCode}>");
260 // Generate the group content
261 $content = sprintf("<{$groupCode} id=\"subgroup_%s_%s\">%s",
262 $this->getLinkName(),
267 // Open the sub group
268 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
272 * Adds a link to the previously opened group or throws an exception if no group has been opened
274 * @param $linkAction Action (action=xxx) value for the link
275 * @param $linkText Link text and title (title="xxx") for the link
277 * @throws NoGroupOpenedException If no previous group was opened
279 protected function addActionLink ($linkAction, $linkText, $linkTitle) {
280 // Check if a previous group was opened
281 if ($this->ifGroupOpenedPreviously() === false) {
282 // No group was opened before!
283 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
286 // Default parameter seperator is &
287 $seperator = $this->parameterSeperator;
289 // Is there a question mark in?
290 $linkArray = explode($this->firstParameter, $this->getLinkBase());
291 if (count($linkArray) == 0) {
293 $seperator = $this->firstParameter;
297 $action = sprintf("%saction=%s",
302 // Renders the link content
303 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
305 // Add the content to the previous group
306 $this->addContentToPreviousGroup($linkContent);
310 * Adds a link to the previously opened group with a text from language system
312 * @param $linkAction Action (action=xxx) value for the link
313 * @param $languageId Language id string to use
316 public function addActionLinkById ($linkAction, $languageId) {
317 // Resolve the language string
318 $languageResolvedText = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_text");
320 // Resolve the language string
321 $languageResolvedTitle = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_title");
323 // Add the action link
324 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
328 * Adds a default link (no extra parameters) to the content with specified
329 * language id string.
331 * @param $languageId Language id string to use
334 public function addLinkWithTextById ($languageId) {
335 // Resolve the language string
336 $languageResolvedText = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_text");
338 // Resolve the language string
339 $languageResolvedTitle = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_title");
342 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
344 // Add the content to the previous group
345 $this->addContentToPreviousGroup($linkContent);