Merge branch 'develop' of https://github.com/tainacan/tainacan into develop
This commit is contained in:
commit
74bc25e682
|
@ -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
|
||||
|
|
|
@ -25,6 +25,7 @@ class Core_Description extends Field_Type {
|
|||
|
||||
public function render( $itemMetadata ){
|
||||
return '<tainacan-textarea
|
||||
id="tainacan-textarea-' . $itemMetadata->get_item()->get_slug() . '"
|
||||
field_id ="'.$itemMetadata->get_field()->get_id().'"
|
||||
item_id="'.$itemMetadata->get_item()->get_id().'"
|
||||
value=\''.json_encode( $itemMetadata->get_value() ).'\'
|
||||
|
|
|
@ -24,7 +24,9 @@ class Core_Title extends Field_Type {
|
|||
*/
|
||||
|
||||
public function render( $itemMetadata ){
|
||||
return '<tainacan-text field_id ="'.$itemMetadata->get_field()->get_id().'"
|
||||
return '<tainacan-text
|
||||
id="tainacan-text-' . $itemMetadata->get_item()->get_slug() . '"
|
||||
field_id ="'.$itemMetadata->get_field()->get_id().'"
|
||||
item_id="'.$itemMetadata->get_item()->get_id().'"
|
||||
value=\''.json_encode( $itemMetadata->get_value() ).'\'
|
||||
name="'.$itemMetadata->get_field()->get_name().'"></tainacan-text>';
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue