/**
  *
  * The view parameter (in combination with view_name) is already in
  * use for Dashboards. The field list is never created from a
  * viewdef for dashboards anyway so we should remove it.
  *
  * @see SugarApi::getFieldsFromArgs()
  */
 protected function getFieldsFromArgs(ServiceBase $api, array $args, SugarBean $bean = null, $viewName = 'view', &$displayParams = array())
 {
     if (isset($args['view'])) {
         unset($args['view']);
     }
     return parent::getFieldsFromArgs($api, $args, $bean, $viewName, $displayParams);
 }
 public function filterList(ServiceBase $api, array $args, $acl = 'list')
 {
     $forecastSettings = Forecast::getSettings();
     if ($forecastSettings['is_setup'] === 1 && !isset($args['use_generic_timeperiods'])) {
         return parent::filterList($api, $args, $acl);
     }
     // since forecast is not setup, we more than likely don't have timeperiods, so grab the default 3
     $tp = BeanFactory::getBean('TimePeriods');
     $data = array();
     $data['next_offset'] = -1;
     $data['records'] = array();
     $app_list_strings = return_app_list_strings_language($GLOBALS['current_language']);
     $options = $app_list_strings['generic_timeperiod_options'];
     foreach ($options as $duration => $name) {
         $data['records'][] = array_merge(array('id' => $duration, 'name' => $name), $tp->getGenericStartEndByDuration($duration));
     }
     return $data;
 }
 /**
  * Forecast Worksheet Filter API Handler
  *
  * @param ServiceBase $api
  * @param array $args
  * @return array
  * @throws SugarApiExceptionNotAuthorized
  */
 public function filterList(ServiceBase $api, array $args)
 {
     if (!SugarACL::checkAccess('Forecasts', 'list')) {
         throw new SugarApiExceptionNotAuthorized('No access to view records for module: Forecasts');
     }
     // some local variables
     $found_assigned_user = false;
     $found_timeperiod = false;
     $found_type = false;
     // if filter is not defined, define it
     if (!isset($args['filter']) || !is_array($args['filter'])) {
         $args['filter'] = array();
     }
     if (isset($args['filter'][0]['$tracker'])) {
         return array('next_offset' => -1, 'records' => array());
     }
     // if there are filters set, process through them
     if (!empty($args['filter'])) {
         // todo-sfa: clean this up as it currently doesn't handle much in the way of nested arrays
         foreach ($args['filter'] as $key => $filter) {
             $filter_key = array_shift(array_keys($filter));
             // if the key is assigned_user_id, take the value and save it for later
             if ($found_assigned_user == false && $filter_key == 'user_id') {
                 $found_assigned_user = array_pop($filter);
             }
             // if the key is timeperiod_id, take the value, save it for later, and remove the filter
             if ($found_timeperiod == false && $filter_key == 'timeperiod_id') {
                 $found_timeperiod = array_pop($filter);
                 // remove the timeperiod_id
                 unset($args['filter'][$key]);
             }
             if ($found_type == false && $filter_key == 'forecast_type') {
                 $found_type = array_pop($filter);
                 unset($args['filter'][$key]);
             }
         }
     }
     $args['filter'] = $this->createFilter($api, $found_assigned_user, $found_timeperiod, $found_type);
     return parent::filterList($api, $args);
 }
Example #4
0
/**
 * This function uses filter to get the records to be exported.
 *
 * @param Mixed $args api arguments that include filter and module
 * @param Mixed $remove_from_members member Array of header columns to filter out; empty by default
 * @param SugarBean $focus sugar bean object
 * @param bool $members used to indicate whether or not to apply filtering for header rows; false by default
 * @return string
 */
