First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,32 @@
<?php
/**
* Class CRM_Custom_Import_Controller
*/
class CRM_Custom_Import_Controller extends CRM_Core_Controller {
/**
* Class constructor.
*
* @param string $title
* @param bool|int $action
* @param bool $modal
*/
public function __construct($title = NULL, $action = CRM_Core_Action::NONE, $modal = TRUE) {
parent::__construct($title, $modal);
// lets get around the time limit issue if possible, CRM-2113
if (!ini_get('safe_mode')) {
set_time_limit(0);
}
$this->_stateMachine = new CRM_Import_StateMachine($this, $action);
// create and instantiate the pages
$this->addPages($this->_stateMachine, $action);
// add all the actions
$config = CRM_Core_Config::singleton();
$this->addActions($config->uploadDir, array('uploadFile'));
}
}

View file

@ -0,0 +1,32 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* Class CRM_Custom_Import_Field
*/
class CRM_Custom_Import_Field extends CRM_Contact_Import_Field {
}

View file

@ -0,0 +1,94 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
/**
* This class gets the name of the file to upload
*/
class CRM_Custom_Import_Form_DataSource extends CRM_Import_Form_DataSource {
const PATH = 'civicrm/import/custom';
const IMPORT_ENTITY = 'Multi value custom data';
/**
* @return array
*/
public function setDefaultValues() {
$config = CRM_Core_Config::singleton();
$defaults = array(
'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
'fieldSeparator' => $config->fieldSeparator,
'multipleCustomData' => $this->_id,
);
if ($loadeMapping = $this->get('loadedMapping')) {
$this->assign('loadedMapping', $loadeMapping);
$defaults['savedMapping'] = $loadeMapping;
}
return $defaults;
}
/**
* Build the form object.
*
* @return void
*/
public function buildQuickForm() {
parent::buildQuickForm();
$multipleCustomData = CRM_Core_BAO_CustomGroup::getMultipleFieldGroup();
$this->add('select', 'multipleCustomData', ts('Multi-value Custom Data'), array('' => ts('- select -')) + $multipleCustomData, TRUE);
$this->addContactTypeSelector();
}
/**
* Process the uploaded file.
*
* @return void
*/
public function postProcess() {
$this->storeFormValues(array(
'contactType',
'dateFormats',
'savedMapping',
'multipleCustomData',
));
$this->submitFileForMapping('CRM_Custom_Import_Parser_Api', 'multipleCustomData');
}
}

View file

