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,88 @@
<?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 defines the DataSource interface but must be subclassed to be
* useful.
*/
abstract class CRM_Import_DataSource {
/**
* Provides information about the data source.
*
* @return array
* Description of this data source, including:
* - title: string, translated, required
* - permissions: array, optional
*
*/
abstract public function getInfo();
/**
* Set variables up before form is built.
*
* @param CRM_Core_Form $form
*/
abstract public function preProcess(&$form);
/**
* This is function is called by the form object to get the DataSource's form snippet.
*
* It should add all fields necessary to get the data uploaded to the temporary table in the DB.
*
* @param CRM_Core_Form $form
*/
abstract public function buildQuickForm(&$form);
/**
* Process the form submission.
*
* @param array $params
* @param string $db
* @param CRM_Core_Form $form
*/
abstract public function postProcess(&$params, &$db, &$form);
/**
* Determine if the current user has access to this data source.
*
* @return bool
*/
public function checkPermission() {
$info = $this->getInfo();
return empty($info['permissions']) || CRM_Core_Permission::check($info['permissions']);
}
}

View file

@ -0,0 +1,269 @@
<?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
*/
class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
const
NUM_ROWS_TO_INSERT = 100;
/**
* Provides information about the data source.
*
* @return array
* collection of info about this data source
*/
public function getInfo() {
return array('title' => ts('Comma-Separated Values (CSV)'));
}
/**
* Set variables up before form is built.
*
* @param CRM_Core_Form $form
*/
public function preProcess(&$form) {
}
/**
* This is function is called by the form object to get the DataSource's form snippet.
*
* It should add all fields necessary to get the data
* uploaded to the temporary table in the DB.
*
* @param CRM_Core_Form $form
*/
public function buildQuickForm(&$form) {
$form->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_CSV');
$config = CRM_Core_Config::singleton();
$uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE);
//Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit".
if (empty($uploadFileSize)) {
$uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), TRUE);
}
$uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
$form->assign('uploadSize', $uploadSize);
$form->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
$form->setMaxFileSize($uploadFileSize);
$form->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
1 => $uploadSize,
2 => $uploadFileSize,
)), 'maxfilesize', $uploadFileSize);
$form->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
$form->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
$form->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
}
/**
* Process the form submission.
*
* @param array $params
* @param string $db
* @param \CRM_Core_Form $form
*/
public function postProcess(&$params, &$db, &$form) {
$file = $params['uploadFile']['name'];
$result = self::_CsvToTable($db,
$file,
CRM_Utils_Array::value('skipColumnHeader', $params, FALSE),
CRM_Utils_Array::value('import_table_name', $params),
CRM_Utils_Array::value('fieldSeparator', $params, ',')
);
$form->set('originalColHeader', CRM_Utils_Array::value('original_col_header', $result));
$table = $result['import_table_name'];
$importJob = new CRM_Contact_Import_ImportJob($table);
$form->set('importTableName', $importJob->getTableName());
}
/**
* Create a table that matches the CSV file and populate it with the file's contents
*
* @param object $db
* Handle to the database connection.
* @param string $file
* File name to load.
* @param bool $headers
* Whether the first row contains headers.
* @param string $table
* Name of table from which data imported.
* @param string $fieldSeparator
* Character that separates the various columns in the file.
*
* @return string
* name of the created table
*/
private static function _CsvToTable(
&$db,
$file,
$headers = FALSE,
$table = NULL,
$fieldSeparator = ','
) {
$result = array();
$fd = fopen($file, 'r');
if (!$fd) {
CRM_Core_Error::fatal("Could not read $file");
}
if (filesize($file) == 0) {
CRM_Core_Error::fatal("$file is empty. Please upload a valid file.");
}
$config = CRM_Core_Config::singleton();
// support tab separated
if (strtolower($fieldSeparator) == 'tab' ||
strtolower($fieldSeparator) == '\t'
) {
$fieldSeparator = "\t";
}
$firstrow = fgetcsv($fd, 0, $fieldSeparator);
// create the column names from the CSV header or as col_0, col_1, etc.
if ($headers) {
//need to get original headers.
$result['original_col_header'] = $firstrow;
$strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
$columns = array_map($strtolower, $firstrow);
$columns = str_replace(' ', '_', $columns);
$columns = preg_replace('/[^a-z_]/', '', $columns);
// need to take care of null as well as duplicate col names.
$duplicateColName = FALSE;
if (count($columns) != count(array_unique($columns))) {
$duplicateColName = TRUE;
}
// need to truncate values per mysql field name length limits
// mysql allows 64, but we need to account for appending colKey
// CRM-9079
foreach ($columns as $colKey => & $colName) {
if (strlen($colName) > 58) {
$colName = substr($colName, 0, 58);
}
}
if (in_array('', $columns) || $duplicateColName) {
foreach ($columns as $colKey => & $colName) {
if (!$colName) {
$colName = "col_$colKey";
}
elseif ($duplicateColName) {
$colName .= "_$colKey";
}
}
}
// CRM-4881: we need to quote column names, as they may be MySQL reserved words
foreach ($columns as & $column) {
$column = "`$column`";
}
}
else {
$columns = array();
foreach ($firstrow as $i => $_) {
$columns[] = "col_$i";
}
}
// FIXME: we should regen this table's name if it exists rather than drop it
if (!$table) {
$table = 'civicrm_import_job_' . md5(uniqid(rand(), TRUE));
}
$db->query("DROP TABLE IF EXISTS $table");
$numColumns = count($columns);
$create = "CREATE TABLE $table (" . implode(' text, ', $columns) . " text) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
$db->query($create);
// the proper approach, but some MySQL installs do not have this enabled
// $load = "LOAD DATA LOCAL INFILE '$file' INTO TABLE $table FIELDS TERMINATED BY '$fieldSeparator' OPTIONALLY ENCLOSED BY '\"'";
// if ($headers) { $load .= ' IGNORE 1 LINES'; }
// $db->query($load);
// parse the CSV line by line and build one big INSERT (while MySQL-escaping the CSV contents)
if (!$headers) {
rewind($fd);
}
$sql = NULL;
$first = TRUE;
$count = 0;
while ($row = fgetcsv($fd, 0, $fieldSeparator)) {
// skip rows that dont match column count, else we get a sql error
if (count($row) != $numColumns) {
continue;
}
if (!$first) {
$sql .= ', ';
}
$first = FALSE;
// CRM-17859 Trim non-breaking spaces from columns.
$row = array_map(
function($string) {
return trim($string, chr(0xC2) . chr(0xA0));
}, $row);
$row = array_map(array('CRM_Core_DAO', 'escapeString'), $row);
$sql .= "('" . implode("', '", $row) . "')";
$count++;
if ($count >= self::NUM_ROWS_TO_INSERT && !empty($sql)) {
$sql = "INSERT IGNORE INTO $table VALUES $sql";
$db->query($sql);
$sql = NULL;
$first = TRUE;
$count = 0;
}
}
if (!empty($sql)) {
$sql = "INSERT IGNORE INTO $table VALUES $sql";
$db->query($sql);
}
fclose($fd);
//get the import tmp table name.
$result['import_table_name'] = $table;
return $result;
}
}

