Skip to content

Commit

Permalink
Merge pull request #23742 from eileenmcnaughton/import_remove
Browse files Browse the repository at this point in the history
Remove more obsolete import code
  • Loading branch information
eileenmcnaughton authored Jun 9, 2022
2 parents dffc11f + ee4e9bf commit 96d9c94
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 223 deletions.
11 changes: 11 additions & 0 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,17 @@ public function getMappingFieldFromMapperInput(array $fieldMapping, int $mapping
];
}

/**
* The initializer code, called before the processing
*
* @return void
*/
public function init() {
// Force re-load of user job.
unset($this->userJob);
$this->setFieldMetadata();
}

/**
* @param array $mappedField
* Field detail as would be saved in field_mapping table
Expand Down
16 changes: 0 additions & 16 deletions CRM/Member/Import/Form/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,6 @@ public function buildQuickForm() {
$this->addContactTypeSelector();
}

/**
* Process the uploaded file.
*
* @return void
*/
public function postProcess() {
$this->storeFormValues([
'onDuplicate',
'contactType',
'dateFormats',
'savedMapping',
]);

$this->submitFileForMapping('CRM_Member_Import_Parser_Membership');
}

/**
* @return \CRM_Member_Import_Parser_Membership
*/
Expand Down
207 changes: 5 additions & 202 deletions CRM/Member/Import/Parser/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser {

protected $_mapperKeys;

/**
* Array of metadata for all available fields.
*
Expand Down Expand Up @@ -56,107 +54,6 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser {
*/
protected $_lineCount;

/**
* Class constructor.
*
* @param $mapperKeys
*/
public function __construct($mapperKeys = []) {
parent::__construct();
$this->_mapperKeys = $mapperKeys;
}

/**
* @param string $fileName
* @param string $separator
* @param $mapper
* @param bool $skipColumnHeader
* @param int $mode
* @param int $contactType
* @param int $onDuplicate
* @param int $statusID
*
* @return mixed
* @throws Exception
*/
public function run(
$fileName,
$separator,
$mapper,
$skipColumnHeader = FALSE,
$mode = self::MODE_PREVIEW,
$contactType = self::CONTACT_INDIVIDUAL,
$onDuplicate = self::DUPLICATE_SKIP,
$statusID = NULL
) {
$this->_contactType = $this->getContactType();
$this->init();

$this->_lineCount = 0;
$this->_invalidRowCount = $this->_validCount = 0;
$this->_totalCount = 0;

$this->_errors = [];
$this->_warnings = [];
if ($statusID) {
$this->progressImport($statusID);
$startTimestamp = $currTimestamp = $prevTimestamp = CRM_Utils_Time::time();
}
$dataSource = $this->getDataSourceObject();
$totalRowCount = $dataSource->getRowCount(['new']);
$dataSource->setStatuses(['new']);
while ($row = $dataSource->getRow()) {
$values = array_values($row);
if ($mode == self::MODE_IMPORT) {
$this->import($values);
if ($statusID && (($this->_lineCount % 50) == 0)) {
$prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
}
}
}
}

/**
* Given a list of the importable field keys that the user has selected
* set the active fields array to this list
*
* @param array $fieldKeys mapped array of values
*
* @return void
*/
public function setActiveFields($fieldKeys) {
$this->_activeFieldCount = count($fieldKeys);
foreach ($fieldKeys as $key) {
if (empty($this->_fields[$key])) {
$this->_activeFields[] = new CRM_Member_Import_Field('', ts('- do not import -'));
}
else {
$this->_activeFields[] = clone($this->_fields[$key]);
}
}
}

/**
* Format the field values for input to the api.
*
* @return array
* (reference ) associative array of name/value pairs
*/
public function getParams() {
$this->getSubmittedValue('mapper');
$params = [];
for ($i = 0; $i < $this->_activeFieldCount; $i++) {
if (isset($this->_activeFields[$i]->_value)
&& !isset($params[$this->_activeFields[$i]->_name])
&& !isset($this->_activeFields[$i]->_related)
) {

$params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value;
}
}
return $params;
}

/**
* @param string $name
* @param $title
Expand All @@ -183,86 +80,6 @@ public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPa
}
}

/**
* Store parser values.
*
* @param CRM_Core_Session $store
*
* @param int $mode
*
* @return void
*/
public function set($store, $mode = self::MODE_SUMMARY) {
$store->set('lineCount', $this->_lineCount);
$store->set('validRowCount', $this->_validCount);
$store->set('invalidRowCount', $this->_invalidRowCount);

if ($this->_invalidRowCount) {
$store->set('errorsFileName', $this->_errorFileName);
}

if ($mode == self::MODE_IMPORT) {
$store->set('duplicateRowCount', $this->_duplicateCount);
if ($this->_duplicateCount) {
$store->set('duplicatesFileName', $this->_duplicateFileName);
}
}
}

/**
* Export data to a CSV file.
*
* @param string $fileName
* @param array $header
* @param array $data
*
* @return void
*/
public static function exportCSV($fileName, $header, $data) {
$output = [];
$fd = fopen($fileName, 'w');

foreach ($header as $key => $value) {
$header[$key] = "\"$value\"";
}
$config = CRM_Core_Config::singleton();
$output[] = implode($config->fieldSeparator, $header);

foreach ($data as $datum) {
foreach ($datum as $key => $value) {
if (is_array($value)) {
foreach ($value[0] as $k1 => $v1) {
if ($k1 == 'location_type_id') {
continue;
}
$datum[$k1] = $v1;
}
}
else {
$datum[$key] = "\"$value\"";
}
}
$output[] = implode($config->fieldSeparator, $datum);
}
fwrite($fd, implode("\n", $output));
fclose($fd);
}

/**
* The initializer code, called before the processing
*
* @return void
*/
public function init() {
// Force re-load of user job.
unset($this->userJob);
$this->setFieldMetadata();

$this->_newMemberships = [];

$this->setActiveFields($this->_mapperKeys);
}

/**
* Validate the values.
*
Expand Down Expand Up @@ -300,7 +117,6 @@ public function validateValues($values): void {
* the result of this processing - which is ignored
*/
public function import($values) {
$onDuplicate = $this->getSubmittedValue('onDuplicate');
$rowNumber = (int) ($values[array_key_last($values)]);
try {
$params = $this->getMappedRow($values);
Expand Down Expand Up @@ -328,7 +144,7 @@ public function import($values) {
//@todo find a way to test removing this formatting
$formatError = $this->membership_format_params($formatValues, $formatted, TRUE);

if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) {
if ($this->isUpdateExisting()) {
$formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted,
NULL,
'Membership'
Expand Down Expand Up @@ -392,7 +208,7 @@ public function import($values) {
$startDate,
$endDate
);
self::formattedDates($calcDates, $formatted);
$this->formattedDates($calcDates, $formatted);

//fix for CRM-3570, exclude the statuses those having is_admin = 1
//now user can import is_admin if is override is true.
Expand Down Expand Up @@ -432,7 +248,7 @@ public function import($values) {
else {
// Using new Dedupe rule.
$ruleParams = [
'contact_type' => $this->_contactType,
'contact_type' => $this->getContactType(),
'used' => 'Unsupervised',
];
$fieldsArray = CRM_Dedupe_BAO_DedupeRule::dedupeRuleFields($ruleParams);
Expand Down Expand Up @@ -477,7 +293,7 @@ public function import($values) {
$startDate,
$endDate
);
self::formattedDates($calcDates, $formatted);
$this->formattedDates($calcDates, $formatted);
//end of date calculation part

//fix for CRM-3570, exclude the statuses those having is_admin = 1
Expand Down Expand Up @@ -508,9 +324,7 @@ public function import($values) {
}

$newMembership = civicrm_api3('membership', 'create', $formatted);

$this->_newMemberships[] = $newMembership['id'];
$this->setImportStatus($rowNumber, 'IMPORTED', '');
$this->setImportStatus($rowNumber, 'IMPORTED', '', $newMembership['id']);
return CRM_Import_Parser::VALID;
}
}
Expand All @@ -524,15 +338,6 @@ public function import($values) {
}
}

/**
* Get the array of successfully imported membership id's
*
* @return array
*/
public function &getImportedMemberships() {
return $this->_newMemberships;
}

/**
* to calculate join, start and end dates
*
Expand Down Expand Up @@ -584,8 +389,6 @@ public function membership_format_params($params, &$values, $create = FALSE) {
$fields = CRM_Member_DAO_Membership::fields();
_civicrm_api3_store_values($fields, $params, $values);

$customFields = CRM_Core_BAO_CustomField::getFields('Membership');

foreach ($params as $key => $value) {

switch ($key) {
Expand Down
5 changes: 0 additions & 5 deletions tests/phpunit/CRM/Activity/Import/Parser/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ class CRM_Activity_Import_Parser_ActivityTest extends CiviUnitTestCase {
use CRMTraits_Custom_CustomDataTrait;
use CRMTraits_Import_ParserTrait;

/**
* @var int|null
*/
private $userJobID;

/**
* Prepare for tests.
*/
Expand Down

0 comments on commit 96d9c94

Please sign in to comment.