3 namespace CoreFramework\Helper;
5 // Import framework stuff
6 use CoreFramework\Bootstrap\FrameworkBootstrap;
7 use CoreFramework\Configuration\FrameworkConfiguration;
8 use CoreFramework\Generic\NullPointerException;
9 use CoreFramework\Registry\Registry;
10 use CoreFramework\Template\CompileableTemplate;
13 * A helper for web links
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate {
38 private $linkName = '';
43 private $linkBase = '';
46 * First parameter separator
48 const FIRST_PARAMETER_SEPARATOR = '?';
51 * SEPARATOR for more paraemters
53 const EXTRA_PARAMETER_SEPARATOR = '&';
56 * Protected constructor
60 protected function __construct () {
61 // Call parent constructor
62 parent::__construct(__CLASS__);
66 * Creates the helper class
68 * @param $templateInstance An instance of a template engine
69 * @param $linkName Name of the link we shall generate
70 * @param $linkBase Link base for the link. This parameter is deprecated.
71 * @return $helperInstance A prepared instance of this helper
72 * @throws NoConfigEntryException A deprecated exception at this point
74 public static final function createHtmlLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase = NULL) {
76 $helperInstance = new HtmlLinkHelper();
78 // Set template instance
79 $helperInstance->setTemplateInstance($templateInstance);
82 $helperInstance->setLinkName($linkName);
84 // Get the application instance
85 $applicationInstance = Registry::getRegistry()->getInstance('application');
87 // Get the request instance
88 $requestInstance = FrameworkBootstrap::getRequestInstance();
91 if (is_null($requestInstance)) {
92 // Throw an exception here
93 throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
96 // Get page (this will throw an exception if not set)
97 $command = $helperInstance->convertDashesToUnderscores($requestInstance->getRequestElement('command'));
99 // Construct config entry
100 $configEntry = $command . '_' . $linkName . '_action_url';
102 // Is the deprecated parameter set?
103 if (!is_null($linkBase)) {
104 // Then output a deprecation message
105 $helperInstance->deprecationWarning('[' . __METHOD__ . ':' . __LINE__ . ']: linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
108 // Determine link base from config now and 'command' request
110 $newLinkBase = $helperInstance->getConfigInstance()->getConfigEntry($configEntry);
111 $linkBase = $newLinkBase;
112 } catch (NoConfigEntryException $e) {
113 // Is the deprecated linkBase not set?
114 if (is_null($linkBase)) {
115 // Then throw again the exception
116 throw new NoConfigEntryException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
121 $helperInstance->setLinkBase($linkBase);
124 $helperInstance->openGroupByIdContent('main', '', '');
126 // Return the prepared instance
127 return $helperInstance;
131 * Renders the link content (HTML code) with given link text and optional
134 * @param $linkText Link text to set in link
135 * @param $linkTitle Link title to set in link
136 * @param $extraContent Optional extra HTML content
137 * @return $linkContent Rendered text link content
139 private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent='') {
140 // Construct link content
141 $linkContent = sprintf('<a href="{?base_url?}/%s%s" title="%s">%s</a>',
142 $this->getLinkBase(),
153 * Setter for link name
155 * @param $linkName Name of the link we shall generate
158 protected final function setLinkName ($linkName) {
159 $this->linkName = (string) $linkName;
163 * Getter for link name
165 * @return $linkName Name of the link we shall generate
167 public final function getLinkName () {
168 return $this->linkName;
172 * Setter for link base
174 * @param $linkBase Base of the link we shall generate
177 protected final function setLinkBase ($linkBase) {
178 $this->linkBase = (string) $linkBase;
182 * Getter for link base
184 * @return $linkBase Base of the link we shall generate
186 public final function getLinkBase () {
187 return $this->linkBase;
191 * Flush the content out,e g. to a template variable
194 * @todo Completely unimplemented
196 public function flushContent () {
197 // Is a previous opened group still open?
198 if ($this->ifGroupOpenedPreviously()) {
200 $this->closePreviousGroupByContent('');
204 $content = $this->renderContent();
206 // Get template engine
207 $templateInstance = $this->getTemplateInstance();
209 // Add content to variable
210 $templateInstance->assignVariable($this->getLinkName(), $content);
214 * Adds a link group (like the form group is) with some raw language to the
217 * @param $groupId Id string of the group
218 * @param $groupText Text for this group to add
219 * @param $groupCode Code to open and close groups
222 public function addLinkGroup ($groupId, $groupText, $groupCode = 'div') {
223 // Is a group with that name open?
224 if ($this->ifGroupOpenedPreviously()) {
225 // Then close it here
226 $this->closePreviousGroupByContent('');
229 // Generate the group content
230 $content = sprintf('<%s id="group_%s_%s">%s',
232 $this->getLinkName(),
237 // Open the new group
238 $this->openGroupByIdContent($groupId, $content, $groupCode);
242 * Adds text (note) to the previously opened group or throws an exception
243 * if no previous group was opened.
245 * @param $groupId Group id to set
246 * @param $groupNote Note to be added to a group
247 * @param $groupCode Code to open and close groups
249 * @throws NoGroupOpenedException If no previous group was opened
251 public function addLinkNote ($groupId, $groupNote, $groupCode = 'div') {
252 // Check if a previous group was opened
253 if ($this->ifGroupOpenedPreviously() === false) {
254 // No group was opened before!
255 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
258 // Is a previous sub group open?
259 if ($this->ifSubGroupOpenedPreviously()) {
261 $this->closePreviousSubGroupByContent('</' . $groupCode . '>');
264 // Generate the group content
265 $content = sprintf('<%s id="subgroup_%s_%s">%s',
267 $this->getLinkName(),
272 // Open the sub group
273 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
277 * Adds a link to the previously opened group or throws an exception if no group has been opened
279 * @param $linkAction Action (action=xxx) value for the link
280 * @param $linkText Link text and title (title="xxx") for the link
282 * @throws NoGroupOpenedException If no previous group was opened
284 protected function addActionLink ($linkAction, $linkText, $linkTitle) {
285 // Check if a previous group was opened
286 if ($this->ifGroupOpenedPreviously() === false) {
287 // No group was opened before!
288 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
291 // Default parameter SEPARATOR is &
292 $separator = self::EXTRA_PARAMETER_SEPARATOR;
294 // Is there a question mark in?
295 $linkArray = explode(self::FIRST_PARAMETER_SEPARATOR, $this->getLinkBase());
296 if (count($linkArray) == 0) {
298 $separator = self::FIRST_PARAMETER_SEPARATOR;
302 $action = sprintf('%saction=%s',
307 // Renders the link content
308 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
310 // Add the content to the previous group
311 $this->addContentToPreviousGroup($linkContent);
315 * Adds a link to the previously opened group with a text from language system
317 * @param $linkAction Action (action=xxx) value for the link
318 * @param $languageId Language id string to use
321 public function addActionLinkById ($linkAction, $languageId) {
322 // Resolve the language string
323 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
325 // Resolve the language string
326 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
328 // Add the action link
329 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
333 * Adds a default link (no extra parameters) to the content with specified
334 * language id string.
336 * @param $languageId Language id string to use
339 public function addLinkWithTextById ($languageId) {
340 // Resolve the language string
341 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
343 // Resolve the language string
344 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
347 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
349 // Add the content to the previous group
350 $this->addContentToPreviousGroup($linkContent);