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
    • NavigationMain
    • 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
  • 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:      */
 84:     public function __construct($tags = false) {
 85:         if (is_array($tags)) {
 86:             $this->tags = $tags;
 87:         }
 88: 
 89:         $this->setEncoding("");
 90:     }
 91: 
 92:     /**
 93:      * Sets the gettext domain to use for translations in a template
 94:      *
 95:      * @param string $sDomain Sets the domain to use for template translations
 96:      */
 97:     public function setDomain($sDomain) {
 98:         $this->_sDomain = $sDomain;
 99:     }
100: 
101:     /**
102:      * Set Templates placeholders and values
103:      *
104:      * With this method you can replace the placeholders
105:      * in the static templates with dynamic data.
106:      *
107:      * @param string $which 's' for Static or else dynamic
108:      * @param string $needle Placeholder
109:      * @param string $replacement Replacement String
110:      */
111:     public function set($which, $needle, $replacement) {
112:         if ($which == 's') {
113:             // static
114:             $this->needles[] = sprintf($this->tags['static'], $needle);
115:             $this->replacements[] = $replacement;
116:         } else {
117:             // dynamic
118:             $this->Dyn_needles[$this->dyn_cnt][] = sprintf($this->tags['static'], $needle);
119:             $this->Dyn_replacements[$this->dyn_cnt][] = $replacement;
120:         }
121:     }
122: 
123:     /**
124:      * Sets an encoding for the template's head block.
125:      *
126:      * @param string $encoding Encoding to set
127:      */
128:     public function setEncoding($encoding) {
129:         $this->_encoding = $encoding;
130:     }
131: 
132:     /**
133:      * Iterate internal counter by one
134:      */
135:     public function next() {
136:         $this->dyn_cnt++;
137:     }
138: 
139:     /**
140:      * Reset template data
141:      */
142:     public function reset() {
143:         $this->dyn_cnt = 0;
144:         $this->needles = array();
145:         $this->replacements = array();
146:         $this->Dyn_needles = array();
147:         $this->Dyn_replacements = array();
148:     }
149: 
150:     /**
151:      * Generate the template and
152:      * print/return it.
153:      * (do translations sequentially to save memory!!!)
154:      *
155:      * @param string $template Either template string or template file path
156:      * @param bool $return Return or print template
157:      * @param bool $note Echo "Generated by ... " Comment
158:      * @return string Complete Template string
159:      */
160:     public function generate($template, $return = 0, $note = 0) {
161:         global $cCurrentModule, $cfg;
162: 
163:         $moduleHandler = NULL;
164:         if (!is_null($cCurrentModule)) {
165:             $moduleHandler = new cModuleHandler($cCurrentModule);
166:         }
167: 
168:         // Check if the template is a file or a string
169:         if (!@is_file($template)) {
170:             if (is_object($moduleHandler) && is_file($moduleHandler->getTemplatePath($template))) {
171:                 // Module directory has higher priority
172:                 $content = $moduleHandler->getFilesContent('template', '', $template);
173:             } else {
174:                 // Template is a string (it is a reference to save memory!!!)
175:                 $content = &$template;
176:             }
177:         } else {
178:             if (is_object($moduleHandler) && is_file($moduleHandler->getTemplatePath($template))) {
179:                 // Module directory has higher priority
180:                 $content = $moduleHandler->getFilesContent('template', '', $template);
181:             } else {
182:                 // Template is a file in template directory
183:                 $content = implode('', file($template));
184:             }
185:         }
186: 
187:         $content = (($note) ? "<!-- Generated by CONTENIDO " . $cfg['version'] . "-->\n" : "") . $content;
188: 
189:         $pieces = array();
190: 
191:         // Replace i18n strings before replacing other placeholders
192:         $this->replacei18n($content, 'i18n');
193:         $this->replacei18n($content, 'trans');
194: 
195:         // If content has dynamic blocks
196:         $startQ = preg_quote($this->tags['start'], '/');
197:         $endQ = preg_quote($this->tags['end'], '/');
198:         if (preg_match('/^.*' . $startQ . '.*?' . $endQ . '.*$/s', $content)) {
199:             // Split everything into an array
200:             preg_match_all('/^(.*)' . $startQ . '(.*?)' . $endQ . '(.*)$/s', $content, $pieces);
201:             // Safe memory
202:             array_shift($pieces);
203:             $content = '';
204:             // Now combine pieces together
205:             // Start block
206:             $content .= str_replace($this->needles, $this->replacements, $pieces[0][0]);
207:             unset($pieces[0][0]);
208: 
209:             // Generate dynamic blocks
210:             for ($a = 0; $a < $this->dyn_cnt; $a++) {
211:                 $content .= str_replace($this->Dyn_needles[$a], $this->Dyn_replacements[$a], $pieces[1][0]);
212:             }
213:             unset($pieces[1][0]);
214: 
215:             // End block
216:             $content .= str_replace($this->needles, $this->replacements, $pieces[2][0]);
217:             unset($pieces[2][0]);
218:         } else {
219:             $content = str_replace($this->needles, $this->replacements, $content);
220:         }
221: 
222:         if ($this->_encoding != '') {
223:             $content = str_replace("</head>", '<meta http-equiv="Content-Type" content="text/html; charset=' . $this->_encoding . '">' . "\n" . '</head>', $content);
224:         }
225: 
226:         if ($return) {
227:             return $content;
228:         } else {
229:             echo $content;
230:         }
231:     }
232: 
233:     /**
234:      * Replaces a named function with the translated variant
235:      *
236:      * @param string $template Contents of the template to translate (it is
237:      *        reference to save memory!!!)
238:      * @param string $functionName Name of the translation function (e.g. i18n)
239:      */
240:     public function replacei18n(&$template, $functionName) {
241:         $container = array();
242: 
243:         // Be sure that php code stays unchanged
244:         $php_matches = array();
245:         /*
246:          * if (preg_match_all('/<\?(php)?((.)|(\s))*?\?>/i', $template,
247:          * $php_matches)) { $x = 0; foreach ($php_matches[0] as $php_match) {
248:          * $x++; $template = str_replace($php_match , '{PHP#' . $x . '#PHP}',
249:          * $template); $container[$x] = $php_match; } }
250:          */
251: 
252:         $functionNameQ = preg_quote($functionName, '/');
253: 
254:         // If template contains functionName + parameter store all matches
255:         $matches = array();
256:         preg_match_all('/' . $functionNameQ . "\\(([\\\"\\'])(.*?)\\1\\)/s", $template, $matches);
257: 
258:         $matches = array_values(array_unique($matches[2]));
259:         for ($a = 0; $a < count($matches); $a++) {
260:             $template = preg_replace('/' . $functionNameQ . "\\([\\\"\\']" . preg_quote($matches[$a], '/') . "[\\\"\\']\\)/s", i18n($matches[$a], $this->_sDomain), $template);
261:         }
262: 
263:         // Change back php placeholder
264:         if (is_array($container)) {
265:             foreach ($container as $x => $php_match) {
266:                 $template = str_replace('{PHP#' . $x . '#PHP}', $php_match, $template);
267:             }
268:         }
269:     }
270: 
271: }
272: 
CMS CONTENIDO 4.9.0 API documentation generated by ApiGen 2.8.0