@ -0,0 +1,223 @@
<?php
/**
* Class CRM_Custom_Import_Form_MapField
*/
class CRM_Custom_Import_Form_MapField extends CRM_Contact_Import_Form_MapField {
protected $_parser = 'CRM_Custom_Import_Parser_Api';
protected $_mappingType = 'Import Multi value custom data';
/**
* Entity being imported to.
* @var string
*/
protected $_entity;
/**
* Set variables up before form is built.
*
* @return void
*/
public function preProcess() {
$this->_mapperFields = $this->get('fields');
asort($this->_mapperFields);
$this->_columnCount = $this->get('columnCount');
$this->assign('columnCount', $this->_columnCount);
$this->_dataValues = $this->get('dataValues');
$highlightedFields = array('contact_id', 'external_identifier');
//Separate column names from actual values.
$columnNames = $this->_dataValues[0];
//actual values need to be in 2d array ($array[$i][$j]) format to be parsed by the template.
$dataValues[] = $this->_dataValues[1];
$this->assign('dataValues', $dataValues);
$this->_entity = $this->_multipleCustomData = $this->get('multipleCustomData');
$skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
$this->_onDuplicate = $this->get('onDuplicate');
if ($skipColumnHeader) {
//showColNames needs to be true to show "Column Names" column
$this->assign('showColNames', $skipColumnHeader);
$this->assign('columnNames', $columnNames);
/* if we had a column header to skip, stash it for later */
$this->_columnHeaders = $this->_dataValues[0];
}
$this->assign('rowDisplayCount', 2);
$this->assign('highlightedFields', $highlightedFields);
}
/**
* Build the form object.
*
* @return void
*/
public function buildQuickForm() {
parent::buildQuickForm();
$this->addFormRule(array('CRM_Custom_Import_Form_MapField', 'formRule'));
}
/**
* Global validation rules for the form.
*
* @param array $fields
* Posted values of the form.
*
* @return array
* list of errors to be posted back to the form
*/
public static function formRule($fields) {
$errors = array();
$fieldMessage = NULL;
if (!array_key_exists('savedMapping', $fields)) {
$importKeys = array();
foreach ($fields['mapper'] as $mapperPart) {
$importKeys[] = $mapperPart[0];
}
// check either contact id or external identifier
if (!in_array('contact_id', $importKeys) && !in_array('external_identifier', $importKeys)) {
if (!isset($errors['_qf_default'])) {
$errors['_qf_default'] = '';
}
$errors['_qf_default'] .= ts('Missing required field: %1', array(1 => ts('Contact ID or External Identifier')));
}
}
if (!empty($fields['saveMapping'])) {
$nameField = CRM_Utils_Array::value('saveMappingName', $fields);
if (empty($nameField)) {
$errors['saveMappingName'] = ts('Name is required to save Import Mapping');
}
else {
$mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', 'Import Multi value custom data', 'name');
if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) {
$errors['saveMappingName'] = ts('Duplicate ' . $self->_mappingType . 'Mapping Name');
}
}
}
//display Error if loaded mapping is not selected
if (array_key_exists('loadMapping', $fields)) {
$getMapName = CRM_Utils_Array::value('savedMapping', $fields);
if (empty($getMapName)) {
$errors['savedMapping'] = ts('Select saved mapping');
}
}
if (!empty($errors)) {
if (!empty($errors['saveMappingName'])) {
$_flag = 1;
$assignError = new CRM_Core_Page();
$assignError->assign('mappingDetailsError', $_flag);
}
return $errors;
}
return TRUE;
}
/**
* Process the mapped fields and map it into the uploaded file.
* preview the file and extract some summary statistics
*
* @return void
*/
public function postProcess() {
$params = $this->controller->exportValues('MapField');
$this->set('multipleCustomData', $this->_multipleCustomData);
//reload the mapfield if load mapping is pressed
if (!empty($params['savedMapping'])) {
$this->set('savedMapping', $params['savedMapping']);
$this->controller->resetPage($this->_name);
return;
}
$fileName = $this->controller->exportValue('DataSource', 'uploadFile');
$separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
$skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
$this->_entity = $this->controller->exportValue('DataSource', 'entity');
$mapperKeys = array();
$mapper = array();
$mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
$mapperKeysMain = array();
for ($i = 0; $i < $this->_columnCount; $i++) {
$mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]];
$mapperKeysMain[$i] = $mapperKeys[$i][0];
}
$this->set('mapper', $mapper);
// store mapping Id to display it in the preview page
$this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params));
//Updating Mapping Records
if (!empty($params['updateMapping'])) {
$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++) {
$updateMappingFields = new CRM_Core_DAO_MappingField();
$updateMappingFields->id = $mappingFieldsId[$i];
$updateMappingFields->mapping_id = $params['mappingId'];
$updateMappingFields->column_number = $i;
$explodedValues = explode('_', $mapperKeys[$i][0]);
$id = CRM_Utils_Array::value(0, $explodedValues);
$first = CRM_Utils_Array::value(1, $explodedValues);
$second = CRM_Utils_Array::value(2, $explodedValues);
$updateMappingFields->name = $mapper[$i];
$updateMappingFields->save();
}
}
//Saving Mapping Details and Records
if (!empty($params['saveMapping'])) {
$mappingParams = array(
'name' => $params['saveMappingName'],
'description' => $params['saveMappingDesc'],
'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type',
$this->_mappingType,
'name'
),
);
$saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
for ($i = 0; $i < $this->_columnCount; $i++) {
$saveMappingFields = new CRM_Core_DAO_MappingField();
$saveMappingFields->mapping_id = $saveMapping->id;
$saveMappingFields->column_number = $i;
$explodedValues = explode('_', $mapperKeys[$i][0]);
$id = CRM_Utils_Array::value(0, $explodedValues);
$first = CRM_Utils_Array::value(1, $explodedValues);
$second = CRM_Utils_Array::value(2, $explodedValues);
$saveMappingFields->name = $mapper[$i];
$saveMappingFields->save();
}
$this->set('savedMapping', $saveMappingFields->mapping_id);
}
$this->set('_entity', $this->_entity);
$parser = new $this->_parser($mapperKeysMain);
$parser->setEntity($this->_multipleCustomData);
$parser->run($fileName, $separator, $mapper, $skipColumnHeader,
CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType')
);
// add all the necessary variables to the form
$parser->set($this);
}
}

