/** * Creates a record list from a saved report * @param $api ServiceBase The API class of the request, used in cases where the API changes how the fields are pulled from the args array. * @param $args array The arguments array passed in from the API containing the module and the records * @throws SugarApiExceptionNotAuthorized * @throws SugarApiException * @return array id, module, records */ public function createRecordList($api, $args) { $this->requireArgs($args, array("record")); $savedReport = BeanFactory::newBean("Reports"); if (!$savedReport->ACLAccess('access')) { throw new SugarApiExceptionNotAuthorized(); } $recordIds = $this->getRecordIdsFromReport($args["record"]); $id = RecordListFactory::saveRecordList($recordIds, "Reports"); $loadedRecordList = RecordListFactory::getRecordList($id); return $loadedRecordList; }
/** * To retrieve a record list * @param $api ServiceBase The API class of the request, used in cases where the API changes how the fields are pulled from the args array. * @param $args array The arguments array passed in from the API containing the module and id of the record list * @return array The record list */ public function recordListGet($api, $args) { $seed = BeanFactory::newBean($args['module']); if (!$seed->ACLAccess('access')) { throw new SugarApiExceptionNotAuthorized(); } $recordList = RecordListFactory::getRecordList($args['record_list_id']); if ($recordList == null) { throw new SugarApiExceptionNotFound(); } if ($recordList['module_name'] != $args['module']) { throw new SugarApiExceptionNotAuthorized(); } if (!$api->user->isAdmin()) { if ($recordList['assigned_user_id'] != $api->user->id) { throw new SugarApiExceptionNotAuthorized(); } } return $recordList; }
/** * builds up a delimited string for export * @param boolean args api argument * @param boolean sample whether it's sample export * @return string delimited string for export */ function exportFromApi($args, $sample = false) { global $current_user; global $app_strings; $sampleRecordNum = 5; $type = clean_string($args['module']); require_once 'include/RecordListFactory.php'; $recordList = RecordListFactory::getRecordList($args['record_list_id']); if (empty($recordList)) { throw new SugarApiExceptionNotFound(); } $records = $recordList['records']; $members = isset($args['members']) ? $args['members'] : false; //Array of fields that should not be exported, and are only used for logic $remove_from_members = array("ea_deleted", "ear_deleted", "primary_address"); $focus = BeanFactory::newBean($type); $searchFields = array(); $db = DBManagerFactory::getInstance(); if ($records) { // we take an array, but we'll make an exception for one record. if (!is_array($records)) { $records = array($records); } $records = "'" . implode("','", $records) . "'"; $where = "{$focus->table_name}.id in ({$records})"; } elseif (isset($args['all'])) { $where = ''; } else { // use filter to get data instead of building a sql if (!empty($args['filter'])) { $content = getExportContentFromFilter($args, $remove_from_members, $focus, $members); return $content; } else { $where = ''; } } if ($focus->bean_implements('ACL')) { if (ACLController::requireOwner($focus->module_dir, 'export')) { if (!empty($where)) { $where .= ' AND '; } $where .= $focus->getOwnerWhere($current_user->id); } } if ($focus->bean_implements('ACL')) { $focus->addVisibilityWhere($where); } // Export entire list was broken because the where clause already has "where" in it // and when the query is built, it has a "where" as well, so the query was ill-formed. // Eliminating the "where" here so that the query can be constructed correctly. if ($members == true) { $query = $focus->create_export_members_query($records); } else { $beginWhere = substr(trim($where), 0, 5); if ($beginWhere == "where") { $where = substr(trim($where), 5, strlen($where)); } $query = $focus->create_export_query("", $where); } $result = null; if ($sample) { $result = $db->limitQuery($query, 0, $sampleRecordNum, true, $app_strings['ERR_EXPORT_TYPE'] . $type . ": <BR>." . $query); $sample = $focus->_get_num_rows_in_query($query) < 1; } else { $result = $db->query($query, true, $app_strings['ERR_EXPORT_TYPE'] . $type . ": <BR>." . $query); } $content = getExportContentFromResult($focus, $result, $members, $remove_from_members, $sample); return $content; }
/** * Relates existing records to related bean. * * @param ServiceBase $api The API class of the request. * @param array $args The arguments array passed in from the API. * @return array Array of formatted fields. * @throws SugarApiExceptionNotFound If bean can't be retrieved. */ public function createRelatedLinksFromRecordList($api, $args) { Activity::disable(); $result = array('related_records' => array('success' => array(), 'error' => array())); $this->requireArgs($args, array('module', 'record', 'remote_id', 'link_name')); $primaryBean = $this->loadBean($api, $args); list($linkName) = $this->checkRelatedSecurity($api, $args, $primaryBean, 'view', 'view'); $recordList = RecordListFactory::getRecordList($args['remote_id']); $relatedBeans = $primaryBean->{$linkName}->add($recordList['records']); if ($relatedBeans === true) { $result['related_records']['success'] = $recordList['records']; } elseif (is_array($relatedBeans)) { $result['related_records']['success'] = array_diff($recordList['records'], $relatedBeans); $result['related_records']['error'] = $relatedBeans; } SugarRelationship::resaveRelatedBeans(); Activity::enable(); $result['record'] = $this->formatBean($api, $args, $primaryBean); return $result; }