A lot naming conventions applied:
[shipsimu.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       $extraContent   Optional extra HTML content
79          * @return      $linkContent    Rendered text link content
80          */
81         private function renderLinkContentWithTextExtraContent ($linkText, $extraContent="") {
82                 // Construct link content
83                 $linkContent = sprintf("<a href=\"%s%s\" title=\"%s\">%s</a>",
84                         $this->getLinkBase(),
85                         $extraContent,
86                         $linkText,
87                         $linkText
88                 );
89
90                 // Return it
91                 return $linkContent;
92         }
93
94         /**
95          * Setter for link name
96          *
97          * @param       $linkName       Name of the link we shall generate
98          * @return      void
99          */
100         protected final function setLinkName ($linkName) {
101                 $this->linkName = (string) $linkName;
102         }
103
104         /**
105          * Getter for link name
106          *
107          * @return      $linkName       Name of the link we shall generate
108          */
109         public final function getLinkName () {
110                 return $this->linkName;
111         }
112
113         /**
114          * Setter for link base
115          *
116          * @param       $linkBase       Base of the link we shall generate
117          * @return      void
118          */
119         protected final function setLinkBase ($linkBase) {
120                 $this->linkBase = (string) $linkBase;
121         }
122
123         /**
124          * Getter for link base
125          *
126          * @return      $linkBase       Base of the link we shall generate
127          */
128         public final function getLinkBase () {
129                 return $this->linkBase;
130         }
131
132         /**
133          * Flush the content out,e g. to a template variable
134          *
135          * @return      void
136          * @todo        Completely unimplemented
137          */
138         public function flushContent () {
139                 // Is a previous opened group still open?
140                 if ($this->ifGroupOpenedPreviously()) {
141                         // Then close it
142                         $this->closePreviousGroupByContent("</div>");
143                 } // END - if
144
145                 // Get the content
146                 $content = $this->renderContent();
147
148                 // Get template engine
149                 $templateInstance = $this->getTemplateInstance();
150
151                 // Add content to variable
152                 $templateInstance->assignVariable($this->getLinkName(), $content);
153         }
154
155         /**
156          * Adds a link group (like the form group is) with some raw language to the
157          * helper.
158          *
159          * @param       $groupId        Id string of the group
160          * @param       $groupText      Text for this group to add
161          * @return      void
162          */
163         public function addLinkGroup ($groupId, $groupText) {
164                 // Is a group with that name open?
165                 if ($this->ifGroupIsOpened($groupId)) {
166                         // Then close it here
167                         $this->closePreviousGroupByContent("</div>");
168                 } else {
169                         // Is a previous opened group still open?
170                         if ($this->ifGroupOpenedPreviously()) {
171                                 // Then close it
172                                 $this->closePreviousGroupByContent("</div>");
173                         } // END - if
174
175                         // Generate the group content
176                         $content = sprintf("<div id=\"group_%s_%s\">%s",
177                                 $this->getLinkName(),
178                                 $groupId,
179                                 $groupText
180                         );
181
182                         // Open the new group
183                         $this->openGroupByIdContent($groupId, $content);
184                 }
185         }
186
187         /**
188          * Adds text (note) to the previously opened group or throws an exception
189          * if no previous group was opened.
190          *
191          * @param       $groupNote      Note to be added to a group
192          * @return      void
193          * @throws      NoGroupOpenedException  If no previous group was opened
194          */
195         public function addLinkNote ($groupNote) {
196                 // Check if a previous group was opened
197                 if (!$this->ifGroupOpenedPreviously()) {
198                         // No group was opened before!
199                         throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
200                 } // END - if
201
202                 // Add the content to the previous group
203                 $this->addContentToPreviousGroup($groupNote);
204         }
205
206         /**
207          * Adds a link to the previously opened group or throws an exception if no group has been opened
208          *
209          * @param       $linkAction             Action (action=xxx) value for the link
210          * @param       $linkText               Link text and title (title="xxx") for the link
211          * @return      void
212          * @throws      NoGroupOpenedException  If no previous group was opened
213          */
214         public function addActionLink ($linkAction, $linkText) {
215                 // Check if a previous group was opened
216                 if (!$this->ifGroupOpenedPreviously()) {
217                         // No group was opened before!
218                         throw new NoGroupOpenedException(array($this, $linkAction."(".$linkText.")"), self::EXCEPTION_GROUP_NOT_OPENED);
219                 } // END - if
220
221                 // Default parameter seperator is &amp;
222                 $seperator = "&amp;";
223
224                 // Is there a question mark in?
225                 $linkArray = explode("?", $this->getLinkBase());
226                 if (count($linkArray) == 0) {
227                         // No question mark
228                         $seperator = "?";
229                 } // END - if
230
231                 // Prepare action
232                 $action = sprintf("%saction=%s",
233                         $seperator,
234                         $linkAction
235                 );
236
237                 // Renders the link content
238                 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $action);
239
240                 // Add the content to the previous group
241                 $this->addContentToPreviousGroup($linkContent);
242         }
243
244         /**
245          * Adds a default link (no extra parameters) to the content with specified
246          * language id string.
247          *
248          * @param       $languageId             Language id string to use
249          * @return      void
250          */
251         public function addLinkWithTextById ($languageId) {
252                 // Resolve the language string
253                 $languageResolved = $this->getLanguageInstance()->getMessage($languageId);
254
255                 // Now add the link
256                 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolved);
257
258                 // Add the content to the previous group
259                 $this->addContentToPreviousGroup($linkContent);
260         }
261 }
262
263 // [EOF]
264 ?>