Exemple #1
0
 function evaluate($target)
 {
     $result = Parser::evaluate($this->conditionFunction, $target)->evaluate();
     if ($result == AbstractExpression::$TRUE) {
         return true;
     } else {
         return false;
     }
 }
 /**
  * Applies the Action to the target.
  *
  * @param SugarBean $target
  */
 function fire(&$target)
 {
     require_once "include/Expressions/Expression/AbstractExpression.php";
     $result = Parser::evaluate($this->expression, $target)->evaluate();
     if ($result === AbstractExpression::$FALSE) {
         $target->field_defs[$this->targetField]['hidden'] = true;
     } else {
         $target->field_defs[$this->targetField]['hidden'] = false;
     }
 }
 /**
  * Applies the Action to the target.
  *
  * @param SugarBean $target
  */
 function fire(&$target)
 {
     require_once 'modules/Home/quicksearchQuery.php';
     require_once 'include/QuickSearchDefaults.php';
     $json = getJSONobj();
     $userName = Parser::evaluate($this . expr, $target) . evaluate();
     $qsd = QuickSearchDefaults::getQuickSearchDefaults();
     $data = $qsd->getQSUser();
     $data['modules'] = array("Users");
     $data['conditions'][0]['value'] = $userName;
     $qs = new quicksearchQuery();
     $result = $qs->query($data);
     $resultBean = $json->decodeReal($result);
     print_r($resultBean);
 }
 function display()
 {
     //First load the primary bean
     $focus = BeanFactory::getBean($this->tmodule, $this->id);
     $params = implode(",", json_decode(html_entity_decode($this->params)));
     $result = Parser::evaluate("{$this->function}({$params})", $focus)->evaluate();
     //If the target field isn't a date, convert it to a user formated string
     if ($result instanceof DateTime) {
         global $timedate;
         if (isset($result->isDate) && $result->isDate) {
             $result = $timedate->asUserDate($result);
         } else {
             $result = $timedate->asUser($result);
         }
     }
     echo json_encode($result);
 }
 /**
  * Applies the Action to the target.
  *
  * @param SugarBean $target
  */
 function fire(&$target)
 {
     set_error_handler('handleExpressionError', E_ERROR);
     try {
         $result = Parser::evaluate($this->expression, $target)->evaluate();
     } catch (Exception $e) {
         $GLOBALS['log']->fatal("Exception evaluating expression in SetValueAction, {$this->expression} : {$e->getMessage()}\n{$e->getTraceAsString()}");
         $result = "";
     }
     restore_error_handler();
     $field = $this->targetField;
     $def = array();
     if (!empty($target->field_defs[$field])) {
         $def = $target->field_defs[$field];
     }
     if ($result instanceof DateTime) {
         global $timedate;
         if (isset($def['type']) && ($def['type'] == "datetime" || $def['type'] == "datetimecombo")) {
             $result = DateExpression::roundTime($result);
             $target->{$field} = $timedate->asDb($result);
         } else {
             if (isset($def['type']) && $def['type'] == "date") {
                 $result = DateExpression::roundTime($result);
                 $target->{$field} = $timedate->asDbDate($result);
             } else {
                 //If the target field isn't a date, convert it to a user formated string
                 if (isset($result->isDate) && $result->isDate) {
                     $target->{$field} = $timedate->asUserDate($result);
                 } else {
                     $target->{$field} = $timedate->asUser($result);
                 }
             }
         }
     } else {
         if (isset($def['type']) && $def['type'] == "bool") {
             $target->{$field} = $result === true || $result === AbstractExpression::$TRUE;
         } else {
             if (is_array($result) && $def['type'] != 'multienum') {
                 $target->{$field} = implode(', ', $result);
             } else {
                 $target->{$field} = $result;
             }
         }
     }
 }
 /**
  * Applies the Action to the target.
  *
  * @param SugarBean $target
  *
  * Should only be fired when saving from an edit view and the expression is false.
  */
 public function fire(&$target)
 {
     $result = Parser::evaluate($this->expression, $target)->evaluate();
     if ($result === AbstractExpression::$FALSE) {
         require_once 'modules/ModuleBuilder/parsers/ParserFactory.php';
         require_once 'modules/ModuleBuilder/parsers/constants.php';
         $view = isModuleBWC($target->module_name) ? MB_EDITVIEW : MB_RECORDVIEW;
         $parser = ParserFactory::getParser($view, $target->module_dir);
         $fields = $parser->getFieldsInPanel($this->targetPanel);
         foreach ($fields as $field) {
             unset($target->{$field});
         }
     }
 }
