add unique ID for each importer created

This commit is contained in:
Eduardo humberto 2018-02-21 09:41:52 -03:00
parent 8b8ff5e400
commit 91095aade2
2 changed files with 31 additions and 8 deletions

View File

@ -4,6 +4,7 @@ use Tainacan;
abstract class Importer {
private $id;
public $collection;
public $mapping;
public $tmp_file;
@ -14,6 +15,16 @@ abstract class Importer {
if (!session_id()) {
@session_start();
}
$this->id = uniqid();
$_SESSION['tainacan_importer'][$this->id] = $this;
}
/**
* @return string
*/
public function get_id(){
return $this->id;
}
/**
@ -21,7 +32,6 @@ abstract class Importer {
*/
public function set_collection( Tainacan\Entities\Collection $collection ){
$this->collection = $collection;
$_SESSION['tainacan_importer'] = $this;
}
/**
@ -29,7 +39,6 @@ abstract class Importer {
*/
public function set_mapping( $mapping ){
$this->mapping = $mapping;
$_SESSION['tainacan_importer'] = $this;
}
/**
@ -41,7 +50,6 @@ abstract class Importer {
if ( is_numeric( $new_file ) ) {
$attach = get_post($new_file);
$this->tmp_file = $attach->guid;
$_SESSION['tainacan_importer'] = $this;
} else {
return false;
}
@ -61,6 +69,19 @@ abstract class Importer {
return media_handle_sideload( $file_array, 0 );
}
/**
* @param $url
*/
public function fetch_from_remote( $url ){
$tmp = download_url( $url );
$path_parts = pathinfo( $url );
$file_array['name'] = $path_parts['basename'];
$file_array['tmp_name'] = $tmp;
$file_array['size'] = filesize( $tmp );
return media_handle_sideload( $file_array, 0 );
}
/**
* get the fields of file/url to allow mapping
* should returns an array

View File

@ -26,10 +26,11 @@ class ImporterTests extends TAINACAN_UnitTestCase {
);
$csv_importer = new Importer\CSV();
$csv_importer->set_collection( $collection );
$id = $csv_importer->get_id();
$_SESSION['tainacan_importer'][$id]->set_collection( $collection );
// here the session is init already
$this->assertEquals( $_SESSION['tainacan_importer'], $csv_importer );
$this->assertEquals( $collection->get_id(), $_SESSION['tainacan_importer'][$id]->collection->get_id() );
}
/**
@ -37,6 +38,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
*/
public function test_file_import_csv () {
$csv_importer = new Importer\CSV();
$id = $csv_importer->get_id();
// open the file "demosaved.csv" for writing
$file = fopen('demosaved.csv', 'w');
@ -44,7 +46,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
// save the column headers
fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));
// Sample data. This can be fetched from mysql too
// Sample data
$data = array(
array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
@ -61,9 +63,9 @@ class ImporterTests extends TAINACAN_UnitTestCase {
// Close the file
fclose($file);
$csv_importer->set_file( 'demosaved.csv' );
$_SESSION['tainacan_importer'][$id]->set_file( 'demosaved.csv' );
// here the session is init already
$this->assertEquals( $_SESSION['tainacan_importer']->tmp_file , $csv_importer->tmp_file );
$this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) );
}
}