View file

@ -0,0 +1,150 @@
<?php
/**
* Class CRM_Custom_Import_Form_Preview
*/
class CRM_Custom_Import_Form_Preview extends CRM_Import_Form_Preview {
public $_parser = 'CRM_Custom_Import_Parser_Api';
protected $_importParserUrl = '&parser=CRM_Custom_Import_Parser';
/**
* Set variables up before form is built.
*
* @return void
*/
public function preProcess() {
$skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
//get the data from the session
$dataValues = $this->get('dataValues');
$mapper = $this->get('mapper');
$invalidRowCount = $this->get('invalidRowCount');
$conflictRowCount = $this->get('conflictRowCount');
$mismatchCount = $this->get('unMatchCount');
$entity = $this->get('_entity');
//get the mapping name displayed if the mappingId is set
$mappingId = $this->get('loadMappingId');
if ($mappingId) {
$mapDAO = new CRM_Core_DAO_Mapping();
$mapDAO->id = $mappingId;
$mapDAO->find(TRUE);
$this->assign('loadedMapping', $mappingId);
$this->assign('savedName', $mapDAO->name);
}
if ($skipColumnHeader) {
$this->assign('skipColumnHeader', $skipColumnHeader);
$this->assign('rowDisplayCount', 3);
}
else {
$this->assign('rowDisplayCount', 2);
}
if ($invalidRowCount) {
$urlParams = 'type=' . CRM_Import_Parser::ERROR . $this->_importParserUrl;
$this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
}
if ($conflictRowCount) {
$urlParams = 'type=' . CRM_Import_Parser::CONFLICT . $this->_importParserUrl;
$this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
}
if ($mismatchCount) {
$urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . $this->_importParserUrl;
$this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
}
$properties = array(
'mapper',
'dataValues',
'columnCount',
'totalRowCount',
'validRowCount',
'invalidRowCount',
'conflictRowCount',
'downloadErrorRecordsUrl',
'downloadConflictRecordsUrl',
'downloadMismatchRecordsUrl',
);
foreach ($properties as $property) {
$this->assign($property, $this->get($property));
}
}
/**
* Process the mapped fields and map it into the uploaded file.
* preview the file and extract some summary statistics
*
* @return void
*/
public function postProcess() {
$fileName = $this->controller->exportValue('DataSource', 'uploadFile');
$separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
$skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
$invalidRowCount = $this->get('invalidRowCount');
$conflictRowCount = $this->get('conflictRowCount');
$onDuplicate = $this->get('onDuplicate');
$entity = $this->get('_entity');
$mapper = $this->controller->exportValue('MapField', 'mapper');
$mapperKeys = array();
foreach ($mapper as $key => $value) {
$mapperKeys[$key] = $mapper[$key][0];
}
$parser = new $this->_parser($mapperKeys);
$parser->setEntity($entity);
$mapFields = $this->get('fields');
foreach ($mapper as $key => $value) {
$header = array();
if (isset($mapFields[$mapper[$key][0]])) {
$header[] = $mapFields[$mapper[$key][0]];
}
$mapperFields[] = implode(' - ', $header);
}
$parser->run($fileName, $separator,
$mapperFields,
$skipColumnHeader,
CRM_Import_Parser::MODE_IMPORT,
$this->get('contactType'),
$onDuplicate
);
// add all the necessary variables to the form
$parser->set($this, CRM_Import_Parser::MODE_IMPORT);
// check if there is any error occurred
$errorStack = CRM_Core_Error::singleton();
$errors = $errorStack->getErrors();
$errorMessage = array();
if (is_array($errors)) {
foreach ($errors as $key => $value) {
$errorMessage[] = $value['message'];
}
$errorFile = $fileName['name'] . '.error.log';
if ($fd = fopen($errorFile, 'w')) {
fwrite($fd, implode('\n', $errorMessage));
}
fclose($fd);
$this->set('errorFile', $errorFile);
$urlParams = 'type=' . CRM_Import_Parser::ERROR . $this->_importParserUrl;
$this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
$urlParams = 'type=' . CRM_Import_Parser::CONFLICT . $this->_importParserUrl;
$this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
$urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . $this->_importParserUrl;
$this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
}
}
}

