Some updates:
[core.git] / framework / main / classes / helper / html / links / class_HtmlLinkHelper.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Helper;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Helper\Template\HelpableTemplate;
10 use Org\Mxchange\CoreFramework\Registry\Registry;
11 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
12
13 /**
14  * A helper for web links
15  *
16  * @author              Roland Haeder <webmaster@shipsimu.org>
17  * @version             0.0.0
18 <<<<<<< HEAD:framework/main/classes/helper/html/links/class_HtmlLinkHelper.php
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 =======
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
22 >>>>>>> Some updates::inc/main/classes/helper/html/links/class_HtmlLinkHelper.php
23  * @license             GNU GPL 3.0 or any newer version
24  * @link                http://www.shipsimu.org
25  *
26  * This program is free software: you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License as published by
28  * the Free Software Foundation, either version 3 of the License, or
29  * (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program. If not, see <http://www.gnu.org/licenses/>.
38  */
39 class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate {
40         /**
41          * Name of the link
42          */
43         private $linkName = '';
44
45         /**
46          * Base of the link
47          */
48         private $linkBase = '';
49
50         /**
51          * First parameter separator
52          */
53         const FIRST_PARAMETER_SEPARATOR = '?';
54
55         /**
56          * SEPARATOR for more paraemters
57          */
58         const EXTRA_PARAMETER_SEPARATOR = '&amp;';
59
60         /**
61          * Protected constructor
62          *
63          * @return      void
64          */
65         protected function __construct () {
66                 // Call parent constructor
67                 parent::__construct(__CLASS__);
68         }
69
70         /**
71          * Creates the helper class
72          *
73          * @param       $templateInstance       An instance of a template engine
74          * @param       $linkName                       Name of the link we shall generate
75          * @param       $linkBase                       Link base for the link. This parameter is deprecated.
76          * @return      $helperInstance         A prepared instance of this helper
77          * @throws      NoConfigEntryException  A deprecated exception at this point
78          */
79         public static final function createHtmlLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase = NULL) {
80                 // Get new instance
81                 $helperInstance = new HtmlLinkHelper();
82
83                 // Set template instance
84                 $helperInstance->setTemplateInstance($templateInstance);
85
86                 // Set link name
87                 $helperInstance->setLinkName($linkName);
88
89                 // Get the application instance
90                 $applicationInstance = Registry::getRegistry()->getInstance('application');
91
92                 // Get the request instance
93                 $requestInstance = FrameworkBootstrap::getRequestInstance();
94
95                 // Sanity-check on it
96                 if (is_null($requestInstance)) {
97                         // Throw an exception here
98                         throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
99                 } // END - if
100
101                 // Get page (this will throw an exception if not set)
102                 $command = $helperInstance->convertDashesToUnderscores($requestInstance->getRequestElement('command'));
103
104                 // Construct config entry
105                 $configEntry = $command . '_' . $linkName . '_action_url';
106
107                 // Is the deprecated parameter set?
108                 if (!is_null($linkBase)) {
109                         // Then output a deprecation message
110                         $helperInstance->deprecationWarning('[' . __METHOD__ . ':' . __LINE__ . ']:  linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
111                 } // END - if
112
113                 // Determine link base from config now and 'command' request
114                 try {
115                         $newLinkBase = $helperInstance->getConfigInstance()->getConfigEntry($configEntry);
116                         $linkBase = $newLinkBase;
117                 } catch (NoConfigEntryException $e) {
118                         // Is the deprecated linkBase not set?
119                         if (is_null($linkBase)) {
120                                 // Then throw again the exception
121                                 throw new NoConfigEntryException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
122                         } // END - if
123                 }
124
125                 // Set link base
126                 $helperInstance->setLinkBase($linkBase);
127
128                 // Add default group
129                 $helperInstance->openGroupByIdContent('main', '', '');
130
131                 // Return the prepared instance
132                 return $helperInstance;
133         }
134
135         /**
136          * Renders the link content (HTML code) with given link text and optional
137          * extra content
138          *
139          * @param       $linkText               Link text to set in link
140          * @param       $linkTitle              Link title to set in link
141          * @param       $extraContent   Optional extra HTML content
142          * @return      $linkContent    Rendered text link content
143          */
144         private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent='') {
145                 // Construct link content
146                 $linkContent = sprintf('<a href="{?base_url?}/%s%s" title="%s">%s</a>',
147                         $this->getLinkBase(),
148                         $extraContent,
149                         $linkTitle,
150                         $linkText
151                 );
152
153                 // Return it
154                 return $linkContent;
155         }
156
157         /**
158          * Setter for link name
159          *
160          * @param       $linkName       Name of the link we shall generate
161          * @return      void
162          */
163         protected final function setLinkName ($linkName) {
164                 $this->linkName = (string) $linkName;
165         }
166
167         /**
168          * Getter for link name
169          *
170          * @return      $linkName       Name of the link we shall generate
171          */
172         public final function getLinkName () {
173                 return $this->linkName;
174         }
175
176         /**
177          * Setter for link base
178          *
179          * @param       $linkBase       Base of the link we shall generate
180          * @return      void
181          */
182         protected final function setLinkBase ($linkBase) {
183                 $this->linkBase = (string) $linkBase;
184         }
185
186         /**
187          * Getter for link base
188          *
189          * @return      $linkBase       Base of the link we shall generate
190          */
191         public final function getLinkBase () {
192                 return $this->linkBase;
193         }
194
195         /**
196          * Flush the content out,e g. to a template variable
197          *
198          * @return      void
199          * @todo        Completely unimplemented
200          */
201         public function flushContent () {
202                 // Is a previous opened group still open?
203                 if ($this->ifGroupOpenedPreviously()) {
204                         // Then close it
205                         $this->closePreviousGroupByContent('');
206                 } // END - if
207
208                 // Get the content
209                 $content = $this->renderContent();
210
211                 // Get template engine
212                 $templateInstance = $this->getTemplateInstance();
213
214                 // Add content to variable
215                 $templateInstance->assignVariable($this->getLinkName(), $content);
216         }
217
218         /**
219          * Adds a link group (like the form group is) with some raw language to the
220          * helper.
221          *
222          * @param       $groupId        Id string of the group
223          * @param       $groupText      Text for this group to add
224          * @param       $groupCode      Code to open and close groups
225          * @return      void
226          */
227         public function addLinkGroup ($groupId, $groupText, $groupCode = 'div') {
228                 // Is a group with that name open?
229                 if ($this->ifGroupOpenedPreviously()) {
230                         // Then close it here
231                         $this->closePreviousGroupByContent('');
232                 } // END - if
233
234                 // Generate the group content
235                 $content = sprintf('<%s id="group_%s_%s">%s',
236                         $groupCode,
237                         $this->getLinkName(),
238                         $groupId,
239                         $groupText
240                 );
241
242                 // Open the new group
243                 $this->openGroupByIdContent($groupId, $content, $groupCode);
244         }
245
246         /**
247          * Adds text (note) to the previously opened group or throws an exception
248          * if no previous group was opened.
249          *
250          * @param       $groupId        Group id to set
251          * @param       $groupNote      Note to be added to a group
252          * @param       $groupCode      Code to open and close groups
253          * @return      void
254          * @throws      NoGroupOpenedException  If no previous group was opened
255          */
256         public function addLinkNote ($groupId, $groupNote, $groupCode = 'div') {
257                 // Check if a previous group was opened
258                 if ($this->ifGroupOpenedPreviously() === false) {
259                         // No group was opened before!
260                         throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
261                 } // END - if
262
263                 // Is a previous sub group open?
264                 if ($this->ifSubGroupOpenedPreviously()) {
265                         // Then close it
266                         $this->closePreviousSubGroupByContent('</' . $groupCode . '>');
267                 } // END - if
268
269                 // Generate the group content
270                 $content = sprintf('<%s id="subgroup_%s_%s">%s',
271                         $groupCode,
272                         $this->getLinkName(),
273                         $groupId,
274                         $groupNote
275                 );
276
277                 // Open the sub group
278                 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
279         }
280
281         /**
282          * Adds a link to the previously opened group or throws an exception if no group has been opened
283          *
284          * @param       $linkAction             Action (action=xxx) value for the link
285          * @param       $linkText               Link text and title (title="xxx") for the link
286          * @return      void
287          * @throws      NoGroupOpenedException  If no previous group was opened
288          */
289         protected function addActionLink ($linkAction, $linkText, $linkTitle) {
290                 // Check if a previous group was opened
291                 if ($this->ifGroupOpenedPreviously() === false) {
292                         // No group was opened before!
293                         throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
294                 } // END - if
295
296                 // Default parameter SEPARATOR is &amp;
297                 $separator = self::EXTRA_PARAMETER_SEPARATOR;
298
299                 // Is there a question mark in?
300                 $linkArray = explode(self::FIRST_PARAMETER_SEPARATOR, $this->getLinkBase());
301                 if (count($linkArray) == 0) {
302                         // No question mark
303                         $separator = self::FIRST_PARAMETER_SEPARATOR;
304                 } // END - if
305
306                 // Prepare action
307                 $action = sprintf('%saction=%s',
308                         $separator,
309                         $linkAction
310                 );
311
312                 // Renders the link content
313                 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
314
315                 // Add the content to the previous group
316                 $this->addContentToPreviousGroup($linkContent);
317         }
318
319         /**
320          * Adds a link to the previously opened group with a text from language system
321          *
322          * @param       $linkAction             Action (action=xxx) value for the link
323          * @param       $languageId             Language id string to use
324          * @return      void
325          */
326         public function addActionLinkById ($linkAction, $languageId) {
327                 // Resolve the language string
328                 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
329
330                 // Resolve the language string
331                 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
332
333                 // Add the action link
334                 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
335         }
336
337         /**
338          * Adds a default link (no extra parameters) to the content with specified
339          * language id string.
340          *
341          * @param       $languageId             Language id string to use
342          * @return      void
343          */
344         public function addLinkWithTextById ($languageId) {
345                 // Resolve the language string
346                 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
347
348                 // Resolve the language string
349                 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
350
351                 // Now add the link
352                 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
353
354                 // Add the content to the previous group
355                 $this->addContentToPreviousGroup($linkContent);
356         }
357
358 }