From b23544d248600cc4c9f9dd1d5c1c76a10e8aef2a Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Sat, 28 Jul 2018 21:14:48 -0300 Subject: [PATCH 1/5] Integration with ElasticPress #90 --- src/classes/class-tainacan-elastic-press.php | 87 +++++++++++++++++++ .../class-tainacan-collections.php | 2 + .../repositories/class-tainacan-filters.php | 2 + .../repositories/class-tainacan-items.php | 2 + .../repositories/class-tainacan-logs.php | 2 + .../repositories/class-tainacan-metadata.php | 20 +++++ .../class-tainacan-repository.php | 8 ++ .../class-tainacan-taxonomies.php | 4 +- src/classes/tainacan-creator.php | 1 + 9 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 src/classes/class-tainacan-elastic-press.php diff --git a/src/classes/class-tainacan-elastic-press.php b/src/classes/class-tainacan-elastic-press.php new file mode 100644 index 000000000..880148473 --- /dev/null +++ b/src/classes/class-tainacan-elastic-press.php @@ -0,0 +1,87 @@ +add_items_args($args); + } + + return $args; + } + + private function add_items_args($args) { + + $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + + if (is_array($args['post_type']) && isset($args['s']) && !empty($args['s'])) { + + $meta_ids = []; + $taxonomies = []; + + foreach ( $args['post_type'] as $cpt ) { + + $col = $Tainacan_Collections->fetch_by_db_identifier($cpt); + + $taxonomies = array_merge( $taxonomies, get_object_taxonomies($cpt) ); + + if ($col) { + + $metadata = $Tainacan_Metadata->fetch_by_collection($col, ['posts_per_page' => -1], 'OBJECT'); + + foreach ($metadata as $meta) { + $meta_ids[] = $meta->get_id(); + } + } + + } + + $search_fields = [ + 'post_title', + 'post_content', + 'post_excerpt' + ]; + + if (!empty($meta_ids)) { + $search_fields['meta'] = array_unique($meta_ids); + } + if (!empty($taxonomies)) { + $search_fields['taxonomies'] = array_unique($taxonomies); + } + + $args['search_fields'] = $search_fields; + + } + + return $args; + + } + + + +} // END diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index dccbe88fe..79d7dbb43 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -336,6 +336,8 @@ class Collections extends Repository { // TODO: Pegar coleções registradas via código + $args = apply_filters('tainacan_fetch_args', $args, 'collections'); + $wp_query = new \WP_Query( $args ); return $this->fetch_output( $wp_query, $output ); diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index c59ee13ea..4f2bd20fe 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -248,6 +248,8 @@ class Filters extends Repository { $args = $this->parse_fetch_args($args); $args['post_type'] = Entities\Filter::get_post_type(); + + $args = apply_filters('tainacan_fetch_args', $args, 'filters'); $wp_query = new \WP_Query($args); return $this->fetch_output($wp_query, $output); diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index 65387ff9c..47b5ad855 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -252,6 +252,8 @@ class Items extends Repository { $args['post_type'] = $cpt; + $args = apply_filters('tainacan_fetch_args', $args, 'items'); + $wp_query = new \WP_Query( $args ); return $this->fetch_output( $wp_query, $output ); diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 6a9e0e402..c58740dc8 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -200,6 +200,8 @@ class Logs extends Repository { $args['post_type'] = Entities\Log::get_post_type(); + $args = apply_filters('tainacan_fetch_args', $args, 'logs'); + $wp_query = new \WP_Query( $args ); return $this->fetch_output( $wp_query, $output ); diff --git a/src/classes/repositories/class-tainacan-metadata.php b/src/classes/repositories/class-tainacan-metadata.php index bdeb61da0..90a2b4335 100644 --- a/src/classes/repositories/class-tainacan-metadata.php +++ b/src/classes/repositories/class-tainacan-metadata.php @@ -324,11 +324,31 @@ class Metadata extends Repository { $args = $this->parse_fetch_args($args); $args['post_type'] = Entities\Metadatum::get_post_type(); + + $args = apply_filters('tainacan_fetch_args', $args, 'metadata'); $wp_query = new \WP_Query($args); return $this->fetch_output($wp_query, $output); } } + + /** + * fetch metadata IDs based on WP_Query args + * + * to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/) + * You can also use a mapped property, such as name and description, as an argument and it will be mapped to the + * appropriate WP_Query argument + * + * @param array $args WP_Query args || int $args the item id + * + * @return Array array of IDs; + */ + public function fetch_ids( $args = [] ) { + + $args['fields'] = 'ids'; + + return $this->fetch( $args )->get_posts(); + } /** * fetch metadatum by collection, considering inheritance diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 74dbeb70c..34d784210 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -333,6 +333,14 @@ abstract class Repository { } $args['meta_query'] = $meta_query; + + // Map orderby parameter + if ( isset($args['orderby']) ) { + if ( array_key_exists($args['orderby'], $map) ) { + $args['orderby'] = $map[ $args['orderby'] ]['map']; + } + } + return $args; diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index 1e00e2446..7d6c43faf 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -172,7 +172,9 @@ class Taxonomies extends Repository { $args['post_type'] = Entities\Taxonomy::get_post_type(); - $wp_query = new \WP_Query($args); + $args = apply_filters('tainacan_fetch_args', $args, 'taxonomies'); + + $wp_query = new \WP_Query($args); return $this->fetch_output($wp_query, $output); } } diff --git a/src/classes/tainacan-creator.php b/src/classes/tainacan-creator.php index 2de0d255d..527e1fdea 100644 --- a/src/classes/tainacan-creator.php +++ b/src/classes/tainacan-creator.php @@ -136,5 +136,6 @@ require_once(__DIR__ . '/../theme-helper/template-tags.php'); $Tainacan_Theme_Helper = \Tainacan\Theme_Helper::get_instance(); $Tainacan_Search_Engine = new \Tainacan\Search_Engine(); +$Tainacan_Elastic_press = new \Tainacan\Elastic_Press(); ?> From 78497d66ba62c2e9ad251480fb17a91678dbcc1c Mon Sep 17 00:00:00 2001 From: eduardohumberto Date: Sun, 29 Jul 2018 22:05:38 -0300 Subject: [PATCH 2/5] csv importer - create option for file path in server --- src/importer/class-tainacan-csv.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/importer/class-tainacan-csv.php b/src/importer/class-tainacan-csv.php index dc4b83027..8668b1aff 100644 --- a/src/importer/class-tainacan-csv.php +++ b/src/importer/class-tainacan-csv.php @@ -172,7 +172,7 @@ class CSV extends Importer { $form .= ''; $form .= ''; - $form = '
'; + $form .= '
'; $form .= ''; $form .= '
'; $form .= ''; @@ -210,6 +210,13 @@ class CSV extends Importer { $form .= '
'; $form .= '
'; + $form .= '
'; + $form .= ''; + $form .= '
'; + $form .= ''; + $form .= '
'; + $form .= '
'; + return $form; } From 7fd2138485536bda7b51bc257b5bcc874b0d8b4b Mon Sep 17 00:00:00 2001 From: eduardohumberto Date: Mon, 30 Jul 2018 21:57:01 -0300 Subject: [PATCH 3/5] csv importer - create tests for different document types and fix index error on process item --- src/importer/class-tainacan-csv.php | 14 ++++++++++---- tests/test-importer.php | 12 +++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/importer/class-tainacan-csv.php b/src/importer/class-tainacan-csv.php index 8668b1aff..5decabd0d 100644 --- a/src/importer/class-tainacan-csv.php +++ b/src/importer/class-tainacan-csv.php @@ -106,14 +106,14 @@ class CSV extends Importer { foreach ( $headers as $indexRaw => $headerRaw ) { if( $headerRaw === $header ){ - $index = $indexRaw; + $column = $indexRaw; } } - if(!isset($index)) + if(!isset($column)) continue; - $valueToInsert = $this->handle_encoding( $values[ $index ] ); + $valueToInsert = $this->handle_encoding( $values[ $column ] ); $processedItem[ $header ] = ( $metadatum->is_multiple() ) ? explode( $this->get_option('multivalued_delimiter'), $valueToInsert) : $valueToInsert; @@ -137,8 +137,14 @@ class CSV extends Importer { $file = new \SplFileObject( $this->tmp_file, 'r' ); $file->setFlags(\SplFileObject::SKIP_EMPTY); $file->seek( $index ); - $values = str_getcsv( rtrim($file->fgets()), $this->get_option('delimiter'), $this->get_option('enclosure') ); + if( $index === 0 ){ + $file->current(); + $file->next(); + } + + $values = str_getcsv( rtrim($file->fgets()), $this->get_option('delimiter'), $this->get_option('enclosure') ); + if( is_array($values) && !empty($column_document) ){ $this->handle_document( $values[$column_document], $inserted_item); } diff --git a/tests/test-importer.php b/tests/test-importer.php index 498c9504a..9632c0577 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -483,6 +483,16 @@ class ImporterTests extends TAINACAN_UnitTestCase { $_SESSION['tainacan_importer'][$id]->add_collection( $collection_definition ); $_SESSION['tainacan_importer'][$id]->set_option('encode','iso88591'); - $this->assertEquals(1, $_SESSION['tainacan_importer'][$id]->run(), 'first step should import 1 item'); + while($_SESSION['tainacan_importer'][$id]->run()){ + continue; + } + + $items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' ); + + $this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_source_number_of_items(), count( $items ) ); + + // test row 5 + $document_id = $items[0]->get_document(); + $this->assertFalse( is_numeric($document_id) ); } } From 0b3ca743fcd7bfad7091f6d4331a62fdda3fa77e Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Tue, 31 Jul 2018 09:12:32 -0300 Subject: [PATCH 4/5] Bettter spacing for filter and metadata edition forms. --- .../edition/filter-edition-form.vue | 8 ++--- .../edition/metadatum-edition-form.vue | 18 +++++----- src/admin/scss/_tainacan-form.scss | 6 ++++ src/admin/tainacan-admin-i18n.php | 1 + src/admin/theme-items-list.vue | 34 +++++++++++++++++++ .../relationship/FormRelationship.vue | 5 ++- 6 files changed, 56 insertions(+), 16 deletions(-) diff --git a/src/admin/components/edition/filter-edition-form.vue b/src/admin/components/edition/filter-edition-form.vue index 8b7da97db..6bde1a048 100644 --- a/src/admin/components/edition/filter-edition-form.vue +++ b/src/admin/components/edition/filter-edition-form.vue @@ -7,7 +7,7 @@ :addons="false" :type="formErrors['name'] != undefined ? 'is-danger' : ''" :message="formErrors['name'] != undefined ? formErrors['name'] : ''"> -