function getExportContentFromFilter($args, $remove_from_members, $focus, $members = false)
{
    // call filter to get data
    $filterApi = new FilterApi();
    $api = new RestService();
    $nextOffset = 0;
    $records = array();
    $filterArgs = array('filter' => $args['filter'], 'module' => $args['module']);
    $filterArgs['max_num'] = 1000;
    while ($nextOffset != -1) {
        // still have records to be fetched
        $filterArgs['offset'] = $nextOffset;
        $data = $filterApi->filterList($api, $filterArgs);
        $records = array_merge($data['records'], $records);
        $nextOffset = $data['next_offset'];
    }
    foreach ($records as &$record) {
        foreach ($record as $name => $val) {
            if (is_array($record[$name])) {
                if ($name != 'team_name' && $name != 'email') {
                    // we do not need arrays like $record['_acl']
                    unset($record[$name]);
                }
            }
        }
    }
    if (is_array($records) && !empty($records)) {
        $fields_array = get_field_order_mapping2($args['module'], array_keys($records[0]), true, true);
    } else {
        // no record found
        return '';
    }
    $delimiter = getDelimiter();
    //set up labels to be used for the header row
    $field_labels = array();
    foreach ($fields_array as $key => $dbName) {
        //Remove fields that are only used for logic
        if ($members && in_array($dbName, $remove_from_members)) {
            continue;
        }
        //default to the db name of label does not exist
        $field_labels[$key] = translateForExport($dbName, $focus);
    }
    // setup the "header" line with proper delimiters
    $content = "\"" . implode("\"" . $delimiter . "\"", array_values($field_labels)) . "\"\r\n";
    foreach ($records as $record) {
        // team_name returned from filter is in array format, get team_id and team_name
        if (isset($record['team_name']) && is_array($record['team_name'])) {
            $firstTeam = true;
            $teamName = '';
            foreach ($record['team_name'] as $team) {
                if ($firstTeam) {
                    $teamName = $team['name'];
                    $firstTeam = false;
                } else {
                    $teamName .= ',' . $team['name'];
                }
                if ($team['primary']) {
                    $record['team_id'] = $team['id'];
                }
            }
            $record['team_name'] = $teamName;
        }
        // email returned from filter is in array format, get primary email address
        if (isset($record['email']) && is_array($record['email'])) {
            foreach ($record['email'] as $email) {
                if ($email['primary_address']) {
                    $record['email'] = $email['email_address'];
                    break;
                }
            }
        }
        $record = get_field_order_mapping2($args['module'], $record, true, false);
        $new_arr = array();
        //replace user_name with full name if use_real_name preference setting is enabled
        //and this is a user name field
        $useRealNames = $GLOBALS['current_user']->getPreference('use_real_names');
        foreach ($record as $key => $value) {
            //getting content values depending on their types
            $fieldNameMapKey = $fields_array[$key];
            if (isset($focus->field_name_map[$fieldNameMapKey]) && $focus->field_name_map[$fieldNameMapKey]['type']) {
                $sfh = SugarFieldHandler::getSugarField($focus->field_name_map[$fieldNameMapKey]['type']);
                $value = $sfh->exportSanitize($value, $focus->field_defs[$key], $focus);
            }
            if ($useRealNames) {
                $value = formatRealNameField($focus, $fields_array, $key, $value);
            }
            array_push($new_arr, preg_replace("/\"/", "\"\"", $value));
        }
        //foreach
        $line = implode("\"" . $delimiter . "\"", $new_arr);
        $line = "\"" . $line;
        $line .= "\"\r\n";
        $content .= $line;
    }
    return $content;
}
 /**
  * Main function that handles the asynchronous massupdate.
  *
  * @param $data job queue data
  */
 public function run($data)
 {
     /*
      - type:init
        - perform filter to get all records to be updated, including id
        - create child jobs (type=work), each job has up to $this->chunkSize records
      - type:work
        - do update/delete
     */
     $data = json_decode(from_html($data), true);
     if (empty($data) || !is_array($data) || empty($data['_jobType_'])) {
         $this->job->failJob('Invalid job data.');
         return false;
     }
     switch ($data['_jobType_']) {
         // this is the parent job, find out all the records to be updated and create child jobs
         case 'init':
             // if uid is already provided, use them
             if (isset($data['uid'])) {
                 if (!is_array($data['uid'])) {
                     $data['uid'] = explode(',', $data['uid']);
                 }
                 $uidChunks = array_chunk($data['uid'], $this->chunkSize);
                 foreach ($uidChunks as $chunk) {
                     $tmpData = $data;
                     $tmpData['uid'] = $chunk;
                     $this->workJobIds[] = $this->createJobQueueConsumer($tmpData, 'work');
                 }
             } else {
                 if (!empty($data['entire'])) {
                     // call filter api to get the ids then create a job queue for each chunk
                     $filterApi = new FilterApi();
                     $api = new RestService();
                     $api->user = $GLOBALS['current_user'];
                     $nextOffset = 0;
                     $filterArgs = array('module' => $data['module'], 'fields' => 'id');
                     if (isset($data['filter'])) {
                         $filterArgs['filter'] = $data['filter'];
                     }
                     $uidArray = array();
                     // max_num does not need to set to chunkSize, it can be any size that makes sense
                     $filterArgs['max_num'] = $this->chunkSize;
                     // start getting all the ids
                     while ($nextOffset != -1) {
                         // still have records to be fetched
                         $filterArgs['offset'] = $nextOffset;
                         $result = $filterApi->filterList($api, $filterArgs);
                         $nextOffset = $result['next_offset'];
                         foreach ($result['records'] as $record) {
                             if (!empty($record['id'])) {
                                 $uidArray[] = $record['id'];
                             }
                         }
                         // create one child job for each chunk
                         if (count($uidArray)) {
                             $uidChunks = array_chunk($uidArray, $this->chunkSize);
                             foreach ($uidChunks as $chunk) {
                                 $tmpData = $data;
                                 $tmpData['uid'] = $chunk;
                                 $this->workJobIds[] = $this->createJobQueueConsumer($tmpData, 'work');
                             }
                         }
                     }
                 } else {
                     $this->job->failJob('Neither uid nor entire specified.');
                     return false;
                 }
             }
             $this->job->succeedJob('Child jobs created.');
             // return the ids of the child jobs that have been created
             return $this->workJobIds;
             // this is the child job, do update
         // this is the child job, do update
         case 'work':
             return $this->workJob($this->job, $data);
         default:
             break;
     }
     return true;
 }
 /**
  * @param ServiceBase $api
  * @param array $args
  * @param string $acl
  * @return array
  * @throws SugarApiExceptionInvalidParameter
  */
 public function filterListAllPA(ServiceBase $api, array $args, $acl = 'list')
 {
     // This send by default 'regular_user' to the custom filter 'visibility'
     $args['filter'][] = array('visibility' => 'regular_user');
     return parent::filterList($api, $args, $acl);
 }
