-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinclude.inc
231 lines (198 loc) · 7.79 KB
/
include.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* A simple templating system for outputing html
* @param $oTemplate is a template object keyed by a template name
* @return string of html
* @author Steve Wirt
* @version 1-1
*/
defineTemplatitPath();
function templatit($oTemplate){
//sanitize and structure
$oTemplate->name = (!empty($oTemplate->name)) ? $oTemplate->name: 'badTemplateName' ;
$oTemplate->callback = (!empty($oTemplate->callback)) ? $oTemplate->callback: '' ;
$oTemplate->callback = (function_exists($oTemplate->callback)) ? $oTemplate->callback: '' ;
//include the file
$sTPLfile = 'templates/'.$oTemplate->name . '.tpl.php';
if (!empty($oTemplate->callback)) {
ob_start($oTemplate->callback);
} else {
ob_start();
}
$bIncludeWorked = include($sTPLfile);
//means the include was successful
$sReturn = ob_get_contents();
ob_end_clean();
if (empty($bIncludeWorked)) {
//means the include failed
$sReturn = "<!-- include of ".$oTemplate->name." failed --> \n";
}
$sReturn = (!empty($sReturn)) ? $sReturn : '' ;
return $sReturn;
} //end function templatit
/**
* Pass it a template name and it builds a template object ready for vars
*
* @param $sTemplateName a string template name withiout tpl.php
*
* @param $sCallbackFunction string optonal callback function name
* calls a function that accesspts a string and returns a string. (can be used to process html or wrap it.
* @return A fully structured template object array lacking only vars.
* @author Steve Wirt
*/
function newTemplateObject($sTemplateName, $sCallbackFunction = '') {
$oTemplateObject = new stdClass;
$oTemplateObject->name = $sTemplateName;
$oTemplateObject->callback = $sCallbackFunction;
$oTemplateObject->vars = new stdClass;
return $oTemplateObject;
}//end function newTemplateObject
/**
* A shorthand version that builds a template with just a name
*
* @param $sTemplateName the name of the template to use
*
* @param mixed $mSomeVar to pass to the object as a var. It will be available within the template
*
* @return string of html output by the tpl
*
* @author Steve Wirt
*/
function tmpl8($sTemplateName = null, $mSomeVar = '') {
if (!empty($sTemplateName) && is_string($sTemplateName)) {
$oTemplateObject = newTemplateObject($sTemplateName);
//Pass the variable to the tpl
$oTemplateObject->vars = $mSomeVar;
return templatit($oTemplateObject);
}
}
/**
* Packages a template into a JS variable data on the page, and out puts the default target, and the tosscall.
* @param string $sTemplateName That name of the template to prepare.
* @param string $sStatesToToss CSV of states that should be tossed.
* @param string $sTargetID optional target id that you would have to place on your page, otherwise it creates a default target.
* @param boolean $bAjax If TRUE the text will use the ajax tosscall.
* @return string HTML output.
*/
function templateToss($sTemplateName=NULL, $sStatesToToss=NULL, $sTargetID=NULL, $bAjax=FALSE ) {
$sReturn = '';
$sBlock = '';
//load the template
if (!empty($sTemplateName)) {
//If not coming from ajax, load the template.
$sHTML = (!$bAjax) ? tmpl8($sTemplateName): '';
//Make a friendly template name without -
$sJSfriendlyTemplateName = str_replace('-','_', $sTemplateName);
//Make the default target if sTargetID is empty.
if(empty($sTargetID)) {
// Means there is no sTargetID so make the default
$sTargetID = "tossTarget_$sTemplateName";
$sReturn .= "<div id='$sTargetID'><!-- --></div>\n";
}
$sReturn .= "<script type='text/javascript' data-tpl='$sTemplateName'>\n";
if (!empty($sHTML)) {
//The template was returned, wrap it in JS
$sBlock .= json_encode($sHTML);
$sReturn .= "oTemplatit.templateTosser.tossable.$sJSfriendlyTemplateName = $sBlock;\n";
}
$sReturn .= "$(document).on('templatitInitialized', function() {\n";
// Write the tosscall
if ($bAjax === TRUE) {
//Should come in via ajax
$sReturn .= " tossTemplateAjax ('$sTemplateName', '{$sStatesToToss}' , '$sTargetID', false);\n";
}
else {
//Tossed the normal way.
$sReturn .= " tossTemplate('$sJSfriendlyTemplateName', '{$sStatesToToss}' , '$sTargetID');\n";
}
$sReturn .= "});\n";
$sReturn .= "</script>\n";
}
return $sReturn;
}
/**
* Outputs a template tracer opening comment if user is admin
*
* @param $sFile string representing the file ' __FILE__' that is calling the function
*
* @param $sOptionalMessage string representing anything you want included in the template tracer output
*
* @return string of html comments to output
* @author Steve Wirt
*/
function tto($sFile = 'tto() called incorrectly. Must be tto(__FILE__)', $OptionalMessage='') {
if (!empty($_GET) && !empty($_GET["debug"]) && ($_GET["debug"] == 'template' )) {
if ((is_array($OptionalMessage)) || (is_object($OptionalMessage))) {
//Means it is a array or object, so convert it to a pretty string
$OptionalMessage = print_r($OptionalMessage, TRUE);
}
$sOptionalMessage = (!empty($OptionalMessage)) ? "<!-- Message: $OptionalMessage -->\n" : '';
return("\n<!-- <TEMPLATE> $sFile -->\n $sOptionalMessage \n");
}
}
/**
* Outputs a template tracer closing comment if user is admin
*
* @param $sFile string representing the file ' __FILE__' that is calling the function
*
* @param $sOptionalMessage string representing anything you want included in the template tracer output
*
* @return string of html comments to output
* @author Steve Wirt
*/
function ttc($sFile = 'ttc() called incorrectly. Must be ttc(__FILE__)', $OptionalMessage='') {
if (!empty($_GET) && !empty($_GET["debug"]) && ($_GET["debug"] == 'template' )) {
if ((is_array($OptionalMessage)) || (is_object($OptionalMessage))) {
//Means it is a array or opject, so convert it to a pretty string
$OptionalMessage = print_r($OptionalMessage, TRUE);
}
$sOptionalMessage = (!empty($OptionalMessage)) ? "<!-- Message: $OptionalMessage -->\n" : '';
return( "\n<!-- </TEMPLATE> $sFile -->\n $sOptionalMessage \n");
}
}
/**
* This function defines the value of root relative path to templatit.
* @return string path of the templatit directory
* @author Steve Wirt
*/
function defineTemplatitPath() {
// Split both strings
$aA = explode('/', $_SERVER["DOCUMENT_ROOT"]);
$aB = explode('/',__DIR__);
$iB = 0;
$aParsed = array ();
$parseFlag = 1;
//loop through $aA and look for sync
foreach ($aA as $key=>$sDirectory) {
//skip empty values
if (empty($aB[$iB])) {
$iB++;
}
//TRUE switch, first on to evaluate to true wins
switch (TRUE) {
case (empty($sDirectory)):
break;
case ((!empty($aB[$iB])) && ($sDirectory == $aB[$iB])):
//we have a sync
$parseFlag = 2;
$aB[$iB] = '';
$iB++;
break;
case ((!empty($aB[$iB])) && ($sDirectory == $aB[$iB]) && ($parseFlag == 2)):
//no match
$iB++;
break;
case ((!empty($aB[$iB])) && ($sDirectory != $aB[$iB]) && ($parseFlag == 2)):
//no match
$aParsed[] = $sDirectory;
$iB++;
break;
}
}
$aB = array_filter($aB);
$sPath = implode('/', $aB);
/**
* Root relative path to templatit
*/
define('TEMPLATIT_PATH', '/' . $sPath);
}