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