[ Index ]
 

Code source de Typo3 4.1.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/t3lib/ -> jsfunc.evalfield.js (source)

   1  /***************************************************************
   2  *
   3  *  Evaluation of TYPO3 form field content
   4  *
   5  * $Id: jsfunc.evalfield.js 2000 2007-02-06 07:15:40Z mundaun $
   6  *
   7  *
   8  *
   9  *  Copyright notice
  10  *
  11  *  (c) 1998-2007 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.caseSwitch = evalFunc_caseSwitch;
  38      this.evalObjValue = evalFunc_evalObjValue;
  39      this.outputObjValue = evalFunc_outputObjValue;
  40      this.split = evalFunc_splitStr;
  41      this.pol = evalFunc_pol;
  42  
  43      this.ltrim = evalFunc_ltrim;
  44      this.btrim = evalFunc_btrim;
  45      var today = new Date();
  46       this.lastYear = this.getYear(today);
  47       this.lastDate = this.getSecs(today);
  48       this.lastTime = this.getTimeSecs(today);
  49      this.isInString = '';
  50      this.USmode = 0;
  51  }
  52  function evalFunc_pol(fortegn, value)    {
  53      return eval (((fortegn=="-")?'-':'')+value);
  54  }
  55  function evalFunc_evalObjValue(FObj,value)    {
  56      var evallist = FObj.evallist;
  57      this.isInString = (FObj.is_in) ? ''+FObj.is_in : '';
  58      var index=1;
  59      var theEvalType = (FObj.evallist) ? this.split(evallist, ",", index) : false;
  60      var newValue=value;
  61      while (theEvalType) {
  62          if (theEvalType.slice(0, 3) == 'tx_') {
  63              if(typeof window[theEvalType] == 'function') {
  64                  newValue = window[theEvalType](newValue);    // variable function call, calling functions like tx_myext_myeval(value)
  65              }
  66          } else {
  67              newValue = evalFunc.input(theEvalType, newValue);
  68          }
  69          index++;
  70          theEvalType = this.split(evallist, ",", index);
  71      }
  72      return newValue;
  73  }
  74  function evalFunc_outputObjValue(FObj,value)    {
  75      var evallist = FObj.evallist;
  76      var index=1;
  77      var theEvalType = this.split(evallist, ",", index);
  78      var newValue=value;
  79      while (theEvalType) {
  80          newValue = evalFunc.output(theEvalType, value, FObj);
  81          index++;
  82          theEvalType = this.split(evallist, ",", index);
  83      }
  84      return newValue;
  85  }
  86  function evalFunc_caseSwitch(type,inVal)    {
  87      var theVal = ''+inVal;
  88      var newString = '';
  89      switch (type)    {
  90          case "alpha":
  91          case "num":
  92          case "alphanum":
  93          case "alphanum_x":
  94              for (var a=0;a<theVal.length;a++)    {
  95                  var theChar = theVal.substr(a,1);
  96                  var special = (theChar=='_'||theChar=='-');
  97                  var alpha = (theChar>='a'&&theChar<='z') || (theChar>='A'&&theChar<='Z');
  98                  var num = (theChar>='0' && theChar<='9');
  99                  switch(type)    {
 100                      case "alphanum":    special=0;        break;
 101                      case "alpha":    num=0; special=0;        break;
 102                      case "num":    alpha=0; special=0;        break;
 103                  }
 104                  if (alpha || num || theChar==' ' || special)    {
 105                      newString+=theChar;
 106                  }
 107              }
 108          break;
 109          case "is_in":
 110              if (this.isInString)    {
 111                  for (var a=0;a<theVal.length;a++)    {
 112                      var theChar = theVal.substr(a,1);
 113                      if (this.isInString.indexOf(theChar)!=-1)    {
 114                          newString+=theChar;
 115                      }
 116                  }
 117              } else {newString = theVal;}
 118          break;
 119          case "nospace":
 120              newString = this.noSpace(theVal);
 121          break;
 122          case "upper":
 123              newString = theVal.toUpperCase();
 124          break;
 125          case "lower":
 126              newString = theVal.toLowerCase();
 127          break;
 128          default:
 129              return inVal;
 130      }
 131      return newString;
 132  }
 133  function evalFunc_parseInt(value)    {
 134      var theVal = ''+value;
 135      if (!value)    return 0;
 136      for (var a=0;a<theVal.length;a++)    {
 137          if (theVal.substr(a,1)!='0')    {
 138              return parseInt(theVal.substr(a,theVal.length)) || 0;
 139          }
 140      }
 141      return 0;
 142  }
 143  function evalFunc_getNumChars(value)    {
 144      var theVal = ''+value;
 145      if (!value)    return 0;
 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      var dec=0;
 157      if (!value)    return 0;
 158      for (var a=theVal.length; a>0; a--)    {
 159          if (theVal.substr(a-1,1)=='.' || theVal.substr(a-1,1)==',')    {
 160              dec = theVal.substr(a);
 161              theVal = theVal.substr(0,a-1);
 162              break;
 163          }
 164      }
 165      dec = this.getNumChars(dec)+'00';
 166      theVal=this.parseInt(this.noSpace(theVal))+TS.decimalSign+dec.substr(0,2);
 167  
 168      return theVal;
 169  }
 170  function evalFunc_noSpace(value)    {
 171      var theVal = ''+value;
 172      var newString="";
 173      for (var a=0;a<theVal.length;a++)    {
 174          var theChar = theVal.substr(a,1);
 175          if (theChar!=' ')    {
 176              newString+=theChar;
 177          }
 178      }
 179      return newString;
 180  }
 181  function evalFunc_ltrim(value)    {
 182      var theVal = ''+value;
 183      if (!value)    return '';
 184      for (var a=0;a<theVal.length;a++)    {
 185          if (theVal.substr(a,1)!=' ')    {
 186              return theVal.substr(a,theVal.length);
 187          }
 188      }
 189      return '';
 190  }
 191  function evalFunc_btrim(value)    {
 192      var theVal = ''+value;
 193      if (!value)    return '';
 194      for (var a=theVal.length;a>0;a--)    {
 195          if (theVal.substr(a-1,1)!=' ')    {
 196              return theVal.substr(0,a);
 197          }
 198      }
 199      return '';
 200  }
 201  function evalFunc_splitSingle(value)    {
 202      var theVal = ''+value;
 203      this.values = new Array();
 204      this.pointer = 3;
 205      this.values[1]=theVal.substr(0,2);
 206      this.values[2]=theVal.substr(2,2);
 207      this.values[3]=theVal.substr(4,10);
 208  }
 209  function evalFunc_split(value)    {
 210      this.values = new Array();
 211      this.valPol = new Array();
 212      this.pointer = 0;
 213      var numberMode = 0;
 214      var theVal = "";
 215      value+=" ";
 216      for (var a=0;a<value.length;a++)    {
 217          var theChar = value.substr(a,1);
 218          if (theChar<"0" || theChar>"9")    {
 219              if (numberMode)    {
 220                  this.pointer++;
 221                  this.values[this.pointer]=theVal;
 222                  theVal = "";
 223                  numberMode=0;
 224              }
 225              if (theChar=="+" || theChar=="-")    {
 226                  this.valPol[this.pointer+1] = theChar;
 227              }
 228          } else {
 229              theVal+=theChar;
 230              numberMode=1;
 231          }
 232      }
 233  }
 234  function evalFunc_input(type,inVal)    {
 235      if (type=="md5") {
 236          return MD5(inVal);
 237      }
 238      if (type=="trim") {
 239          return this.ltrim(this.btrim(inVal));
 240      }
 241      if (type=="int") {
 242          return this.parseInt(inVal);
 243      }
 244      if (type=="double2") {
 245          return this.parseDouble(inVal);
 246      }
 247  
 248      var today = new Date()
 249      var add=0;
 250      var value = this.ltrim(inVal);
 251      var values = new evalFunc_split(value);
 252      var theCmd = value.substr(0,1);
 253      value = this.caseSwitch(type,value);
 254      if (value=="") {
 255          return "";
 256          return 0;    // Why would I ever return a zero??? (20/12/01)
 257      }
 258      switch (type)    {
 259          case "datetime":
 260              switch (theCmd)    {
 261                  case "d":
 262                  case "t":
 263                  case "n":
 264                      var theTime = new Date(this.getYear(today), today.getMonth(), today.getDate(), today.getHours(), today.getMinutes());
 265                      this.lastDate = this.getSecs(theTime)
 266                      if (values.valPol[1])    {
 267                          add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
 268                      }
 269                  break;
 270                  case "+":
 271                  case "-":
 272                      if (values.valPol[1])    {
 273                          add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
 274                      }
 275                  break;
 276                  default:
 277                      var index = value.indexOf(' ');
 278                      if (index!=-1)    {
 279                          var theSecs = this.input("date",value.substr(index,value.length)) + this.input("time",value.substr(0,index));
 280                          this.lastDate = theSecs;
 281                      }
 282              }
 283              this.lastDate+=add*24*60*60;
 284              return this.lastDate;
 285          break;
 286          case "year":
 287              switch (theCmd)    {
 288                  case "d":
 289                  case "t":
 290                  case "n":
 291                      var theTime = today;
 292                      this.lastYear = this.getYear(theTime);
 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>=1970&&year<2038)    )    {
 309                              if (year<100)    {
 310                                  year = (year<38) ? year+=2000 : year+=1900;
 311                              }
 312                          } else {year = this.getYear(today);}
 313                      this.lastYear = year
 314              }
 315              this.lastYear+=add;
 316              return this.lastYear;
 317          break;
 318          case "date":
 319              switch (theCmd)    {
 320                  case "d":
 321                  case "t":
 322                  case "n":
 323                      var theTime = new Date(this.getYear(today), today.getMonth(), today.getDate());
 324                      this.lastDate = this.getSecs(theTime);
 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>=1970&&year<2038)    )    {
 350                              if (year<100)    {
 351                                  year = (year<38) ? year+=2000 : year+=1900;
 352                              }
 353                          } else {year = this.getYear(today);}
 354                      var month = (values.values[this.USmode?1:2])?this.parseInt(values.values[this.USmode?1:2]):today.getMonth()+1;
 355                          if (month > 12)    {month=12;}
 356                          if (month < 1)    {month=1;}
 357                      var day = (values.values[this.USmode?2:1])?this.parseInt(values.values[this.USmode?2:1]):today.getDate();
 358                          if (day > 31)    {day=31;}
 359                          if (day < 1)    {day=1;}
 360                      if (''+day+'-'+month+'-'+year == "1-1-1970")    {
 361                          var theTime = new Date();  theTime.setTime(0);
 362                      } else {
 363                          var theTime = new Date(parseInt(year), parseInt(month)-1, parseInt(day));
 364                      }
 365                      this.lastDate = this.getSecs(theTime)
 366              }
 367              this.lastDate+=add*24*60*60;
 368              if (this.lastDate<0) {this.lastDate=0;}
 369              return this.lastDate;
 370          break;
 371          case "time":
 372          case "timesec":
 373              switch (theCmd)    {
 374                  case "d":
 375                  case "t":
 376                  case "n":
 377                      var theTime = new Date(this.getYear(today), today.getMonth(), today.getDate(), today.getHours(), today.getMinutes(), ((type=="timesec")?today.getSeconds():0));
 378                      this.lastTime = this.getTimeSecs(theTime);
 379                      if (values.valPol[1])    {
 380                          add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
 381                      }
 382                  break;
 383                  case "+":
 384                  case "-":
 385                      if (values.valPol[1])    {
 386                          add = this.pol(values.valPol[1],this.parseInt(values.values[1]));
 387                      }
 388                  break;
 389                  default:
 390                      var index = (type=="timesec")?4:3;
 391                      if (values.valPol[index])    {
 392                          add = this.pol(values.valPol[index],this.parseInt(values.values[index]));
 393                      }
 394                      if (values.values[1] && values.values[1].length>2)    {
 395                          if (values.valPol[2])    {
 396                              add = this.pol(values.valPol[2],this.parseInt(values.values[2]));
 397                          }
 398                          var temp = values.values[1];
 399                          values = new evalFunc_splitSingle(temp);
 400                      }
 401                      var sec = (values.values[3])?this.parseInt(values.values[3]):today.getSeconds();
 402                          if (sec > 59)    {sec=59;}
 403                      var min = (values.values[2])?this.parseInt(values.values[2]):today.getMinutes();
 404                          if (min > 59)    {min=59;}
 405                      var hour = (values.values[1])?this.parseInt(values.values[1]):today.getHours();
 406                          if (hour > 23)    {hour=23;}
 407                      var theTime = new Date(this.getYear(today), today.getMonth(), today.getDate(), hour, min, ((type=="timesec")?sec:0));
 408                      this.lastTime = this.getTimeSecs(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();
 424              theTime.setTime(value*1000);
 425              if (this.USmode)    {
 426                  theString = (theTime.getMonth()+1)+'-'+theTime.getDate()+'-'+this.getYear(theTime);
 427              } else {
 428                  theString = theTime.getDate()+'-'+(theTime.getMonth()+1)+'-'+this.getYear(theTime);
 429              }
 430          break;
 431          case "datetime":
 432              if (!parseInt(value))    {return '';}
 433              var theTime = new Date();
 434              theTime.setTime(value*1000);
 435              theString = this.output("time",this.getTimeSecs(theTime))+' '+this.output("date",value);
 436          break;
 437          case "time":
 438          case "timesec":
 439              if (!parseInt(value))    {return '';}
 440              var theTime = new Date();
 441              theTime.setTime(value*1000);
 442              var h = Math.floor(value/3600);
 443              var m = Math.floor((value-h*3600)/60);
 444              var s = Math.floor(value-h*3600-m*60);
 445              theString = h+':'+((m<10)?'0':'')+m + ((type=="timesec")?':'+((s<10)?'0':'')+s:'');
 446          break;
 447          case "password":
 448              theString = (value)    ? TS.passwordDummy : "";
 449          break;
 450          case "int":
 451              theString = (FObj.checkbox && value==FObj.checkboxValue)?'':value;
 452          break;
 453          default:
 454              theString = value;
 455      }
 456      return theString;
 457  }
 458  function evalFunc_getSecs(timeObj)    {
 459      return Math.round(timeObj.getTime()/1000);
 460  }
 461  function evalFunc_getYear(timeObj)    {
 462      return (timeObj.getYear()>200) ? timeObj.getYear() : (timeObj.getYear()+1900);
 463  }
 464  function evalFunc_getTimeSecs(timeObj)    {
 465      return timeObj.getHours()*60*60+timeObj.getMinutes()*60+timeObj.getSeconds();
 466  }
 467  function evalFunc_dummy (evallist,is_in,checkbox,checkboxValue) {
 468      this.evallist = evallist;
 469      this.is_in = is_in;
 470      this.checkboxValue = checkboxValue;
 471      this.checkbox = checkbox;
 472  }
 473  function evalFunc_splitStr(theStr1, delim, index) {
 474      var theStr = ''+theStr1;
 475      var lengthOfDelim = delim.length;
 476      sPos = -lengthOfDelim;
 477      if (index<1) {index=1;}
 478      for (a=1; a<index; a++)    {
 479          sPos = theStr.indexOf(delim, sPos+lengthOfDelim);
 480          if (sPos==-1)    {return null;}
 481      }
 482      ePos = theStr.indexOf(delim, sPos+lengthOfDelim);
 483      if(ePos == -1)    {ePos = theStr.length;}
 484      return (theStr.substring(sPos+lengthOfDelim,ePos));
 485  }
 486  


Généré le : Sun Nov 25 17:13:16 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics