Пример #1
0
 /**
  * Display the view
  */
 public function display()
 {
     $valid_links = array();
     $links = FormulaHelper::getLinksForModule($this->tmodule, $this->package);
     // loop over all the $links and if we don't have any fields, don't pass it down
     $current_fields = array();
     //Preload the related fields from the first relationship
     if (!empty($links)) {
         reset($links);
         $selected_link = isset($links[$this->selLink]) ? $links[$this->selLink] : $links[key($links)];
         foreach ($links as $link_key => $link) {
             $rfields = FormulaHelper::getRelatableFieldsForLink($link, $this->package, array("number"));
             if (!empty($rfields)) {
                 $valid_links[$link_key] = $link['label'];
                 if ($link === $selected_link || empty($current_fields)) {
                     $current_fields = $rfields;
                 }
             }
         }
     }
     $this->ss->assign("rmodules", $valid_links);
     $this->ss->assign("rfields", $current_fields);
     $this->ss->assign("tmodule", $this->tmodule);
     $this->ss->assign("selLink", $this->selLink);
     $this->ss->assign("rollup_types", array("Sum" => "Sum", "Ave" => "Average", "Min" => "Minimum", "Max" => "Maximum"));
     $this->ss->assign("rollupType", $this->type);
     $this->ss->display('modules/ExpressionEngine/tpls/rollupWizard.tpl');
 }
Пример #2
0
 function display()
 {
     global $app_strings, $current_user, $mod_strings, $theme, $beanList, $beanFiles;
     $smarty = new Sugar_Smarty();
     $json = new JSON();
     require_once 'include/JSON.php';
     //Load the field list from the target module
     if (!empty($_REQUEST['targetModule']) && $_REQUEST['targetModule'] != 'undefined') {
         $module = $_REQUEST['targetModule'];
         if (isset($_REQUEST['package']) && $_REQUEST['package'] != 'studio' && $_REQUEST['package'] != '') {
             //Get the MB Parsers
             require_once 'modules/ModuleBuilder/MB/MBPackage.php';
             $pak = new MBPackage($_REQUEST['package']);
             $defs = $pak->modules[$module]->getVardefs();
             $fields = FormulaHelper::cleanFields(array_merge($pak->modules[$module]->getLinkFields(), $defs['fields']));
         } else {
             $seed = BeanFactory::getBean($module);
             $fields = FormulaHelper::cleanFields($seed->field_defs);
         }
         $smarty->assign('Field_Array', $json->encode($fields));
     } else {
         $fields = array(array('income', 'number'), array('employed', 'boolean'), array('first_name', 'string'), array('last_name', 'string'));
         $smarty->assign('Field_Array', $json->encode($fields));
     }
     if (!empty($_REQUEST['targetField'])) {
         $smarty->assign("target", $_REQUEST['targetField']);
     }
     if (isset($_REQUEST['returnType'])) {
         $smarty->assign("returnType", $_REQUEST['returnType']);
     }
     //Assign any requested Javascript event actions
     foreach (array('onSave', 'onLoad', 'onClose') as $e) {
         if (!empty($_REQUEST[$e])) {
             $smarty->assign($e, html_entity_decode($_REQUEST[$e], ENT_QUOTES));
         } else {
             $smarty->assign($e, 'function(){}');
         }
     }
     //Check if we need to load Ext ourselves
     if (!isset($_REQUEST['loadExt']) || $_REQUEST['loadExt'] && $_REQUEST['loadExt'] != "false") {
         $smarty->assign('loadExt', true);
     } else {
         $smarty->assign('loadExt', false);
     }
     if (!empty($_REQUEST['formula'])) {
         $smarty->assign('formula', $json->decode(htmlspecialchars_decode($_REQUEST['formula'])));
     }
     if (isset($_REQUEST['returnType'])) {
         $smarty->assign('returnType', $_REQUEST['returnType']);
     }
     $smarty->assign('app_strings', $app_strings);
     $smarty->assign('mod', $mod_strings);
     $smarty->display('modules/ExpressionEngine/tpls/formulaBuilder.tpl');
 }
 function display()
 {
     $rmodules = array();
     $links = FormulaHelper::getLinksForModule($this->tmodule, $this->package);
     $rfields = array();
     foreach ($links as $lname => $link) {
         $rmodules[$lname] = $link['label'];
     }
     //Preload the related fields from the first relationship
     if (!empty($links)) {
         reset($links);
         $link = isset($links[$this->selLink]) ? $links[$this->selLink] : $links[key($links)];
         $rfields = FormulaHelper::getRelatableFieldsForLink($link, $this->package);
     }
     $this->ss->assign("rmodules", $rmodules);
     $this->ss->assign("rfields", $rfields);
     $this->ss->assign("tmodule", $this->tmodule);
     $this->ss->assign("selLink", $this->selLink);
     $this->ss->assign("rollup_types", array("rollupSum" => "Sum", "rollupMin" => "Minimum", "rollupMax" => "Maximum", "rollupAverage" => "Average"));
     $this->ss->display('modules/ExpressionEngine/tpls/selectRelatedField.tpl');
 }
