Rewritten a lot double-quotes to single-requotes, removed deprecated exception, some...
[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 - 2009 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          * First parameter seperator
37          */
38         private $firstParameter = '?';
39
40         /**
41          * Seperator for more paraemters
42          */
43         private $parameterSeperator = '&amp;';
44
45         /**
46          * Protected constructor
47          *
48          * @return      void
49          */
50         protected function __construct () {
51                 // Call parent constructor
52                 parent::__construct(__CLASS__);
53         }
54
55         /**
56          * Creates the helper class
57          *
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
62          */
63         public final static function createWebLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase) {
64                 // Get new instance
65                 $helperInstance = new WebLinkHelper();
66
67                 // Set template instance
68                 $helperInstance->setTemplateInstance($templateInstance);
69
70                 // Set link name
71                 $helperInstance->setLinkName($linkName);
72
73                 // Set link base
74                 $helperInstance->setLinkBase($linkBase);
75
76                 // Add default group
77                 $helperInstance->openGroupByIdContent('main', '', '');
78
79                 // Return the prepared instance
80                 return $helperInstance;
81         }
82
83         /**
84          * Renders the link content (HTML code) with given link text and optional
85          * extra content
86          *
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
91          */
92         private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent='') {
93                 // Construct link content
94                 $linkContent = sprintf("<a href=\"%s%s\" title=\"%s\">%s</a>",
95                         $this->getLinkBase(),
96                         $extraContent,
97                         $linkTitle,
98                         $linkText
99                 );
100
101                 // Return it
102                 return $linkContent;
103         }
104
105         /**
106          * Setter for link name
107          *
108          * @param       $linkName       Name of the link we shall generate
109          * @return      void
110          */
111         protected final function setLinkName ($linkName) {
112                 $this->linkName = (string) $linkName;
113         }
114
115         /**
116          * Getter for link name
117          *
118          * @return      $linkName       Name of the link we shall generate
119          */
120         public final function getLinkName () {
121                 return $this->linkName;
122         }
123
124         /**
125          * Setter for link base
126          *
127          * @param       $linkBase       Base of the link we shall generate
128          * @return      void
129          */
130         protected final function setLinkBase ($linkBase) {
131                 $this->linkBase = (string) $linkBase;
132         }
133
134         /**
135          * Getter for link base
136          *
137          * @return      $linkBase       Base of the link we shall generate
138          */
139         public final function getLinkBase () {
140                 return $this->linkBase;
141         }
142
143         /**
144          * Flush the content out,e g. to a template variable
145          *
146          * @return      void
147          * @todo        Completely unimplemented
148          */
149         public function flushContent () {
150                 // Is a previous opened group still open?
151                 if ($this->ifGroupOpenedPreviously()) {
152                         // Then close it
153                         $this->closePreviousGroupByContent('');
154                 } // END - if
155
156                 // Get the content
157                 $content = $this->renderContent();
158
159                 // Get template engine
160                 $templateInstance = $this->getTemplateInstance();
161
162                 // Add content to variable
163                 $templateInstance->assignVariable($this->getLinkName(), $content);
164         }
165
166         /**
167          * Adds a link group (like the form group is) with some raw language to the
168          * helper.
169          *
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
173          * @return      void
174          */
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('');
180                 } // END - if
181
182                 // Generate the group content
183                 $content = sprintf("<{$groupCode} id=\"group_%s_%s\">%s",
184                         $this->getLinkName(),
185                         $groupId,
186                         $groupText
187                 );
188
189                 // Open the new group
190                 $this->openGroupByIdContent($groupId, $content, $groupCode);
191         }
192
193         /**
194          * Adds text (note) to the previously opened group or throws an exception
195          * if no previous group was opened.
196          *
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
200          * @return      void
201          * @throws      NoGroupOpenedException  If no previous group was opened
202          */
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);
208                 } // END - if
209
210                 // Is a previous sub group open?
211                 if ($this->ifSubGroupOpenedPreviously()) {
212                         // Then close it
213                         $this->closePreviousSubGroupByContent("</{$groupCode}>");
214                 } // END - if
215
216                 // Generate the group content
217                 $content = sprintf("<{$groupCode} id=\"subgroup_%s_%s\">%s",
218                         $this->getLinkName(),
219                         $groupId,
220                         $groupNote
221                 );
222
223                 // Open the sub group
224                 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
225         }
226
227         /**
228          * Adds a link to the previously opened group or throws an exception if no group has been opened
229          *
230          * @param       $linkAction             Action (action=xxx) value for the link
231          * @param       $linkText               Link text and title (title="xxx") for the link
232          * @return      void
233          * @throws      NoGroupOpenedException  If no previous group was opened
234          */
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);
240                 } // END - if
241
242                 // Default parameter seperator is &amp;
243                 $seperator = $this->parameterSeperator;
244
245                 // Is there a question mark in?
246                 $linkArray = explode($this->firstParameter, $this->getLinkBase());
247                 if (count($linkArray) == 0) {
248                         // No question mark
249                         $seperator = $this->firstParameter;
250                 } // END - if
251
252                 // Prepare action
253                 $action = sprintf("%saction=%s",
254                         $seperator,
255                         $linkAction
256                 );
257
258                 // Renders the link content
259                 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
260
261                 // Add the content to the previous group
262                 $this->addContentToPreviousGroup($linkContent);
263         }
264
265         /**
266          * Adds a link to the previously opened group with a text from language system
267          *
268          * @param       $linkAction             Action (action=xxx) value for the link
269          * @param       $languageId             Language id string to use
270          * @return      void
271          */
272         public function addActionLinkById ($linkAction, $languageId) {
273                 // Resolve the language string
274                 $languageResolvedText = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_text");
275
276                 // Resolve the language string
277                 $languageResolvedTitle = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_title");
278
279                 // Add the action link
280                 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
281         }
282
283         /**
284          * Adds a default link (no extra parameters) to the content with specified
285          * language id string.
286          *
287          * @param       $languageId             Language id string to use
288          * @return      void
289          */
290         public function addLinkWithTextById ($languageId) {
291                 // Resolve the language string
292                 $languageResolvedText = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_text");
293
294                 // Resolve the language string
295                 $languageResolvedTitle = $this->getLanguageInstance()->getMessage("link_" . $languageId . "_title");
296
297                 // Now add the link
298                 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
299
300                 // Add the content to the previous group
301                 $this->addContentToPreviousGroup($linkContent);
302         }
303 }
304
305 // [EOF]
306 ?>