0855bff6cb9bee01f29639490710a1024829ef02
[core.git] / framework / main / classes / helper / html / blocks / class_HtmlBlockHelper.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Helper;
4
5 // Import framework stuff
6 use CoreFramework\Template\CompileableTemplate;
7
8 /**
9  * A helper for generating blocks (div or span) on web pages
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 class HtmlBlockHelper extends BaseHtmlHelper implements HelpableTemplate {
31         /**
32          * Name of the block
33          */
34         private $blockName = '';
35
36         /**
37          * Protected constructor
38          *
39          * @return      void
40          */
41         protected function __construct () {
42                 // Call parent constructor
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
47          * Creates the helper class
48          *
49          * @param       $templateInstance       An instance of a template engine
50          * @param       $blockName                      Name of the block we shall generate
51          * @return      $helperInstance         A prepared instance of this helper
52          */
53         public static final function createHtmlBlockHelper (CompileableTemplate $templateInstance, $blockName) {
54                 // Get new instance
55                 $helperInstance = new HtmlBlockHelper();
56
57                 // Set template instance
58                 $helperInstance->setTemplateInstance($templateInstance);
59
60                 // Set block name
61                 $helperInstance->setBlockName($blockName);
62
63                 // Return the prepared instance
64                 return $helperInstance;
65         }
66
67         /**
68          * Setter for block name
69          *
70          * @param       $blockName      Name of the block we shall generate
71          * @return      void
72          */
73         protected final function setBlockName ($blockName) {
74                 $this->blockName = (string) $blockName;
75         }
76
77         /**
78          * Getter for block name
79          *
80          * @return      $blockName      Name of the block we shall generate
81          */
82         public final function getBlockName () {
83                 return $this->blockName;
84         }
85
86         /**
87          * Checks whether include registration date in this block
88          *
89          * @return      $withRegistration       Whether with registration date
90          */
91         public function ifIncludeRegistrationStamp () {
92                 $withRegistration = ($this->getConfigInstance()->getConfigEntry('block_shows_registration') == 'Y');
93                 return $withRegistration;
94         }
95
96         /**
97          * Assignes a template variable with a message from a given message id
98          *
99          * @param       $templateVariable       Template variable to assign
100          * @param       $messageId                      Message id to load an assign
101          * @return      void
102          */
103         public function assignMessageField ($templateVariable, $messageId) {
104                 // Get message
105                 $message = $this->getLanguageInstance()->getMessage($messageId);
106
107                 // And assign it
108                 $this->getTemplateInstance()->assignVariable($templateVariable, $message);
109         }
110
111         /**
112          * Assigns a link field with a given value
113          *
114          * @param       $linkField              "Link field" (variable) to assign
115          * @param       $actionValue    Action value to assign
116          * @return      void
117          */
118         public function assignLinkFieldWithAction ($linkField, $actionValue) {
119                 $this->getTemplateInstance()->assignVariable($linkField . '_action', $actionValue);
120         }
121
122         /**
123          * "Filter" method for translating the raw user status into something human-readable
124          *
125          * @param       $userStatus             Raw user status from database layer
126          * @return      $translated             Translated user status
127          */
128         protected function doFilterUserStatusTranslator ($userStatus) {
129                 // Generate message id
130                 $messageId = 'user_status_' . strtolower($userStatus);
131
132                 // Get that message
133                 $translated = $this->getLanguageInstance()->getMessage($messageId);
134
135                 // Return it
136                 return $translated;
137         }
138
139         /**
140          * Flush the content out,e g. to a template variable
141          *
142          * @return      void
143          */
144         public function flushContent () {
145                 // Get template instance
146                 $templateInstance = $this->getTemplateInstance();
147
148                 // Get the template named like this block
149                 $templateInstance->loadCodeTemplate('block_' . $this->getBlockName());
150
151                 // Transfer it to the template instance
152                 $templateInstance->assignVariable($this->getBlockName(), $templateInstance->getRawTemplateData());
153         }
154
155 }