/** * This function exports all the data for a given module * Param $type - module name * Return type text */ function export($type) { global $log, $list_max_entries_per_page; $log->debug("Entering export(" . $type . ") method ..."); global $adb; $focus = 0; $content = ''; if ($type != "") { // vtlib customization: Hook to dynamically include required module file. // Refer to the logic in setting $currentModule in index.php $focus = CRMEntity::getInstance($type); } $log = LoggerManager::getLogger('export_' . $type); $db = PearDatabase::getInstance(); $oCustomView = new CustomView("{$type}"); $viewid = $oCustomView->getViewId("{$type}"); $sorder = $focus->getSortOrder(); $order_by = $focus->getOrderBy(); $search_type = $_REQUEST['search_type']; $export_data = $_REQUEST['export_data']; if (isset($_SESSION['export_where']) && $_SESSION['export_where'] != '' && $search_type == 'includesearch') { $where = $_SESSION['export_where']; } $query = $focus->create_export_query($where); if ($search_type != 'includesearch' && $type != 'Calendar') { $stdfiltersql = $oCustomView->getCVStdFilterSQL($viewid); $advfiltersql = $oCustomView->getCVAdvFilterSQL($viewid); if (isset($stdfiltersql) && $stdfiltersql != '') { $query .= ' and ' . $stdfiltersql; } if (isset($advfiltersql) && $advfiltersql != '') { $query .= ' and ' . $advfiltersql; } } $params = array(); if (($search_type == 'withoutsearch' || $search_type == 'includesearch') && $export_data == 'selecteddata') { $idstring = explode(";", $_REQUEST['idstring']); if ($type == 'Accounts' && count($idstring) > 0) { $query .= ' and vtiger_account.accountid in (' . generateQuestionMarks($idstring) . ')'; array_push($params, $idstring); } elseif ($type == 'Contacts' && count($idstring) > 0) { $query .= ' and vtiger_contactdetails.contactid in (' . generateQuestionMarks($idstring) . ')'; array_push($params, $idstring); } elseif ($type == 'Potentials' && count($idstring) > 0) { $query .= ' and vtiger_potential.potentialid in (' . generateQuestionMarks($idstring) . ')'; array_push($params, $idstring); } elseif ($type == 'Leads' && count($idstring) > 0) { $query .= ' and vtiger_leaddetails.leadid in (' . generateQuestionMarks($idstring) . ')'; array_push($params, $idstring); } elseif ($type == 'Products' && count($idstring) > 0) { $query .= ' and vtiger_products.productid in (' . generateQuestionMarks($idstring) . ')'; array_push($params, $idstring); } elseif ($type == 'Documents' && count($idstring) > 0) { $query .= ' and vtiger_notes.notesid in (' . generateQuestionMarks($idstring) . ')'; array_push($params, $idstring); } elseif ($type == 'HelpDesk' && count($idstring) > 0) { $query .= ' and vtiger_troubletickets.ticketid in (' . generateQuestionMarks($idstring) . ')'; array_push($params, $idstring); } elseif ($type == 'Vendors' && count($idstring) > 0) { $query .= ' and vtiger_vendor.vendorid in (' . generateQuestionMarks($idstring) . ')'; array_push($params, $idstring); } else { if (count($idstring) > 0) { // vtlib customization: Hook to make the export feature available for custom modules. $query .= " and {$focus->table_name}.{$focus->table_index} in (" . generateQuestionMarks($idstring) . ')'; array_push($params, $idstring); // END } } } if (isset($order_by) && $order_by != '') { if ($order_by == 'smownerid') { $query .= ' ORDER BY user_name ' . $sorder; } elseif ($order_by == 'lastname' && $type == 'Documents') { $query .= ' ORDER BY vtiger_contactdetails.lastname ' . $sorder; } elseif ($order_by == 'crmid' && $type == 'HelpDesk') { $query .= ' ORDER BY vtiger_troubletickets.ticketid ' . $sorder; } else { $tablename = getTableNameForField($type, $order_by); $tablename = $tablename != '' ? $tablename . "." : ''; if ($adb->dbType == "pgsql") { $query .= ' GROUP BY ' . $tablename . $order_by; } $query .= ' ORDER BY ' . $tablename . $order_by . ' ' . $sorder; } } if ($export_data == 'currentpage') { $current_page = ListViewSession::getCurrentPage($type, $viewid); $limit_start_rec = ($current_page - 1) * $list_max_entries_per_page; if ($limit_start_rec < 0) { $limit_start_rec = 0; } $query .= ' LIMIT ' . $limit_start_rec . ',' . $list_max_entries_per_page; } $result = $adb->pquery($query, $params, true, "Error exporting {$type}: " . "<BR>{$query}"); $fields_array = $adb->getFieldsArray($result); $fields_array = array_diff($fields_array, array("user_name")); $__processor = new ExportUtils($type, $fields_array); // Translated the field names based on the language used. $translated_fields_array = array(); for ($i = 0; $i < count($fields_array); $i++) { $translated_fields_array[$i] = getTranslatedString($fields_array[$i], $type); } $header = implode("\",\"", array_values($translated_fields_array)); $header = "\"" . $header; $header .= "\"\r\n"; /** Output header information */ echo $header; $column_list = implode(",", array_values($fields_array)); while ($val = $adb->fetchByAssoc($result, -1, false)) { $new_arr = array(); $val = $__processor->sanitizeValues($val); foreach ($val as $key => $value) { if ($type == 'Documents' && $key == 'description') { $value = strip_tags($value); $value = str_replace(' ', '', $value); array_push($new_arr, $value); } elseif ($key != "user_name") { // Let us provide the module to transform the value before we save it to CSV file $value = $focus->transform_export_value($key, $value); array_push($new_arr, preg_replace("/\"/", "\"\"", $value)); } } $line = implode("\",\"", $new_arr); $line = "\"" . $line; $line .= "\"\r\n"; /** Output each row information */ echo $line; } $log->debug("Exiting export method ..."); return true; }
/** * Function returns standard filter sql * @return <String> */ public function getCVStdFilterSQL() { $customView = new CustomView(); return $customView->getCVStdFilterSQL($this->getId()); }