Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • ArticleForumRightBottom
  • cApiClickableAction
  • cApiClickableQuestionAction
  • cGuiBackendHelpbox
  • cGuiFoldingRow
  • cGuiList
  • cGuiMenu
  • cGuiNavigation
  • cGuiNotification
  • cGuiObjectPager
  • cGuiPage
  • cGuiScrollList
  • cGuiTableForm
  • cGuiTree
  • cPager
  • cTemplate
  • cTree
  • cTreeItem
  • NoteLink
  • NoteList
  • NoteListItem
  • NoteView
  • TODOBackendList
  • TODOLink
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the former template class.
  4:  *
  5:  * @package    Core
  6:  * @subpackage GUI
  7:  * @version    SVN Revision $Rev:$
  8:  *
  9:  * @author     Jan Lengowski, Stefan Jelner
 10:  * @copyright  four for business AG <www.4fb.de>
 11:  * @license    http://www.contenido.org/license/LIZENZ.txt
 12:  * @link       http://www.4fb.de
 13:  * @link       http://www.contenido.org
 14:  */
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: /**
 19:  * class Template
 20:  * Light template mechanism
 21:  *
 22:  * @package Core
 23:  * @subpackage GUI
 24:  */
 25: class cTemplate {
 26: 
 27:     /**
 28:      * Needles (static)
 29:      *
 30:      * @var array
 31:      */
 32:     public $needles = array();
 33: 
 34:     /**
 35:      * Replacements (static)
 36:      *
 37:      * @var array
 38:      */
 39:     public $replacements = array();
 40: 
 41:     /**
 42:      * Dyn_Needles (dynamic)
 43:      *
 44:      * @var array
 45:      */
 46:     public $Dyn_needles = array();
 47: 
 48:     /**
 49:      * Dyn_Replacements (dynamic)
 50:      *
 51:      * @var array
 52:      */
 53:     public $Dyn_replacements = array();
 54: 
 55: 
 56:     /**
 57:      * Dynamic counter
 58:      *
 59:      * @var int
 60:      */
 61:     public $dyn_cnt = 0;
 62: 
 63:     /**
 64:      * Tags array (for dynamic blocks);
 65:      *
 66:      * @var array
 67:      */
 68:     public $tags = array(
 69:         'static' => '{%s}',
 70:         'start' => '<!-- BEGIN:BLOCK -->',
 71:         'end' => '<!-- END:BLOCK -->'
 72:     );
 73: 
 74:     /**
 75:      * gettext domain (default: contenido)
 76:      *
 77:      * @var string
 78:      */
 79:     protected $_sDomain = 'contenido';
 80: 
 81:     /**
 82:      * Constructor function
 83:      * @param array|bool $tags
 84:      */
 85:     public function __construct($tags = false) {
 86:         if (is_array($tags)) {
 87:             $this->tags = $tags;
 88:         }
 89: 
 90:         $this->setEncoding("");
 91:     }
 92: 
 93:     /**
 94:      * Sets the gettext domain to use for translations in a template
 95:      *
 96:      * @param string $sDomain Sets the domain to use for template translations
 97:      */
 98:     public function setDomain($sDomain) {
 99:         $this->_sDomain = $sDomain;
100:     }
101: 
102:     /**
103:      * Set Templates placeholders and values
104:      *
105:      * With this method you can replace the placeholders
106:      * in the static templates with dynamic data.
107:      *
108:      * @param string $which 's' for Static or else dynamic
109:      * @param string $needle Placeholder
110:      * @param string $replacement Replacement String
111:      */
112:     public function set($which, $needle, $replacement) {
113:         if ($which == 's') {
114:             // static
115:             $this->needles[] = sprintf($this->tags['static'], $needle);
116:             $this->replacements[] = $replacement;
117:         } else {
118:             // dynamic
119:             $this->Dyn_needles[$this->dyn_cnt][] = sprintf($this->tags['static'], $needle);
120:             $this->Dyn_replacements[$this->dyn_cnt][] = $replacement;
121:         }
122:     }
123: 
124:     /**
125:      * Sets an encoding for the template's head block.
126:      *
127:      * @param string $encoding Encoding to set
128:      */
129:     public function setEncoding($encoding) {
130:         $this->_encoding = $encoding;
131:     }
132: 
133:     /**
134:      * Iterate internal counter by one
135:      */
136:     public function next() {
137:         $this->dyn_cnt++;
138:     }
139: 
140:     /**
141:      * Reset template data
142:      */
143:     public function reset() {
144:         $this->dyn_cnt = 0;
145:         $this->needles = array();
146:         $this->replacements = array();
147:         $this->Dyn_needles = array();
148:         $this->Dyn_replacements = array();
149:     }
150: 
151:     /**
152:      * Generate the template and
153:      * print/return it.
154:      * (do translations sequentially to save memory!!!)
155:      *
156:      * @param string $template Either template string or template file path
157:      * @param bool $return Return or print template
158:      * @param bool $note Echo "Generated by ... " Comment
159:      * @return string Complete Template string
160:      */
161:     public function generate($template, $return = 0, $note = 0) {
162:         global $cCurrentModule, $cfg, $frontend_debug;
163: 
164:         $moduleHandler = NULL;
165:         if (!is_null($cCurrentModule)) {
166:             $moduleHandler = new cModuleHandler($cCurrentModule);
167:         }
168: 
169:         // Check if the template is a file or a string
170:         if (!@is_file($template)) {
171:             if (is_object($moduleHandler) && is_file($moduleHandler->getTemplatePath($template))) {
172:                 // Module directory has higher priority
173:                 $content = $moduleHandler->getFilesContent('template', '', $template);
174:                 if ($frontend_debug['template_display']) {
175:                     echo('<!-- CTEMPLATE ' . $template . ' -->');
176:                 }
177:             } else {
178:                 // Template is a string (it is a reference to save memory!!!)
179:                 $content = &$template;
180:             }
181:         } else {
182:             if (is_object($moduleHandler) && is_file($moduleHandler->getTemplatePath($template))) {
183:                 // Module directory has higher priority
184:                 $content = $moduleHandler->getFilesContent('template', '', $template);
185:             } else {
186:                 // Template is a file in template directory
187:                 $content = implode('', file($template));
188:             }
189:         }
190: 
191:         $content = (($note) ? "<!-- Generated by CONTENIDO " . CON_VERSION . "-->\n" : "") . $content;
192: 
193:         $pieces = array();
194: 
195:         // Replace i18n strings before replacing other placeholders
196:         $this->replacei18n($content, 'i18n');
197:         $this->replacei18n($content, 'trans');
198: 
199:         // If content has dynamic blocks
200:         $startQ = preg_quote($this->tags['start'], '/');
201:         $endQ = preg_quote($this->tags['end'], '/');
202:         if (preg_match('/^.*' . $startQ . '.*?' . $endQ . '.*$/s', $content)) {
203:             // Split everything into an array
204:             preg_match_all('/^(.*)' . $startQ . '(.*?)' . $endQ . '(.*)$/s', $content, $pieces);
205:             // Safe memory
206:             array_shift($pieces);
207:             $content = '';
208:             // Now combine pieces together
209:             // Start block
210:             $content .= str_replace($this->needles, $this->replacements, $pieces[0][0]);
211:             unset($pieces[0][0]);
212: 
213:             // Generate dynamic blocks
214:             for ($a = 0; $a < $this->dyn_cnt; $a++) {
215:                 $content .= str_replace($this->Dyn_needles[$a], $this->Dyn_replacements[$a], $pieces[1][0]);
216:             }
217:             unset($pieces[1][0]);
218: 
219:             // End block
220:             $content .= str_replace($this->needles, $this->replacements, $pieces[2][0]);
221:             unset($pieces[2][0]);
222:         } else {
223:             $content = str_replace($this->needles, $this->replacements, $content);
224:         }
225: 
226:         if ($this->_encoding != '') {
227:             $content = str_replace("</head>", '<meta http-equiv="Content-Type" content="text/html; charset=' . $this->_encoding . '">' . "\n" . '</head>', $content);
228:         }
229: 
230:         if ($return) {
231:             return $content;
232:         } else {
233:             echo $content;
234:         }
235:     }
236: 
237:     /**
238:      * Replaces a named function with the translated variant
239:      *
240:      * @param string $template Contents of the template to translate (it is
241:      *        reference to save memory!!!)
242:      * @param string $functionName Name of the translation function (e.g. i18n)
243:      */
244:     public function replacei18n(&$template, $functionName) {
245:         $container = array();
246: 
247:         // Be sure that php code stays unchanged
248:         $php_matches = array();
249:         /*
250:          * if (preg_match_all('/<\?(php)?((.)|(\s))*?\?>/i', $template,
251:          * $php_matches)) { $x = 0; foreach ($php_matches[0] as $php_match) {
252:          * $x++; $template = str_replace($php_match , '{PHP#' . $x . '#PHP}',
253:          * $template); $container[$x] = $php_match; } }
254:          */
255: 
256:         $functionNameQ = preg_quote($functionName, '/');
257: 
258:         // If template contains functionName + parameter store all matches
259:         $matches = array();
260:         preg_match_all('/' . $functionNameQ . "\\(([\\\"\\'])(.*?)\\1\\)/s", $template, $matches);
261: 
262:         $matches = array_values(array_unique($matches[2]));
263:         for ($a = 0; $a < count($matches); $a++) {
264:             $template = preg_replace('/' . $functionNameQ . "\\([\\\"\\']" . preg_quote($matches[$a], '/') . "[\\\"\\']\\)/s", i18n($matches[$a], $this->_sDomain), $template);
265:         }
266: 
267:         // Change back php placeholder
268:         if (is_array($container)) {
269:             foreach ($container as $x => $php_match) {
270:                 $template = str_replace('{PHP#' . $x . '#PHP}', $php_match, $template);
271:             }
272:         }
273:     }
274: 
275: }
276: 
CMS CONTENIDO 4.9.1 API documentation generated by ApiGen 2.8.0