3 namespace CoreFramework\Helper;
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\Template\CompileableTemplate;
10 * A helper for web links
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate {
35 private $linkName = '';
40 private $linkBase = '';
43 * First parameter separator
45 const FIRST_PARAMETER_SEPARATOR = '?';
48 * SEPARATOR for more paraemters
50 const EXTRA_PARAMETER_SEPARATOR = '&';
53 * Protected constructor
57 protected function __construct () {
58 // Call parent constructor
59 parent::__construct(__CLASS__);
63 * Creates the helper class
65 * @param $templateInstance An instance of a template engine
66 * @param $linkName Name of the link we shall generate
67 * @param $linkBase Link base for the link. This parameter is deprecated.
68 * @return $helperInstance A prepared instance of this helper
69 * @throws NoConfigEntryException A deprecated exception at this point
71 public static final function createHtmlLinkHelper (CompileableTemplate $templateInstance, $linkName, $linkBase = NULL) {
73 $helperInstance = new HtmlLinkHelper();
75 // Set template instance
76 $helperInstance->setTemplateInstance($templateInstance);
79 $helperInstance->setLinkName($linkName);
81 // Get the application instance
82 $applicationInstance = Registry::getRegistry()->getInstance('application');
84 // Get the request instance
85 $requestInstance = $applicationInstance->getRequestInstance();
88 if (is_null($requestInstance)) {
89 // Throw an exception here
90 throw new NullPointerException($helperInstance, self::EXCEPTION_IS_NULL_POINTER);
93 // Get page (this will throw an exception if not set)
94 $command = $helperInstance->convertDashesToUnderscores($requestInstance->getRequestElement('command'));
96 // Construct config entry
97 $configEntry = $command . '_' . $linkName . '_action_url';
99 // Is the deprecated parameter set?
100 if (!is_null($linkBase)) {
101 // Then output a deprecation message
102 $helperInstance->deprecationWarning('[' . __METHOD__ . ':' . __LINE__ . ']: linkBase is deprecated. Please remove it from your templates and add a config entry ' . $configEntry . ' in your config.php file.');
105 // Determine link base from config now and 'command' request
107 $newLinkBase = $helperInstance->getConfigInstance()->getConfigEntry($configEntry);
108 $linkBase = $newLinkBase;
109 } catch (NoConfigEntryException $e) {
110 // Is the deprecated linkBase not set?
111 if (is_null($linkBase)) {
112 // Then throw again the exception
113 throw new NoConfigEntryException(array(__CLASS__, ($configEntry)), FrameworkConfiguration::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
118 $helperInstance->setLinkBase($linkBase);
121 $helperInstance->openGroupByIdContent('main', '', '');
123 // Return the prepared instance
124 return $helperInstance;
128 * Renders the link content (HTML code) with given link text and optional
131 * @param $linkText Link text to set in link
132 * @param $linkTitle Link title to set in link
133 * @param $extraContent Optional extra HTML content
134 * @return $linkContent Rendered text link content
136 private function renderLinkContentWithTextExtraContent ($linkText, $linkTitle, $extraContent='') {
137 // Construct link content
138 $linkContent = sprintf('<a href="{?base_url?}/%s%s" title="%s">%s</a>',
139 $this->getLinkBase(),
150 * Setter for link name
152 * @param $linkName Name of the link we shall generate
155 protected final function setLinkName ($linkName) {
156 $this->linkName = (string) $linkName;
160 * Getter for link name
162 * @return $linkName Name of the link we shall generate
164 public final function getLinkName () {
165 return $this->linkName;
169 * Setter for link base
171 * @param $linkBase Base of the link we shall generate
174 protected final function setLinkBase ($linkBase) {
175 $this->linkBase = (string) $linkBase;
179 * Getter for link base
181 * @return $linkBase Base of the link we shall generate
183 public final function getLinkBase () {
184 return $this->linkBase;
188 * Flush the content out,e g. to a template variable
191 * @todo Completely unimplemented
193 public function flushContent () {
194 // Is a previous opened group still open?
195 if ($this->ifGroupOpenedPreviously()) {
197 $this->closePreviousGroupByContent('');
201 $content = $this->renderContent();
203 // Get template engine
204 $templateInstance = $this->getTemplateInstance();
206 // Add content to variable
207 $templateInstance->assignVariable($this->getLinkName(), $content);
211 * Adds a link group (like the form group is) with some raw language to the
214 * @param $groupId Id string of the group
215 * @param $groupText Text for this group to add
216 * @param $groupCode Code to open and close groups
219 public function addLinkGroup ($groupId, $groupText, $groupCode = 'div') {
220 // Is a group with that name open?
221 if ($this->ifGroupOpenedPreviously()) {
222 // Then close it here
223 $this->closePreviousGroupByContent('');
226 // Generate the group content
227 $content = sprintf('<%s id="group_%s_%s">%s',
229 $this->getLinkName(),
234 // Open the new group
235 $this->openGroupByIdContent($groupId, $content, $groupCode);
239 * Adds text (note) to the previously opened group or throws an exception
240 * if no previous group was opened.
242 * @param $groupId Group id to set
243 * @param $groupNote Note to be added to a group
244 * @param $groupCode Code to open and close groups
246 * @throws NoGroupOpenedException If no previous group was opened
248 public function addLinkNote ($groupId, $groupNote, $groupCode = 'div') {
249 // Check if a previous group was opened
250 if ($this->ifGroupOpenedPreviously() === FALSE) {
251 // No group was opened before!
252 throw new NoGroupOpenedException(array($this, $groupNote), self::EXCEPTION_GROUP_NOT_OPENED);
255 // Is a previous sub group open?
256 if ($this->ifSubGroupOpenedPreviously()) {
258 $this->closePreviousSubGroupByContent('</' . $groupCode . '>');
261 // Generate the group content
262 $content = sprintf('<%s id="subgroup_%s_%s">%s',
264 $this->getLinkName(),
269 // Open the sub group
270 $this->openSubGroupByIdContent($groupId, $content, $groupCode);
274 * Adds a link to the previously opened group or throws an exception if no group has been opened
276 * @param $linkAction Action (action=xxx) value for the link
277 * @param $linkText Link text and title (title="xxx") for the link
279 * @throws NoGroupOpenedException If no previous group was opened
281 protected function addActionLink ($linkAction, $linkText, $linkTitle) {
282 // Check if a previous group was opened
283 if ($this->ifGroupOpenedPreviously() === FALSE) {
284 // No group was opened before!
285 throw new NoGroupOpenedException(array($this, $linkAction . '(' . $linkText . ')'), self::EXCEPTION_GROUP_NOT_OPENED);
288 // Default parameter SEPARATOR is &
289 $separator = self::EXTRA_PARAMETER_SEPARATOR;
291 // Is there a question mark in?
292 $linkArray = explode(self::FIRST_PARAMETER_SEPARATOR, $this->getLinkBase());
293 if (count($linkArray) == 0) {
295 $separator = self::FIRST_PARAMETER_SEPARATOR;
299 $action = sprintf('%saction=%s',
304 // Renders the link content
305 $linkContent = $this->renderLinkContentWithTextExtraContent($linkText, $linkTitle, $action);
307 // Add the content to the previous group
308 $this->addContentToPreviousGroup($linkContent);
312 * Adds a link to the previously opened group with a text from language system
314 * @param $linkAction Action (action=xxx) value for the link
315 * @param $languageId Language id string to use
318 public function addActionLinkById ($linkAction, $languageId) {
319 // Resolve the language string
320 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
322 // Resolve the language string
323 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
325 // Add the action link
326 $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle);
330 * Adds a default link (no extra parameters) to the content with specified
331 * language id string.
333 * @param $languageId Language id string to use
336 public function addLinkWithTextById ($languageId) {
337 // Resolve the language string
338 $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text');
340 // Resolve the language string
341 $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title');
344 $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle);
346 // Add the content to the previous group
347 $this->addContentToPreviousGroup($linkContent);