View file

@ -0,0 +1,111 @@
<?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$
*
*/
class CRM_Import_DataSource_SQL extends CRM_Import_DataSource {
/**
* Provides information about the data source.
*
* @return array
* collection of info about this data source
*/
public function getInfo() {
return array(
'title' => ts('SQL Query'),
'permissions' => array('import SQL datasource'),
);
}
/**
* Set variables up before form is built.
*
* @param CRM_Core_Form $form
*/
public function preProcess(&$form) {
}
/**
* This is function is called by the form object to get the DataSource's
* form snippet. It should add all fields necesarry to get the data
* uploaded to the temporary table in the DB.
*
* @param CRM_Core_Form $form
*
* @return void
* (operates directly on form argument)
*/
public function buildQuickForm(&$form) {
$form->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_SQL');
$form->add('textarea', 'sqlQuery', ts('Specify SQL Query'), 'rows=10 cols=45', TRUE);
$form->addFormRule(array('CRM_Import_DataSource_SQL', 'formRule'), $form);
}
/**
* @param $fields
* @param $files
* @param CRM_Core_Form $form
*
* @return array|bool
*/
public static function formRule($fields, $files, $form) {
$errors = array();
// Makeshift query validation (case-insensitive regex matching on word boundaries)
$forbidden = array('ALTER', 'CREATE', 'DELETE', 'DESCRIBE', 'DROP', 'SHOW', 'UPDATE', 'information_schema');
foreach ($forbidden as $pattern) {
if (preg_match("/\\b$pattern\\b/i", $fields['sqlQuery'])) {
$errors['sqlQuery'] = ts('The query contains the forbidden %1 command.', array(1 => $pattern));
}
}
return $errors ? $errors : TRUE;
}
/**
* Process the form submission.
*
* @param array $params
* @param string $db
* @param \CRM_Core_Form $form
*/
public function postProcess(&$params, &$db, &$form) {
$importJob = new CRM_Contact_Import_ImportJob(
CRM_Utils_Array::value('import_table_name', $params),
$params['sqlQuery'], TRUE
);
$form->set('importTableName', $importJob->getTableName());
}
}

