3 * A helper for web links
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007 - 2009 Roland Haeder, this is free software
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 all generated links
61 * @return $helperInstance A prepared instance of this helper
63 public final static function createWebLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase) {
65 $helperInstance = new WebLinkHelper();
67 // Set template instance
68 $helperInstance->setTemplateInstance($templateInstance);
71 $helperInstance->setLinkName($linkName);
74 $helperInstance->setLinkBase($linkBase);
77 $helperInstance->openGroupByIdContent('main', '', '');
79 // Return the prepared instance
80 return $helperInstance;
84 * Renders the link content (HTML code) with given link text and optional
87 * @param $linkText Link text to set in link
88 * @param $linkTitle Link title to set in link
89 * @param $extraContent Optional extra HTML content
90 * @return $linkContent Rendered text link content
92 private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent='') {
93 // Construct link content
94 $linkContent = sprintf("<a href=\"%s%s\" title=\"%s\">%s</a>",
106 * Setter for link name
108 * @param $linkName Name of the link we shall generate
111 protected final function setLinkName ($linkName) {
112 $this->linkName = (string) $linkName;
116 * Getter for link name
118 * @return $linkName Name of the link we shall generate
120 public final function getLinkName () {
121 return $this->linkName;
125 * Setter for link base
127 * @param $linkBase Base of the link we shall generate
130 protected final function setLinkBase ($linkBase) {
131 $this->linkBase = (string) $linkBase;
135 * Getter for link base
137 * @return $linkBase Base of the link we shall generate
139 public final function getLinkBase () {
140 return $this->linkBase;
144 * Flush the content out,e g. to a template variable
147 * @todo Completely unimplemented
149 public function flushContent () {
150 // Is a previous opened group still open?
151 if ($this->ifGroupOpenedPreviously()) {
153 $this->closePreviousGroupByContent('');
157 $content = $this->renderContent();
159 // Get template engine
160 $templateInstance = $this->getTemplateInstance();
162 // Add content to variable
163 $templateInstance->assignVariable($this->getLinkName(), $content);
167 * Adds a link group (like the form group is) with some raw language to the
170 * @param $groupId Id string of the group
171 * @param $groupText Text for this group to add
172 * @param $groupCode Code to open and close groups
175 public function addLinkGroup ($groupId, $groupText, $groupCode = "div") {
176 // Is a group with that name open?
177 if ($this->ifGroupOpenedPreviously()) {
178 // Then close it here
179 $this->closePreviousGroupByContent('');
182 // Generate the group content
183 $content = sprintf("<{$groupCode} id=\"group_%s_%s\">%s",
184 $this->getLinkName(),
189 // Open the new group
190 $this->openGroupByIdContent($groupId, $content, $groupCode);
194 * Adds text (note) to the previously opened group or throws an exception
195 * if no previous group was opened.
197 * @param $groupId Group id to set
198 * @param $groupNote Note to be added to a group
199 * @param $groupCode Code to open and close groups
201 * @throws NoGroupOpenedException If no previous group was opened
203 public function addLinkNote ($groupId, $groupNote, $groupCode = "div") {
204 // Check if a previous group was opened
205 if ($this->ifGroupOpenedPreviously() === false) {
206 // No group was opened before!
207 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
210 // Is a previous sub group open?
211 if ($this->ifSubGroupOpenedPreviously()) {
213 $this->closePreviousSubGroupByContent("</{$groupCode}>");
216 // Generate the group content
217 $content = sprintf("<{$groupCode} id=\"subgroup_%s_%s\">%s",
218 $this->getLinkName(),
223 // Open the sub group
224 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
228 * Adds a link to the previously opened group or throws an exception if no group has been opened
230 * @param $linkAction Action (action=xxx) value for the link
231 * @param $linkText Link text and title (title="xxx") for the link
233 * @throws NoGroupOpenedException If no previous group was opened
235 protected function addActionLink ($linkAction, $linkText, $linkTitle) {
236 // Check if a previous group was opened
237 if ($this->ifGroupOpenedPreviously() === false) {
238 // No group was opened before!
239 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
242 // Default parameter seperator is &
243 $seperator = $this->parameterSeperator;
245 // Is there a question mark in?
246 $linkArray = explode($this->firstParameter, $this->getLinkBase());
247 if (count($linkArray) == 0) {
249 $seperator = $this->firstParameter;
253 $action = sprintf("%saction=%s",
258 // Renders the link content
259 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
261 // Add the content to the previous group
262 $this->addContentToPreviousGroup($linkContent);
266 * Adds a link to the previously opened group with a text from language system
268 * @param $linkAction Action (action=xxx) value for the link
269 * @param $languageId Language id string to use
272 public function addActionLinkById ($linkAction, $languageId) {
273 // Resolve the language string
274 $languageResolvedText = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_text");
276 // Resolve the language string
277 $languageResolvedTitle = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_title");
279 // Add the action link
280 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
284 * Adds a default link (no extra parameters) to the content with specified
285 * language id string.
287 * @param $languageId Language id string to use
290 public function addLinkWithTextById ($languageId) {
291 // Resolve the language string
292 $languageResolvedText = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_text");
294 // Resolve the language string
295 $languageResolvedTitle = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_title");
298 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
300 // Add the content to the previous group
301 $this->addContentToPreviousGroup($linkContent);