function template_handle_export(&$reporter) { ini_set('zlib.output_compression', 'Off'); $reporter->plain_text_output = true; //disable paging so we get all results in one pass $reporter->enable_paging = false; $reporter->run_query(); $reporter->_load_currency(); $header_arr = array(); $header_row = $reporter->get_header_row(); $content = ''; foreach ($header_row as $cell) { array_push($header_arr, $cell); } $header = implode("\"" . getDelimiter() . "\"", array_values($header_arr)); $header = "\"" . $header; $header .= "\"\r\n"; $content .= $header; while (($row = $reporter->get_next_row('result', 'display_columns', false, true)) != 0) { $new_arr = array(); for ($i = 0; $i < count($row['cells']); $i++) { array_push($new_arr, preg_replace("/\"/", "\"\"", from_html($row['cells'][$i]))); } $line = implode("\"" . getDelimiter() . "\"", $new_arr); $line = "\"" . $line; $line .= "\"\r\n"; $content .= $line; } global $locale; $transContent = $GLOBALS['locale']->translateCharset($content, 'UTF-8', $GLOBALS['locale']->getExportCharset()); ob_clean(); header("Pragma: cache"); header("Content-type: application/octet-stream; charset=" . $locale->getExportCharset()); header("Content-Disposition: attachment; filename={$_REQUEST['module']}.csv"); header("Content-transfer-encoding: binary"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . TimeDate::httpTime()); header("Cache-Control: post-check=0, pre-check=0", false); header("Content-Length: " . mb_strlen($transContent, '8bit')); $BOM = ""; print $BOM . $transContent; }
$xtpl->assign('REPORTS_TO_ID', $focus->reports_to_id); $xtpl->assign('REPORTS_TO_NAME', $focus->reports_to_name); $xtpl->assign('PHONE_HOME', $focus->phone_home); $xtpl->assign('PHONE_MOBILE', $focus->phone_mobile); $xtpl->assign('PHONE_WORK', $focus->phone_work); $xtpl->assign('PHONE_OTHER', $focus->phone_other); $xtpl->assign('PHONE_FAX', $focus->phone_fax); $xtpl->assign('EMAIL1', $focus->email1); $xtpl->assign('EMAIL2', $focus->email2); $xtpl->assign('ADDRESS_STREET', $focus->address_street); $xtpl->assign('ADDRESS_CITY', $focus->address_city); $xtpl->assign('ADDRESS_STATE', $focus->address_state); $xtpl->assign('ADDRESS_POSTALCODE', $focus->address_postalcode); $xtpl->assign('ADDRESS_COUNTRY', $focus->address_country); $xtpl->assign('DESCRIPTION', $focus->description); $xtpl->assign('EXPORT_DELIMITER', getDelimiter()); $xtpl->assign('EXPORT_CHARSET', get_select_options_with_id($locale->availableCharsets, $locale->getExportCharset())); if ($focus->getPreference('use_real_names') == 'on') { $xtpl->assign('USE_REAL_NAMES', 'CHECKED'); } if ($focus->getPreference('no_opps') == 'on') { $xtpl->assign('NO_OPPS', 'CHECKED'); } /////////////////////////////////////////////////////////////////////////////// //// NEW USER CREATION ONLY if (empty($focus->id)) { $xtpl->assign('LBL_NEW_PASSWORD1', $mod_strings['LBL_NEW_PASSWORD1'] . ': <span class="required">' . $app_strings['LBL_REQUIRED_SYMBOL'] . '</span>'); $xtpl->assign('LBL_NEW_PASSWORD2', $mod_strings['LBL_NEW_PASSWORD2'] . ': <span class="required">' . $app_strings['LBL_REQUIRED_SYMBOL'] . '</span>'); $xtpl->assign('NEW_PASSWORD1', '<input id="new_password1" name="new_password1" tabindex="2" type="password" size="25" maxlength="25">'); $xtpl->assign('NEW_PASSWORD2', '<input id="new_password2" name="new_password2" tabindex="2" type="password" size="25" maxlength="25">'); }
/** * builds up a delimited string for export * @param string type the bean-type to export * @param array records an array of records if coming directly from a query * @return string delimited string for export */ function export($type, $records = null, $members = false) { global $beanList; global $beanFiles; global $current_user; global $app_strings; global $app_list_strings; global $timedate; $contact_fields = array("id" => "Contact ID", "lead_source" => "Lead Source", "date_entered" => "Date Entered", "date_modified" => "Date Modified", "first_name" => "First Name", "last_name" => "Last Name", "salutation" => "Salutation", "birthdate" => "Lead Source", "do_not_call" => "Do Not Call", "email_opt_out" => "Email Opt Out", "title" => "Title", "department" => "Department", "birthdate" => "Birthdate", "do_not_call" => "Do Not Call", "phone_home" => "Phone (Home)", "phone_mobile" => "Phone (Mobile)", "phone_work" => "Phone (Work)", "phone_other" => "Phone (Other)", "phone_fax" => "Fax", "email1" => "Email", "email2" => "Email (Other)", "assistant" => "Assistant", "assistant_phone" => "Assistant Phone", "primary_address_street" => "Primary Address Street", "primary_address_city" => "Primary Address City", "primary_address_state" => "Primary Address State", "primary_address_postalcode" => "Primary Address Postalcode", "primary_address_country" => "Primary Address Country", "alt_address_street" => "Other Address Street", "alt_address_city" => "Other Address City", "alt_address_state" => "Other Address State", "alt_address_postalcode" => "Other Address Postalcode", "alt_address_country" => "Other Address Country", "description" => "Description"); $account_fields = array("id" => "Account ID", "name" => "Account Name", "website" => "Website", "industry" => "Industry", "account_type" => "Type", "ticker_symbol" => "Ticker Symbol", "employees" => "Employees", "ownership" => "Ownership", "phone_office" => "Phone", "phone_fax" => "Fax", "phone_alternate" => "Other Phone", "email1" => "Email", "email2" => "Other Email", "rating" => "Rating", "sic_code" => "SIC Code", "annual_revenue" => "Annual Revenue", "billing_address_street" => "Billing Address Street", "billing_address_city" => "Billing Address City", "billing_address_state" => "Billing Address State", "billing_address_postalcode" => "Billing Address Postalcode", "billing_address_country" => "Billing Address Country", "shipping_address_street" => "Shipping Address Street", "shipping_address_city" => "Shipping Address City", "shipping_address_state" => "Shipping Address State", "shipping_address_postalcode" => "Shipping Address Postalcode", "shipping_address_country" => "Shipping Address Country", "description" => "Description"); $focus = 0; $content = ''; $bean = $beanList[$type]; require_once $beanFiles[$bean]; $focus = new $bean(); $searchFields = array(); $db = DBManagerFactory::getInstance(); if ($records) { $records = explode(',', $records); $records = "'" . implode("','", $records) . "'"; $where = "{$focus->table_name}.id in ({$records})"; } elseif (isset($_REQUEST['all'])) { $where = ''; } else { if (!empty($_REQUEST['current_post'])) { $ret_array = generateSearchWhere($type, $_REQUEST['current_post']); $where = $ret_array['where']; $searchFields = $ret_array['searchFields']; } else { $where = ''; } } $order_by = ""; if ($focus->bean_implements('ACL')) { if (!ACLController::checkAccess($focus->module_dir, 'export', true)) { ACLController::displayNoAccess(); sugar_die(''); } if (ACLController::requireOwner($focus->module_dir, 'export')) { if (!empty($where)) { $where .= ' AND '; } $where .= $focus->getOwnerWhere($current_user->id); } } // 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)); } $ret_array = create_export_query_relate_link_patch($type, $searchFields, $where); if (!empty($ret_array['join'])) { $query = $focus->create_export_query($order_by, $ret_array['where'], $ret_array['join']); } else { $query = $focus->create_export_query($order_by, $ret_array['where']); } } $result = $db->query($query, true, $app_strings['ERR_EXPORT_TYPE'] . $type . ": <BR>." . $query); $fields_array = $db->getFieldsArray($result, true); // setup the "header" line with proper delimiters $header = implode("\"" . getDelimiter() . "\"", array_values($fields_array)); if ($members) { $header = str_replace('"ea_deleted"' . getDelimiter() . '"ear_deleted"' . getDelimiter() . '"primary_address"' . getDelimiter() . '', '', $header); } $header = "\"" . $header; $header .= "\"\r\n"; $content .= $header; $pre_id = ''; while ($val = $db->fetchByAssoc($result, -1, false)) { $new_arr = array(); if ($members) { if ($pre_id == $val['id']) { continue; } if ($val['ea_deleted'] == 1 || $val['ear_deleted'] == 1) { $val['primary_email_address'] = ''; } unset($val['ea_deleted']); unset($val['ear_deleted']); unset($val['primary_address']); } $pre_id = $val['id']; $vals = array_values($val); foreach ($vals as $key => $value) { //getting content values depending on their types $fieldType = $focus->field_name_map[$fields_array[$key]]['type']; if (isset($fieldType)) { switch ($fieldType) { //if our value is a currency field, then apply the users locale case 'currency': require_once 'modules/Currencies/Currency.php'; $value = currency_format_number($value, array('currency_symbol' => false)); break; //if our value is a datetime field, then apply the users locale //if our value is a datetime field, then apply the users locale case 'datetime': case 'datetimecombo': $value = $timedate->to_display_date_time($value); $value = preg_replace('/([pm|PM|am|AM]+)/', ' \\1', $value); break; //kbrill Bug #16296 //kbrill Bug #16296 case 'date': $value = $timedate->to_display_date($value, false); break; // Bug 32463 - Properly have multienum field translated into something useful for the client // Bug 32463 - Properly have multienum field translated into something useful for the client case 'multienum': $value = str_replace("^", "", $value); if (isset($focus->field_name_map[$fields_array[$key]]['options']) && isset($app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']])) { $valueArray = explode(",", $value); foreach ($valueArray as $multikey => $multivalue) { if (isset($app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']][$multivalue])) { $valueArray[$multikey] = $app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']][$multivalue]; } } $value = implode(",", $valueArray); } break; } } array_push($new_arr, preg_replace("/\"/", "\"\"", $value)); } $line = implode("\"" . getDelimiter() . "\"", $new_arr); $line = "\"" . $line; $line .= "\"\r\n"; $content .= $line; } return $content; }
function build_report_csv() { ini_set('zlib.output_compression', 'Off'); ob_start(); require_once 'include/export_utils.php'; $delimiter = getDelimiter(); $csv = ''; //text/comma-separated-values $sql = "SELECT id FROM aor_fields WHERE aor_report_id = '" . $this->id . "' AND deleted = 0 ORDER BY field_order ASC"; $result = $this->db->query($sql); $fields = array(); $i = 0; while ($row = $this->db->fetchByAssoc($result)) { $field = new AOR_Field(); $field->retrieve($row['id']); $path = unserialize(base64_decode($field->module_path)); $field_module = $this->report_module; if ($path[0] != $this->report_module) { foreach ($path as $rel) { $field_module = getRelatedModule($field_module, $rel); } } $label = str_replace(' ', '_', $field->label) . $i; $fields[$label]['field'] = $field->field; $fields[$label]['display'] = $field->display; $fields[$label]['function'] = $field->field_function; $fields[$label]['module'] = $field_module; if ($field->display) { $csv .= $this->encloseForCSV($field->label); $csv .= $delimiter; } ++$i; } $sql = $this->build_report_query(); $result = $this->db->query($sql); while ($row = $this->db->fetchByAssoc($result)) { $csv .= "\r\n"; foreach ($fields as $name => $att) { if ($att['display']) { if ($att['function'] != '') { $csv .= $this->encloseForCSV($row[$name]); } else { $csv .= $this->encloseForCSV(trim(strip_tags(getModuleField($att['module'], $att['field'], $att['field'], 'DetailView', $row[$name])))); } $csv .= $delimiter; } } } $csv = $GLOBALS['locale']->translateCharset($csv, 'UTF-8', $GLOBALS['locale']->getExportCharset()); ob_clean(); header("Pragma: cache"); header("Content-type: text/comma-separated-values; charset=" . $GLOBALS['locale']->getExportCharset()); header("Content-Disposition: attachment; filename={$this->name}.csv"); header("Content-transfer-encoding: binary"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . TimeDate::httpTime()); header("Cache-Control: post-check=0, pre-check=0", false); header("Content-Length: " . mb_strlen($csv, '8bit')); if (!empty($sugar_config['export_excel_compatible'])) { $csv == chr(255) . chr(254) . mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8'); } print $csv; sugar_cleanup(true); }
$sugar_smarty->assign('REPORTS_TO_ID', $focus->reports_to_id); $sugar_smarty->assign('REPORTS_TO_NAME', $focus->reports_to_name); $sugar_smarty->assign('PHONE_HOME', $focus->phone_home); $sugar_smarty->assign('PHONE_MOBILE', $focus->phone_mobile); $sugar_smarty->assign('PHONE_WORK', $focus->phone_work); $sugar_smarty->assign('PHONE_OTHER', $focus->phone_other); $sugar_smarty->assign('PHONE_FAX', $focus->phone_fax); $sugar_smarty->assign('EMAIL1', $focus->email1); $sugar_smarty->assign('EMAIL2', $focus->email2); $sugar_smarty->assign('ADDRESS_STREET', $focus->address_street); $sugar_smarty->assign('ADDRESS_CITY', $focus->address_city); $sugar_smarty->assign('ADDRESS_STATE', $focus->address_state); $sugar_smarty->assign('ADDRESS_POSTALCODE', $focus->address_postalcode); $sugar_smarty->assign('ADDRESS_COUNTRY', $focus->address_country); $sugar_smarty->assign('DESCRIPTION', $focus->description); $sugar_smarty->assign('EXPORT_DELIMITER', getDelimiter()); $sugar_smarty->assign('PWDSETTINGS', $GLOBALS['sugar_config']['passwordsetting']); if (!empty($GLOBALS['sugar_config']['authenticationClass'])) { $sugar_smarty->assign('EXTERNAL_AUTH_CLASS_1', $GLOBALS['sugar_config']['authenticationClass']); $sugar_smarty->assign('EXTERNAL_AUTH_CLASS', $GLOBALS['sugar_config']['authenticationClass']); } else { if (!empty($GLOBALS['system_config']->settings['system_ldap_enabled'])) { $sugar_smarty->assign('EXTERNAL_AUTH_CLASS_1', $mod_strings['LBL_LDAP']); $sugar_smarty->assign('EXTERNAL_AUTH_CLASS', $mod_strings['LBL_LDAP_AUTHENTICATION']); } } if (!empty($focus->external_auth_only)) { $sugar_smarty->assign('EXTERNAL_AUTH_ONLY_CHECKED', 'CHECKED'); } if ($is_current_admin) { $sugar_smarty->assign('IS_ADMIN', '1');
/** * getExportContentsFromResult * * This is a function to handle the processing of generating the export contents. * * @param Mixed $focus SugarBean instance we are retrieving export results for * @param Mixed $result database result resource from the export SQL * @param bool $members used to indicate whether or not to apply filtering for header rows; false by default * @param array $remove_from_members Array of header columns to filter out; empty by default * @param bool $populate boolean used to indicate whether or not to populate with test data; false by default * @return string */ function getExportContentFromResult($focus, $result, $members = false, $remove_from_members = array(), $populate = false) { global $current_user, $locale, $app_strings; $sampleRecordNum = 5; $delimiter = getDelimiter(); $timedate = TimeDate::getInstance(); $db = DBManagerFactory::getInstance(); $fields_array = $db->getFieldsArray($result, true); // check if ID field is contained in query result $is_id_exported = in_array('id', $fields_array); //set up the order on the header row $fields_array = get_field_order_mapping($focus->module_dir, $fields_array, true, $focus->fields_to_exclude); //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); } $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; if ($locale->getExportCharset() == 'UTF-8' && !preg_match('/macintosh|mac os x|mac_powerpc/i', $user_agent)) { //Bug 55520 - add BOM to the exporting CSV so any symbols are displayed correctly in Excel $BOM = ""; $content = $BOM; } else { $content = ''; } $pre_id = ''; // Setup the "header" row with proper delimiters $content .= "\"" . implode("\"" . getDelimiter() . "\"", array_values($field_labels)) . "\"\r\n"; if ($populate) { //this is a sample request with no data, so create fake datarows $content .= returnFakeDataRow($focus, $fields_array, $sampleRecordNum); } else { $records = array(); //process retrieved record $isAdminUser = is_admin($current_user); while ($val = $db->fetchByAssoc($result, false)) { //order the values in the record array $val = get_field_order_mapping($focus->module_dir, $val); $new_arr = array(); if (!$isAdminUser) { $focus->id = !empty($val['id']) ? $val['id'] : ''; $focus->assigned_user_id = !empty($val['assigned_user_id']) ? $val['assigned_user_id'] : ''; $focus->created_by = !empty($val['created_by']) ? $val['created_by'] : ''; $focus->ACLFilterFieldList($val, array(), array("blank_value" => true)); } if ($members) { if ($is_id_exported && $pre_id == $val['id']) { continue; } if (isset($val['ea_deleted']) && isset($val['primary_email_address']) && ($val['ea_deleted'] == 1 || $val['ear_deleted'] == 1)) { $val['primary_email_address'] = ''; } unset($val['ea_deleted']); unset($val['ear_deleted']); unset($val['primary_address']); } $pre_id = $is_id_exported ? $val['id'] : ''; require_once 'include/SugarFields/SugarFieldHandler.php'; //replace user_name with full name if use_real_name preference setting is enabled //and this is a user name field $useRealNames = $current_user->getPreference('use_real_names'); foreach ($val as $key => $value) { //if key is not part of field map, then continue if (!isset($fields_array[$key])) { continue; } //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, $val); } if (isset($focus->field_name_map[$fields_array[$key]]['custom_type']) && $focus->field_name_map[$fields_array[$key]]['custom_type'] == 'teamset') { require_once 'modules/Teams/TeamSetManager.php'; $value = TeamSetManager::getCommaDelimitedTeams($val['team_set_id'], !empty($val['team_id']) ? $val['team_id'] : ''); } if ($useRealNames) { $value = formatRealNameField($focus, $fields_array, $key, $value); } // Keep as $key => $value for post-processing $new_arr[$key] = str_replace('"', '""', $value); } //foreach // Use Bean ID as key for records if it exists if ($is_id_exported) { $records[$pre_id] = $new_arr; } else { $records[] = $new_arr; } } // Check if we're going to export non-primary emails if ($is_id_exported && $focus->hasEmails()) { // Add header column $field_labels['email_addresses_non_primary'] = translateForExport('email_addresses_non_primary', $focus); // $records keys are bean ids $keys = array_keys($records); $email_data = getNonPrimaryEmailsData($focus, $keys); foreach (array_keys($records) as $bean_id) { $records[$bean_id]['email_addresses_non_primary'] = isset($email_data[$bean_id]) ? $email_data[$bean_id] : ''; } } // Write the export data foreach ($records as $record) { $line = implode("\"" . $delimiter . "\"", $record); $line = "\"" . $line; $line .= "\"\r\n"; $content .= $line; } } return $content; }
// TODO: Convert to Idiorm if this is ever needed again require_once 'phplib/util.php'; require_once 'phplib/ads/adsModule.php'; require_once 'phplib/ads/diverta/divertaAdsModule.php'; define('IMG_URL_PREFIX', 'http://www.dol.ro/wcsstore/DOL/'); define('ORIG_FILE_PREFIX', '/tmp/dol/big/'); define('THUMB_PREFIX', util_getRootPath() . 'wwwbase/img/diverta/thumb/'); define('IMG_NORMAL', 0); define('IMG_NOT_JPEG', 1); define('IMG_CORRUPT', 2); $opts = getopt('f:h:d:s:t:a:p:u:i:o:'); if (count($opts) != 10) { usage(); } define('CSV_DELIMITER', getDelimiter($opts['d'])); OS::executeAndAssert('mkdir -p ' . ORIG_FILE_PREFIX); if (!file_exists($opts['f'])) { print "Input file does not exist.\n"; exit; } $handle = fopen($opts['f'], "r"); $i = 0; while (($fields = fgetcsv($handle, 10000, CSV_DELIMITER)) !== false) { $i++; if ($i <= $opts['h']) { continue; } $sku = $fields[$opts['s']]; print "Line {$i}: [{$sku}]\n"; // Reuse the record or create a new one
/** * builds up a delimited string for export * @param string type the bean-type to export * @param array records an array of records if coming directly from a query * @return string delimited string for export */ function export($type, $records = null, $members = false, $sample = false) { global $locale; global $beanList; global $beanFiles; global $current_user; global $app_strings; global $app_list_strings; global $timedate; global $mod_strings; global $current_language; $sampleRecordNum = 5; $contact_fields = array("id" => "Contact ID", "lead_source" => "Lead Source", "date_entered" => "Date Entered", "date_modified" => "Date Modified", "first_name" => "First Name", "last_name" => "Last Name", "salutation" => "Salutation", "birthdate" => "Lead Source", "do_not_call" => "Do Not Call", "email_opt_out" => "Email Opt Out", "title" => "Title", "department" => "Department", "birthdate" => "Birthdate", "do_not_call" => "Do Not Call", "phone_home" => "Phone (Home)", "phone_mobile" => "Phone (Mobile)", "phone_work" => "Phone (Work)", "phone_other" => "Phone (Other)", "phone_fax" => "Fax", "email1" => "Email", "email2" => "Email (Other)", "assistant" => "Assistant", "assistant_phone" => "Assistant Phone", "primary_address_street" => "Primary Address Street", "primary_address_city" => "Primary Address City", "primary_address_state" => "Primary Address State", "primary_address_postalcode" => "Primary Address Postalcode", "primary_address_country" => "Primary Address Country", "alt_address_street" => "Other Address Street", "alt_address_city" => "Other Address City", "alt_address_state" => "Other Address State", "alt_address_postalcode" => "Other Address Postalcode", "alt_address_country" => "Other Address Country", "description" => "Description"); $account_fields = array("id" => "Account ID", "name" => "Account Name", "website" => "Website", "industry" => "Industry", "account_type" => "Type", "ticker_symbol" => "Ticker Symbol", "employees" => "Employees", "ownership" => "Ownership", "phone_office" => "Phone", "phone_fax" => "Fax", "phone_alternate" => "Other Phone", "email1" => "Email", "email2" => "Other Email", "rating" => "Rating", "sic_code" => "SIC Code", "annual_revenue" => "Annual Revenue", "billing_address_street" => "Billing Address Street", "billing_address_city" => "Billing Address City", "billing_address_state" => "Billing Address State", "billing_address_postalcode" => "Billing Address Postalcode", "billing_address_country" => "Billing Address Country", "shipping_address_street" => "Shipping Address Street", "shipping_address_city" => "Shipping Address City", "shipping_address_state" => "Shipping Address State", "shipping_address_postalcode" => "Shipping Address Postalcode", "shipping_address_country" => "Shipping Address Country", "description" => "Description"); //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 = 0; $bean = $beanList[$type]; require_once $beanFiles[$bean]; $focus = new $bean(); $searchFields = array(); $db = DBManagerFactory::getInstance(); if ($records) { $records = explode(',', $records); $records = "'" . implode("','", $records) . "'"; $where = "{$focus->table_name}.id in ({$records})"; } elseif (isset($_REQUEST['all'])) { $where = ''; } else { if (!empty($_REQUEST['current_post'])) { $ret_array = generateSearchWhere($type, $_REQUEST['current_post']); $where = $ret_array['where']; $searchFields = $ret_array['searchFields']; } else { $where = ''; } } $order_by = ""; if ($focus->bean_implements('ACL')) { if (!ACLController::checkAccess($focus->module_dir, 'export', true)) { ACLController::displayNoAccess(); sugar_die(''); } if (ACLController::requireOwner($focus->module_dir, 'export')) { if (!empty($where)) { $where .= ' AND '; } $where .= $focus->getOwnerWhere($current_user->id); } /* BEGIN - SECURITY GROUPS */ if (ACLController::requireSecurityGroup($focus->module_dir, 'export')) { require_once 'modules/SecurityGroups/SecurityGroup.php'; global $current_user; $owner_where = $focus->getOwnerWhere($current_user->id); $group_where = SecurityGroup::getGroupWhere($focus->table_name, $focus->module_dir, $current_user->id); if (!empty($owner_where)) { if (empty($where)) { $where = " (" . $owner_where . " or " . $group_where . ")"; } else { $where .= " AND (" . $owner_where . " or " . $group_where . ")"; } } else { if (!empty($where)) { $where .= ' AND '; } $where .= $group_where; } } /* END - SECURITY GROUPS */ } // 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($order_by, $where); } $result = ''; $populate = false; if ($sample) { $result = $db->limitQuery($query, 0, $sampleRecordNum, true, $app_strings['ERR_EXPORT_TYPE'] . $type . ": <BR>." . $query); if ($focus->_get_num_rows_in_query($query) < 1) { $populate = true; } } else { $result = $db->query($query, true, $app_strings['ERR_EXPORT_TYPE'] . $type . ": <BR>." . $query); } $fields_array = $db->getFieldsArray($result, true); //set up the order on the header row $fields_array = get_field_order_mapping($focus->module_dir, $fields_array); //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); } $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; if ($locale->getExportCharset() == 'UTF-8' && !preg_match('/macintosh|mac os x|mac_powerpc/i', $user_agent)) { //Bug 55520 - add BOM to the exporting CSV so any symbols are displayed correctly in Excel $BOM = ""; $content = $BOM; } else { $content = ''; } // setup the "header" line with proper delimiters $content .= "\"" . implode("\"" . getDelimiter() . "\"", array_values($field_labels)) . "\"\r\n"; $pre_id = ''; if ($populate) { //this is a sample request with no data, so create fake datarows $content .= returnFakeDataRow($focus, $fields_array, $sampleRecordNum); } else { $records = array(); //process retrieved record while ($val = $db->fetchByAssoc($result, false)) { //order the values in the record array $val = get_field_order_mapping($focus->module_dir, $val); $new_arr = array(); if ($members) { if ($pre_id == $val['id']) { continue; } if ($val['ea_deleted'] == 1 || $val['ear_deleted'] == 1) { $val['primary_email_address'] = ''; } unset($val['ea_deleted']); unset($val['ear_deleted']); unset($val['primary_address']); } $pre_id = $val['id']; foreach ($val 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']) { $fieldType = $focus->field_name_map[$fieldNameMapKey]['type']; switch ($fieldType) { //if our value is a currency field, then apply the users locale case 'currency': require_once 'modules/Currencies/Currency.php'; $value = currency_format_number($value); break; //if our value is a datetime field, then apply the users locale //if our value is a datetime field, then apply the users locale case 'datetime': case 'datetimecombo': $value = $timedate->to_display_date_time($db->fromConvert($value, 'datetime')); $value = preg_replace('/([pm|PM|am|AM]+)/', ' \\1', $value); break; //kbrill Bug #16296 //kbrill Bug #16296 case 'date': $value = $timedate->to_display_date($db->fromConvert($value, 'date'), false); break; // Bug 32463 - Properly have multienum field translated into something useful for the client // Bug 32463 - Properly have multienum field translated into something useful for the client case 'multienum': $value = str_replace("^", "", $value); if (isset($focus->field_name_map[$fields_array[$key]]['options']) && isset($app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']])) { $valueArray = explode(",", $value); foreach ($valueArray as $multikey => $multivalue) { if (isset($app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']][$multivalue])) { $valueArray[$multikey] = $app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']][$multivalue]; } } $value = implode(",", $valueArray); } break; } } // Keep as $key => $value for post-processing $new_arr[$key] = preg_replace("/\"/", "\"\"", $value); } // Use Bean ID as key for records $records[$pre_id] = $new_arr; } // Check if we're going to export non-primary emails if ($focus->hasEmails() && in_array('email_addresses_non_primary', $fields_array)) { // $records keys are bean ids $keys = array_keys($records); // Split the ids array into chunks of size 100 $chunks = array_chunk($keys, 100); foreach ($chunks as $chunk) { // Pick all the non-primary mails for the chunk $query = "\n SELECT eabr.bean_id, ea.email_address\n FROM email_addr_bean_rel eabr\n LEFT JOIN email_addresses ea ON ea.id = eabr.email_address_id\n WHERE eabr.bean_module = '{$focus->module_dir}'\n AND eabr.primary_address = '0'\n AND eabr.bean_id IN ('" . implode("', '", $chunk) . "')\n AND eabr.deleted != '1'\n ORDER BY eabr.bean_id, eabr.reply_to_address, eabr.primary_address DESC\n "; $result = $db->query($query, true, $app_strings['ERR_EXPORT_TYPE'] . $type . ": <BR>." . $query); while ($val = $db->fetchByAssoc($result, false)) { if (empty($records[$val['bean_id']]['email_addresses_non_primary'])) { $records[$val['bean_id']]['email_addresses_non_primary'] = $val['email_address']; } else { // No custom non-primary mail delimeter yet, use semi-colon $records[$val['bean_id']]['email_addresses_non_primary'] .= ';' . $val['email_address']; } } } } foreach ($records as $record) { $line = implode("\"" . getDelimiter() . "\"", $record); $line = "\"" . $line; $line .= "\"\r\n"; $content .= $line; } } return $content; }
$xtpl->assign("IS_GROUP", "checked"); } if ((is_admin($current_user) || $_REQUEST['record'] == $current_user->id) && $focus->is_admin == '1') { $xtpl->assign("IS_ADMIN", "checked"); } if ($focus->receive_notifications) { $xtpl->assign("RECEIVE_NOTIFICATIONS", "checked"); } if ($focus->getPreference('gridline') == 'on') { $xtpl->assign("GRIDLINE_CHECK", "checked"); } if ($focus->getPreference('mailmerge_on') == 'on') { $xtpl->assign("MAILMERGE_ON", "checked"); } $xtpl->assign("SETTINGS_URL", $sugar_config['site_url']); $xtpl->assign("EXPORT_DELIMITER", getDelimiter()); $xtpl->assign('EXPORT_CHARSET', $locale->getExportCharset('', $focus)); $xtpl->assign('USE_REAL_NAMES', $focus->getPreference('use_real_names')); global $timedate; $xtpl->assign("DATEFORMAT", $sugar_config['date_formats'][$timedate->get_date_format()]); $xtpl->assign("TIMEFORMAT", $sugar_config['time_formats'][$timedate->get_time_format()]); $userTZ = $focus->getPreference('timezone'); if (!empty($userTZ) && !empty($timezones[$userTZ])) { $value = $timezones[$userTZ]; } if (!empty($value['dstOffset'])) { $dst = " (+DST)"; } else { $dst = ""; } $gmtOffset = $value['gmtOffset'] / 60;
/** * builds up a delimited string for export * @param string type the bean-type to export * @param array records an array of records if coming directly from a query * @return string delimited string for export */ function export($type, $records = null) { global $beanList; global $beanFiles; global $current_user; global $app_strings; $contact_fields = array("id" => "Contact ID", "lead_source" => "Lead Source", "date_entered" => "Date Entered", "date_modified" => "Date Modified", "first_name" => "First Name", "last_name" => "Last Name", "salutation" => "Salutation", "birthdate" => "Lead Source", "do_not_call" => "Do Not Call", "email_opt_out" => "Email Opt Out", "title" => "Title", "department" => "Department", "birthdate" => "Birthdate", "do_not_call" => "Do Not Call", "phone_home" => "Phone (Home)", "phone_mobile" => "Phone (Mobile)", "phone_work" => "Phone (Work)", "phone_other" => "Phone (Other)", "phone_fax" => "Fax", "email1" => "Email", "email2" => "Email (Other)", "assistant" => "Assistant", "assistant_phone" => "Assistant Phone", "primary_address_street" => "Primary Address Street", "primary_address_city" => "Primary Address City", "primary_address_state" => "Primary Address State", "primary_address_postalcode" => "Primary Address Postalcode", "primary_address_country" => "Primary Address Country", "alt_address_street" => "Other Address Street", "alt_address_city" => "Other Address City", "alt_address_state" => "Other Address State", "alt_address_postalcode" => "Other Address Postalcode", "alt_address_country" => "Other Address Country", "description" => "Description"); $account_fields = array("id" => "Account ID", "name" => "Account Name", "website" => "Website", "industry" => "Industry", "account_type" => "Type", "ticker_symbol" => "Ticker Symbol", "employees" => "Employees", "ownership" => "Ownership", "phone_office" => "Phone", "phone_fax" => "Fax", "phone_alternate" => "Other Phone", "email1" => "Email", "email2" => "Other Email", "rating" => "Rating", "sic_code" => "SIC Code", "annual_revenue" => "Annual Revenue", "billing_address_street" => "Billing Address Street", "billing_address_city" => "Billing Address City", "billing_address_state" => "Billing Address State", "billing_address_postalcode" => "Billing Address Postalcode", "billing_address_country" => "Billing Address Country", "shipping_address_street" => "Shipping Address Street", "shipping_address_city" => "Shipping Address City", "shipping_address_state" => "Shipping Address State", "shipping_address_postalcode" => "Shipping Address Postalcode", "shipping_address_country" => "Shipping Address Country", "description" => "Description"); $focus = 0; $content = ''; $bean = $beanList[$type]; require_once $beanFiles[$bean]; $focus = new $bean(); $db = PearDatabase::getInstance(); if ($records) { $records = explode(',', $records); $records = "'" . implode("','", $records) . "'"; $where = "{$focus->table_name}.id in ({$records})"; } elseif (isset($_REQUEST['all'])) { $where = ''; } else { if (isset($_SESSION['export_where']) && !empty($_SESSION['export_where'])) { // bug 4679 $where = $_SESSION['export_where']; } else { $where = ''; } } $order_by = ""; if ($focus->bean_implements('ACL')) { if (!ACLController::checkAccess($focus->module_dir, 'export', true)) { ACLController::displayNoAccess(); sugar_die(''); } if (ACLController::requireOwner($focus->module_dir, 'export')) { if (!empty($where)) { $where .= ' AND '; } $where .= $focus->getOwnerWhere($current_user->id); } } // 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. $beginWhere = substr(trim($where), 0, 5); if ($beginWhere == "where") { $where = substr(trim($where), 5, strlen($where)); } $query = $focus->create_export_query($order_by, $where); $result = $db->query($query, true, $app_strings['ERR_EXPORT_TYPE'] . $type . ": <BR>." . $query); $fields_array = $db->getFieldsArray($result); // setup the "header" line with quotation marks $header = implode("\"" . getDelimiter() . "\"", array_values($fields_array)); $header = "\"" . $header; $header .= "\"\r\n"; $content .= $header; while ($val = $db->fetchByAssoc($result, -1, false)) { $new_arr = array(); foreach (array_values($val) as $value) { array_push($new_arr, preg_replace("/\"/", "\"\"", $value)); } $line = implode("\"" . getDelimiter() . "\"", $new_arr); $line = "\"" . $line; $line .= "\"\r\n"; $content .= $line; } return $content; }
/** * builds up a delimited string for export * @param string type the bean-type to export * @param array records an array of records if coming directly from a query * @return string delimited string for export */ function export($type, $records = null, $members = false, $sample = false) { global $locale; global $beanList; global $beanFiles; global $current_user; global $app_strings; global $app_list_strings; global $timedate; global $mod_strings; global $current_language; $sampleRecordNum = 5; //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 = 0; $bean = $beanList[$type]; require_once $beanFiles[$bean]; $focus = new $bean(); $searchFields = array(); $db = DBManagerFactory::getInstance(); if ($records) { $records = explode(',', $records); $records = "'" . implode("','", $records) . "'"; $where = "{$focus->table_name}.id in ({$records})"; } elseif (isset($_REQUEST['all'])) { $where = ''; } else { if (!empty($_REQUEST['current_post'])) { $ret_array = generateSearchWhere($type, $_REQUEST['current_post']); $where = $ret_array['where']; $searchFields = $ret_array['searchFields']; } else { $where = ''; } } $order_by = ""; if ($focus->bean_implements('ACL')) { if (!ACLController::checkAccess($focus->module_dir, 'export', true)) { ACLController::displayNoAccess(); sugar_die(''); } if (ACLController::requireOwner($focus->module_dir, 'export')) { if (!empty($where)) { $where .= ' AND '; } $where .= $focus->getOwnerWhere($current_user->id); } /* BEGIN - SECURITY GROUPS */ if (ACLController::requireSecurityGroup($focus->module_dir, 'export')) { require_once 'modules/SecurityGroups/SecurityGroup.php'; global $current_user; $owner_where = $focus->getOwnerWhere($current_user->id); $group_where = SecurityGroup::getGroupWhere($focus->table_name, $focus->module_dir, $current_user->id); if (!empty($owner_where)) { if (empty($where)) { $where = " (" . $owner_where . " or " . $group_where . ")"; } else { $where .= " AND (" . $owner_where . " or " . $group_where . ")"; } } else { if (!empty($where)) { $where .= ' AND '; } $where .= $group_where; } } /* END - SECURITY GROUPS */ } // 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($order_by, $where); } $result = ''; $populate = false; if ($sample) { $result = $db->limitQuery($query, 0, $sampleRecordNum, true, $app_strings['ERR_EXPORT_TYPE'] . $type . ": <BR>." . $query); if ($focus->_get_num_rows_in_query($query) < 1) { $populate = true; } } else { $result = $db->query($query, true, $app_strings['ERR_EXPORT_TYPE'] . $type . ": <BR>." . $query); } $fields_array = $db->getFieldsArray($result, true); //set up the order on the header row $fields_array = get_field_order_mapping($focus->module_dir, $fields_array); //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; } //If labels should not be exportable skip them if (isset($focus->field_name_map[$key]) && isset($focus->field_name_map[$key]['exportable']) && $focus->field_name_map[$key]['exportable'] === false) { continue; } //default to the db name of label does not exist $field_labels[$key] = translateForExport($dbname, $focus); } $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; if ($locale->getExportCharset() == 'UTF-8' && !preg_match('/macintosh|mac os x|mac_powerpc/i', $user_agent)) { //Bug 55520 - add BOM to the exporting CSV so any symbols are displayed correctly in Excel $BOM = ""; $content = $BOM; } else { $content = ''; } // setup the "header" line with proper delimiters $content .= "\"" . implode("\"" . getDelimiter() . "\"", array_values($field_labels)) . "\"\r\n"; $pre_id = ''; if ($populate) { //this is a sample request with no data, so create fake datarows $content .= returnFakeDataRow($focus, $fields_array, $sampleRecordNum); } else { $records = array(); //process retrieved record while ($val = $db->fetchByAssoc($result, false)) { if ($members) { $focus = BeanFactory::getBean($val['related_type']); } else { // field order mapping is not applied for member-exports, as they include multiple modules //order the values in the record array $val = get_field_order_mapping($focus->module_dir, $val); } $new_arr = array(); if ($members) { if ($pre_id == $val['id']) { continue; } if ($val['ea_deleted'] == 1 || $val['ear_deleted'] == 1) { $val['primary_email_address'] = ''; } unset($val['ea_deleted']); unset($val['ear_deleted']); unset($val['primary_address']); } $pre_id = $val['id']; foreach ($val as $key => $value) { //getting content values depending on their types $fieldNameMapKey = $fields_array[$key]; //Dont export fields that have been explicitly marked not to be exportable if (isset($focus->field_name_map[$fieldNameMapKey]) && isset($focus->field_name_map[$fieldNameMapKey]['exportable']) && $focus->field_name_map[$fieldNameMapKey]['exportable'] === false) { continue; } if (isset($focus->field_name_map[$fieldNameMapKey]) && $focus->field_name_map[$fieldNameMapKey]['type']) { $fieldType = $focus->field_name_map[$fieldNameMapKey]['type']; switch ($fieldType) { //if our value is a currency field, then apply the users locale case 'currency': require_once 'modules/Currencies/Currency.php'; $value = currency_format_number($value); break; //if our value is a datetime field, then apply the users locale //if our value is a datetime field, then apply the users locale case 'datetime': case 'datetimecombo': $value = $timedate->to_display_date_time($db->fromConvert($value, 'datetime')); $value = preg_replace('/([pm|PM|am|AM]+)/', ' \\1', $value); break; //kbrill Bug #16296 //kbrill Bug #16296 case 'date': $value = $timedate->to_display_date($db->fromConvert($value, 'date'), false); break; // Bug 32463 - Properly have multienum field translated into something useful for the client // Bug 32463 - Properly have multienum field translated into something useful for the client case 'multienum': $valueArray = unencodeMultiEnum($value); if (isset($focus->field_name_map[$fields_array[$key]]['options']) && isset($app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']])) { foreach ($valueArray as $multikey => $multivalue) { if (isset($app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']][$multivalue])) { $valueArray[$multikey] = $app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']][$multivalue]; } } } $value = implode(",", $valueArray); break; case 'enum': if (isset($focus->field_name_map[$fields_array[$key]]['options']) && isset($app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']]) && isset($app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']][$value])) { $value = $app_list_strings[$focus->field_name_map[$fields_array[$key]]['options']][$value]; } break; } } // Keep as $key => $value for post-processing $new_arr[$key] = preg_replace("/\"/", "\"\"", $value); } // Use Bean ID as key for records $records[$pre_id] = $new_arr; } // Check if we're going to export non-primary emails if ($focus->hasEmails() && in_array('email_addresses_non_primary', $fields_array)) { // $records keys are bean ids $keys = array_keys($records); // Split the ids array into chunks of size 100 $chunks = array_chunk($keys, 100); foreach ($chunks as $chunk) { // Pick all the non-primary mails for the chunk $query = "\n SELECT eabr.bean_id, ea.email_address\n FROM email_addr_bean_rel eabr\n LEFT JOIN email_addresses ea ON ea.id = eabr.email_address_id\n WHERE eabr.bean_module = '{$focus->module_dir}'\n AND eabr.primary_address = '0'\n AND eabr.bean_id IN ('" . implode("', '", $chunk) . "')\n AND eabr.deleted != '1'\n ORDER BY eabr.bean_id, eabr.reply_to_address, eabr.primary_address DESC\n "; $result = $db->query($query, true, $app_strings['ERR_EXPORT_TYPE'] . $type . ": <BR>." . $query); while ($val = $db->fetchByAssoc($result, false)) { if (empty($records[$val['bean_id']]['email_addresses_non_primary'])) { $records[$val['bean_id']]['email_addresses_non_primary'] = $val['email_address']; } else { // No custom non-primary mail delimeter yet, use semi-colon $records[$val['bean_id']]['email_addresses_non_primary'] .= ';' . $val['email_address']; } } } } $customRelateFields = array(); $selects = array(); foreach ($records as $record) { foreach ($record as $recordKey => $recordValue) { if (preg_match('/{relate\\s+from=""([^"]+)""\\s+to=""([^"]+)""}/', $recordValue, $matches)) { $marker = $matches[0]; $relatedValue = ''; $splits = explode('.', $matches[1]); $currentModule = $splits[0]; $currentField = $splits[1]; $currentBean = BeanFactory::getBean($currentModule); $currentTable = $currentBean->table_name; $splits = explode('.', $matches[2]); $relatedModule = $splits[0]; $relatedField = $splits[1]; $relatedBean = BeanFactory::getBean($relatedModule); $relatedTable = $relatedBean->table_name; $relatedLabel = "{$relatedTable}.name AS related_label, NULL AS related_label1"; if (isset($relatedBean->field_defs['name']['source']) && $relatedBean->field_defs['name']['source'] == 'non-db') { //$relatedLabel = 'NULL AS related_label, NULL AS related_label1'; if (!isset($relatedBean->field_defs['first_name']['source']) || $relatedBean->field_defs['first_name']['source'] != 'non-db' && !isset($relatedBean->field_defs['last_name']['source']) || $relatedBean->field_defs['last_name']['source'] != 'non-db') { $relatedLabel = "{$relatedTable}.last_name AS related_label, {$relatedTable}.first_name AS related_label1"; } } $relatedTableCustomJoin = ''; $relatedFieldSelect = "NULL AS related_value"; if (!isset($existsTables["{$relatedTable}_cstm"])) { $existsTables["{$relatedTable}_cstm"] = $db->tableExists("{$relatedTable}_cstm"); } if ($existsTables["{$relatedTable}_cstm"]) { $relatedTableCustomJoin = "\n JOIN {$relatedTable}_cstm ON {$relatedTable}_cstm.id_c = {$currentTable}_cstm.{$relatedField}\n "; $relatedFieldSelect = "{$currentTable}_cstm.{$relatedField} AS related_value"; } $relatedTableJoin = "LEFT JOIN {$relatedTable} ON {$relatedTable}.id = {$currentTable}_cstm.id_c"; if (isset($currentBean->field_defs[$relatedField])) { $relatedTableJoin = "LEFT JOIN {$relatedTable} ON {$relatedTable}.id = {$currentTable}_cstm.{$relatedField}"; } //-- $relatedTable.id AS related_id, //-- {$currentTable}_cstm.id_c AS current_id_c, //-- {$relatedTable}_cstm.id_c AS related_id_c, $selects[] = "(SELECT {$currentTable}.id AS current_id,'{$currentModule}' AS current_module,'{$currentField}' AS current_field,'{$relatedModule}' AS related_module,'{$relatedField}' AS related_field,{$relatedFieldSelect},{$relatedLabel} FROM {$currentTable} JOIN {$currentTable}_cstm ON {$currentTable}_cstm.id_c={$currentTable}.id {$relatedTableCustomJoin} {$relatedTableJoin} WHERE {$currentTable}.id='{$record['id']}')"; } } } $selects = array_unique($selects); // grab custom related fields information // query max length optimization, measured by mssql FreeTDS connection too $queryMaxLength = 620000; $query = ''; $i = 0; $selectsCount = count($selects) - 1; foreach ($selects as $select) { $queryTemp = $query . ($i == 0 ? $select : " UNION {$select}"); if ($i == $selectsCount || strlen($queryTemp) > $queryMaxLength) { $result = $db->query($query, 'export error on custom related type: ' . $query); while ($val = $db->fetchByAssoc($result, false)) { $customRelateFields[$val['current_module']][$val['current_id']][$val['related_module']][$val['related_field']] = trim($val['related_label'] . ' ' . $val['related_label1']); } $query = $select; } else { $query = $queryTemp; } $i++; } foreach ($records as $record) { $line = implode("\"" . getDelimiter() . "\"", $record); $line = "\"" . $line; $line .= "\"\r\n"; $line = parseRelateFields($line, $record, $customRelateFields); $content .= $line; } } return $content; }
/** * getExportDataContent * * Returns the export content for the data portion * * @param $data * @param $focus * @param $fields_array * @param string $filter_by What field that should be filtered on * @param array $filters The values to check $filter_by * * @return string content for the data portion of export */ protected function getExportDataContent($data, $focus, $fields_array, $filter_by = null, array $filters = array()) { require_once 'include/SugarFields/SugarFieldHandler.php'; require_once 'include/export_utils.php'; global $current_user; $content = ''; $delimiter = getDelimiter(); //process retrieved record $isAdminUser = is_admin($current_user); foreach ($data as $val) { if (!empty($filter_by) && !empty($filters) && isset($val[$filter_by]) && !in_array($val[$filter_by], $filters)) { // make sure the value from the row is in the filters // skip this row as it's not in the filters continue; } $new_arr = array(); if (!$isAdminUser) { $focus->id = !empty($val['id']) ? $val['id'] : ''; $focus->assigned_user_id = !empty($val['assigned_user_id']) ? $val['assigned_user_id'] : ''; $focus->created_by = !empty($val['created_by']) ? $val['created_by'] : ''; $focus->ACLFilterFieldList($val, array(), array("blank_value" => true)); } foreach ($fields_array as $key => $label) { $value = $val[$key]; //getting content values depending on their types if (isset($focus->field_defs[$key])) { $sfh = SugarFieldHandler::getSugarField($focus->field_defs[$key]['type']); $value = $sfh->exportSanitize($value, $focus->field_defs[$key], $focus, $val); } $new_arr[$key] = preg_replace("/\"/", "\"\"", $value); } $line = implode("\"" . $delimiter . "\"", $new_arr); $content .= "\"" . $line . "\"\r\n"; } return $content; }
$activity->date_start = $timedate->to_display_date_time($row['date_start']); if ($row['status'] == 'None') { $activity->status = $mod_strings['LBL_NONE_STRING']; } else { $activity->status = $row['status']; } $activity->type = $app_list_strings['moduleListSingular'][$row['call']]; $activity->id = $row['id']; array_push($activities, $activity); $row = $focus->db->fetchByAssoc($result); } if (isset($_REQUEST['export_report']) && $_REQUEST['export_report'] == '1') { require_once 'include/export_utils.php'; $content = '"' . preg_replace("/\"/", "\"\"", $mod_strings['LBL_TYPE']) . '"' . getDelimiter() . '"' . preg_replace("/\"/", "\"\"", $mod_strings['LBL_SUBJECT']) . '"' . getDelimiter() . '"' . preg_replace("/\"/", "\"\"", $mod_strings['LBL_DATE']) . '"' . getDelimiter() . '"' . preg_replace("/\"/", "\"\"", $mod_strings['LBL_STATUS']) . '"' . getDelimiter() . '"' . preg_replace("/\"/", "\"\"", $mod_strings['LBL_CHART_DESCRIPTION']) . '"' . "\r\n"; foreach ($activities as $activity) { $content .= '"' . preg_replace("/\"/", "\"\"", $activity->type) . '"' . getDelimiter() . '"' . preg_replace("/\"/", "\"\"", $activity->name) . '"' . getDelimiter() . '"' . preg_replace("/\"/", "\"\"", $timedate->to_display_date_time($activity->date_start)) . '"' . getDelimiter() . '"' . preg_replace("/\"/", "\"\"", $activity->status) . '"' . getDelimiter() . '"' . preg_replace("/\"/", "\"\"", $activity->description) . '"' . "\r\n"; } ob_clean(); header("Pragma: cache"); header("Content-type: application/octet-stream; charset=" . $locale->getExportCharset()); header("Content-Disposition: attachment; filename={$_REQUEST['module']}.csv"); header("Content-transfer-encoding: binary"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: post-check=0, pre-check=0", false); header("Content-Length: " . strlen($content)); print $GLOBALS['locale']->translateCharset($content, 'UTF-8', $locale->getExportCharset()); exit; } } $sugar_smarty->assign('count', count($activities));
$content = export(clean_string($_REQUEST['module'])); } } $filename = $_REQUEST['module']; //use label if one is defined if (!empty($app_list_strings['moduleList'][$_REQUEST['module']])) { $filename = $app_list_strings['moduleList'][$_REQUEST['module']]; } //strip away any blank spaces $filename = str_replace(' ', '', $filename); if ($_REQUEST['members'] == true) { $filename .= '_' . 'members'; } $content = explode("\"\r\n\"", $content); foreach ($content as $val) { $temp = explode("\"" . getDelimiter() . "\"", $val); $data[] = $temp; } /** * added by Hoc Bui */ $objPHPExcel = new PHPExcel(); // Set document properties $objPHPExcel->getProperties()->setCreator("OnlineCRM")->setLastModifiedBy("OnlineCRM")->setTitle($filename)->setSubject("Office 2007 XLSX Test Document")->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")->setKeywords("office 2007 openxml php")->setCategory("Test result file"); // Add export data $activeSheet = $objPHPExcel->setActiveSheetIndex(0); $focus = new $GLOBALS['beanList'][$the_module](); $row_num = 1; foreach ($data as $row) { $col_num = 0; foreach ($row as $key => $value) {