Example #7
0
 public function filterListSetup(ServiceBase $api, array $args, $acl = 'list')
 {
     $seed = BeanFactory::newBean($args['module']);
     if (!$seed->ACLAccess($acl)) {
         throw new SugarApiExceptionNotAuthorized('No access to view records for module: ' . $args['module']);
     }
     $options = $this->parseArguments($api, $args, $seed);
     // In case the view parameter is set, reflect those fields in the
     // fields argument as well so formatBean only takes those fields
     // into account instead of every bean property.
     if (!empty($args['view'])) {
         $args['fields'] = $options['select'];
     }
     $q = self::getQueryObject($seed, $options);
     // return $args['filter'];
     if (!isset($args['filter']) || !is_array($args['filter'])) {
         $args['filter'] = array();
     }
     static::addFilters($args['filter'], $q->where(), $q);
     if (!empty($args['my_items'])) {
         static::addOwnerFilter($q, $q->where(), '_this');
     }
     if (!empty($args['favorites'])) {
         self::$isFavorite = true;
         static::addFavoriteFilter($q, $q->where(), '_this', 'INNER');
     }
     return array($args, $q, $options, $seed);
 }
 protected function callFilterApi($filter)
 {
     // call filter to get data
     $filterApi = new FilterApi();
     $api = new RestService();
     $api->user = $GLOBALS['current_user'];
     $filterArgs = array('filter' => $filter, 'module' => $this->bean->module_name, 'max_num' => self::FILTER_QUERY_LIMIT);
     return $filterApi->filterList($api, $filterArgs, 'view');
 }
 /**
  * Forecast Worksheet Filter API Handler
  *
  * @param ServiceBase $api
  * @param array $args
  * @return array
  */
 public function filterList(ServiceBase $api, array $args)
 {
     // some local variables
     $found_assigned_user = false;
     $found_timeperiod = false;
     $found_type = false;
     // if filter is not defined, define it
     if (!isset($args['filter']) || !is_array($args['filter'])) {
         $args['filter'] = array();
     }
     // if there are filters set, process through them
     if (!empty($args['filter'])) {
         // todo-sfa: clean this up as it currently doesn't handle much in the way of nested arrays
         foreach ($args['filter'] as $key => $filter) {
             $filter_key = array_shift(array_keys($filter));
             // if the key is assigned_user_id, take the value and save it for later
             if ($found_assigned_user == false && $filter_key == 'assigned_user_id') {
                 $found_assigned_user = array_pop($filter);
             }
             // if the key is timeperiod_id, take the value, save it for later, and remove the filter
             if ($found_timeperiod == false && $filter_key == 'timeperiod_id') {
                 $found_timeperiod = array_pop($filter);
                 // remove the timeperiod_id
                 unset($args['filter'][$key]);
             }
             // if the key is 'draft', remote it from the filter
             if ($filter_key == 'draft') {
                 unset($args['filter'][$key]);
             }
             if ($found_type == false && $filter_key == 'type') {
                 $found_type = array_pop($filter);
                 unset($args['filter'][$key]);
             }
         }
     }
     $args['filter'] = $this->createFilter($api, $found_assigned_user, $found_timeperiod, $found_type);
     return parent::filterList($api, $args);
 }