2ac227f47563fac25411dbdd8d21f4a1dd896f20
[Packages/TYPO3.CMS.git] / t3lib / jsfunc.evalfield.js
1 /***************************************************************
2 *
3 * Evaluation of TYPO3 form field content
4 *
5 * $Id$
6 *
7 *
8 *
9 * Copyright notice
10 *
11 * (c) 1998-2008 Kasper Skaarhoj
12 * All rights reserved
13 *
14 * This script is part of the TYPO3 t3lib/ library provided by
15 * Kasper Skaarhoj <kasper@typo3.com> together with TYPO3
16 *
17 * Released under GNU/GPL (see license file in typo3/sysext/cms/tslib/)
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 *
23 * This copyright notice MUST APPEAR in all copies of this script
24 ***************************************************************/
25
26
27 function evalFunc() {
28 this.input = evalFunc_input;
29 this.output = evalFunc_output;
30 this.parseInt = evalFunc_parseInt;
31 this.getNumChars = evalFunc_getNumChars;
32 this.parseDouble = evalFunc_parseDouble;
33 this.noSpace = evalFunc_noSpace;
34 this.getSecs = evalFunc_getSecs;
35 this.getYear = evalFunc_getYear;
36 this.getTimeSecs = evalFunc_getTimeSecs;
37 this.getTime = evalFunc_getTime;
38 this.getDate = evalFunc_getDate;
39 this.getTimestamp = evalFunc_getTimestamp;
40 this.caseSwitch = evalFunc_caseSwitch;
41 this.evalObjValue = evalFunc_evalObjValue;
42 this.outputObjValue = evalFunc_outputObjValue;
43 this.split = evalFunc_splitStr;
44 this.pol = evalFunc_pol;
45
46 this.ltrim = evalFunc_ltrim;
47 this.btrim = evalFunc_btrim;
48 var today = new Date();
49 this.lastYear = this.getYear(today);
50 this.lastDate = this.getDate(today);
51 this.lastTime = this.getTimestamp(today);
52 this.isInString = '';
53 this.USmode = 0;
54 }
55 function evalFunc_pol(fortegn, value) {
56 return eval (((fortegn=="-")?'-':'')+value);
57 }
58 function evalFunc_evalObjValue(FObj,value) {
59 var evallist = FObj.evallist;
60 this.isInString = (FObj.is_in) ? ''+FObj.is_in : '';
61 var index=1;
62 var theEvalType = (FObj.evallist) ? this.split(evallist, ",", index) : false;
63 var newValue=value;
64 while (theEvalType) {
65 if (theEvalType.slice(0, 3) == 'tx_') {
66 if(typeof window[theEvalType] == 'function') {
67 newValue = window[theEvalType](newValue); // variable function call, calling functions like tx_myext_myeval(value)
68 }
69 } else {
70 newValue = evalFunc.input(theEvalType, newValue);
71 }
72 index++;
73 theEvalType = this.split(evallist, ",", index);
74 }
75 return newValue;
76 }
77 function evalFunc_outputObjValue(FObj,value) {
78 var evallist = FObj.evallist;
79 var index=1;
80 var theEvalType = this.split(evallist, ",", index);
81 var newValue=value;
82 while (theEvalType) {
83 if (theEvalType != 'required') {
84 newValue = evalFunc.output(theEvalType, value, FObj);
85 }
86 index++;
87 theEvalType = this.split(evallist, ",", index);
88 }
89 return newValue;
90 }
91 function evalFunc_caseSwitch(type,inVal) {
92 var theVal = ''+inVal;
93 var newString = '';
94 switch (type) {
95 case "alpha":
96 case "num":
97 case "alphanum":
98 case "alphanum_x":
99 for (var a=0;a<theVal.length;a++) {
100 var theChar = theVal.substr(a,1);
101 var special = (theChar=='_'||theChar=='-');
102 var alpha = (theChar>='a'&&theChar<='z') || (theChar>='A'&&theChar<='Z');
103 var num = (theChar>='0' && theChar<='9');
104 switch(type) {
105 case "alphanum": special=0; break;
106 case "alpha": num=0; special=0; break;
107 case "num": alpha=0; special=0; break;
108 }
109 if (alpha || num || theChar==' ' || special) {
110 newString+=theChar;
111 }
112 }
113 break;
114 case "is_in":
115 if (this.isInString) {
116 for (var a=0;a<theVal.length;a++) {
117 var theChar = theVal.substr(a,1);
118 if (this.isInString.indexOf(theChar)!=-1) {
119 newString+=theChar;
120 }
121 }
122 } else {newString = theVal;}
123 break;
124 case "nospace":
125 newString = this.noSpace(theVal);
126 break;
127 case "upper":
128 newString = theVal.toUpperCase();
129 break;
130 case "lower":
131 newString = theVal.toLowerCase();
132 break;
133 default:
134 return inVal;
135 }
136 return newString;
137 }
138 function evalFunc_parseInt(value) {
139 var theVal = ''+value;
140 if (!value) return 0;
141 for (var a=0;a<theVal.length;a++) {
142 if (theVal.substr(a,1)!='0') {
143 return parseInt(theVal.substr(a,theVal.length)) || 0;
144 }
145 }
146 return 0;
147 }
148 function evalFunc_getNumChars(value) {
149 var theVal = ''+value;
150 if (!value) return 0;
151 var outVal="";
152 for (var a=0;a<theVal.length;a++) {
153 if (theVal.substr(a,1)==parseInt(theVal.substr(a,1))) {
154 outVal+=theVal.substr(a,1);
155 }
156 }
157 return outVal;
158 }
159 function evalFunc_parseDouble(value) {
160 var theVal = ''+value;
161 var dec=0;
162 if (!value) return 0;
163 for (var a=theVal.length; a>0; a--) {
164 if (theVal.substr(a-1,1)=='.' || theVal.substr(a-1,1)==',') {
165 dec = theVal.substr(a);
166 theVal = theVal.substr(0,a-1);
167 break;
168 }
169 }
170 dec = this.getNumChars(dec)+'00';
171 theVal=this.parseInt(this.noSpace(theVal))+TS.decimalSign+dec.substr(0,2);
172
173 return theVal;
174 }
175 function evalFunc_noSpace(value) {
176 var theVal = ''+value;
177 var newString="";
178 for (var a=0;a<theVal.length;a++) {
179 var theChar = theVal.substr(a,1);
180 if (theChar!=' ') {
181 newString+=theChar;
182 }
183 }
184 return newString;
185 }
186 function evalFunc_ltrim(value) {
187 var theVal = ''+value;
188 if (!value) return '';
189 for (var a=0;a<theVal.length;a++) {
190 if (theVal.substr(a,1)!=' ') {
191 return theVal.substr(a,theVal.length);
192 }
193 }
194 return '';
195 }
196 function evalFunc_btrim(value) {
197 var theVal = ''+value;
198 if (!value) return '';
199 for (var a=theVal.length;a>0;a--) {
200 if (theVal.substr(a-1,1)!=' ') {
201 return theVal.substr(0,a);
202 }
203 }
204 return '';
205 }
206 function evalFunc_splitSingle(value) {
207 var theVal = ''+value;
208 this.values = new Array();
209 this.pointer = 3;
210 this.values[1]=theVal.substr(0,2);
211 this.values[2]=theVal.substr(2,2);
212 this.values[3]=theVal.substr(4,10);
213 }
214 function evalFunc_split(value) {
215 this.values = new Array();
216 this.valPol = new Array();
217 this.pointer = 0;
218 var numberMode = 0;
219 var theVal = "";
220 value+=" ";
221 for (var a=0;a<value.length;a++) {
222 var theChar = value.substr(a,1);
223 if (theChar<"0" || theChar>"9") {
224 if (numberMode) {
225 this.pointer++;
226 this.values[this.pointer]=theVal;
227 theVal = "";
228 numberMode=0;
229 }
230 if (theChar=="+" || theChar=="-") {
231 this.valPol[this.pointer+1] = theChar;
232 }
233 } else {
234 theVal+=theChar;
235 numberMode=1;
236 }
237 }
238 }
239 function evalFunc_input(type,inVal) {
240 if (type=="md5") {
241 return MD5(inVal);
242 }
243 if (type=="trim") {
244 return this.ltrim(this.btrim(inVal));
245 }
246 if (type=="int") {
247 return this.parseInt(inVal);
248 }
249 if (type=="double2") {
250 return this.parseDouble(inVal);
251 }
252
253 var today = new Date();
254 var add=0;
255 var value = this.ltrim(inVal);
256 var values = new evalFunc_split(value);
257 var theCmd = value.substr(0,1);
258 value = this.caseSwitch(type,value);
259 if (value=="") {
260 return "";
261 return 0; // Why would I ever return a zero??? (20/12/01)
262 }
263 switch (type) {
264 case "datetime":
265 switch (theCmd) {
266 case "d":
267 case "t":
268 case "n":
269 if (values.valPol[1]) {
270 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
271 }
272 break;
273 case "+":
274 case "-":
275 if (values.valPol[1]) {
276 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
277 }
278 break;
279 default:
280 var index = value.indexOf(' ');
281 if (index!=-1) {
282 this.lastTime = this.input("date",value.substr(index,value.length)) + this.input("time",value.substr(0,index));
283 }
284 }
285 this.lastTime+=add*24*60*60;
286 return this.lastTime;
287 break;
288 case "year":
289 switch (theCmd) {
290 case "d":
291 case "t":
292 case "n":
293 if (values.valPol[1]) {
294 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
295 }
296 break;
297 case "+":
298 case "-":
299 if (values.valPol[1]) {
300 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
301 }
302 break;
303 default:
304 if (values.valPol[2]) {
305 add = this.pol(values.valPol[2],this.parseInt(values.values[2]));
306 }
307 var year = (values.values[1])?this.parseInt(values.values[1]):this.getYear(today);
308 if ( (year>=0&&year<38) || (year>=70&&year<100) || (year>=1902&&year<2038) ) {
309 if (year<100) {
310 year = (year<38) ? year+=2000 : year+=1900;
311 }
312 } else {
313 year = this.getYear(today);
314 }
315 this.lastYear = year
316 }
317 this.lastYear+=add;
318 return this.lastYear;
319 break;
320 case "date":
321 switch (theCmd) {
322 case "d":
323 case "t":
324 case "n":
325 if (values.valPol[1]) {
326 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
327 }
328 break;
329 case "+":
330 case "-":
331 if (values.valPol[1]) {
332 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
333 }
334 break;
335 default:
336 var index = 4;
337 if (values.valPol[index]) {
338 add = this.pol(values.valPol[index],this.parseInt(values.values[index]));
339 }
340 if (values.values[1] && values.values[1].length>2) {
341 if (values.valPol[2]) {
342 add = this.pol(values.valPol[2],this.parseInt(values.values[2]));
343 }
344 var temp = values.values[1];
345 values = new evalFunc_splitSingle(temp);
346 }
347
348 var year = (values.values[3])?this.parseInt(values.values[3]):this.getYear(today);
349 if ( (year>=0&&year<38) || (year>=70&&year<100) || (year>=1902&&year<2038) ) {
350 if (year<100) {
351 year = (year<38) ? year+=2000 : year+=1900;
352 }
353 } else {
354 year = this.getYear(today);
355 }
356 var month = (values.values[this.USmode?1:2])?this.parseInt(values.values[this.USmode?1:2]):today.getUTCMonth()+1;
357 var day = (values.values[this.USmode?2:1])?this.parseInt(values.values[this.USmode?2:1]):today.getUTCDate();
358
359 var theTime = new Date(parseInt(year), parseInt(month)-1, parseInt(day));
360
361 // Substract timezone offset from client
362 this.lastDate = this.getTimestamp(theTime);
363 theTime.setTime((this.lastDate - theTime.getTimezoneOffset()*60)*1000);
364 this.lastDate = this.getTimestamp(theTime);
365 }
366 this.lastDate+=add*24*60*60;
367 return this.lastDate;
368 break;
369 case "time":
370 case "timesec":
371 switch (theCmd) {
372 case "d":
373 case "t":
374 case "n":
375 if (values.valPol[1]) {
376 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
377 }
378 break;
379 case "+":
380 case "-":
381 if (values.valPol[1]) {
382 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
383 }
384 break;
385 default:
386 var index = (type=="timesec")?4:3;
387 if (values.valPol[index]) {
388 add = this.pol(values.valPol[index],this.parseInt(values.values[index]));
389 }
390 if (values.values[1] && values.values[1].length>2) {
391 if (values.valPol[2]) {
392 add = this.pol(values.valPol[2],this.parseInt(values.values[2]));
393 }
394 var temp = values.values[1];
395 values = new evalFunc_splitSingle(temp);
396 }
397 var sec = (values.values[3])?this.parseInt(values.values[3]):today.getUTCSeconds();
398 if (sec > 59) {sec=59;}
399 var min = (values.values[2])?this.parseInt(values.values[2]):today.getUTCMinutes();
400 if (min > 59) {min=59;}
401 var hour = (values.values[1])?this.parseInt(values.values[1]):today.getUTCHours();
402 if (hour > 23) {hour=23;}
403
404 var theTime = new Date(this.getYear(today), today.getUTCMonth(), today.getUTCDate(), hour, min, ((type=="timesec")?sec:0));
405
406 this.lastTime = this.getTimestamp(theTime);
407 theTime.setTime((this.lastTime - theTime.getTimezoneOffset()*60)*1000);
408 this.lastTime = this.getTime(theTime);
409 }
410 this.lastTime+=add*60;
411 if (this.lastTime<0) {this.lastTime+=24*60*60;}
412 return this.lastTime;
413 break;
414 default:
415 return value;
416 }
417 }
418 function evalFunc_output(type,value,FObj) {
419 var theString = "";
420 switch (type) {
421 case "date":
422 if (!parseInt(value)) {return '';}
423 var theTime = new Date(parseInt(value) * 1000);
424 if (this.USmode) {
425 theString = (theTime.getUTCMonth()+1)+'-'+theTime.getUTCDate()+'-'+this.getYear(theTime);
426 } else {
427 theString = theTime.getUTCDate()+'-'+(theTime.getUTCMonth()+1)+'-'+this.getYear(theTime);
428 }
429 break;
430 case "datetime":
431 if (!parseInt(value)) {return '';}
432 theString = this.output("time",value)+' '+this.output("date",value);
433 break;
434 case "time":
435 case "timesec":
436 if (!parseInt(value)) {return '';}
437 var theTime = new Date(parseInt(value) * 1000);
438 var h = theTime.getUTCHours();
439 var m = theTime.getUTCMinutes();
440 var s = theTime.getUTCSeconds();
441 theString = h+':'+((m<10)?'0':'')+m + ((type=="timesec")?':'+((s<10)?'0':'')+s:'');
442 break;
443 case "password":
444 theString = (value) ? TS.passwordDummy : "";
445 break;
446 case "int":
447 theString = (FObj.checkbox && value==FObj.checkboxValue)?'':value;
448 break;
449 default:
450 theString = value;
451 }
452 return theString;
453 }
454 function evalFunc_getSecs(timeObj) {
455 return Math.round(timeObj.getUTCSeconds()/1000);
456 }
457 // Seconds since midnight:
458 function evalFunc_getTime(timeObj) {
459 return timeObj.getUTCHours()*60*60+timeObj.getUTCMinutes()*60+Math.round(timeObj.getUTCSeconds()/1000);
460 }
461 function evalFunc_getYear(timeObj) {
462 return timeObj.getUTCFullYear();
463 }
464 // Seconds since midnight with client timezone offset:
465 function evalFunc_getTimeSecs(timeObj) {
466 return timeObj.getHours()*60*60+timeObj.getMinutes()*60+timeObj.getSeconds();
467 }
468 function evalFunc_getDate(timeObj) {
469 var theTime = new Date(this.getYear(timeObj), timeObj.getUTCMonth(), timeObj.getUTCDate());
470 return this.getTimestamp(theTime);
471 }
472 function evalFunc_dummy (evallist,is_in,checkbox,checkboxValue) {
473 this.evallist = evallist;
474 this.is_in = is_in;
475 this.checkboxValue = checkboxValue;
476 this.checkbox = checkbox;
477 }
478 function evalFunc_splitStr(theStr1, delim, index) {
479 var theStr = ''+theStr1;
480 var lengthOfDelim = delim.length;
481 sPos = -lengthOfDelim;
482 if (index<1) {index=1;}
483 for (a=1; a<index; a++) {
484 sPos = theStr.indexOf(delim, sPos+lengthOfDelim);
485 if (sPos==-1) {return null;}
486 }
487 ePos = theStr.indexOf(delim, sPos+lengthOfDelim);
488 if(ePos == -1) {ePos = theStr.length;}
489 return (theStr.substring(sPos+lengthOfDelim,ePos));
490 }
491 function evalFunc_getTimestamp(timeObj) {
492 return Date.parse(timeObj)/1000;
493 }