diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index 6618972b5..f7d1d4503 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -254,7 +254,16 @@ class Collection extends Entity { function get_fields_order() { return $this->get_mapped_property('fields_order'); } - + + /** + * Get collection filters ordination + * + * @return string + */ + function get_filters_order() { + return $this->get_mapped_property('filters_order'); + } + /** * Get collection moderators ids * @@ -394,6 +403,16 @@ class Collection extends Entity { function set_fields_order($value) { $this->set_mapped_property('fields_order', $value); } + + /** + * Set collection filters ordination + * + * @param [string] $value + * @return void + */ + function set_filters_order($value) { + $this->set_mapped_property('filters_order', $value); + } /** * Set collection moderators ids diff --git a/src/classes/field-types/core-description/class-tainacan-core-description.php b/src/classes/field-types/core-description/class-tainacan-core-description.php index e2fe53c42..2fdfd093d 100644 --- a/src/classes/field-types/core-description/class-tainacan-core-description.php +++ b/src/classes/field-types/core-description/class-tainacan-core-description.php @@ -25,6 +25,7 @@ class Core_Description extends Field_Type { public function render( $itemMetadata ){ return 'get_value() ).'\' diff --git a/src/classes/field-types/core-title/class-tainacan-core-title.php b/src/classes/field-types/core-title/class-tainacan-core-title.php index b9982e5fd..73401d5b9 100644 --- a/src/classes/field-types/core-title/class-tainacan-core-title.php +++ b/src/classes/field-types/core-title/class-tainacan-core-title.php @@ -24,7 +24,9 @@ class Core_Title extends Field_Type { */ public function render( $itemMetadata ){ - return 'get_value() ).'\' name="'.$itemMetadata->get_field()->get_name().'">'; diff --git a/src/classes/filter-types/class-tainacan-filter-type.php b/src/classes/filter-types/class-tainacan-filter-type.php index 387e02ef8..953d98c70 100644 --- a/src/classes/filter-types/class-tainacan-filter-type.php +++ b/src/classes/filter-types/class-tainacan-filter-type.php @@ -11,10 +11,18 @@ abstract class Filter_Type { abstract function render( $field ); + /** + * @return array Supported types by the filter + */ public function get_supported_types(){ return $this->supported_types; } + /** + * specifies the types supported for the filter + * + * @param array $supported_types the types supported + */ public function set_supported_types($supported_types){ $this->supported_types = $supported_types; } diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index 0440c0fa0..5fa709c98 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -138,6 +138,13 @@ class Collections extends Repository { 'description'=> __('Collection fields ordination', 'tainacan'), //'validation' => v::stringType(), ], + 'filters_order' => [ + 'map' => 'meta', + 'title' => __('Ordination filters', 'tainacan'), + 'type' => 'string', + 'description'=> __('Collection filters ordination', 'tainacan'), + //'validation' => v::stringType(), + ], /* Isnt it just post status private? diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index dc6e9dc2a..b2b44c4c5 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -252,4 +252,106 @@ class Filters extends Repository { return $supported_filter_types; } + + /** + * fetch filters by collection, searches all filters available + * + * @param Entities\Collection $collection + * @param array $args WP_Query args plus disabled_fields + * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) + * + * @return Array Entities\Field + * @throws \Exception + */ + public function fetch_by_collection(Entities\Collection $collection, $args = [], $output = null){ + $collection_id = $collection->get_id(); + + //get parent collections + $parents = get_post_ancestors( $collection_id ); + + //insert the actual collection + $parents[] = $collection_id; + + //search for default field + $parents[] = $this->get_default_metadata_attribute(); + + $meta_query = array( + 'key' => 'collection_id', + 'value' => $parents, + 'compare' => 'IN', + ); + + if( isset( $args['meta_query'] ) ){ + $args['meta_query'][] = $meta_query; + }else{ + $args['meta_query'] = array( $meta_query ); + } + + return $this->order_result( + $this->fetch( $args, $output ), + $collection, + isset( $args['disabled_fields'] ) ? $args['disabled_fields'] : false + ); + } + + /** + * Ordinate the result from fetch response if $collection has an ordination, + * filters not ordinated appear on the end of the list + * + * + * @param $result Response from method fetch + * @param Entities\Collection $collection + * @return array or WP_Query ordinate + */ + public function order_result( $result, Entities\Collection $collection ){ + $order = $collection->get_filters_order(); + if($order) { + $order = ( is_array($order) ) ? $order : unserialize($order); + + if ( is_array($result) ){ + $result_ordinate = []; + $not_ordinate = []; + + foreach ( $result as $item ) { + $id = $item->WP_Post->ID; + $index = array_search ( $id , array_column( $order , 'id') ); + + if( $index !== false ) { + $result_ordinate[$index] = $item; + } else { + $not_ordinate[] = $item; + } + } + + ksort ( $result_ordinate ); + $result_ordinate = array_merge( $result_ordinate, $not_ordinate ); + + return $result_ordinate; + } + // if the result is a wp query object + else { + $posts = $result->posts; + $result_ordinate = []; + $not_ordinate = []; + + foreach ( $posts as $item ) { + $id = $item->ID; + $index = array_search ( $id , array_column( $order , 'id') ); + + if( $index !== false ){ + $result_ordinate[$index] = $item; + } else { + $not_ordinate[] = $item; + } + } + + ksort ( $result_ordinate ); + $result->posts = $result_ordinate; + $result->posts = array_merge( $result->posts, $not_ordinate ); + + return $result; + } + } + return $result; + } } \ No newline at end of file