View file

@ -0,0 +1,18 @@
<?php
/**
* Class CRM_Custom_Import_Form_Summary
*/
class CRM_Custom_Import_Form_Summary extends CRM_Contact_Import_Form_Summary {
/**
* Set variables up before form is built.
*
* @return void
*/
public function preProcess() {
parent::preProcess();
$session = CRM_Core_Session::singleton();
$session->pushUserContext(CRM_Utils_System::url('civicrm/import/custom', 'reset=1'));
}
}

View file

@ -0,0 +1,385 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
abstract class CRM_Custom_Import_Parser extends CRM_Contact_Import_Parser {
protected $_fileName;
/**#@+
* @var integer
*/
/**
* Imported file size
*/
protected $_fileSize;
/**
* Separator being used
*/
protected $_separator;
/**
* Total number of lines in file
*/
protected $_lineCount;
/**
* Whether the file has a column header or not
*
* @var boolean
*/
protected $_haveColumnHeader;
/**
* @param string $fileName
* @param string $separator
* @param int $mapper
* @param bool $skipColumnHeader
* @param int|string $mode
* @param int|string $contactType
* @param int $onDuplicate
*
* @return mixed
* @throws Exception
*/
public function run(
$fileName,
$separator = ',',
&$mapper,
$skipColumnHeader = FALSE,
$mode = self::MODE_PREVIEW,
$contactType = self::CONTACT_INDIVIDUAL,
$onDuplicate = self::DUPLICATE_SKIP
) {
if (!is_array($fileName)) {
CRM_Core_Error::fatal();
}
$fileName = $fileName['name'];
switch ($contactType) {
case CRM_Import_Parser::CONTACT_INDIVIDUAL:
$this->_contactType = 'Individual';
break;
case CRM_Import_Parser::CONTACT_HOUSEHOLD:
$this->_contactType = 'Household';
break;
case CRM_Import_Parser::CONTACT_ORGANIZATION:
$this->_contactType = 'Organization';
}
$this->init();
$this->_haveColumnHeader = $skipColumnHeader;
$this->_separator = $separator;
$fd = fopen($fileName, "r");
if (!$fd) {
return FALSE;
}
$this->_lineCount = $this->_warningCount = 0;
$this->_invalidRowCount = $this->_validCount = 0;
$this->_totalCount = $this->_conflictCount = 0;
$this->_errors = array();
$this->_warnings = array();
$this->_conflicts = array();
$this->_fileSize = number_format(filesize($fileName) / 1024.0, 2);
if ($mode == self::MODE_MAPFIELD) {
$this->_rows = array();
}
else {
$this->_activeFieldCount = count($this->_activeFields);
}
while (!feof($fd)) {
$this->_lineCount++;
$values = fgetcsv($fd, 8192, $separator);
if (!$values) {
continue;
}
self::encloseScrub($values);
// skip column header if we're not in mapfield mode
if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) {
$skipColumnHeader = FALSE;
continue;
}
/* trim whitespace around the values */
$empty = TRUE;
foreach ($values as $k => $v) {
$values[$k] = trim($v, " \t\r\n");
}
if (CRM_Utils_System::isNull($values)) {
continue;
}
$this->_totalCount++;
if ($mode == self::MODE_MAPFIELD) {
$returnCode = $this->mapField($values);
}
elseif ($mode == self::MODE_PREVIEW) {
$returnCode = $this->preview($values);
}
elseif ($mode == self::MODE_SUMMARY) {
$returnCode = $this->summary($values);
}
elseif ($mode == self::MODE_IMPORT) {
$returnCode = $this->import($onDuplicate, $values);
}
else {
$returnCode = self::ERROR;
}
// note that a line could be valid but still produce a warning
if ($returnCode & self::VALID) {
$this->_validCount++;
if ($mode == self::MODE_MAPFIELD) {
$this->_rows[] = $values;
$this->_activeFieldCount = max($this->_activeFieldCount, count($values));
}
}
if ($returnCode & self::WARNING) {
$this->_warningCount++;
if ($this->_warningCount < $this->_maxWarningCount) {
$this->_warningCount[] = $line;
}
}
if ($returnCode & self::ERROR) {
$this->_invalidRowCount++;
if ($this->_invalidRowCount < $this->_maxErrorCount) {
$recordNumber = $this->_lineCount;
if ($this->_haveColumnHeader) {
$recordNumber--;
}
array_unshift($values, $recordNumber);
$this->_errors[] = $values;
}
}
if ($returnCode & self::CONFLICT) {
$this->_conflictCount++;
$recordNumber = $this->_lineCount;
if ($this->_haveColumnHeader) {
$recordNumber--;
}
array_unshift($values, $recordNumber);
$this->_conflicts[] = $values;
}
if ($returnCode & self::DUPLICATE) {
if ($returnCode & self::MULTIPLE_DUPE) {
/* TODO: multi-dupes should be counted apart from singles
* on non-skip action */
}
$this->_duplicateCount++;
$recordNumber = $this->_lineCount;
if ($this->_haveColumnHeader) {
$recordNumber--;
}
array_unshift($values, $recordNumber);
$this->_duplicates[] = $values;
if ($onDuplicate != self::DUPLICATE_SKIP) {
$this->_validCount++;
}
}
// we give the derived class a way of aborting the process
// note that the return code could be multiple code or'ed together
if ($returnCode & self::STOP) {
break;
}
// if we are done processing the maxNumber of lines, break
if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) {
break;
}
}
fclose($fd);
if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) {
$customHeaders = $mapper;
$customfields = CRM_Core_BAO_CustomField::getFields('Activity');
foreach ($customHeaders as $key => $value) {
if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) {
$customHeaders[$key] = $customfields[$id][0];
}
}
if ($this->_invalidRowCount) {
// removed view url for invlaid contacts
$headers = array_merge(array(
ts('Line Number'),
ts('Reason'),
),
$customHeaders
);
$this->_errorFileName = self::errorFileName(self::ERROR);
self::exportCSV($this->_errorFileName, $headers, $this->_errors);
}
if ($this->_conflictCount) {
$headers = array_merge(array(
ts('Line Number'),
ts('Reason'),
),
$customHeaders
);
$this->_conflictFileName = self::errorFileName(self::CONFLICT);
self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts);
}
if ($this->_duplicateCount) {
$headers = array_merge(array(
ts('Line Number'),
ts('View Activity History URL'),
),
$customHeaders
);
$this->_duplicateFileName = self::errorFileName(self::DUPLICATE);
self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates);
}
}
return $this->fini();
}
/**
* 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_Custom_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 &getActiveFieldParams() {
$params = array();
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;
}
/**
* Store parser values.
*
* @param CRM_Core_Session $store
*
* @param int $mode
*
* @return void
*/
public function set($store, $mode = self::MODE_SUMMARY) {
$store->set('fileSize', $this->_fileSize);
$store->set('lineCount', $this->_lineCount);
$store->set('seperator', $this->_separator);
$store->set('fields', $this->getSelectValues());
$store->set('fieldTypes', $this->getSelectTypes());
$store->set('headerPatterns', $this->getHeaderPatterns());
$store->set('dataPatterns', $this->getDataPatterns());
$store->set('columnCount', $this->_activeFieldCount);
$store->set('_entity', $this->_entity);
$store->set('totalRowCount', $this->_totalCount);
$store->set('validRowCount', $this->_validCount);
$store->set('invalidRowCount', $this->_invalidRowCount);
$store->set('conflictRowCount', $this->_conflictCount);
switch ($this->_contactType) {
case 'Individual':
$store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
break;
case 'Household':
$store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
break;
case 'Organization':
$store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
}
if ($this->_invalidRowCount) {
$store->set('errorsFileName', $this->_errorFileName);
}
if ($this->_conflictCount) {
$store->set('conflictsFileName', $this->_conflictFileName);
}
if (isset($this->_rows) && !empty($this->_rows)) {
$store->set('dataValues', $this->_rows);
}
if ($mode == self::MODE_IMPORT) {
$store->set('duplicateRowCount', $this->_duplicateCount);
if ($this->_duplicateCount) {
$store->set('duplicatesFileName', $this->_duplicateFileName);
}
}
}
}

