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