View file

@ -0,0 +1,199 @@
<?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
*/
/**
* Base class for upload-only import forms (all but Contact import).
*/
abstract class CRM_Import_Form_DataSource extends CRM_Core_Form {
/**
* Set variables up before form is built.
*/
public function preProcess() {
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
$params = "reset=1";
if ($this->_id) {
$params .= "&id={$this->_id}";
}
CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(static::PATH, $params));
// check for post max size
CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE);
}
/**
* Common form elements.
*/
public function buildQuickForm() {
$config = CRM_Core_Config::singleton();
$uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE);
//Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit".
if (empty($uploadFileSize)) {
$uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), TRUE);
}
$uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
$this->assign('uploadSize', $uploadSize);
$this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
$this->setMaxFileSize($uploadFileSize);
$this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
1 => $uploadSize,
2 => $uploadFileSize,
)), 'maxfilesize', $uploadFileSize);
$this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
$this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
$this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
$this->add('text', 'fieldSeparator', ts('Import Field Separator'), array('size' => 2), TRUE);
$this->setDefaults(array('fieldSeparator' => $config->fieldSeparator));
$mappingArray = CRM_Core_BAO_Mapping::getCreateMappingValues('Import ' . static::IMPORT_ENTITY);
$this->assign('savedMapping', $mappingArray);
$this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
if ($loadedMapping = $this->get('loadedMapping')) {
$this->assign('loadedMapping', $loadedMapping);
$this->setDefaults(array('savedMapping' => $loadedMapping));
}
//build date formats
CRM_Core_Form_Date::buildAllowedDateFormats($this);
$this->addButtons(array(
array(
'type' => 'upload',
'name' => ts('Continue'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
)
);
}
/**
* A long-winded way to add one radio element to the form.
*/
protected function addContactTypeSelector() {
//contact types option
$contactOptions = array();
if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
$contactOptions[] = $this->createElement('radio',
NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL
);
}
if (CRM_Contact_BAO_ContactType::isActive('Household')) {
$contactOptions[] = $this->createElement('radio',
NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD
);
}
if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
$contactOptions[] = $this->createElement('radio',
NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION
);
}
$this->addGroup($contactOptions, 'contactType',
ts('Contact Type')
);
$this->setDefaults(array(
'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
));
}
/**
* Store form values.
*
* @param array $names
*/
protected function storeFormValues($names) {
foreach ($names as $name) {
$this->set($name, $this->controller->exportValue($this->_name, $name));
}
}
/**
* Common form postProcess.
*
* @param string $parserClassName
*
* @param string|null $entity
* Entity to set for paraser currently only for custom import
*/
protected function submitFileForMapping($parserClassName, $entity = NULL) {
$this->controller->resetPage('MapField');
$fileName = $this->controller->exportValue($this->_name, 'uploadFile');
$skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
$session = CRM_Core_Session::singleton();
$session->set("dateTypes", $this->get('dateFormats'));
$separator = $this->controller->exportValue($this->_name, 'fieldSeparator');
$mapper = array();
$parser = new $parserClassName($mapper);
if ($entity) {
$parser->setEntity($this->get($entity));
}
$parser->setMaxLinesToProcess(100);
$parser->run($fileName,
$separator,
$mapper,
$skipColumnHeader,
CRM_Import_Parser::MODE_MAPFIELD,
$this->get('contactType')
);
// add all the necessary variables to the form
$parser->set($this);
}
/**
* Return a descriptive name for the page, used in wizard header.
*
* @return string
*/
public function getTitle() {
return ts('Upload Data');
}
}