Пример #4
0
 /**
  * Used by the dependency manager to pre-load all the related fields required
  * to load an entire view.
  */
 public function action_getRelatedValues()
 {
     /** @var LoggerManager */
     global $log;
     $ret = array();
     if (empty($_REQUEST['tmodule']) || empty($_REQUEST['fields'])) {
         return;
     }
     $fields = json_decode(html_entity_decode($_REQUEST['fields']), true);
     if (!is_array($fields)) {
         $log->fatal('"fields" is not a valid JSON string');
         $this->display($ret);
         return;
     }
     $module = $_REQUEST['tmodule'];
     $id = empty($_REQUEST['record_id']) ? null : $_REQUEST['record_id'];
     $focus = BeanFactory::retrieveBean($module, $id);
     if (!$focus) {
         $log->fatal('Unable to load bean');
         $this->display($ret);
         return;
     }
     foreach ($fields as $rfDef) {
         if (!isset($rfDef['link'], $rfDef['type'])) {
             $log->fatal('At least one of "link" and "type" attributes is not specified');
             continue;
         }
         $link = $rfDef['link'];
         $type = $rfDef['type'];
         if (!isset($ret[$link])) {
             $ret[$link] = array();
         }
         if (empty($ret[$link][$type])) {
             $ret[$link][$type] = array();
         }
         switch ($type) {
             //The Related function is used for pulling a sing field from a related record
             case "related":
                 if (!isset($rfDef['relate'])) {
                     $log->fatal('"relate" attribute of related expression is not specified');
                     break;
                 }
                 //Default it to a blank value
                 $ret[$link]['related'][$rfDef['relate']] = "";
                 //If we have neither a focus id nor a related record id, we can't retrieve anything
                 if (!empty($id) || !empty($rfDef['relId'])) {
                     $relBean = null;
                     if (empty($rfDef['relId']) || empty($rfDef['relModule'])) {
                         //If the relationship is invalid, just move onto another field
                         if (!$focus->load_relationship($link)) {
                             break;
                         }
                         $beans = $focus->{$link}->getBeans(array("enforce_teams" => true));
                         //No related beans means no value
                         if (empty($beans)) {
                             break;
                         }
                         //Grab the first bean on the list
                         reset($beans);
                         $relBean = current($beans);
                     } else {
                         $relBean = BeanFactory::getBean($rfDef['relModule'], $rfDef['relId']);
                     }
                     //If we found a bean and the current user has access to the related field, grab a value from it
                     if (!empty($relBean) && ACLField::hasAccess($rfDef['relate'], $relBean->module_dir, $GLOBALS['current_user']->id, true)) {
                         $validFields = FormulaHelper::cleanFields($relBean->field_defs, false, true, true);
                         if (isset($validFields[$rfDef['relate']])) {
                             $ret[$link]['relId'] = $relBean->id;
                             $ret[$link]['related'][$rfDef['relate']] = FormulaHelper::getFieldValue($relBean, $rfDef['relate']);
                         }
                     }
                 }
                 break;
             case "count":
                 if (!empty($id) && $focus->load_relationship($link)) {
                     $ret[$link][$type] = count($focus->{$link}->get());
                 } else {
                     $ret[$link][$type] = 0;
                 }
                 break;
             case "rollupSum":
             case "rollupAve":
             case "rollupMin":
             case "rollupMax":
                 //If we are going to calculate one rollup, calculate all the rollups since there is so little cost
                 if (!isset($rfDef['relate'])) {
                     $log->fatal('"relate" attribute of rollup expression is not specified');
                     break;
                 }
                 $rField = $rfDef['relate'];
                 if (!empty($id) && $focus->load_relationship($link)) {
                     $relBeans = $focus->{$link}->getBeans(array("enforce_teams" => true));
                     $sum = 0;
                     $count = 0;
                     $min = false;
                     $max = false;
                     if (!empty($relBeans)) {
                         //Check if the related record vardef has banned this field from formulas
                         $relBean = reset($relBeans);
                         $validFields = FormulaHelper::cleanFields($relBean->field_defs, false, true, true);
                         if (!isset($validFields[$rField])) {
                             break;
                         }
                     }
                     foreach ($relBeans as $bean) {
                         if (isset($bean->{$rField}) && is_numeric($bean->{$rField}) && ACLField::hasAccess($rField, $bean->module_dir, $GLOBALS['current_user']->id, true)) {
                             $count++;
                             $sum += floatval($bean->{$rField});
                             if ($min === false || $bean->{$rField} < $min) {
                                 $min = floatval($bean->{$rField});
                             }
                             if ($max === false || $bean->{$rField} > $max) {
                                 $max = floatval($bean->{$rField});
                             }
                         }
                     }
                     if ($type == "rollupSum") {
                         $ret[$link][$type][$rField] = $sum;
                     }
                     if ($type == "rollupAve") {
                         $ret[$link][$type][$rField] = $count == 0 ? 0 : $sum / $count;
                     }
                     if ($type == "rollupMin") {
                         $ret[$link][$type][$rField] = $min;
                     }
                     if ($type == "rollupMax") {
                         $ret[$link][$type][$rField] = $max;
                     }
                 } else {
                     $ret[$link][$type][$rField] = 0;
                 }
                 break;
         }
     }
     $this->display($ret);
 }
