Merge branch 'develop' of https://github.com/tainacan/tainacan into develop
This commit is contained in:
commit
ab5b5464b3
|
@ -230,14 +230,13 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
if(!empty($request->get_body())){
|
||||
$body = json_decode($request->get_body());
|
||||
$field_id = $request['field_id'];
|
||||
|
||||
$collection_id = $request['collection_id'];
|
||||
$field_id = $body['field_id'];
|
||||
$field_trashed = $this->field_repository->delete($field_id);
|
||||
|
||||
return new WP_REST_Response(['error' => 'Not Implemented.'], 400);
|
||||
}
|
||||
$prepared = $this->prepare_item_for_response($field_trashed, $request);
|
||||
|
||||
return new WP_REST_Response($prepared, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -142,41 +142,6 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
|
|||
return parent::get_collection_params(); // TODO: Change the autogenerated stub
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
if(!empty($request->get_body())){
|
||||
$body = json_decode($request->get_body());
|
||||
|
||||
$collection_id = $request['collection_id'];
|
||||
$field_id = $body['metadata_id'];
|
||||
|
||||
return new WP_REST_Response(['error' => 'Not Implemented.'], 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
if(isset($request['item_id'])){
|
||||
$item = $this->item_repository->fetch($request['item_id']);
|
||||
|
||||
if($item instanceof Entities\Item) {
|
||||
return $item->can_delete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
|
|
|
@ -414,8 +414,8 @@ class Fields extends Repository {
|
|||
return $this->insert($object);
|
||||
}
|
||||
|
||||
public function delete($object){
|
||||
|
||||
public function delete($field_id){
|
||||
return new Entities\Field( wp_trash_post( $field_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -39,23 +39,11 @@ class Item_Metadata extends Repository {
|
|||
}
|
||||
|
||||
/**
|
||||
* Delete Item Field
|
||||
*
|
||||
* @param $item_metadata
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function delete($item_metadata){
|
||||
// if(is_array($item_metadata->get_value())){
|
||||
// $values = $item_metadata->get_value();
|
||||
//
|
||||
// foreach ($values as $value){
|
||||
// delete_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash($value));
|
||||
// }
|
||||
// } else {
|
||||
// delete_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash($item_metadata->get_value()));
|
||||
// }
|
||||
}
|
||||
public function delete($item_metadata){}
|
||||
|
||||
public function save_core_field_value(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
|
||||
$field_type = $item_metadata->get_field()->get_field_type_object();
|
||||
|
|
|
@ -6,6 +6,7 @@ use Tainacan;
|
|||
class CSV extends Importer {
|
||||
|
||||
public $delimiter = ',';
|
||||
private $file;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
@ -18,7 +19,9 @@ class CSV extends Importer {
|
|||
return $this->delimiter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $delimiter
|
||||
*/
|
||||
public function set_delimiter( $delimiter ){
|
||||
$this->delimiter = $delimiter;
|
||||
}
|
||||
|
@ -27,16 +30,45 @@ class CSV extends Importer {
|
|||
* @inheritdoc
|
||||
*/
|
||||
public function get_fields_source(){
|
||||
$file = new \SplFileObject( $this->tmp_file, 'r' );
|
||||
$file->seek(0 );
|
||||
return $file->fgetcsv( $this->get_delimiter() );
|
||||
$this->file = ( !isset( $this->file ) ) ? new \SplFileObject( $this->tmp_file, 'r' ) : $this->file;
|
||||
$this->file->seek(0 );
|
||||
return $this->file->fgetcsv( $this->get_delimiter() );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function process_item(){
|
||||
// TODO: process single item
|
||||
public function process( $start, $end ){
|
||||
$this->file = ( !isset( $this->file ) ) ? new \SplFileObject( $this->tmp_file, 'r' ) : $this->file;
|
||||
|
||||
while ( $start < $end ){
|
||||
if( $start === 0 ){
|
||||
$start++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$processed_item = $this->process_item( $start );
|
||||
$this->insert( $start, $processed_item );
|
||||
$start++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function process_item( $index ){
|
||||
$processedItem = [];
|
||||
$headers = $this->get_fields_source();
|
||||
|
||||
// search the index in the file and get values
|
||||
$this->file->seek( $index );
|
||||
$values = $this->file->fgetcsv( $this->get_delimiter() );
|
||||
|
||||
foreach ($headers as $index => $header) {
|
||||
$processedItem[ $header ] = $values[ $index ];
|
||||
}
|
||||
|
||||
return $processedItem;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,7 +78,9 @@ class CSV extends Importer {
|
|||
// TODO: Implement get_options() method.
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function get_total_items(){
|
||||
if( isset( $this->total_items ) ){
|
||||
return $this->total_items;
|
||||
|
|
|
@ -5,12 +5,16 @@ use Tainacan;
|
|||
abstract class Importer {
|
||||
|
||||
private $id;
|
||||
private $processed_items;
|
||||
private $last_index;
|
||||
|
||||
public $collection;
|
||||
public $mapping;
|
||||
public $tmp_file;
|
||||
public $total_items;
|
||||
public $limit_query;
|
||||
public $start;
|
||||
public $end;
|
||||
public $logs = [];
|
||||
|
||||
public function __construct() {
|
||||
|
@ -19,6 +23,10 @@ abstract class Importer {
|
|||
}
|
||||
|
||||
$this->id = uniqid();
|
||||
$this->limit_query = 100;
|
||||
$this->start = 0;
|
||||
$this->end = $this->start + $this->limit_query;
|
||||
$this->processed_items = [];
|
||||
$_SESSION['tainacan_importer'][$this->id] = $this;
|
||||
}
|
||||
|
||||
|
@ -31,12 +39,26 @@ abstract class Importer {
|
|||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @return array Mapping
|
||||
*/
|
||||
public function get_mapping(){
|
||||
return $this->mapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array Array with ids inserted in Tainacan
|
||||
*/
|
||||
public function get_processed_items(){
|
||||
return $this->processed_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed the last index from source
|
||||
*/
|
||||
public function get_last_index(){
|
||||
return $this->last_index;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tainacan\Entities\Collection $collection
|
||||
*/
|
||||
|
@ -45,7 +67,7 @@ abstract class Importer {
|
|||
}
|
||||
|
||||
/**
|
||||
* save an associative array with tainacan field id and field from source
|
||||
* save an associative array with tainacan field id as index and field from source as value
|
||||
*
|
||||
* @param array $mapping Mapping importer-fields
|
||||
*/
|
||||
|
@ -53,6 +75,29 @@ abstract class Importer {
|
|||
$this->mapping = $mapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the limit of query to be processed
|
||||
*
|
||||
* @param $size The total of items
|
||||
*/
|
||||
public function set_limit_query( $size ){
|
||||
$this->limit_query = $size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $start the first index to init the process
|
||||
*/
|
||||
public function set_start( $start ){
|
||||
$this->start = $start;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $end the last index in process
|
||||
*/
|
||||
public function set_end( $end ){
|
||||
$this->end = $end;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file File to be managed by importer
|
||||
* @return bool
|
||||
|
@ -114,10 +159,11 @@ abstract class Importer {
|
|||
/**
|
||||
* get values for a single item
|
||||
*
|
||||
* @param $index
|
||||
* @return array with field_source's as the index and values for the
|
||||
* item Ex: [ 'Field1' => 'value1', 'Field2' => [ 'value2','value3' ]
|
||||
*/
|
||||
abstract public function process_item();
|
||||
abstract public function process_item( $index );
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
|
@ -132,12 +178,12 @@ abstract class Importer {
|
|||
abstract public function get_total_items();
|
||||
|
||||
/**
|
||||
* @param $start
|
||||
* @param $end
|
||||
* process a limited size of items
|
||||
*
|
||||
* @param $start init index
|
||||
* @param $end last index
|
||||
*/
|
||||
public function process( $start, $end ){
|
||||
|
||||
}
|
||||
abstract public function process( $start, $end );
|
||||
|
||||
/**
|
||||
* insert processed item from source to Tainacan
|
||||
|
@ -191,6 +237,13 @@ abstract class Importer {
|
|||
}
|
||||
|
||||
$item->set_status('publish' );
|
||||
|
||||
// inserted the id on processed item with its index as array index
|
||||
$this->processed_items[ $index ] = $item->get_id();
|
||||
|
||||
// set the last index
|
||||
$this->last_index = $index;
|
||||
|
||||
$Tainacan_Items->update( $item );
|
||||
return $item;
|
||||
} else {
|
||||
|
@ -201,17 +254,19 @@ abstract class Importer {
|
|||
}
|
||||
|
||||
/**
|
||||
* log the actions from importer
|
||||
*
|
||||
* @param $type
|
||||
* @param $message
|
||||
*/
|
||||
public function set_log ( $type, $message ){
|
||||
public function set_log( $type, $message ){
|
||||
$this->logs[] = [ 'type' => $type, 'message' => $message ];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* run the process
|
||||
*/
|
||||
public function run(){
|
||||
|
||||
$this->process( $this->start, $this->end );
|
||||
}
|
||||
}
|
|
@ -210,6 +210,43 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
|
|||
|
||||
}
|
||||
|
||||
public function test_trash_field(){
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'Statement',
|
||||
'description' => 'No Statement'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$field = $this->tainacan_entity_factory->create_entity(
|
||||
'field',
|
||||
array(
|
||||
'name' => 'Field Statement',
|
||||
'description' => 'No Statement',
|
||||
'collection' => $collection,
|
||||
'status' => 'publish',
|
||||
'field_type' => 'Tainacan\Field_Types\Text',
|
||||
'multiple' => 'yes'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$trash_field_request = new \WP_REST_Request(
|
||||
'DELETE',
|
||||
$this->namespace . '/collection/'. $collection->get_id() . '/fields/' . $field->get_id()
|
||||
);
|
||||
|
||||
$trash_field_response = $this->server->dispatch($trash_field_request);
|
||||
$data1 = $trash_field_response->get_data();
|
||||
|
||||
$this->assertEquals($field->get_id(), $data1['id']);
|
||||
|
||||
$field_trashed = get_post($data1['id']);
|
||||
$this->assertEquals('trash', $field_trashed->post_status);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue