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