Exemple #7
0
 /**
  * Returns array of linked bean's calculated fields which use relation to
  * the current bean in their formulas
  *
  * @param string $linkName Name of current bean's link
  * @return array
  */
 protected function get_fields_influencing_linked_bean_calc_fields($linkName)
 {
     global $dictionary;
     $result = array();
     if (!$this->load_relationship($linkName)) {
         return $result;
     }
     /** @var Link2 $link */
     $link = $this->{$linkName};
     $relatedModuleName = $link->getRelatedModuleName();
     $relatedBeanName = BeanFactory::getObjectName($relatedModuleName);
     $relatedLinkName = $link->getRelatedModuleLinkName();
     if (empty($relatedBeanName) || empty($dictionary[$relatedBeanName])) {
         $GLOBALS['log']->fatal("Cannot load field defs for {$relatedBeanName}");
         return $result;
     }
     // iterate over related bean fields
     foreach ($dictionary[$relatedBeanName]['fields'] as $def) {
         if (!empty($def['formula'])) {
             $expr = Parser::evaluate($def['formula'], $this);
             $fields = $this->get_formula_related_fields($expr, $relatedLinkName);
             $result = array_merge($result, $fields);
         }
     }
     return array_unique($result);
 }
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/
//change directories to where this file is located.
//this is to make sure it can find dce_config.php
chdir("../../");
require_once 'include/entryPoint.php';
require_once 'include/Expressions/Expression/Parser/Parser.php';
require_once 'modules/Users/User.php';
if (!empty($_REQUEST['expression'])) {
    $admin = new User();
    $admin = $admin->retrieve(1);
    global $current_user, $timezones;
    $current_user = $admin;
    try {
        $expression = Parser::evaluate(from_html($_REQUEST['expression']));
        print_r($expression->evaluate());
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
Exemple #9
0
/*
 * Your installation or use of this SugarCRM file is subject to the applicable
 * terms available at
 * http://support.sugarcrm.com/06_Customer_Center/10_Master_Subscription_Agreements/.
 * If you do not agree to all of the applicable terms or do not have the
 * authority to bind the entity as an authorized representative, then do not
 * install or use this SugarCRM file.
 *
 * Copyright (C) SugarCRM Inc. All rights reserved.
 */
require_once 'Expression/Parser/Parser.php';
if (!empty($_GET['expression'])) {
    $expr = $_GET['expression'];
    try {
        echo Parser::evaluate($expr)->evaluate();
    } catch (Exception $e) {
        echo $e;
    }
    die;
}
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>SUGAR Arithmetic Engine (JS)</title>
Exemple #10
0
 /**
  * Evaluates an expression.
  *
  * @param string	the expression to evaluate
  *
  */
 static function evaluate($expr, $context = false)
 {
     // the function map
     static $FUNCTION_MAP = array();
     // trim spaces, left and right, and remove newlines
     $expr = str_replace("\n", "", trim($expr));
     // check if its a constant and return a constant expression
     $const = Parser::toConstant($expr);
     if (isset($const)) {
         return $const;
     }
     if (preg_match('/^\\$[a-zA-Z0-9_\\-]+$/', $expr)) {
         $var = substr($expr, 1);
         $ret = new SugarFieldExpression($var);
         if ($context) {
             $ret->context = $context;
         }
         return $ret;
     }
     //Related field shorthand
     if (preg_match('/^\\$[a-zA-Z0-9_\\-]+\\.[a-zA-Z0-9_\\-]+$/', $expr)) {
         $link = substr($expr, 1, strpos($expr, ".") - 1);
         $related = substr($expr, strpos($expr, ".") + 1);
         $linkField = new SugarFieldExpression($link);
         if ($context) {
             $linkField->context = $context;
         }
         return new RelatedFieldExpression(array($linkField, Parser::toConstant('"' . $related . '"')));
     }
     // VALIDATE: expression format
     if (!preg_match('/^[a-zA-Z0-9_\\-$]+\\(.*\\)$/', $expr)) {
         throw new Exception("Attempted to evaluate expression with an invalid format: {$expr}");
         return;
     }
     // EXTRACT: Function
     $open_paren_loc = strpos($expr, '(');
     // if no open-paren '(' found
     if ($open_paren_loc < 0) {
         throw new Exception("Attempted to evaluate expression with a Syntax Error (No opening paranthesis found): {$expr}");
         return;
     }
     // get the function
     $func = substr($expr, 0, $open_paren_loc);
     // handle if function is not valid
     if (empty($FUNCTION_MAP)) {
         if (!file_exists(sugar_cached('Expressions/functionmap.php'))) {
             $GLOBALS['updateSilent'] = true;
             include "include/Expressions/updatecache.php";
         }
         require_once sugar_cached('Expressions/functionmap.php');
     }
     if (!isset($FUNCTION_MAP[$func])) {
         throw new Exception("Attempted to evaluate expression with an invalid function '{$func}': {$expr}");
         return;
     }
     // EXTRACT: Parameters
     $params = substr($expr, $open_paren_loc + 1, -1);
     // now parse the individual parameters recursively
     $level = 0;
     $length = strlen($params);
     $argument = "";
     $args = array();
     // flags
     $char = null;
     $lastCharRead = null;
     $justReadString = false;
     // did i just read in a string
     $isInQuotes = false;
     // am i currently reading in a string
     $isPrevCharBK = false;
     // is my previous character a backslash
     $isInVariable = false;
     // is my previous character a backslash
     for ($i = 0; $i <= $length; $i++) {
         // store the last character read
         $lastCharRead = $char;
         // the last parameter
         if ($i == $length) {
             if ($argument != "") {
                 $subExp = Parser::evaluate($argument, $context);
                 $subExp->context = $context;
                 $args[] = $subExp;
             }
             break;
         }
         // set isprevcharbk
         $isPrevCharBK = $lastCharRead == '\\';
         // get the charAt index $i
         $char = $params[$i];
         // if i am in quotes, then keep reading
         if ($isInQuotes && $char != '"' && !$isPrevCharBK) {
             $argument .= $char;
             continue;
         }
         // check for quotes
         if ($char == '"' && !$isPrevCharBK && $level == 0) {
             // if i am ending a quote, then make sure nothing follows
             if ($isInQuotes) {
                 // only spaces may follow the end of a string
                 $temp = substr($params, $i + 1, strpos($params, ",", $i) - $i - 1);
                 if (!preg_match('/^(\\s*|\\s*\\))$/', $temp)) {
                     throw new Exception("Syntax Error:Improperly Terminated String '{$temp}' in formula: {$expr}");
                     return;
                 }
             }
             // negate if i am in quotes
             $isInQuotes = !$isInQuotes;
         }
         if ($char == '$' && !$isInQuotes && !$isPrevCharBK) {
             if ($isInVariable) {
                 throw new Exception("Syntax Error: Invalid variable name in formula: {$expr}");
             }
         }
         // check parantheses open/close
         if ($char == '(') {
             $level++;
         } else {
             if ($char == ')') {
                 $level--;
             } else {
                 if ($char == ',' && $level == 0) {
                     $subExp = Parser::evaluate($argument, $context);
                     $subExp->context = $context;
                     $args[] = $subExp;
                     $argument = "";
                     continue;
                 }
             }
         }
         // construct the next argument
         $argument .= $char;
     }
     // now check to make sure all the parantheses opened were closed
     if ($level != 0) {
         throw new Exception("Syntax Error (Incorrectly Matched Parantheses) in formula: {$expr}");
         return;
     }
     // now check to make sure all the quotes opened were closed
     if ($isInQuotes) {
         if ($level != 0) {
             throw new Exception("Syntax Error (Unterminated String Literal) in formula: {$expr}");
             return;
         }
     }
     // require and return the appropriate expression object
     $expObject = new $FUNCTION_MAP[$func]['class']($args);
     if ($context) {
         $expObject->context = $context;
     }
     return $expObject;
 }