3 * A helper for web links
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 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 * Protected constructor
40 protected function __construct () {
41 // Call parent constructor
42 parent::__construct(__CLASS__);
46 * Creates the helper class
48 * @param $templateInstance An instance of a template engine
49 * @param $linkName Name of the link we shall generate
50 * @param $linkBase Link base for all generated links
51 * @return $helperInstance A prepared instance of this helper
53 public final static function createWebLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase) {
55 $helperInstance = new WebLinkHelper();
57 // Set template instance
58 $helperInstance->setTemplateInstance($templateInstance);
61 $helperInstance->setLinkName($linkName);
64 $helperInstance->setLinkBase($linkBase);
67 $helperInstance->openGroupByIdContent('main', "", "");
69 // Return the prepared instance
70 return $helperInstance;
74 * Renders the link content (HTML code) with given link text and optional
77 * @param $linkText Link text to set in link
78 * @param $extraContent Optional extra HTML content
79 * @return $linkContent Rendered text link content
81 private function renderLinkContentWithTextExtraContent ($linkText, $extraContent="") {
82 // Construct link content
83 $linkContent = sprintf("<a href=\"%s%s\" title=\"%s\">%s</a>",
95 * Setter for link name
97 * @param $linkName Name of the link we shall generate
100 protected final function setLinkName ($linkName) {
101 $this->linkName = (string) $linkName;
105 * Getter for link name
107 * @return $linkName Name of the link we shall generate
109 public final function getLinkName () {
110 return $this->linkName;
114 * Setter for link base
116 * @param $linkBase Base of the link we shall generate
119 protected final function setLinkBase ($linkBase) {
120 $this->linkBase = (string) $linkBase;
124 * Getter for link base
126 * @return $linkBase Base of the link we shall generate
128 public final function getLinkBase () {
129 return $this->linkBase;
133 * Flush the content out,e g. to a template variable
136 * @todo Completely unimplemented
138 public function flushContent () {
139 // Is a previous opened group still open?
140 if ($this->ifGroupOpenedPreviously()) {
142 $this->closePreviousGroupByContent("");
146 $content = $this->renderContent();
148 // Get template engine
149 $templateInstance = $this->getTemplateInstance();
151 // Add content to variable
152 $templateInstance->assignVariable($this->getLinkName(), $content);
156 * Adds a link group (like the form group is) with some raw language to the
159 * @param $groupId Id string of the group
160 * @param $groupText Text for this group to add
161 * @param $groupCode Code to open and close groups
164 public function addLinkGroup ($groupId, $groupText, $groupCode = "div") {
165 // Is a group with that name open?
166 if ($this->ifGroupOpenedPreviously()) {
167 // Then close it here
168 $this->closePreviousGroupByContent("");
171 // Generate the group content
172 $content = sprintf("<{$groupCode} id=\"group_%s_%s\">%s",
173 $this->getLinkName(),
178 // Open the new group
179 $this->openGroupByIdContent($groupId, $content, $groupCode);
183 * Adds text (note) to the previously opened group or throws an exception
184 * if no previous group was opened.
186 * @param $groupId Group id to set
187 * @param $groupNote Note to be added to a group
188 * @param $groupCode Code to open and close groups
190 * @throws NoGroupOpenedException If no previous group was opened
192 public function addLinkNote ($groupId, $groupNote, $groupCode = "div") {
193 // Check if a previous group was opened
194 if ($this->ifGroupOpenedPreviously() === false) {
195 // No group was opened before!
196 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
199 // Is a previous sub group open?
200 if ($this->ifSubGroupOpenedPreviously()) {
202 $this->closePreviousSubGroupByContent("</{$groupCode}>");
205 // Generate the group content
206 $content = sprintf("<{$groupCode} id=\"subgroup_%s_%s\">%s",
207 $this->getLinkName(),
212 // Open the sub group
213 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
217 * Adds a link to the previously opened group or throws an exception if no group has been opened
219 * @param $linkAction Action (action=xxx) value for the link
220 * @param $linkText Link text and title (title="xxx") for the link
222 * @throws NoGroupOpenedException If no previous group was opened
224 public function addActionLink ($linkAction, $linkText) {
225 // Check if a previous group was opened
226 if ($this->ifGroupOpenedPreviously() === false) {
227 // No group was opened before!
228 throw new NoGroupOpenedException(array($this, $linkAction."(".$linkText.")"), self::EXCEPTION_GROUP_NOT_OPENED);
231 // Default parameter seperator is &
232 $seperator = "&";
234 // Is there a question mark in?
235 $linkArray = explode("?", $this->getLinkBase());
236 if (count($linkArray) == 0) {
242 $action = sprintf("%saction=%s",
247 // Renders the link content
248 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $action);
250 // Add the content to the previous group
251 $this->addContentToPreviousGroup($linkContent);
255 * Adds a link to the previously opened group with a text from language system
257 * @param $linkAction Action (action=xxx) value for the link
258 * @param $languageId Language id string to use
261 public function addActionLinkById ($linkAction, $languageId) {
262 // Resolve the language string
263 $languageResolved = $this->getLanguageInstance()->getMessage($languageId);
265 // Add the action link
266 $this->addActionLink($linkAction, $languageResolved);
270 * Adds a default link (no extra parameters) to the content with specified
271 * language id string.
273 * @param $languageId Language id string to use
276 public function addLinkWithTextById ($languageId) {
277 // Resolve the language string
278 $languageResolved = $this->getLanguageInstance()->getMessage($languageId);
281 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolved);
283 // Add the content to the previous group
284 $this->addContentToPreviousGroup($linkContent);