/** * Constructor * Argument list is used as array of valid values expected */ function __construct() { $validValues = func_get_args(); $ff = FilterFactory::getInstance(); $this->inFilter = $ff->newFilter('InNonStrict', $validValues); $this->intFilter = $ff->newFilter('Int'); }
/** * fillBeans * This function wraps the call to getList, but takes an additional Array argument * and loads the SugarBean's fields with the results as defined in the connector * loadBean configuration mapping * * @param $args Array of arguments to pass into getItem * @param $module String value of the module to map bean to * @param $bean Array to load SugarBean intances into * @throws Exception Thrown if errors are found */ public function fillBeans($args = array(), $module = null, $beans = array()) { $results = array(); $args = $this->mapInput($args, $module); if (empty($args)) { $GLOBALS['log']->fatal($GLOBALS['app_strings']['ERR_MISSING_MAPPING_ENTRY_FORM_MODULE']); throw new Exception($GLOBALS['app_strings']['ERR_MISSING_MAPPING_ENTRY_FORM_MODULE']); } require_once 'include/connectors/filters/FilterFactory.php'; $filter = FilterFactory::getInstance(get_class($this->_source)); $list = $filter->getList($args, $module); if (!empty($list)) { $resultSize = count($list); if (!empty($beans)) { if (count($beans) != $resultSize) { throw new Exception($GLOBALS['app_strings']['ERR_CONNECTOR_FILL_BEANS_SIZE_MISMATCH']); } } else { for ($x = 0; $x < $resultSize; $x++) { $beans[$x] = BeanFactory::getBean($module); } } $keys = array_keys($beans); $count = 0; foreach ($list as $entry) { //Change the result keys to lower case. This has important ramifications. //This was done because the listviewdefs.php files may not know the proper casing //of the fields to display. We change the keys to lowercase so that the values //may be mapped to the beans without having to rely on the proper string casing //in the listviewdefs.php files. $entry = array_change_key_case($entry, CASE_LOWER); $results[] = $this->mapOutput($beans[$keys[$count]], $entry); $count++; } $field_defs = $this->getFieldDefs(); $map = $this->getMapping(); $hasOptions = !empty($map['options']) ? true : false; if ($hasOptions) { $options = $map['options']; $optionFields = array(); foreach ($field_defs as $name => $field) { if (!empty($field['options']) && !empty($map['options'][$field['options']]) && !empty($map['beans'][$module][$name])) { $optionFields[$name] = $map['beans'][$module][$name]; } } foreach ($results as $key => $bean) { foreach ($optionFields as $sourceField => $sugarField) { $options_map = $options[$field_defs[$sourceField]['options']]; $results[$key]->{$sugarField} = !empty($options_map[$results[$key]->{$sugarField}]) ? $options_map[$results[$key]->{$sugarField}] : $results[$key]->{$sugarField}; } } //foreach } } return $results; }
/** * Apply an array of filter to one value * @param mixed $value * @param array $filters * @return mixed * @throws FilterException */ static function apply($value = null, array $filters = array()) { //error_log(__METHOD__ . ' value: ' . json_encode($value)); $filterFactory = FilterFactory::getInstance(); foreach ($filters as $idx => $filterArr) { //error_log(__METHOD__ . ' loop: ' . $idx . ': ' . json_encode($filterArr)); if (empty($filterArr[0])) { continue; } //skip $filterName = $filterArr[0]; //error_log(__METHOD__ . ' loop: ' . $idx . ' filter ' . $filterName); $filterErr = isset($filterArr[1]) ? $filterArr[1] : ''; $filterParams = isset($filterArr[2]) ? $filterArr[2] : array(); $filter = $filterFactory->newFilter($filterName, $filterParams); try { $value = $filter->filter($value); } catch (FilterException $ex) { $msg = $filterErr !== '' ? $filterErr : $ex->getMessage(); throw new FilterException($msg); } } return $value; }