begin tainacan importer

This commit is contained in:
Eduardo humberto 2018-02-20 10:35:43 -03:00
parent 3ef8bd2392
commit 5452928b0a
4 changed files with 121 additions and 1 deletions

View File

@ -11,6 +11,7 @@ const VENDOR_DIR = __DIR__ . '/../vendor/';
const TAPI_DIR = __DIR__ . '/../api/'; const TAPI_DIR = __DIR__ . '/../api/';
const ENDPOINTS_DIR = __DIR__ . '/../api/endpoints/'; const ENDPOINTS_DIR = __DIR__ . '/../api/endpoints/';
const HELPERS_DIR = __DIR__ . '/../helpers/'; const HELPERS_DIR = __DIR__ . '/../helpers/';
const IMPORTER_DIR = __DIR__ . '/../importer/';
const DIRS = [ const DIRS = [
CLASSES_DIR, CLASSES_DIR,
@ -21,10 +22,12 @@ const DIRS = [
TRAITS_DIR, TRAITS_DIR,
TAPI_DIR, TAPI_DIR,
ENDPOINTS_DIR, ENDPOINTS_DIR,
IMPORTER_DIR
]; ];
require_once(VENDOR_DIR . 'autoload.php'); require_once(VENDOR_DIR . 'autoload.php');
require_once(HELPERS_DIR . 'class-tainacan-helpers-html.php'); require_once(HELPERS_DIR . 'class-tainacan-helpers-html.php');
require_once(IMPORTER_DIR . 'class-tainacan-importer.php');
spl_autoload_register('tainacan_autoload'); spl_autoload_register('tainacan_autoload');
@ -44,7 +47,9 @@ function tainacan_autoload($class_name){
elseif ($class_path[0] == 'Tainacan') { elseif ($class_path[0] == 'Tainacan') {
$sliced = array_slice($class_path, 1, count($class_path) -2); $sliced = array_slice($class_path, 1, count($class_path) -2);
if($sliced) { if( isset( $class_path[1] ) && $class_path[1] === 'Importer' ){
$dir = IMPORTER_DIR;
} else if($sliced) {
$lower = $sliced[0]; $lower = $sliced[0];
$sliced[0] = strtolower( $lower ); $sliced[0] = strtolower( $lower );

View File

@ -0,0 +1,18 @@
<?php
namespace Tainacan\Importer;
use Tainacan;
class CSV extends Importer {
public function __construct() {
parent::__construct();
}
/**
*
*/
public function get_fields_source(){
// TODO: Implement get_fields_source() method.
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace Tainacan\Importer;
use Tainacan;
abstract class Importer {
public $collection;
public $mapping;
public $tmp_file;
public function __construct() {
if (!session_id()) {
@session_start();
}
}
/**
* @param Tainacan\Entities\Collection $collection
*/
public function set_collection( Tainacan\Entities\Collection $collection ){
$this->collection = $collection;
$_SESSION['tainacan_importer'] = $this;
}
/**
* @param array the mapping
*/
public function set_mapping( $mapping ){
$this->mapping = $mapping;
$_SESSION['tainacan_importer'] = $this;
}
/**
* @param $file File to be managed by importer
*/
public function set_file( $file ){
$new_file = wp_handle_upload( $file );
if ( $new_file && ! isset( $new_file['error'] ) ) {
$this->tmp_file = $new_file['file'];
} else {
echo $new_file['error'];
}
}
/**
* get the fields of file/url to allow mapping
* should returns an array
*/
abstract public function get_fields_source();
/**
* @param $start
* @param $end
*/
public function process( $start, $end ){
}
public function run(){
}
}

34
tests/test-importer.php Normal file
View File

@ -0,0 +1,34 @@
<?php
namespace Tainacan\Tests;
use Tainacan\Importer;
/**
* Class Importer
*
* @package Test_Tainacan
*/
class ImporterTests extends TAINACAN_UnitTestCase {
/**
* @group importer
*/
public function test_instance () {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Other',
'description' => 'adasdasdsa',
'default_order' => 'DESC',
'status' => 'publish'
),
true
);
$csv_importer = new Importer\CSV();
$csv_importer->set_collection( $collection );
// here the session is init already
$this->assertEquals( $_SESSION['tainacan_importer'], $csv_importer );
}
}