[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/com_vtiger_workflow/expression_engine/ -> VTExpressionEvaluater.inc (source)

   1  <?php
   2  /*+*******************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   ******************************************************************************/
  10  interface VTExpressionEnv{
  11  	function get($var);
  12  }
  13  
  14  function __vt_add($arr){
  15      if(sizeof($arr)==1){
  16          return $arr[0];
  17      }else{
  18          if(strlen(substr(strrchr($arr[0], "."), 1))>strlen(substr(strrchr($arr[1], "."), 1))){
  19              $maxDigit = strlen(substr(strrchr($arr[0], "."), 1));
  20          } else {
  21              $maxDigit = strlen(substr(strrchr($arr[1], "."), 1));
  22          }
  23          return bcadd($arr[0], $arr[1] , $maxDigit);
  24      }
  25  }
  26  
  27  function __vt_sub($arr){
  28      if(sizeof($arr)==1){
  29          return -$arr[0];
  30      }else{
  31          if(strlen(substr(strrchr($arr[0], "."), 1))>strlen(substr(strrchr($arr[1], "."), 1))){
  32              $maxDigit = strlen(substr(strrchr($arr[0], "."), 1));
  33          } else {
  34              $maxDigit = strlen(substr(strrchr($arr[1], "."), 1));
  35          }
  36          return bcsub($arr[0], $arr[1], $maxDigit);
  37      }
  38  }
  39  
  40  function __vt_mul($arr){
  41      return $arr[0]*$arr[1];
  42  }
  43  
  44  function __vt_div($arr){
  45      try {
  46          return $arr[0]/$arr[1];
  47      }catch(Exception $e) {
  48          return 0;
  49      }
  50  }
  51  
  52  function __vt_equals($arr){
  53      return $arr[0] == $arr[1];
  54  }
  55  
  56  function __vt_ltequals($arr) {
  57      return $arr[0] <= $arr[1];
  58  }
  59  
  60  function __vt_gtequals($arr) {
  61      return $arr[0] >= $arr[1];
  62  }
  63  
  64  function __vt_lt($arr) {
  65      return $arr[0] < $arr[1];
  66  }
  67  
  68  function __vt_gt($arr) {
  69      return $arr[0] > $arr[1];
  70  }
  71  
  72  function __vt_concat($arr){
  73      return implode($arr);
  74  }
  75  /* Date difference between (input times) or (current time and input time)
  76   *
  77   * @param Array $a $a[0] - Input time1, $a[1] - Input time2
  78   * (if $a[1] is not available $a[0] = Current Time, $a[1] = Input time1)
  79   * @return int difference timestamp
  80   */
  81  
  82  function __vt_time_diff($arr) {
  83  
  84      $time_operand1 = $time_operand2 = 0;
  85      if(count($arr) > 1) {
  86              $time_operand1 = $time1 = $arr[0];
  87              $time_operand2 = $time2 = $arr[1];
  88      } else {
  89  
  90          // Added as we need to compare with the values based on the user date format and timezone
  91          global $default_timezone;
  92  
  93          $time_operand1 = date('Y-m-d H:i:s'); // Current time
  94  
  95          $time_operand2 = $arr[0];
  96      }
  97  
  98      if(empty($time_operand1) || empty($time_operand2)) return 0;
  99  
 100      $time_operand1 = getValidDBInsertDateTimeValue($time_operand1);
 101      $time_operand2 = getValidDBInsertDateTimeValue($time_operand2);
 102      
 103      //to give the difference if it is only time field
 104      if(empty($time_operand1) && empty($time_operand2)) {
 105          $pattern = "/([01]?[0-9]|2[0-3]):[0-5][0-9]/";
 106          if(preg_match($pattern, $time1) && preg_match($pattern, $time2)){
 107              $timeDiff = strtotime($time1) - strtotime($time2);
 108              return date('H:i:s', $timeDiff);
 109          }
 110      }
 111      return (strtotime($time_operand1) - strtotime($time_operand2));
 112  }
 113  /**
 114   * Calculate the time difference (input times) or (current time and input time) and
 115   * convert it into number of days.
 116   * @param Array $a $a[0] - Input time1, $a[1] - Input time2
 117   * (if $a[1] is not available $a[0] = Current Time, $a[1] = Input time1)
 118   * @return int number of days
 119   */
 120  function __vt_time_diffdays($arr) {
 121      $timediff  = __vt_time_diff($arr);
 122      $days_diff = floor($timediff / (60 * 60 * 24));
 123      return $days_diff;
 124  }
 125  
 126  function __vt_add_days($arr) {
 127  
 128      if (count($arr) > 1) {
 129          $baseDate = $arr[0];
 130          $noOfDays = $arr[1];
 131      } else {
 132          $noOfDays = $arr[0];
 133      }
 134      if($baseDate==null || empty($baseDate)) {
 135          $baseDate = date('Y-m-d'); // Current date
 136      }
 137      preg_match('/\d\d\d\d-\d\d-\d\d/', $baseDate, $match);
 138      $baseDate = strtotime($match[0]);
 139      $date = strftime('%Y-%m-%d', $baseDate + ($noOfDays * 24 * 60 * 60));
 140      return $date;
 141  }
 142  
 143  function __vt_sub_days($arr) {
 144  
 145      if (count($arr) > 1) {
 146          $baseDate = $arr[0];
 147          $noOfDays = $arr[1];
 148      } else {
 149          $noOfDays = $arr[0];
 150      }
 151      if($baseDate==null || empty($baseDate)) {
 152          $baseDate = date('Y-m-d'); // Current date
 153      }
 154      preg_match('/\d\d\d\d-\d\d-\d\d/', $baseDate, $match);
 155      $baseDate = strtotime($match[0]);
 156      $date = strftime('%Y-%m-%d', $baseDate - ($noOfDays * 24 * 60 * 60));
 157      return $date;
 158  }
 159  
 160  function __vt_get_date($arr) {
 161      $type = $arr[0];
 162      switch ($type) {
 163          case 'today': return date('Y-m-d');
 164              break;
 165          case 'tomorrow': return date('Y-m-d', strtotime('+1 day'));
 166              break;
 167          case 'yesterday': return date('Y-m-d', strtotime('-1 day'));
 168              break;
 169          default : return date('Y-m-d');
 170              break;
 171      }
 172  }
 173  
 174  function __vt_add_time($arr) {
 175      if(count($arr) > 1) {
 176          $baseTime = $arr[0];
 177          $minutes = $arr[1];
 178      } else {
 179          $baseTime = date('H:i:s');
 180          $minutes = $arr[0];
 181      }
 182      $endTime = strtotime("+$minutes minutes", strtotime($baseTime));
 183      return date('H:i:s',$endTime);
 184  }
 185  
 186  function __vt_sub_time($arr) {
 187      if(count($arr) > 1) {
 188          $baseTime = $arr[0];
 189          $minutes = $arr[1];
 190      } else {
 191          $baseTime = date('H:i:s');
 192          $minutes = $arr[0];
 193      }
 194      $endTime = strtotime("-$minutes minutes", strtotime($baseTime));
 195      return date('H:i:s',$endTime);
 196  }
 197  
 198  /** END * */
 199  class VTFieldExpressionEvaluater{
 200  	function __construct($expr){
 201  
 202          $this->operators = array(
 203                  '+'  => '__vt_add',
 204                  '-'  => '__vt_sub',
 205                  '*'  => '__vt_mul',
 206                  '/'  => '__vt_div',
 207                  '==' => '__vt_equals',
 208                  '<=' => '__vt_ltequals',
 209                  '>=' => '__vt_gtequals',
 210                  '<' => '__vt_lt',
 211                  '>' => '__vt_gt',
 212          );
 213          $this->functions = array(
 214                  'concat'=>'__vt_concat',
 215                  'time_diff' => '__vt_time_diff',
 216                   'time_diffdays' => '__vt_time_diffdays',
 217                  'add_days' => '__vt_add_days',
 218                  'sub_days' => '__vt_sub_days',
 219                  'get_date' => '__vt_get_date',
 220                  'add_time' => '__vt_add_time',
 221                  'sub_time' => '__vt_sub_time'
 222          );
 223  
 224          $this->operations = array_merge($this->functions, $this->operators);
 225          $this->expr = $expr;
 226  
 227      }
 228  
 229  	function evaluate($env){
 230          $this->env = $env;
 231          return $this->exec($this->expr);
 232      }
 233  
 234  	function exec($expr){
 235          if($expr instanceof VTExpressionSymbol){
 236              return $this->env($expr);
 237          }else if($expr instanceof VTExpressionTreeNode){
 238              $op = $expr->getName();
 239              if($op->value=='if'){
 240                  $params = $expr->getParams();
 241                  $cond = $this->exec($params[0]);
 242                  if($cond){
 243                      return $this->exec($params[1]);
 244                  }else{
 245                      return $this->exec($params[2]);
 246                  }
 247              }else{
 248                  $params = array_map(array($this, 'exec'), $expr->getParams());
 249                  $func = $this->operations[$op->value];
 250                  return $func($params);
 251              }
 252          }else{
 253              return $expr;
 254          }
 255      }
 256  
 257  	function env($sym){
 258          if($this->env) {
 259              return $this->env->get($sym->value);
 260          } else {
 261              return $sym->value;
 262          }
 263      }
 264  }
 265  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1