diff --git a/src/importer/class-tainacan-csv.php b/src/importer/class-tainacan-csv.php index 6ba18cc33..732c2365e 100644 --- a/src/importer/class-tainacan-csv.php +++ b/src/importer/class-tainacan-csv.php @@ -49,6 +49,8 @@ class CSV extends Importer { $this->set_option('document_index', $index); } else if( $rawColumn === 'special_attachments' ){ $this->set_option('attachment_index', $index); + } else if( $rawColumn === 'special_item_status' ){ + $this->set_option('item_status_index', $index); } } else { @@ -164,8 +166,9 @@ class CSV extends Importer { public function after_inserted_item( $inserted_item, $collection_index ) { $column_document = $this->get_option('document_index'); $column_attachment = $this->get_option('attachment_index'); + $column_item_status = $this->get_option('item_status_index'); - if( !empty($column_document) || !empty( $column_attachment ) ){ + if( !empty($column_document) || !empty( $column_attachment ) || !empty( $column_item_status ) ){ if (($handle = fopen($this->tmp_file, "r")) !== false) { $file = $handle; @@ -191,6 +194,11 @@ class CSV extends Importer { if( is_array($values) && !empty($column_attachment) ){ $this->handle_attachment( $values[$column_attachment], $inserted_item); } + + if( is_array($values) && !empty($column_item_status) ){ + $this->handle_item_status( $values[$column_item_status], $inserted_item); + } + } } @@ -509,4 +517,19 @@ class CSV extends Importer { $values = explode($delimiter, $line); return $values; } + + /** + * @param $status string the item status + */ + private function handle_item_status( $status, $item_inserted ){ + + if ( in_array( $status, array( 'auto-draft', 'draft', 'pending', 'future', 'publish', 'trash', 'inherit' ) ) ) { + $item_inserted->set_status($status); + + if( $item_inserted->validate() ) { + $item_inserted = $this->items_repo->update($item_inserted); + } + } + + } } \ No newline at end of file diff --git a/tests/test-importer.php b/tests/test-importer.php index 93cde402b..8a568b273 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -519,4 +519,120 @@ class ImporterTests extends TAINACAN_UnitTestCase { $this->assertFalse( is_numeric($document_id) ); } + + + /** + * @group importer_csv_special_fields + */ + public function test_special_fields_status_and_id(){ + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + $file_name = 'demosaved.csv'; + $csv_importer = new Importer\CSV(); + $id = $csv_importer->get_id(); + + // open the file "demosaved.csv" for writing + $file = fopen($file_name, 'w'); + + // save the column headers + fputcsv($file, array('Column 1', 'special_item_status', 'Unknow Column')); + + // Sample data + $data = array( + array('Data 11', 'publish', 'nothing'), + array('Data 21', 'private', 'void'), + array('Data 31', 'trash', 'empty'), + array('Data 41', 'future', 'null'), + array('Data 51', 'trash', 'zero') + ); + + // save each row of the data + foreach ($data as $row){ + fputcsv($file, $row); + } + + // Close the file + fclose($file); + + $_SESSION['tainacan_importer'][$id]->set_tmp_file( $file_name ); + + // file isset on importer + $this->assertTrue( !empty( $_SESSION['tainacan_importer'][$id]->get_tmp_file() ) ); + + // count total items + $this->assertEquals( 5, $_SESSION['tainacan_importer'][$id]->get_source_number_of_items() ); + + // get metadata to mapping AVOIDING special fields + $headers = $_SESSION['tainacan_importer'][$id]->get_source_metadata(); + $this->assertEquals( $headers[1], 'Unknow Column' ); + + $this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_option('item_status_index'), 1 ); + + // inserting the collection + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Other', + 'description' => 'adasdasdsa', + 'default_order' => 'DESC', + 'status' => 'publish' + ), + true + ); + + $metadatum = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'Data multiplo', + 'description' => 'Descreve o dado do campo data.', + 'collection' => $collection, + 'status' => 'publish', + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + 'multiple' => 'yes' + ), + true + ); + + $metadatum = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'Texto simples', + 'description' => 'Descreve o dado do campo data.', + 'collection' => $collection, + 'status' => 'publish', + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + 'multiple' => 'no' + ), + true + ); + + $collection_definition = [ + 'id' => $collection->get_id(), + 'total_items' => $_SESSION['tainacan_importer'][$id]->get_source_number_of_items(), + ]; + + // get collection metadata to map + $metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ; + + //create a random mapping + $map = []; + foreach ( $metadata as $index => $metadatum ){ + if(isset($headers[$index])) + $map[$metadatum->get_id()] = $headers[$index]; + } + + $collection_definition['mapping'] = $map; + + // add the collection + $_SESSION['tainacan_importer'][$id]->add_collection( $collection_definition ); + + while($_SESSION['tainacan_importer'][$id]->run()){ + continue; + } + + $items = $Tainacan_Items->fetch( ['order'=> 'DESC', 'orderby' => 'ID'], $collection, 'OBJECT' ); + + // only 3 items should be published + $this->assertEquals( 3, count( $items ) ); + } }