Exemple #1
0
 /**
  * Tests if the current module has a calculated field with a link.
  * if a link name is specified, it will return true when a field uses that specific link
  * Otherwise it will test for all link fields.
  * @param string $linkName
  * @return bool
  */
 function has_calc_field_with_link($linkName = "")
 {
     $links = array();
     if (empty($linkName)) {
         foreach ($this->field_defs as $field => $def) {
             if (!empty($def['type']) && $def['type'] == "link") {
                 $links[$field] = true;
             }
         }
     } else {
         $links[$linkName] = true;
     }
     if (!empty($links)) {
         foreach ($this->field_defs as $name => $def) {
             //Look through all calculated fields for uses of this link field
             if (!empty($def['formula'])) {
                 require_once "include/Expressions/Expression/Parser/Parser.php";
                 $fields = Parser::getFieldsFromExpression($def['formula']);
                 foreach ($fields as $var) {
                     if (!empty($links[$var])) {
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }
 /**
  * prepare Calculation Fields for creation formula
  *
  * @param array $fieldDefs The fields defs for the current module.
  * @param string $module current module.
  */
 protected function prepareCalculationFields($fieldDefs, $module)
 {
     $fields = array();
     foreach ($fieldDefs as $field => $def) {
         if (isset($def['calculated']) && $def['calculated'] && !empty($def['formula'])) {
             $triggerFields = Parser::getFieldsFromExpression($def['formula'], $fields);
             foreach ($triggerFields as $field_c) {
                 if (isset($fieldDefs[$field_c])) {
                     $fieldDefs[$field]['formula'] = str_replace($field_c, $fieldDefs[$field_c]['id'], $fieldDefs[$field]['formula']);
                 }
             }
             $temp_field = $fieldDefs[$field];
             $fieldDefs[$module . $field] = $temp_field;
             unset($fieldDefs[$field]);
         }
     }
     return $fieldDefs;
 }
 /**
  * Expands list fields by adding those ones which existing fields depend on.
  *
  * @param  SugarBean $bean   Instance of SugarBean which is displayed
  *                           in the subpanel
  * @param  array     $fields Definition if list fields
  *
  * @return array             Expanded definition
  */
 public function expand_list_fields(SugarBean $bean, array $fields)
 {
     $expanded = array();
     foreach (array_keys($fields) as $name) {
         if (!empty($bean->field_defs[$name]['dependency'])) {
             $expr = $bean->field_defs[$name]['dependency'];
             $extracted = Parser::getFieldsFromExpression($expr, $bean->field_defs);
             $extracted = array_flip($extracted);
             // remove fields that do not exist in field definitions
             $expanded += array_intersect_key($extracted, $bean->field_defs);
             // make the dependent field non-sortable since availability of the field
             // is calculated after the data is retrieved from database
             $fields[$name]['sortable'] = false;
         }
         // get currency symbol if this is a currency field
         if (isset($bean->field_defs[$name]['type']) && $bean->field_defs[$name]['type'] == 'currency' && !empty($bean->field_defs['currency_id'])) {
             $expanded['currency_id'] = $bean->field_defs['currency_id'];
             $fields[$name]['sortable'] = false;
         }
     }
     // ignore dependencies that already present in the list
     $expanded = array_diff_key($expanded, $fields);
     foreach (array_keys($expanded) as $name) {
         $fields[$name] = array('name' => $name, 'usage' => 'query_only');
     }
     return $fields;
 }
 /**
  * Returns the set of the custom SugarLogic Dependencies defined in the dependency metadata
  * for a module that are valid for the given action.
  * @static
  * @param string $module Primary module for this action
  * @param string $action name of the action to get dependencies for ("edit", "view", "save", ect)
  * @param string $form name of the form element used on html forms
  * @return array<Dependency>
  */
 public static function getModuleDependenciesForAction($module, $action, $form = "EditView")
 {
     $meta = self::getModuleDependencyMetadata($module);
     $deps = array();
     foreach ($meta as $key => $def) {
         $hooks = empty($def['hooks']) ? array("all") : $def['hooks'];
         if (!is_array($hooks)) {
             $hooks = array($hooks);
         }
         if (in_array('all', $hooks) || in_array($action, $hooks)) {
             self::filterActionDefinitionsForView($def, $action);
             if (empty($def['actions']) && empty($def['notActions'])) {
                 continue;
                 // Skip if no actions left after filtering
             }
             $triggerExp = empty($def['trigger']) ? self::$default_trigger : $def['trigger'];
             $triggerFields = empty($def['triggerFields']) ? Parser::getFieldsFromExpression($triggerExp) : $def['triggerFields'];
             $actions = empty($def['actions']) || !is_array($def['actions']) ? array() : $def['actions'];
             $notActions = empty($def['notActions']) || !is_array($def['notActions']) ? array() : $def['notActions'];
             $dep = new Dependency("{$module}{$form}_{$key}");
             $dep->setTrigger(new Trigger($triggerExp, $triggerFields));
             foreach ($actions as $aDef) {
                 $dep->addAction(ActionFactory::getNewAction($aDef['name'], $aDef['params']));
             }
             foreach ($notActions as $aDef) {
                 $dep->addFalseAction(ActionFactory::getNewAction($aDef['name'], $aDef['params']));
             }
             $dep->setFireOnLoad(!isset($def['onload']) || $def['onload'] !== false);
             $deps[] = $dep;
         }
     }
     return $deps;
 }
Exemple #5
0
 /**
  * @deprecated
  * returns the expression with the variables replaced with the values in target.
  *
  * @param string $expr
  * @param Array/SugarBean $target
  */
 static function replaceVariables($expr, $target)
 {
     $target->load_relationships();
     $variables = Parser::getFieldsFromExpression($expr);
     $ret = $expr;
     foreach ($variables as $field) {
         if (is_array($target)) {
             if (isset($target[$field])) {
                 $val = Parser::getFormatedValue($target[$field], $field);
                 $ret = str_replace("\${$field}", $val, $ret);
             } else {
                 continue;
                 //throw new Exception("Unknown variable $$field in formula: $expr");
                 //return;
             }
         } else {
             //Special case for link fields
             if (isset($target->field_defs[$field]) && $target->field_defs[$field]['type'] == "link") {
                 $val = "link(\"{$field}\")";
                 $ret = str_replace("\${$field}", $val, $ret);
             } else {
                 if (isset($target->{$field})) {
                     $val = Parser::getFormatedValue($target->{$field}, $field);
                     $ret = str_replace("\${$field}", $val, $ret);
                 } else {
                     continue;
                     // throw new Exception("Unknown variable $$field in formula: $expr");
                     // return;
                 }
             }
         }
     }
     return $ret;
 }