[TASK] Prevent console warning in jsfunc.evalfield.js
[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 || theChar==' ' || 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 }
264 switch (type) {
265 case "datetime":
266 switch (theCmd) {
267 case "d":
268 case "t":
269 case "n":
270 this.lastTime = this.convertClientTimestampToUTC(this.getTimestamp(today), 0);
271 if (values.valPol[1]) {
272 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
273 }
274 break;
275 case "+":
276 case "-":
277 if (this.lastTime == 0) {
278 this.lastTime = this.convertClientTimestampToUTC(this.getTimestamp(today), 0);
279 }
280 if (values.valPol[1]) {
281 add = this.pol(values.valPol[1], this.parseInt(values.values[1]));
282 }
283 break;
284 default:
285 var index = value.indexOf(' ');
286 if (index!=-1) {
287 var dateVal = this.input("date", value.substr(index,value.length));
288 // set refDate so that evalFunc_input on time will work with correct DST information
289 this.refDate = new Date(dateVal*1000);
290 this.lastTime = dateVal + this.input("time", value.substr(0,index));
291 } else {
292 // only date, no time
293 this.lastTime = this.input("date", value);
294 }
295 }
296 this.lastTime+=add*24*60*60;
297 return this.lastTime;
298 break;
299 case "year":
300 switch (theCmd) {
301 case "d":
302 case "t":
303 case "n":
304 this.lastYear = this.getYear(today);
305 if (values.valPol[1]) {
306 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
307 }
308 break;
309 case "+":
310 case "-":
311 if (values.valPol[1]) {
312 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
313 }
314 break;
315 default:
316 if (values.valPol[2]) {
317 add = this.pol(values.valPol[2],this.parseInt(values.values[2]));
318 }
319 var year = (values.values[1])?this.parseInt(values.values[1]):this.getYear(today);
320 if ((year >= 0 && year < 38) || (year >= 70 && year<100) || (year >= 1902 && year < 2038)) {
321 if (year<100) {
322 year = (year<38) ? year+=2000 : year+=1900;
323 }
324 } else {
325 year = this.getYear(today);
326 }
327 this.lastYear = year;
328 }
329 this.lastYear+=add;
330 return this.lastYear;
331 break;
332 case "date":
333 switch (theCmd) {
334 case "d":
335 case "t":
336 case "n":
337 this.lastDate = this.getTimestamp(today);
338 if (values.valPol[1]) {
339 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
340 }
341 break;
342 case "+":
343 case "-":
344 if (values.valPol[1]) {
345 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
346 }
347 break;
348 default:
349 var index = 4;
350 if (values.valPol[index]) {
351 add = this.pol(values.valPol[index],this.parseInt(values.values[index]));
352 }
353 if (values.values[1] && values.values[1].length>2) {
354 if (values.valPol[2]) {
355 add = this.pol(values.valPol[2],this.parseInt(values.values[2]));
356 }
357 var temp = values.values[1];
358 values = new evalFunc_splitSingle(temp);
359 }
360
361 var year = (values.values[3])?this.parseInt(values.values[3]):this.getYear(today);
362 if ((year >= 0 && year < 38) || (year >= 70 && year < 100) || (year >= 1902 && year<2038)) {
363 if (year<100) {
364 year = (year<38) ? year+=2000 : year+=1900;
365 }
366 } else {
367 year = this.getYear(today);
368 }
369 var month = (values.values[this.USmode?1:2])?this.parseInt(values.values[this.USmode?1:2]):today.getUTCMonth()+1;
370 var day = (values.values[this.USmode?2:1])?this.parseInt(values.values[this.USmode?2:1]):today.getUTCDate();
371
372 var theTime = new Date(parseInt(year), parseInt(month)-1, parseInt(day));
373
374 // Substract timezone offset from client
375 this.lastDate = this.convertClientTimestampToUTC(this.getTimestamp(theTime), 0);
376 }
377 this.lastDate+=add*24*60*60;
378 return this.lastDate;
379 break;
380 case "time":
381 case "timesec":
382 switch (theCmd) {
383 case "d":
384 case "t":
385 case "n":
386 this.lastTime = this.getTimeSecs(today);
387 if (values.valPol[1]) {
388 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
389 }
390 break;
391 case "+":
392 case "-":
393 if (this.lastTime == 0) {
394 this.lastTime = this.getTimeSecs(today);
395 }
396 if (values.valPol[1]) {
397 add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
398 }
399 break;
400 default:
401 var index = (type=="timesec")?4:3;
402 if (values.valPol[index]) {
403 add = this.pol(values.valPol[index],this.parseInt(values.values[index]));
404 }
405 if (values.values[1] && values.values[1].length>2) {
406 if (values.valPol[2]) {
407 add = this.pol(values.valPol[2],this.parseInt(values.values[2]));
408 }
409 var temp = values.values[1];
410 values = new evalFunc_splitSingle(temp);
411 }
412 var sec = (values.values[3])?this.parseInt(values.values[3]):today.getUTCSeconds();
413 if (sec > 59) {sec=59;}
414 var min = (values.values[2])?this.parseInt(values.values[2]):today.getUTCMinutes();
415 if (min > 59) {min=59;}
416 var hour = (values.values[1])?this.parseInt(values.values[1]):today.getUTCHours();
417 if (hour >= 24) {hour=0;}
418
419 var theTime = new Date(this.getYear(this.refDate), this.refDate.getUTCMonth(), this.refDate.getUTCDate(), hour, min, ((type=="timesec")?sec:0));
420
421 // Substract timezone offset from client
422 this.lastTime = this.convertClientTimestampToUTC(this.getTimestamp(theTime), 1);
423 }
424 this.lastTime+=add*60;
425 if (this.lastTime<0) {this.lastTime+=24*60*60;}
426 return this.lastTime;
427 break;
428 default:
429 return value;
430 }
431 }
432 function evalFunc_output(type,value,FObj) {
433 var theString = "";
434 switch (type) {
435 case "date":
436 if (!parseInt(value)) {return '';}
437 var theTime = new Date(parseInt(value) * 1000);
438 if (this.USmode) {
439 theString = (theTime.getUTCMonth()+1)+'-'+theTime.getUTCDate()+'-'+this.getYear(theTime);
440 } else {
441 theString = theTime.getUTCDate()+'-'+(theTime.getUTCMonth()+1)+'-'+this.getYear(theTime);
442 }
443 break;
444 case "datetime":
445 if (!parseInt(value)) {return '';}
446 theString = this.output("time",value)+' '+this.output("date",value);
447 break;
448 case "time":
449 case "timesec":
450 if (!parseInt(value)) {return '';}
451 var theTime = new Date(parseInt(value) * 1000);
452 var h = theTime.getUTCHours();
453 var m = theTime.getUTCMinutes();
454 var s = theTime.getUTCSeconds();
455 theString = h+':'+((m<10)?'0':'')+m + ((type=="timesec")?':'+((s<10)?'0':'')+s:'');
456 break;
457 case "password":
458 theString = (value) ? TS.passwordDummy : "";
459 break;
460 case "int":
461 theString = (FObj.checkbox && value==FObj.checkboxValue)?'':value;
462 break;
463 default:
464 theString = value;
465 }
466 return theString;
467 }
468 function evalFunc_getSecs(timeObj) {
469 return timeObj.getUTCSeconds();
470 }
471 // Seconds since midnight:
472 function evalFunc_getTime(timeObj) {
473 return timeObj.getUTCHours() * 60 * 60 + timeObj.getUTCMinutes() * 60 + this.getSecs(timeObj);
474 }
475 function evalFunc_getYear(timeObj) {
476 return timeObj.getUTCFullYear();
477 }
478 // Seconds since midnight with client timezone offset:
479 function evalFunc_getTimeSecs(timeObj) {
480 return timeObj.getHours()*60*60+timeObj.getMinutes()*60+timeObj.getSeconds();
481 }
482 function evalFunc_getDate(timeObj) {
483 var theTime = new Date(this.getYear(timeObj), timeObj.getUTCMonth(), timeObj.getUTCDate());
484 return this.getTimestamp(theTime);
485 }
486 function evalFunc_dummy (evallist,is_in,checkbox,checkboxValue) {
487 this.evallist = evallist;
488 this.is_in = is_in;
489 this.checkboxValue = checkboxValue;
490 this.checkbox = checkbox;
491 }
492 function evalFunc_splitStr(theStr1, delim, index) {
493 var theStr = ''+theStr1;
494 var lengthOfDelim = delim.length;
495 sPos = -lengthOfDelim;
496 if (index<1) {index=1;}
497 for (a=1; a<index; a++) {
498 sPos = theStr.indexOf(delim, sPos+lengthOfDelim);
499 if (sPos==-1) {return null;}
500 }
501 ePos = theStr.indexOf(delim, sPos+lengthOfDelim);
502 if(ePos == -1) {ePos = theStr.length;}
503 return (theStr.substring(sPos+lengthOfDelim,ePos));
504 }
505 function evalFunc_getTimestamp(timeObj) {
506 return Date.parse(timeObj)/1000;
507 }
508
509 // Substract timezone offset from client to a timestamp to get UTC-timestamp to be send to server
510 function evalFunc_convertClientTimestampToUTC(timestamp, timeonly) {
511 var timeObj = new Date(timestamp*1000);
512 timeObj.setTime((timestamp - timeObj.getTimezoneOffset()*60)*1000);
513 if (timeonly) {
514 // only seconds since midnight
515 return this.getTime(timeObj);
516 } else {
517 // seconds since the "unix-epoch"
518 return this.getTimestamp(timeObj);
519 }
520 }