/** * Function to get the list the export fields * * @param int $exportContact type of export * * @access public */ function exportContacts($selectAll, $ids, $formValues, $order = null, $fields = null) { $headerRows = array(); $returnProperties = array(); $primary = false; if ($fields) { $location = array(); $locationType = array("Work" => array(), "Home" => array(), "Main" => array(), "Other" => array()); $returnFields = $fields; foreach ($returnFields as $key => $field) { $flag = true; $phone_type = ""; $phoneFlag = false; if ($field[3] && $field[1] == 'phone') { if ($field[3] == 'Phone') { $phone_type = $field[1] . "-" . "Phone"; } else { if ($field[3] == 'Mobile') { $phone_type = $field[1] . "-" . "Mobile"; } else { if ($field[3] == 'Fax') { $phone_type = $field[1] . "-" . "Fax"; } else { if ($field[3] == 'Pager') { $phone_type = $field[1] . "-" . "Pager"; } } } } $phoneFlag = true; } if ($field[2]) { if ($field[2] == 1) { if ($phoneFlag) { $locationType["Home"][$phone_type] = 1; } else { $locationType["Home"][$field[1]] = 1; } } else { if ($field[2] == 2) { if ($phoneFlag) { $locationType["Work"][$phone_type] = 1; } else { $locationType["Work"][$field[1]] = 1; } } else { if ($field[2] == 3) { if ($phoneFlag) { $locationType["Main"][$phone_type] = 1; } else { $locationType["Main"][$field[1]] = 1; } } else { if ($field[2] == 4) { if ($phoneFlag) { $locationType["Other"][$phone_type] = 1; } else { $locationType["Other"][$field[1]] = 1; } } } } } $flag = false; } if ($flag) { $returnProperties[$field[1]] = 1; } } $returnProperties['location'] = $locationType; } else { $primary = true; $fields = CRM_Contact_BAO_Contact::exportableFields('All', true, true); foreach ($fields as $key => $var) { if ($key) { $returnProperties[$key] = 1; } } } if ($primary) { $returnProperties['location_type'] = 1; $returnProperties['im_provider'] = 1; $returnProperties['phone_type'] = 1; } $session =& new CRM_Core_Session(); if ($selectAll) { if ($primary) { $query =& new CRM_Contact_BAO_Query($formValues, $returnProperties, $fields); } else { $query =& new CRM_Contact_BAO_Query($formValues, $returnProperties); } } else { $params = array(); foreach ($ids as $id) { $params[CRM_CORE_FORM_CB_PREFIX . $id] = 1; } if ($primary) { $query =& new CRM_Contact_BAO_Query($params, $returnProperties, $fields, true); } else { $query =& new CRM_Contact_BAO_Query($params, $returnProperties, null, true); } } list($select, $from, $where) = $query->query(); $queryString = "{$select} {$from} {$where}"; if ($order) { list($field, $dir) = explode(' ', $order, 2); $field = trim($field); if (CRM_Utils_Array::value($field, $returnProperties)) { $queryString .= " ORDER BY {$order}"; } } if (CRM_Utils_Array::value('tags', $returnProperties) || CRM_Utils_Array::value('groups', $returnProperties)) { $queryString .= " GROUP BY civicrm_contact.id"; } $dao =& CRM_Core_DAO::executeQuery($queryString); $header = false; $contactDetails = array(); while ($dao->fetch()) { $row = array(); $validRow = false; foreach ($dao as $key => $varValue) { $flag = false; foreach ($returnProperties as $propKey => $props) { if (is_array($props)) { foreach ($props as $propKey1 => $prop) { foreach ($prop as $propkey2 => $prop1) { if ($propKey1 . "-" . $propkey2 == $key) { $flag = true; } } } } } if (array_key_exists($key, $returnProperties)) { $flag = true; } if ($flag) { if (isset($varValue) && $varValue != '') { if ($cfID = CRM_Core_BAO_CustomField::getKeyID($key)) { $row[$key] = CRM_Core_BAO_CustomField::getDisplayValue($varValue, $cfID, $query->_options); } else { $row[$key] = $varValue; } $validRow = true; } else { $row[$key] = ''; } if (!$header) { if (isset($query->_fields[$key]['title'])) { $headerRows[] = $query->_fields[$key]['title']; } else { if ($key == 'phone_type') { $headerRows[] = 'Phone Type'; } else { $keyArray = explode('-', $key); $hdr = $keyArray[0] . "-" . $query->_fields[$keyArray[1]]['title']; if (CRM_Utils_Array::value(2, $keyArray)) { $hdr .= " " . $keyArray[2]; } $headerRows[] = $hdr; } } } } } if ($validRow) { $contactDetails[$dao->contact_id] = $row; } $header = true; } require_once 'CRM/Core/Report/Excel.php'; CRM_Core_Report_Excel::writeCSVFile(CRM_Contact_BAO_Export::getExportFileName(), $headerRows, $contactDetails); exit; }
/** * Process the uploaded file * * @return void * @access public */ function postProcess() { $params = $this->controller->exportValues($this->_name); if ($this->controller->exportValue($this->_name, 'addMore')) { $this->set('columnCount', $this->_columnCount); return; } //reload the mapfield if load mapping is pressed //if ( CRM_Utils_Array::value( 'savedMapping', $params ) ) { if ($this->controller->exportValue($this->_name, 'loadMapping')) { CRM_Utils_Array::value('savedMapping', $params); $this->set('savedMapping', $params['savedMapping']); $this->controller->resetPage($this->_name); return; } $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); $checkEmpty = 0; foreach ($mapperKeys as $value) { if ($value[0]) { $checkEmpty++; } } if (!$checkEmpty) { require_once 'CRM/Utils/System.php'; CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/search/basic', '_qf_Map_display=true')); } //Updating Mapping Records if (CRM_Utils_Array::value('updateMapping', $params)) { $mappingFields =& new CRM_Core_DAO_MappingField(); $mappingFields->mapping_id = $params['mappingId']; $mappingFields->find(); $mappingFieldsId = array(); while ($mappingFields->fetch()) { if ($mappingFields->id) { $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id; } } for ($i = 0; $i < $this->_columnCount; $i++) { if (!empty($mapperKeys[$i][0])) { $updateMappingFields =& new CRM_Core_DAO_MappingField(); $updateMappingFields->id = $mappingFieldsId[$i]; $updateMappingFields->mapping_id = $params['mappingId']; $updateMappingFields->name = $this->_mapperFields[$mapperKeys[$i][0]][$mapperKeys[$i][1]]; $updateMappingFields->column_number = $i; $locationId = $mapperKeys[$i][2]; $updateMappingFields->location_type_id = isset($locationId) ? $locationId : null; $relation = $mapperKeys[$i][1]; list($id, $first, $second) = explode('_', $relation); if ($first == 'a' && $second == 'b' || $first == 'b' && $second == 'a') { $updateMappingFields->relationship_type_id = $id; } else { $updateMappingFields->relationship_type_id = null; } $phoneType = $mapperKeys[$i][3]; $updateMappingFields->phone_type = isset($phoneType) ? $phoneType : null; $updateMappingFields->save(); } } } //Saving Mapping Details and Records if (CRM_Utils_Array::value('saveMapping', $params)) { $saveMapping =& new CRM_Core_DAO_Mapping(); $saveMapping->domain_id = CRM_Core_Config::domainID(); $saveMapping->name = $params['saveMappingName']; $saveMapping->description = $params['saveMappingDesc']; $saveMapping->mapping_type = 'Export'; $saveMapping->save(); for ($i = 0; $i < $this->_columnCount; $i++) { if (!empty($mapperKeys[$i][0])) { $saveMappingFields =& new CRM_Core_DAO_MappingField(); $saveMappingFields->mapping_id = $saveMapping->id; $saveMappingFields->name = $this->_mapperFields[$mapperKeys[$i][0]][$mapperKeys[$i][1]]; $saveMappingFields->contact_type = $mapperKeys[$i][0]; $saveMappingFields->column_number = $i; $locationId = $mapperKeys[$i][2]; $saveMappingFields->location_type_id = isset($locationId) ? $locationId : null; $saveMappingFields->phone_type = $mapperKeys[$i][3]; $relation = $mapperKeys[$i][1]; list($id, $first, $second) = explode('_', $relation); if ($first == 'a' && $second == 'b' || $first == 'b' && $second == 'a') { $saveMappingFields->relationship_type_id = $id; } else { $saveMappingFields->relationship_type_id = null; } $saveMappingFields->save(); } } } //get the csv file require_once 'CRM/Contact/BAO/Export.php'; CRM_Contact_BAO_Export::exportContacts($this->get('selectAll'), $this->get('contactIds'), $this->get('formValues'), $this->get(CRM_UTILS_SORT_SORT_ORDER), $mapperKeys); }