Initial repo created
[timetracker.git] / WEB-INF / lib / smarty / sysplugins / smarty_internal_compile_insert.php
1 <?php
2
3 /**
4  * Smarty Internal Plugin Compile Insert
5  * 
6  * Compiles the {insert} tag
7  * 
8  * @package Smarty
9  * @subpackage Compiler
10  * @author Uwe Tews 
11  */
12
13 /**
14  * Smarty Internal Plugin Compile Insert Class
15  */
16 class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase {
17         // attribute definitions
18     public $required_attributes = array('name');
19         public $shorttag_order = array('name');
20     public $optional_attributes = array('_any'); 
21
22     /**
23      * Compiles code for the {insert} tag
24      * 
25      * @param array $args array with attributes from parser
26      * @param object $compiler compiler object
27      * @return string compiled code
28      */
29     public function compile($args, $compiler)
30     {
31         $this->compiler = $compiler;
32         // check and get attributes
33         $_attr = $this->_get_attributes($args); 
34         // never compile as nocache code
35         $this->compiler->suppressNocacheProcessing = true;
36         $this->compiler->tag_nocache = true;
37         $_smarty_tpl = $compiler->template;
38         $_name = null;
39         $_script = null;
40
41         $_output = '<?php '; 
42         // save posible attributes
43         eval('$_name = ' . $_attr['name'] . ';');
44         if (isset($_attr['assign'])) {
45             // output will be stored in a smarty variable instead of being displayed
46             $_assign = $_attr['assign']; 
47             // create variable to make shure that the compiler knows about its nocache status
48             $this->compiler->template->tpl_vars[trim($_attr['assign'], "'")] = new Smarty_Variable(null, true);
49         } 
50         if (isset($_attr['script'])) {
51             // script which must be included
52             $_function = "smarty_insert_{$_name}";
53             $_smarty_tpl = $compiler->template;
54             $_filepath = false;
55             eval('$_script = ' . $_attr['script'] . ';');
56             if (!isset($this->compiler->smarty->security_policy) && file_exists($_script)) {
57                 $_filepath = $_script;
58             } else {
59                 if (isset($this->compiler->smarty->security_policy)) {
60                     $_dir = $this->compiler->smarty->security_policy->trusted_dir;
61                 } else {
62                     $_dir = $this->compiler->smarty->trusted_dir;
63                 } 
64                 if (!empty($_dir)) {
65                     foreach((array)$_dir as $_script_dir) {
66                         if (strpos('/\\', substr($_script_dir, -1)) === false) {
67                             $_script_dir .= DS;
68                         } 
69                         if (file_exists($_script_dir . $_script)) {
70                             $_filepath = $_script_dir . $_script;
71                             break;
72                         } 
73                     } 
74                 } 
75             } 
76             if ($_filepath == false) {
77                 $this->compiler->trigger_template_error("{insert} missing script file '{$_script}'", $this->compiler->lex->taglineno);
78             } 
79             // code for script file loading
80             $_output .= "require_once '{$_filepath}' ;";
81             require_once $_filepath;
82             if (!is_callable($_function)) {
83                 $this->compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'", $this->compiler->lex->taglineno);
84             } 
85         } else {
86             $_filepath = 'null';
87             $_function = "insert_{$_name}"; 
88             // function in PHP script ?
89             if (!is_callable($_function)) {
90                 // try plugin
91                 if (!$_function = $this->compiler->getPlugin($_name, 'insert')) {
92                     $this->compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", $this->compiler->lex->taglineno);
93                 } 
94             } 
95         } 
96         // delete {insert} standard attributes
97         unset($_attr['name'], $_attr['assign'], $_attr['script'], $_attr['nocache']); 
98         // convert attributes into parameter array string
99         $_paramsArray = array();
100         foreach ($_attr as $_key => $_value) {
101             $_paramsArray[] = "'$_key' => $_value";
102         } 
103         $_params = 'array(' . implode(", ", $_paramsArray) . ')'; 
104         // call insert
105         if (isset($_assign)) {
106             if ($_smarty_tpl->caching) {
107                 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>";
108             } else {
109                 $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl), true);?>";
110             } 
111         } else {
112             $this->compiler->has_output = true;
113             if ($_smarty_tpl->caching) {
114                 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}');?>";
115             } else {
116                 $_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";
117             } 
118         } 
119         return $_output;
120     } 
121
122
123 ?>