Пример #5
0
 /**
  * Used by the dependency manager to pre-load all the related fields required
  * to load an entire view.
  */
 public function getRelatedValues($api, $args)
 {
     if (empty($args['module']) || empty($args['fields'])) {
         return;
     }
     $fields = json_decode(html_entity_decode($args['fields']), true);
     $focus = $this->loadBean($api, $args);
     $ret = array();
     foreach ($fields as $rfDef) {
         if (!isset($rfDef['link']) || !isset($rfDef['type'])) {
             continue;
         }
         $link = $rfDef['link'];
         $type = $rfDef['type'];
         $rField = '';
         if (!isset($ret[$link])) {
             $ret[$link] = array();
         }
         if (empty($ret[$link][$type])) {
             $ret[$link][$type] = array();
         }
         // count formulas don't have a relate attribute
         if (isset($rfDef['relate'])) {
             $rField = $rfDef['relate'];
         }
         switch ($type) {
             //The Related function is used for pulling a sing field from a related record
             case "related":
                 //Default it to a blank value
                 $ret[$link]['related'][$rfDef['relate']] = "";
                 //If we have neither a focus id nor a related record id, we can't retrieve anything
                 $relBean = null;
                 if (empty($rfDef['relId']) || empty($rfDef['relModule'])) {
                     //If the relationship is invalid, just move onto another field
                     if (!$focus->load_relationship($link)) {
                         break;
                     }
                     $beans = $focus->{$link}->getBeans(array("enforce_teams" => true));
                     //No related beans means no value
                     if (empty($beans)) {
                         break;
                     }
                     //Grab the first bean on the list
                     reset($beans);
                     $relBean = current($beans);
                 } else {
                     $relBean = BeanFactory::getBean($rfDef['relModule'], $rfDef['relId']);
                 }
                 //If we found a bean and the current user has access to the related field, grab a value from it
                 if (!empty($relBean) && ACLField::hasAccess($rfDef['relate'], $relBean->module_dir, $GLOBALS['current_user']->id, true)) {
                     $validFields = FormulaHelper::cleanFields($relBean->field_defs, false, true, true);
                     if (isset($validFields[$rfDef['relate']])) {
                         $ret[$link]['relId'] = $relBean->id;
                         $ret[$link]['related'][$rfDef['relate']] = FormulaHelper::getFieldValue($relBean, $rfDef['relate']);
                     }
                 }
                 break;
             case "count":
                 if ($focus->load_relationship($link)) {
                     $ret[$link][$type] = count($focus->{$link}->get());
                 } else {
                     $ret[$link][$type] = 0;
                 }
                 break;
             case "rollupSum":
             case "rollupAve":
             case "rollupMin":
             case "rollupMax":
                 //If we are going to calculate one rollup, calculate all the rollups since there is so little cost
                 if ($focus->load_relationship($link)) {
                     $relBeans = $focus->{$link}->getBeans(array("enforce_teams" => true));
                     $sum = 0;
                     $count = 0;
                     $min = false;
                     $max = false;
                     if (!empty($relBeans)) {
                         //Check if the related record vardef has banned this field from formulas
                         $relBean = reset($relBeans);
                         $validFields = FormulaHelper::cleanFields($relBean->field_defs, false, true, true);
                         if (!isset($validFields[$rField])) {
                             $ret[$link][$type][$rField] = 0;
                             break;
                         }
                     }
                     foreach ($relBeans as $bean) {
                         if (isset($bean->{$rField}) && is_numeric($bean->{$rField}) && ACLField::hasAccess($rField, $bean->module_dir, $GLOBALS['current_user']->id, true)) {
                             $count++;
                             $sum += floatval($bean->{$rField});
                             if ($min === false || $bean->{$rField} < $min) {
                                 $min = floatval($bean->{$rField});
                             }
                             if ($max === false || $bean->{$rField} > $max) {
                                 $max = floatval($bean->{$rField});
                             }
                         }
                     }
                     if ($type == "rollupSum") {
                         $ret[$link][$type][$rField] = $sum;
                     }
                     if ($type == "rollupAve") {
                         $ret[$link][$type][$rField] = $count == 0 ? 0 : $sum / $count;
                     }
                     if ($type == "rollupMin") {
                         $ret[$link][$type][$rField] = $min;
                     }
                     if ($type == "rollupMax") {
                         $ret[$link][$type][$rField] = $max;
                     }
                 } else {
                     $ret[$link][$type][$rField] = 0;
                 }
                 break;
             case "rollupCurrencySum":
                 $ret[$link][$type][$rField] = 0;
                 if ($focus->load_relationship($link)) {
                     $toRate = isset($focus->base_rate) ? $focus->base_rate : null;
                     $relBeans = $focus->{$link}->getBeans(array("enforce_teams" => true));
                     $sum = 0;
                     foreach ($relBeans as $bean) {
                         if (!empty($bean->{$rField}) && is_numeric($bean->{$rField}) && ACLField::hasAccess($rField, $bean->module_dir, $GLOBALS['current_user']->id, true)) {
                             $sum = SugarMath::init($sum)->add(SugarCurrency::convertWithRate($bean->{$rField}, $bean->base_rate, $toRate))->result();
                         }
                     }
                     $ret[$link][$type][$rField] = $sum;
                 }
                 break;
         }
     }
     return $ret;
 }