View file

@ -0,0 +1,156 @@
<?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
*/
/**
* This class gets the name of the file to upload.
*
* TODO: CRM-11254 - There's still a lot of duplicate code in the 5 child classes that should be moved here
*/
abstract class CRM_Import_Form_MapField extends CRM_Core_Form {
/**
* Cache of preview data values
*
* @var array
*/
protected $_dataValues;
/**
* Mapper fields
*
* @var array
*/
protected $_mapperFields;
/**
* Loaded mapping ID
*
* @var int
*/
protected $_loadedMappingId;
/**
* Number of columns in import file
*
* @var int
*/
protected $_columnCount;
/**
* Column headers, if we have them
*
* @var array
*/
protected $_columnHeaders;
/**
* An array of booleans to keep track of whether a field has been used in
* form building already.
*
* @var array
*/
protected $_fieldUsed;
/**
* Return a descriptive name for the page, used in wizard header.
*
* @return string
*/
public function getTitle() {
return ts('Match Fields');
}
/**
* Attempt to match header labels with our mapper fields.
*
* @param string $header
* @param array $patterns
*
* @return string
*/
public function defaultFromHeader($header, &$patterns) {
foreach ($patterns as $key => $re) {
// Skip empty key/patterns
if (!$key || !$re || strlen("$re") < 5) {
continue;
}
// Scan through the headerPatterns defined in the schema for a match
if (preg_match($re, $header)) {
$this->_fieldUsed[$key] = TRUE;
return $key;
}
}
return '';
}
/**
* Guess at the field names given the data and patterns from the schema.
*
* @param array $patterns
* @param string $index
*
* @return string
*/
public function defaultFromData(&$patterns, $index) {
$best = '';
$bestHits = 0;
$n = count($this->_dataValues);
foreach ($patterns as $key => $re) {
// Skip empty key/patterns
if (!$key || !$re || strlen("$re") < 5) {
continue;
}
/* Take a vote over the preview data set */
$hits = 0;
for ($i = 0; $i < $n; $i++) {
if (isset($this->_dataValues[$i][$index])) {
if (preg_match($re, $this->_dataValues[$i][$index])) {
$hits++;
}
}
}
if ($hits > $bestHits) {
$bestHits = $hits;
$best = $key;
}
}
if ($best != '') {
$this->_fieldUsed[$best] = TRUE;
}
return $best;
}
}

View file

