Add special field to set comment_status in importer #243

This commit is contained in:
vnmedeiros 2019-06-07 11:44:25 -03:00
parent 1a76604d1c
commit 9e4d34a16b
4 changed files with 38 additions and 8 deletions

View File

@ -23,6 +23,7 @@ The special columns that can be used are:
* **special_item_id** - Inform the item ID in the Tainacan database. This is useful when re-importing items and let the user decide wether to update existing items or ignore them and only add new items.
* **special_document** - let the user inform the item document. See "Importing files and attachments"
* **special_attachments** - let the user inform the attachments. See "Importing files and attachments"
* **special_comment_status** - Inform the item allows comment. Possible values are open or closed.
## Importing files and attachments

View File

@ -47,6 +47,8 @@ class CSV extends Importer {
$this->set_option('item_status_index', $index);
} else if( $rawColumn === 'special_item_id' ) {
$this->set_option('item_id_index', $index);
} else if( $rawColumn === 'special_comment_status' ) {
$this->set_option('item_comment_status_index', $index);
}
} else {
$columns[] = $rawColumn;
@ -154,6 +156,7 @@ class CSV extends Importer {
$column_document = $this->get_option('document_index');
$column_attachment = $this->get_option('attachment_index');
$column_item_status = $this->get_option('item_status_index');
$column_item_comment_status = $this->get_option('item_comment_status_index');
if( !empty($column_document) || !empty( $column_attachment ) || !empty( $column_item_status ) ){
@ -184,6 +187,10 @@ class CSV extends Importer {
if( is_array($values) && !empty($column_item_status) ) {
$this->handle_item_status( $values[$column_item_status], $inserted_item);
}
if( is_array($values) && !empty($column_item_comment_status) ) {
$this->handle_item_comment_status( $values[$column_item_comment_status], $inserted_item);
}
}
}
@ -519,6 +526,20 @@ class CSV extends Importer {
//}
}
/**
* @param $comment_status string the item comment status
*/
private function handle_item_comment_status( $comment_status, $item_inserted ) {
if ( ! in_array( $comment_status, array( 'open', 'closed' ) ) ) {
$comment_status = 'closed';
}
$item_inserted->set_comment_status($comment_status);
if( $item_inserted->validate() ) {
$item_inserted = $this->items_repo->update($item_inserted);
}
}
/**
* @param $status string the item ID
*/

View File

@ -559,15 +559,15 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$file = fopen($file_name, 'w');
// save the column headers
fputcsv($file, array('Column 1', 'special_item_status', 'Unknow Column'));
fputcsv($file, array('Column 1', 'special_item_status', 'Unknow Column', 'special_comment_status'));
// 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')
array('Data 11', 'publish', 'nothing', 'open'),
array('Data 21', 'private', 'void', 'closed'),
array('Data 31', 'trash', 'empty', 'open'),
array('Data 41', 'future', 'null', 'closed'),
array('Data 51', 'trash', 'zero', 'open')
);
// save each row of the data
@ -658,5 +658,13 @@ class ImporterTests extends TAINACAN_UnitTestCase {
// only 3 items should be published
$this->assertEquals( 3, count( $items ) );
foreach ($items as $item) {
if ( \in_array( $item->get_description(), ['Data 11', 'Data 31', 'Data 51'] ) ) {
$this->assertEquals( 'open', $item->get_comment_status() );
} else {
$this->assertEquals( 'closed', $item->get_comment_status() );
}
}
}
}

View File

@ -163,8 +163,8 @@ class TestUtilities extends TAINACAN_UnitTestCase {
$text_multiple_links_response = $text->make_clickable_links($text_multiple_links);
$this->assertEquals($text_multiple_links_expected, $text_multiple_links_response);
$text_multiple_links = 'Lorem https://tainacan.org hahahahahahhttps://tainacan.org hahaha ';
$text_multiple_links_expected = 'Lorem <a href="https://tainacan.org" target="_blank" title="https://tainacan.org">https://tainacan.org</a> hahahahahah<a href="https://tainacan.org" target="_blank" title="https://tainacan.org">https://tainacan.org</a> hahaha ';
$text_multiple_links = 'Lorem <a href="https://www.tainacan.org" target="_blank" title="https://www.tainacan.org">https://www.tainacan.org</a> Lorem https://tainacan.org hahahahahahhttps://tainacan.org hahaha ';
$text_multiple_links_expected = 'Lorem <a href="https://www.tainacan.org" target="_blank" title="https://www.tainacan.org">https://www.tainacan.org</a> Lorem <a href="https://tainacan.org" target="_blank" title="https://tainacan.org">https://tainacan.org</a> hahahahahah<a href="https://tainacan.org" target="_blank" title="https://tainacan.org">https://tainacan.org</a> hahaha ';
$text_multiple_links_response = $text->make_clickable_links($text_multiple_links);
$this->assertEquals($text_multiple_links_expected, $text_multiple_links_response);