View file

@ -0,0 +1,272 @@
<?php
/**
* Class CRM_Custom_Import_Parser_Api
*/
class CRM_Custom_Import_Parser_Api extends CRM_Custom_Import_Parser {
protected $_entity = '';
protected $_fields = array();
protected $_requiredFields = array();
protected $_dateFields = array();
protected $_multipleCustomData = '';
/**
* Params for the current entity being prepared for the api.
* @var array
*/
protected $_params = array();
/**
* Class constructor.
*
* @param array $mapperKeys
* @param null $mapperLocType
* @param null $mapperPhoneType
*/
public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) {
parent::__construct();
$this->_mapperKeys = &$mapperKeys;
}
public function setFields() {
$customGroupID = $this->_multipleCustomData;
$importableFields = $this->getGroupFieldsForImport($customGroupID, $this);
$this->_fields = array_merge(array(
'do_not_import' => array('title' => ts('- do not import -')),
'contact_id' => array('title' => ts('Contact ID')),
'external_identifier' => array('title' => ts('External Identifier')),
), $importableFields);
}
/**
* The initializer code, called before the processing
*
* @return void
*/
public function init() {
$this->setFields();
$fields = $this->_fields;
$hasLocationType = FALSE;
foreach ($fields as $name => $field) {
$field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT);
$field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//');
$field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//');
$this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern'], $hasLocationType);
}
$this->setActiveFields($this->_mapperKeys);
}
/**
* Handle the values in mapField mode.
*
* @param array $values
* The array of values belonging to this line.
*
* @return bool
*/
public function mapField(&$values) {
return CRM_Import_Parser::VALID;
}
/**
* Handle the values in preview mode.
*
* @param array $values
* The array of values belonging to this line.
*
* @return bool
* the result of this processing
*/
public function preview(&$values) {
return $this->summary($values);
}
/**
* @param array $values
* The array of values belonging to this line.
*
* @return bool
* the result of this processing
* It is called from both the preview & the import actions
*
* @see CRM_Custom_Import_Parser_BaseClass::summary()
*/
public function summary(&$values) {
$erroneousField = NULL;
$response = $this->setActiveFieldValues($values, $erroneousField);
$errorRequired = FALSE;
$missingField = '';
$this->_params = &$this->getActiveFieldParams();
$formatted = $this->_params;
$this->_updateWithId = FALSE;
$this->_parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options'), FALSE);
$this->_params = $this->getActiveFieldParams();
foreach ($this->_requiredFields as $requiredField) {
if (empty($this->_params[$requiredField])) {
$errorRequired = TRUE;
$missingField .= ' ' . $requiredField;
CRM_Contact_Import_Parser_Contact::addToErrorMsg($this->_entity, $requiredField);
}
}
if ($errorRequired) {
array_unshift($values, ts('Missing required field(s) :') . $missingField);
return CRM_Import_Parser::ERROR;
}
$errorMessage = NULL;
$contactType = $this->_contactType ? $this->_contactType : 'Organization';
CRM_Contact_Import_Parser_Contact::isErrorInCustomData($this->_params + array('contact_type' => $contactType), $errorMessage, $this->_contactSubType, NULL);
// pseudoconstants
if ($errorMessage) {
$tempMsg = "Invalid value for field(s) : $errorMessage";
array_unshift($values, $tempMsg);
$errorMessage = NULL;
return CRM_Import_Parser::ERROR;
}
return CRM_Import_Parser::VALID;
}
/**
* Handle the values in import mode.
*
* @param int $onDuplicate
* The code for what action to take on duplicates.
* @param array $values
* The array of values belonging to this line.
*
* @return bool
* the result of this processing
*/
public function import($onDuplicate, &$values) {
$response = $this->summary($values);
if ($response != CRM_Import_Parser::VALID) {
$importRecordParams = array(
$statusFieldName => 'INVALID',
"${statusFieldName}Msg" => "Invalid (Error Code: $response)",
);
return $response;
}
$this->_updateWithId = FALSE;
$this->_parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options'), FALSE);
$params = $this->getActiveFieldParams();
$contactType = $this->_contactType ? $this->_contactType : 'Organization';
$formatted = array(
'contact_type' => $contactType,
);
$session = CRM_Core_Session::singleton();
$dateType = $session->get('dateTypes');
if (isset($this->_params['external_identifier']) && !isset($this->_params['contact_id'])) {
$checkCid = new CRM_Contact_DAO_Contact();
$checkCid->external_identifier = $this->_params['external_identifier'];
$checkCid->find(TRUE);
$formatted['id'] = $checkCid->id;
}
else {
$formatted['id'] = $this->_params['contact_id'];
}
$setDateFields = array_intersect_key($this->_params, array_flip($this->_dateFields));
$this->formatCommonData($this->_params, $formatted, $formatted);
foreach ($formatted['custom'] as $key => $val) {
$this->_params['custom_' . $key] = $val[-1]['value'];
}
$this->_params['skipRecentView'] = TRUE;
$this->_params['check_permissions'] = TRUE;
$this->_params['entity_id'] = $formatted['id'];
try {
civicrm_api3('custom_value', 'create', $this->_params);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
array_unshift($values, $error);
return CRM_Import_Parser::ERROR;
}
}
/**
* Format Date params.
*
* Although the api will accept any strtotime valid string CiviCRM accepts at least one date format
* not supported by strtotime so we should run this through a conversion
*/
public function formatDateParams() {
$session = CRM_Core_Session::singleton();
$dateType = $session->get('dateTypes');
$setDateFields = array_intersect_key($this->_params, array_flip($this->_dateFields));
foreach ($setDateFields as $key => $value) {
CRM_Utils_Date::convertToDefaultDate($this->_params, $dateType, $key);
$this->_params[$key] = CRM_Utils_Date::processDate($this->_params[$key]);
}
}
/**
* Set import entity.
* @param string $entity
*/
public function setEntity($entity) {
$this->_entity = $entity;
$this->_multipleCustomData = $entity;
}
/**
* The initializer code, called before the processing
*
* @return void
*/
public function fini() {
}
/**
* Return the field ids and names (with groups) for import purpose.
*
* @param int $id
* Custom group ID.
*
* @return array
*
*/
public function getGroupFieldsForImport($id) {
$importableFields = array();
$params = array('custom_group_id' => $id);
$allFields = civicrm_api3('custom_field', 'get', $params);
$fields = $allFields['values'];
foreach ($fields as $id => $values) {
$datatype = CRM_Utils_Array::value('data_type', $values);
if ($datatype == 'File') {
continue;
}
/* generate the key for the fields array */
$key = "custom_$id";
$regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array::value(0, $values));
$importableFields[$key] = array(
'name' => $key,
'title' => CRM_Utils_Array::value('label', $values),
'headerPattern' => '/' . preg_quote($regexp, '/') . '/',
'import' => 1,
'custom_field_id' => $id,
'options_per_line' => CRM_Utils_Array::value('options_per_line', $values),
'data_type' => CRM_Utils_Array::value('data_type', $values),
'html_type' => CRM_Utils_Array::value('html_type', $values),
'is_search_range' => CRM_Utils_Array::value('is_search_range', $values),
);
if (CRM_Utils_Array::value('html_type', $values) == 'Select Date') {
$importableFields[$key]['date_format'] = CRM_Utils_Array::value('date_format', $values);
$importableFields[$key]['time_format'] = CRM_Utils_Array::value('time_format', $values);
$this->_dateFields[] = $key;
}
}
return $importableFields;
}
}