Skip to content

Commit

Permalink
bring newimport to trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
chastell committed Dec 16, 2008
1 parent e420a3a commit b38c743
Show file tree
Hide file tree
Showing 24 changed files with 1,466 additions and 438 deletions.
3 changes: 1 addition & 2 deletions CRM/Core/Config/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ function setCoreVariables( ) {
$this->importDataSourceDir =
$civicrm_root . DIRECTORY_SEPARATOR .
'CRM' . DIRECTORY_SEPARATOR .
#'Import' . DIRECTORY_SEPARATOR .
'NewImport' . DIRECTORY_SEPARATOR .
'Import' . DIRECTORY_SEPARATOR .
'DataSource' . DIRECTORY_SEPARATOR ;

$this->gettextResourceDir =
Expand Down
2 changes: 1 addition & 1 deletion CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ static function invoke( $args )
} else if ( $realm == 'activity' ) {
$controller = 'CRM_Activity_Import_Controller';
} else {
$controller = 'CRM_NewImport_Controller';
$controller = 'CRM_Import_Controller';
}

require_once 'CRM/Core/Key.php';
Expand Down
2 changes: 1 addition & 1 deletion CRM/Import/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function __construct( $title = null, $action = CRM_Core_Action::NONE, $modal = t
}

require_once 'CRM/Import/StateMachine.php';
$this->_stateMachine =& new CRM_Import_StateMachine( $this, $action );
$this->_stateMachine = new CRM_Import_StateMachine( $this, $action );

// create and instantiate the pages
$this->addPages( $this->_stateMachine, $action );
Expand Down
78 changes: 78 additions & 0 deletions CRM/Import/DataSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
+--------------------------------------------------------------------+
| CiviCRM version 2.2 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2009 |
+--------------------------------------------------------------------+
| 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 2009. |
| |
| 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 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-2009
* $Id$
*
*/

require_once 'CRM/Core/Form.php';
#require_once 'CRM/Import/Parser/Contact.php';

/**
* 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 collection of info about this data source
*
* @access public
*
*/
abstract public function getInfo();

/**
* Function to set variables up before form is built
*
* @access public
*/
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 necesarry to get the data
* uploaded to the temporary table in the DB.
*
* @return None (operates directly on form argument)
* @access public
*/
abstract public function buildQuickForm( &$form );

/**
* Function to process the form
*
* @access public
*/
abstract public function postProcess( &$params, &$db );
}
135 changes: 135 additions & 0 deletions CRM/Import/DataSource/CSV.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

/*
+--------------------------------------------------------------------+
| CiviCRM version 2.2 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2009 |
+--------------------------------------------------------------------+
| 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 2009. |
| |
| 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 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-2009
* $Id$
*
*/

require_once 'CRM/Import/DataSource.php';

class CRM_Import_DataSource_CSV extends CRM_Import_DataSource
{
function getInfo()
{
return array('title' => 'CSV Import');
}

function preProcess(&$form)
{
}

function buildQuickForm(&$form)
{
$form->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_CSV');

$config =& CRM_Core_Config::singleton();

// FIXME: why do we limit the file size to 8 MiB if it's larger in config?
$uploadFileSize = $config->maxImportFileSize >= 8388608 ? 8388608 : $config->maxImportFileSize;
$uploadSize = round(($uploadFileSize / (1024*1024)), 2);
$form->assign('uploadSize', $uploadSize);
$form->add('file', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=60', 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'));
}

function postProcess(&$params, &$db)
{
$file = $params['uploadFile']['name'];

$table = self::_CsvToTable($db, $file, $params['skipColumnHeader']);

require_once 'CRM/Import/ImportJob.php';
$importJob = new CRM_Import_ImportJob($table);
$this->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
*
* @return string name of the created table
*/
private static function _CsvToTable(&$db, $file, $headers = false)
{
$fd = fopen($file, 'r');
if (!$fd) CRM_Core_Error::fatal("Could not read $file");

$config =& CRM_Core_Config::singleton();
$firstrow = fgetcsv($fd, 0, $config->fieldSeparator);

// create the column names from the CSV header or as col_0, col_1, etc.
if ($headers) {
$columns = array_map('strtolower', $firstrow);
$columns = str_replace(' ', '_', $columns);
$columns = preg_replace('/[^a-z_]/', '', $columns);
} 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
$table = 'civicrm_import_job_' . md5(uniqid(rand(), true));
$db->query("DROP TABLE IF EXISTS $table");

$create = "CREATE TABLE $table (" . implode(' text, ', $columns) . " text)";
$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 '$config->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 = "INSERT IGNORE INTO $table VALUES ";
$first = true;
while ($row = fgetcsv($fd, 0, $config->fieldSeparator)) {
if (!$first) $sql .= ', ';
$first = false;
$row = array_map('mysql_real_escape_string', $row);
$sql .= "('" . implode("', '", $row) . "')";
}
$db->query($sql);

fclose($fd);

return $table;
}
}
79 changes: 79 additions & 0 deletions CRM/Import/DataSource/SQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
+--------------------------------------------------------------------+
| CiviCRM version 2.2 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2009 |
+--------------------------------------------------------------------+
| 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. |
| |
| 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 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-2009
* $Id$
*
*/

require_once 'CRM/Import/DataSource.php';

class CRM_Import_DataSource_SQL extends CRM_Import_DataSource
{

public function getInfo()
{
return array('title' => 'SQL Import');
}

public function preProcess(&$form)
{
}

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');
$form->addFormRule(array('CRM_Import_DataSource_SQL', 'formRule'), $form);
}

static function formRule(&$fields, &$files, &$form)
{
$errors = array();

// poor man's 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 $pattern command.");
}
}

return $errors ? $errors : true;
}


public function postProcess(&$params, &$db)
{
require_once 'CRM/Import/ImportJob.php';
$importJob = new CRM_Import_ImportJob(null, $params['sqlQuery']);
$this->set('importTableName', $importJob->getTableName());
}
}
6 changes: 3 additions & 3 deletions CRM/Import/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CRM_Import_Field {
* regexp to match the CSV header of this column/field
* @var string
*/
public $_headerPattern;
public $_columnPattern;

/**
* regexp to match the pattern of data from various column/fields
Expand Down Expand Up @@ -124,11 +124,11 @@ class CRM_Import_Field {



function __construct( $name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//', $hasLocationType = null, $phoneType = null, $related=null, $relatedContactType=null, $relatedContactDetails=null, $relatedContactLocType=null, $relatedContactPhoneType=null) {
function __construct( $name, $title, $type = CRM_Utils_Type::T_INT, $columnPattern = '//', $dataPattern = '//', $hasLocationType = null, $phoneType = null, $related=null, $relatedContactType=null, $relatedContactDetails=null, $relatedContactLocType=null, $relatedContactPhoneType=null) {
$this->_name = $name;
$this->_title = $title;
$this->_type = $type;
$this->_headerPattern = $headerPattern;
$this->_columnPattern = $columnPattern;
$this->_dataPattern = $dataPattern;
$this->_hasLocationType = $hasLocationType;
$this->_phoneType = $phoneType;
Expand Down
Loading

0 comments on commit b38c743

Please sign in to comment.