Now protected method
[core.git] / inc / classes / main / helper / web / links / class_WebLinkHelper.php
1 <?php
2 /**
3  * A helper for web links
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class WebLinkHelper extends BaseWebHelper implements HelpableTemplate {
25         /**
26          * Name of the link
27          */
28         private $linkName = "";
29
30         /**
31          * Base of the link
32          */
33         private $linkBase = "";
34
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         protected function __construct () {
41                 // Call parent constructor
42                 parent::__construct(__CLASS__);
43         }
44
45         /**
46          * Creates the helper class
47          *
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
52          */
53         public final static function createWebLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase) {
54                 // Get new instance
55                 $helperInstance = new WebLinkHelper();
56
57                 // Set template instance
58                 $helperInstance->setTemplateInstance($templateInstance);
59
60                 // Set link name
61                 $helperInstance->setLinkName($linkName);
62
63                 // Set link base
64                 $helperInstance->setLinkBase($linkBase);
65
66                 // Add default group
67                 $helperInstance->openGroupByIdContent('main', "", "");
68
69                 // Return the prepared instance
70                 return $helperInstance;
71         }
72
73         /**
74          * Renders the link content (HTML code) with given link text and optional
75          * extra content
76          *
77          * @param       $linkText               Link text to set in link
78          * @param       $linkTitle              Link title to set in link
79          * @param       $extraContent   Optional extra HTML content
80          * @return      $linkContent    Rendered text link content
81          */
82         private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent="") {
83                 // Construct link content
84                 $linkContent = sprintf("<a href=\"%s%s\" title=\"%s\">%s</a>",
85                         $this->getLinkBase(),
86                         $extraContent,
87                         $linkTitle,
88                         $linkText
89                 );
90
91                 // Return it
92                 return $linkContent;
93         }
94
95         /**
96          * Setter for link name
97          *
98          * @param       $linkName       Name of the link we shall generate
99          * @return      void
100          */
101         protected final function setLinkName ($linkName) {
102                 $this->linkName = (string) $linkName;
103         }
104
105         /**
106          * Getter for link name
107          *
108          * @return      $linkName       Name of the link we shall generate
109          */
110         public final function getLinkName () {
111                 return $this->linkName;
112         }
113
114         /**
115          * Setter for link base
116          *
117          * @param       $linkBase       Base of the link we shall generate
118          * @return      void
119          */
120         protected final function setLinkBase ($linkBase) {
121                 $this->linkBase = (string) $linkBase;
122         }
123
124         /**
125          * Getter for link base
126          *
127          * @return      $linkBase       Base of the link we shall generate
128          */
129         public final function getLinkBase () {
130                 return $this->linkBase;
131         }
132
133         /**
134          * Flush the content out,e g. to a template variable
135          *
136          * @return      void
137          * @todo        Completely unimplemented
138          */
139         public function flushContent () {
140                 // Is a previous opened group still open?
141                 if ($this->ifGroupOpenedPreviously()) {
142                         // Then close it
143                         $this->closePreviousGroupByContent("");
144                 } // END - if
145
146                 // Get the content
147                 $content = $this->renderContent();
148
149                 // Get template engine
150                 $templateInstance = $this->getTemplateInstance();
151
152                 // Add content to variable
153                 $templateInstance->assignVariable($this->getLinkName(), $content);
154         }
155
156         /**
157          * Adds a link group (like the form group is) with some raw language to the
158          * helper.
159          *
160          * @param       $groupId        Id string of the group
161          * @param       $groupText      Text for this group to add
162          * @param       $groupCode      Code to open and close groups
163          * @return      void
164          */
165         public function addLinkGroup ($groupId, $groupText, $groupCode = "div") {
166                 // Is a group with that name open?
167                 if ($this->ifGroupOpenedPreviously()) {
168                         // Then close it here
169                         $this->closePreviousGroupByContent("");
170                 } // END - if
171
172                 // Generate the group content
173                 $content = sprintf("<{$groupCode} id=\"group_%s_%s\">%s",
174                         $this->getLinkName(),
175                         $groupId,
176                         $groupText
177                 );
178
179                 // Open the new group
180                 $this->openGroupByIdContent($groupId, $content, $groupCode);
181         }
182
183         /**
184          * Adds text (note) to the previously opened group or throws an exception
185          * if no previous group was opened.
186          *
187          * @param       $groupId        Group id to set
188          * @param       $groupNote      Note to be added to a group
189          * @param       $groupCode      Code to open and close groups
190          * @return      void
191          * @throws      NoGroupOpenedException  If no previous group was opened
192          */
193         public function addLinkNote ($groupId, $groupNote, $groupCode = "div") {
194                 // Check if a previous group was opened
195                 if ($this->ifGroupOpenedPreviously() === false) {
196                         // No group was opened before!
197                         throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
198                 } // END - if
199
200                 // Is a previous sub group open?
201                 if ($this->ifSubGroupOpenedPreviously()) {
202                         // Then close it
203                         $this->closePreviousSubGroupByContent("</{$groupCode}>");
204                 } // END - if
205
206                 // Generate the group content
207                 $content = sprintf("<{$groupCode} id=\"subgroup_%s_%s\">%s",
208                         $this->getLinkName(),
209                         $groupId,
210                         $groupNote
211                 );
212
213                 // Open the sub group
214                 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
215         }
216
217         /**
218          * Adds a link to the previously opened group or throws an exception if no group has been opened
219          *
220          * @param       $linkAction             Action (action=xxx) value for the link
221          * @param       $linkText               Link text and title (title="xxx") for the link
222          * @return      void
223          * @throws      NoGroupOpenedException  If no previous group was opened
224          */
225         protected function addActionLink ($linkAction, $linkText, $linkTitle) {
226                 // Check if a previous group was opened
227                 if ($this->ifGroupOpenedPreviously() === false) {
228                         // No group was opened before!
229                         throw new NoGroupOpenedException(array($this, $linkAction."(".$linkText.")"), self::EXCEPTION_GROUP_NOT_OPENED);
230                 } // END - if
231
232                 // Default parameter seperator is &amp;
233                 $seperator = "&amp;";
234
235                 // Is there a question mark in?
236                 $linkArray = explode("?", $this->getLinkBase());
237                 if (count($linkArray) == 0) {
238                         // No question mark
239                         $seperator = "?";
240                 } // END - if
241
242                 // Prepare action
243                 $action = sprintf("%saction=%s",
244                         $seperator,
245                         $linkAction
246                 );
247
248                 // Renders the link content
249                 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
250
251                 // Add the content to the previous group
252                 $this->addContentToPreviousGroup($linkContent);
253         }
254
255         /**
256          * Adds a link to the previously opened group with a text from language system
257          *
258          * @param       $linkAction             Action (action=xxx) value for the link
259          * @param       $languageId             Language id string to use
260          * @return      void
261          */
262         public function addActionLinkById ($linkAction, $languageId) {
263                 // Resolve the language string
264                 $languageResolvedText = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_text");
265
266                 // Resolve the language string
267                 $languageResolvedTitle = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_title");
268
269                 // Add the action link
270                 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
271         }
272
273         /**
274          * Adds a default link (no extra parameters) to the content with specified
275          * language id string.
276          *
277          * @param       $languageId             Language id string to use
278          * @return      void
279          */
280         public function addLinkWithTextById ($languageId) {
281                 // Resolve the language string
282                 $languageResolvedText = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_text");
283
284                 // Resolve the language string
285                 $languageResolvedTitle = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_title");
286
287                 // Now add the link
288                 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
289
290                 // Add the content to the previous group
291                 $this->addContentToPreviousGroup($linkContent);
292         }
293 }
294
295 // [EOF]
296 ?>