Пример #6
0
 /**
  * @static
  * @param array $link
  * @param MBPackage $package
  * @param array $allowedTypes list of types to allow as related fields
  * @return array
  */
 public static function getRelatableFieldsForLink($link, $package = null, $allowedTypes = array())
 {
     $rfields = array();
     $relatedModule = $link["module"];
     $mbModule = null;
     if (!empty($package)) {
         $mbModule = $package->getModuleByFullName($relatedModule);
     }
     //First, create a dummy bean to access the relationship info
     if (empty($mbModule)) {
         $relatedBean = BeanFactory::getBean($relatedModule);
         $field_defs = $relatedBean->field_defs;
     } else {
         $field_defs = $mbModule->getVardefs(false);
         $field_defs = $field_defs['fields'];
     }
     $relatedFields = FormulaHelper::cleanFields($field_defs, false, true);
     foreach ($relatedFields as $val) {
         $name = $val[0];
         //Rollups must be either a number or a possible number (like a string) to roll up
         if (!empty($allowedTypes) && !in_array($val[1], $allowedTypes)) {
             continue;
         }
         $def = $field_defs[$name];
         if (empty($mbModule)) {
             $rfields[$name] = empty($def['vname']) ? $name : translate($def['vname'], $relatedModule);
         } else {
             $rfields[$name] = empty($def['vname']) ? $name : $mbModule->mblanguage->translate($def['vname']);
         }
         //Strip the ":" from any labels that have one
         if (substr($rfields[$name], -1) == ":") {
             $rfields[$name] = substr($rfields[$name], 0, strlen($rfields[$name]) - 1);
         }
     }
     return $rfields;
 }