@ -0,0 +1,74 @@
<?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
*/
/**
* This class previews the uploaded file and returns summary statistics.
*
* TODO: CRM-11254 - if preProcess and postProcess functions can be reconciled between the 5 child classes,
* those classes can be removed entirely and this class will not need to be abstract
*/
abstract class CRM_Import_Form_Preview extends CRM_Core_Form {
/**
* Return a descriptive name for the page, used in wizard header.
*
* @return string
*/
public function getTitle() {
return ts('Preview');
}
/**
* Build the form object.
*/
public function buildQuickForm() {
$this->addButtons(array(
array(
'type' => 'back',
'name' => ts('Previous'),
),
array(
'type' => 'next',
'name' => ts('Import Now'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
)
);
}
}

View file

@ -0,0 +1,65 @@
<?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
*/
/**
* This class summarizes the import results.
*
* TODO: CRM-11254 - if preProcess and postProcess functions can be reconciled between the 5 child classes,
* those classes can be removed entirely and this class will not need to be abstract
*/
abstract class CRM_Import_Form_Summary extends CRM_Core_Form {
/**
* Build the form object.
*/
public function buildQuickForm() {
$this->addButtons(array(
array(
'type' => 'next',
'name' => ts('Done'),
'isDefault' => TRUE,
),
)
);
}
/**
* Return a descriptive name for the page, used in wizard header.
*
* @return string
*/
public function getTitle() {
return ts('Summary');
}
}

View file

@ -0,0 +1,463 @@
<?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
*/
abstract class CRM_Import_Parser {
/**
* Settings
*/
const MAX_ERRORS = 250, MAX_WARNINGS = 25, DEFAULT_TIMEOUT = 30;
/**
* Return codes
*/
const VALID = 1, WARNING = 2, ERROR = 4, CONFLICT = 8, STOP = 16, DUPLICATE = 32, MULTIPLE_DUPE = 64, NO_MATCH = 128, UNPARSED_ADDRESS_WARNING = 256;
/**
* Parser modes
*/
const MODE_MAPFIELD = 1, MODE_PREVIEW = 2, MODE_SUMMARY = 4, MODE_IMPORT = 8;
/**
* Codes for duplicate record handling
*/
const DUPLICATE_SKIP = 1, DUPLICATE_REPLACE = 2, DUPLICATE_UPDATE = 4, DUPLICATE_FILL = 8, DUPLICATE_NOCHECK = 16;
/**
* Contact types
*/
const CONTACT_INDIVIDUAL = 1, CONTACT_HOUSEHOLD = 2, CONTACT_ORGANIZATION = 4;
/**
* Total number of non empty lines
*/
protected $_totalCount;
/**
* Running total number of valid lines
*/
protected $_validCount;
/**
* Running total number of invalid rows
*/
protected $_invalidRowCount;
/**
* Maximum number of non-empty/comment lines to process
*
* @var int
*/
protected $_maxLinesToProcess;
/**
* Maximum number of invalid rows to store
*/
protected $_maxErrorCount;
/**
* Array of error lines, bounded by MAX_ERROR
*/
protected $_errors;
/**
* Total number of conflict lines
*/
protected $_conflictCount;
/**
* Array of conflict lines
*/
protected $_conflicts;
/**
* Total number of duplicate (from database) lines
*/
protected $_duplicateCount;
/**
* Array of duplicate lines
*/
protected $_duplicates;
/**
* Running total number of warnings
*/
protected $_warningCount;
/**
* Maximum number of warnings to store
*/
protected $_maxWarningCount = self::MAX_WARNINGS;
/**
* Array of warning lines, bounded by MAX_WARNING
*/
protected $_warnings;
/**
* Array of all the fields that could potentially be part
* of this import process
* @var array
*/
protected $_fields;
/**
* Array of the fields that are actually part of the import process
* the position in the array also dictates their position in the import
* file
* @var array
*/
protected $_activeFields;
/**
* Cache the count of active fields
*
* @var int
*/
protected $_activeFieldCount;
/**
* Cache of preview rows
*
* @var array
*/
protected $_rows;
/**
* Filename of error data
*
* @var string
*/
protected $_errorFileName;
/**
* Filename of conflict data
*
* @var string
*/
protected $_conflictFileName;
/**
* Filename of duplicate data
*
* @var string
*/
protected $_duplicateFileName;
/**
* Contact type
*
* @var int
*/
public $_contactType;
/**
* Contact sub-type
*
* @var int
*/
public $_contactSubType;
/**
* Class constructor.
*/
public function __construct() {
$this->_maxLinesToProcess = 0;
$this->_maxErrorCount = self::MAX_ERRORS;
}
/**
* Abstract function definitions.
*/
abstract protected function init();
/**
* @return mixed
*/
abstract protected function fini();
/**
* Map field.
*
* @param array $values
*
* @return mixed
*/
abstract protected function mapField(&$values);
/**
* Preview.
*
* @param array $values
*
* @return mixed
*/
abstract protected function preview(&$values);
/**
* @param $values
*
* @return mixed
*/
abstract protected function summary(&$values);
/**
* @param $onDuplicate
* @param $values
*
* @return mixed
*/
abstract protected function import($onDuplicate, &$values);
/**
* Set and validate field values.
*
* @param array $elements
* array.
* @param $erroneousField
* reference.
*
* @return int
*/
public function setActiveFieldValues($elements, &$erroneousField) {
$maxCount = count($elements) < $this->_activeFieldCount ? count($elements) : $this->_activeFieldCount;
for ($i = 0; $i < $maxCount; $i++) {
$this->_activeFields[$i]->setValue($elements[$i]);
}
// reset all the values that we did not have an equivalent import element
for (; $i < $this->_activeFieldCount; $i++) {
$this->_activeFields[$i]->resetValue();
}
// now validate the fields and return false if error
$valid = self::VALID;
for ($i = 0; $i < $this->_activeFieldCount; $i++) {
if (!$this->_activeFields[$i]->validate()) {
// no need to do any more validation
$erroneousField = $i;
$valid = self::ERROR;
break;
}
}
return $valid;
}
/**
* 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;
}
/**
* @return array
*/
public function getSelectValues() {
$values = array();
foreach ($this->_fields as $name => $field) {
$values[$name] = $field->_title;
}
return $values;
}
/**
* @return array
*/
public function getSelectTypes() {
$values = array();
foreach ($this->_fields as $name => $field) {
if (isset($field->_hasLocationType)) {
$values[$name] = $field->_hasLocationType;
}
}
return $values;
}
/**
* @return array
*/
public function getHeaderPatterns() {
$values = array();
foreach ($this->_fields as $name => $field) {
if (isset($field->_headerPattern)) {
$values[$name] = $field->_headerPattern;
}
}
return $values;
}
/**
* @return array
*/
public function getDataPatterns() {
$values = array();
foreach ($this->_fields as $name => $field) {
$values[$name] = $field->_dataPattern;
}
return $values;
}
/**
* Remove single-quote enclosures from a value array (row).
*
* @param array $values
* @param string $enclosure
*
* @return void
*/
public static function encloseScrub(&$values, $enclosure = "'") {
if (empty($values)) {
return;
}
foreach ($values as $k => $v) {
$values[$k] = preg_replace("/^$enclosure(.*)$enclosure$/", '$1', $v);
}
}
/**
* Setter function.
*
* @param int $max
*
* @return void
*/
public function setMaxLinesToProcess($max) {
$this->_maxLinesToProcess = $max;
}
/**
* Determines the file extension based on error code.
*
* @var $type error code constant
* @return string
*/
public static function errorFileName($type) {
$fileName = NULL;
if (empty($type)) {
return $fileName;
}
$config = CRM_Core_Config::singleton();
$fileName = $config->uploadDir . "sqlImport";
switch ($type) {
case self::ERROR:
$fileName .= '.errors';
break;
case self::CONFLICT:
$fileName .= '.conflicts';
break;
case self::DUPLICATE:
$fileName .= '.duplicates';
break;
case self::NO_MATCH:
$fileName .= '.mismatch';
break;
case self::UNPARSED_ADDRESS_WARNING:
$fileName .= '.unparsedAddress';
break;
}
return $fileName;
}
/**
* Determines the file name based on error code.
*
* @var $type error code constant
* @return string
*/
public static function saveFileName($type) {
$fileName = NULL;
if (empty($type)) {
return $fileName;
}
switch ($type) {
case self::ERROR:
$fileName = 'Import_Errors.csv';
break;
case self::CONFLICT:
$fileName = 'Import_Conflicts.csv';
break;
case self::DUPLICATE:
$fileName = 'Import_Duplicates.csv';
break;
case self::NO_MATCH:
$fileName = 'Import_Mismatch.csv';
break;
case self::UNPARSED_ADDRESS_WARNING:
$fileName = 'Import_Unparsed_Address.csv';
break;
}
return $fileName;
}
/**
* Check if contact is a duplicate .
*
* @param array $formatValues
*
* @return array
*/
protected function checkContactDuplicate(&$formatValues) {
//retrieve contact id using contact dedupe rule
$formatValues['contact_type'] = $this->_contactType;
$formatValues['version'] = 3;
require_once 'CRM/Utils/DeprecatedUtils.php';
$error = _civicrm_api3_deprecated_check_contact_dedupe($formatValues);
return $error;
}
}

View file

@ -0,0 +1,59 @@
<?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
*/
/**
* State machine for managing different states of the Import process.
*/
class CRM_Import_StateMachine extends CRM_Core_StateMachine {
/**
* Class constructor.
*
* @param object $controller
* @param \const|int $action
*/
public function __construct($controller, $action = CRM_Core_Action::NONE) {
parent::__construct($controller, $action);
$classType = str_replace('_Controller', '', get_class($controller));
$this->_pages = array(
$classType . '_Form_DataSource' => NULL,
$classType . '_Form_MapField' => NULL,
$classType . '_Form_Preview' => NULL,
$classType . '_Form_Summary' => NULL,
);
$this->addSequentialPages($this->_pages, $action);
}
}