Merge branch 'develop' of https://github.com/tainacan/tainacan into develop
This commit is contained in:
commit
19b8aa4992
|
@ -166,7 +166,7 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
|
|||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function prepare_meta($mapped, $request, $query, $mapped_v, $args){
|
||||
private function prepare_meta($mapped, $request, $query, $mapped_v, $args){
|
||||
$request_meta_query = $request[$mapped];
|
||||
|
||||
// If is a multidimensional array (array of array)
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
use Tainacan\Field_Types;
|
||||
|
||||
class TAINACAN_REST_Field_Types_Controller extends TAINACAN_REST_Controller {
|
||||
private $field_type;
|
||||
|
||||
/**
|
||||
* TAINACAN_REST_Field_Types_Controller constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->namespace = 'tainacan/v2';
|
||||
$this->rest_base = 'field-types';
|
||||
|
||||
add_action('rest_api_init', array($this, 'register_routes'));
|
||||
}
|
||||
|
||||
public function register_routes() {
|
||||
register_rest_route($this->namespace, '/' . $this->rest_base,
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array($this, 'get_items'),
|
||||
'permission_callback' => array($this, 'get_items_permissions_check')
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $item
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return mixed|WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
$name = "\Tainacan\Field_Types\\$item";
|
||||
$field_type = new $name();
|
||||
|
||||
$field_arr = $field_type->__toArray();
|
||||
$field_arr['name'] = $item;
|
||||
|
||||
return $field_arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
global $Tainacan_Fields;
|
||||
|
||||
$field_types = $Tainacan_Fields->fetch_field_types('NAME');
|
||||
|
||||
$prepared = [];
|
||||
foreach ($field_types as $field_type){
|
||||
array_push($prepared, $this->prepare_item_for_response($field_type, $request));
|
||||
}
|
||||
|
||||
return new WP_REST_Response($prepared, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -147,8 +147,6 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
$body = json_decode($request->get_body(), true);
|
||||
|
||||
$metadata = $this->field_repository->fetch($request['field_id']);
|
||||
$collection = $this->collection_repository->fetch($request['collection_id']);
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ $rest_terms_controller = new TAINACAN_REST_Terms_Controller();
|
|||
$rest_filters_controller = new TAINACAN_REST_Filters_Controller();
|
||||
$rest_item_metadata_controller = new TAINACAN_REST_Item_Metadata_Controller();
|
||||
$rest_logs_controller = new TAINACAN_REST_Logs_Controller();
|
||||
$rest_field_types_controller = new TAINACAN_REST_Field_Types_Controller();
|
||||
// Add here other endpoints imports
|
||||
|
||||
?>
|
|
@ -25,7 +25,7 @@ class Core_Description extends Field_Type {
|
|||
|
||||
public function render( $itemMetadata ){
|
||||
return '<tainacan-textarea
|
||||
id="tainacan-textarea-' . $itemMetadata->get_item()->get_slug() . '"
|
||||
id="tainacan-textarea-' . $itemMetadata->get_item()->WP_Post->post_name . '"
|
||||
field_id ="'.$itemMetadata->get_field()->get_id().'"
|
||||
item_id="'.$itemMetadata->get_item()->get_id().'"
|
||||
value=\''.json_encode( $itemMetadata->get_value() ).'\'
|
||||
|
|
|
@ -25,7 +25,7 @@ class Core_Title extends Field_Type {
|
|||
|
||||
public function render( $itemMetadata ){
|
||||
return '<tainacan-text
|
||||
id="tainacan-text-' . $itemMetadata->get_item()->get_slug() . '"
|
||||
id="tainacan-text-' . $itemMetadata->get_item()->WP_Post->post_name . '"
|
||||
field_id ="'.$itemMetadata->get_field()->get_id().'"
|
||||
item_id="'.$itemMetadata->get_item()->get_id().'"
|
||||
value=\''.json_encode( $itemMetadata->get_value() ).'\'
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
<template>
|
||||
<div class="block">
|
||||
<div v-if="type === 'date'" class="columns">
|
||||
<b-datepicker
|
||||
class="column"
|
||||
v-model="date_init"
|
||||
@input="validate_values()"
|
||||
icon="calendar-today">
|
||||
</b-datepicker>
|
||||
<b-datepicker
|
||||
class="column"
|
||||
v-model="date_end"
|
||||
@input="validate_values()"
|
||||
icon="calendar-today">
|
||||
</b-datepicker>
|
||||
<div class="column">
|
||||
<a class="button is-small" @click="emit">
|
||||
<b-icon icon="send"></b-icon>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="columns" v-else>
|
||||
<b-input
|
||||
type="number"
|
||||
@input="validate_values()"
|
||||
class="column"
|
||||
v-model="value_init">
|
||||
</b-input>
|
||||
<b-input
|
||||
type="number"
|
||||
@input="validate_values()"
|
||||
class="column"
|
||||
v-model="value_end">
|
||||
</b-input>
|
||||
<div class="column is-one-fifth">
|
||||
<a class="button is-small" @click="emit">
|
||||
<b-icon icon="send"></b-icon>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
created(){
|
||||
if( this.typeRange ) {
|
||||
this.type = this.typeRange;
|
||||
}
|
||||
},
|
||||
data(){
|
||||
return {
|
||||
value_init: 0,
|
||||
value_end: 0,
|
||||
date_init: new Date,
|
||||
date_end: new Date,
|
||||
type: 'date'
|
||||
}
|
||||
},
|
||||
props: {
|
||||
filter: {
|
||||
type: Object // concentrate all attributes field id and type
|
||||
},
|
||||
field_id: [Number], // not required, but overrides the filter field id if is set
|
||||
typeRange: [String], // not required, but overrides the filter field type if is set
|
||||
id: ''
|
||||
},
|
||||
methods: {
|
||||
// only validate if the first value is higher than first
|
||||
validate_values(){
|
||||
if( this.type === 'date' ){
|
||||
if ( this.date_init > this.date_end ) {
|
||||
let result = this.date_init;
|
||||
result.setDate(result.getDate() + 1);
|
||||
this.date_end = result;
|
||||
this.error_message();
|
||||
}
|
||||
} else {
|
||||
if ( parseFloat( this.value_init ) > parseFloat( this.value_end )) {
|
||||
this.value_end = parseFloat( this.value_init ) + 1;
|
||||
this.error_message();
|
||||
}
|
||||
}
|
||||
},
|
||||
// emit the operation for component listener
|
||||
emit(){
|
||||
if( this.type === 'date' ){
|
||||
this.$emit('input', {
|
||||
filter: 'range',
|
||||
type: 'date',
|
||||
field_id: ( this.field_id ) ? this.field_id : this.filter.field,
|
||||
values: [ this.date_init, this.date_end ]
|
||||
});
|
||||
} else {
|
||||
this.$emit('input', {
|
||||
filter: 'range',
|
||||
type: 'numeric',
|
||||
field_id: ( this.field_id ) ? this.field_id : this.filter.field,
|
||||
values: [ this.value_init, this.value_end ]
|
||||
});
|
||||
}
|
||||
},
|
||||
// message for error
|
||||
error_message(){
|
||||
this.$toast.open({
|
||||
duration: 3000,
|
||||
message: `First value should be lower than second value`,
|
||||
position: 'is-bottom',
|
||||
type: 'is-danger'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
|
@ -19,6 +19,8 @@ class Range extends Filter_Type {
|
|||
* @return string
|
||||
*/
|
||||
public function render( $filter ){
|
||||
return '<tainacan-filter-range name="'.$filter->get_name().'"></tainacan-filter-range>';
|
||||
return '<tainacan-filter-range
|
||||
name="'.$filter->get_name().'"
|
||||
field_id="'.$filter->get_field()->get_id().'"></tainacan-filter-range>';
|
||||
}
|
||||
}
|
|
@ -10,15 +10,15 @@ class Selectbox extends Filter_Type {
|
|||
|
||||
function __construct(){
|
||||
parent::set_supported_types(['string']);
|
||||
$this->component = 'tainacan-filter-list';
|
||||
$this->component = 'tainacan-filter-selectbox';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $field
|
||||
* @param $filter
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function render( $filter ){
|
||||
return '<tainacan-filter-list name="'.$filter->get_name().'"></tainacan-filter-list>';
|
||||
return '<tainacan-filter-selectbox name="'.$filter->get_name().'"></tainacan-filter-selectbox>';
|
||||
}
|
||||
}
|
|
@ -301,7 +301,7 @@ class Fields extends Repository {
|
|||
* @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
|
||||
* @return array Entities\Field
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function fetch_by_collection(Entities\Collection $collection, $args = [], $output = null){
|
||||
|
@ -444,11 +444,13 @@ class Fields extends Repository {
|
|||
return $this->field_types;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Entities\Collection $collection
|
||||
* @return array
|
||||
* @throws \ErrorException
|
||||
*/
|
||||
/**
|
||||
* @param Entities\Collection $collection
|
||||
*
|
||||
* @return bool
|
||||
* @throws \ErrorException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function register_core_fields( Entities\Collection $collection ){
|
||||
|
||||
$fields = $this->get_core_fields( $collection );
|
||||
|
@ -493,14 +495,15 @@ class Fields extends Repository {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* block user from remove core fields
|
||||
*
|
||||
* @param $before wordpress pass a null value
|
||||
* @param $post the post which is moving to trash
|
||||
* @return null/bool
|
||||
* @throws \ErrorException
|
||||
*/
|
||||
/**
|
||||
* block user from remove core fields
|
||||
*
|
||||
* @param $before wordpress pass a null value
|
||||
* @param $post the post which is moving to trash
|
||||
*
|
||||
* @return null/bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function disable_delete_core_fields( $before, $post ){
|
||||
$field = $this->fetch( $post->ID );
|
||||
|
||||
|
@ -509,15 +512,17 @@ class Fields extends Repository {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* block user from remove core fields ( if use wp_delete_post)
|
||||
*
|
||||
* @param $before wordpress pass a null value
|
||||
* @param $post the post which is deleting
|
||||
* @param $force_delete a boolean that force the deleting
|
||||
* @return null /bool
|
||||
* @internal param The $post_id post ID which is deleting
|
||||
*/
|
||||
/**
|
||||
* block user from remove core fields ( if use wp_delete_post)
|
||||
*
|
||||
* @param $before wordpress pass a null value
|
||||
* @param $post the post which is deleting
|
||||
* @param $force_delete a boolean that force the deleting
|
||||
*
|
||||
* @return null /bool
|
||||
* @throws \Exception
|
||||
* @internal param The $post_id post ID which is deleting
|
||||
*/
|
||||
public function force_delete_core_fields( $before, $post, $force_delete ){
|
||||
$field = $this->fetch( $post->ID );
|
||||
|
||||
|
@ -526,12 +531,14 @@ class Fields extends Repository {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns all core items from a specific collection
|
||||
*
|
||||
* @param Entities\Collection $collection
|
||||
* @return Array|\WP_Query
|
||||
*/
|
||||
/**
|
||||
* returns all core items from a specific collection
|
||||
*
|
||||
* @param Entities\Collection $collection
|
||||
*
|
||||
* @return Array|\WP_Query
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function get_core_fields( Entities\Collection $collection ){
|
||||
$args = [];
|
||||
|
||||
|
@ -553,13 +560,15 @@ class Fields extends Repository {
|
|||
return $this->fetch( $args, 'OBJECT' );
|
||||
}
|
||||
|
||||
/**
|
||||
* create a field entity and insert by an associative array ( attribute => value )
|
||||
*
|
||||
* @param Array $data the array of attributes to insert a field
|
||||
* @return int the field id inserted
|
||||
* @throws \ErrorException
|
||||
*/
|
||||
/**
|
||||
* create a field entity and insert by an associative array ( attribute => value )
|
||||
*
|
||||
* @param Array $data the array of attributes to insert a field
|
||||
*
|
||||
* @return int the field id inserted
|
||||
* @throws \ErrorException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function insert_array_field( $data ){
|
||||
$field = new Entities\Field();
|
||||
foreach ( $data as $attribute => $value ) {
|
||||
|
|
|
@ -186,6 +186,12 @@ class DevInterface {
|
|||
<?php Helpers\HtmlHelpers::collections_dropdown( $value ); ?>
|
||||
<?php elseif ($prop == 'collections_ids'): ?>
|
||||
<?php Helpers\HtmlHelpers::collections_checkbox_list( $value ); ?>
|
||||
<?php elseif ($prop == 'field'): ?>
|
||||
<?php Helpers\HtmlHelpers::metadata_dropdown(
|
||||
$entity->get_collection_id(),
|
||||
( isset( $value ) ) ? $value : '',
|
||||
'tnc_prop_field'
|
||||
) ?>
|
||||
<?php elseif ($prop == 'field_type_options' || $prop == 'filter_type_options'): ?>
|
||||
<?php echo $value; ?>
|
||||
<?php elseif ($prop == 'field_type'): ?>
|
||||
|
@ -195,8 +201,8 @@ class DevInterface {
|
|||
<?php else: ?>
|
||||
<textarea name="tnc_prop_<?php echo $prop; ?>"><?php echo htmlspecialchars($value); ?></textarea>
|
||||
<?php endif; ?>
|
||||
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
@ -388,6 +394,11 @@ class DevInterface {
|
|||
$class = ( class_exists( $selected ) ) ? new $selected() : '';
|
||||
|
||||
if(is_object( $class )){
|
||||
$filter = $Tainacan_Filters->fetch( $id );
|
||||
if ( $filter ) {
|
||||
echo '<h3>Exemplo:</h3>';
|
||||
echo $class->render( $filter );
|
||||
}
|
||||
$selected = str_replace('Tainacan\Filter_Types\\','', get_class( $class ) );
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
import Vue from 'vue';
|
||||
|
||||
export const eventFilterBus = new Vue({
|
||||
data: {
|
||||
componentsTag: [],
|
||||
errors : [],
|
||||
query: {}
|
||||
},
|
||||
created(){
|
||||
this.$on('input', data => this.search(data) );
|
||||
},
|
||||
methods: {
|
||||
search( ){
|
||||
console.log( data );
|
||||
},
|
||||
|
||||
/* Dev interfaces methods */
|
||||
|
||||
registerComponent( name ){
|
||||
if (this.componentsTag.indexOf(name) < 0) {
|
||||
this.componentsTag.push( name );
|
||||
}
|
||||
},
|
||||
getAllComponents(){
|
||||
const components = [];
|
||||
for( let component of this.componentsTag ){
|
||||
const eventElements = document.getElementsByTagName( component );
|
||||
if( eventElements ) {
|
||||
for (let eventElement of eventElements){
|
||||
components.push( eventElement );
|
||||
}
|
||||
}
|
||||
}
|
||||
return components;
|
||||
},
|
||||
listener(){
|
||||
const components = this.getAllComponents();
|
||||
for (let eventElement of components){
|
||||
eventElement.addEventListener('input', (event) => {
|
||||
console.log( event.detail, 'dev' );
|
||||
});
|
||||
}
|
||||
},
|
||||
}
|
||||
});
|
|
@ -3,6 +3,7 @@ import Vue from 'vue'
|
|||
// include vue-custom-element plugin to Vue
|
||||
import VueCustomElement from 'vue-custom-element';
|
||||
import { eventBus } from './event-bus-web-components';
|
||||
import { eventFilterBus } from './event-bus-filters';
|
||||
import Buefy from 'buefy'
|
||||
|
||||
|
||||
|
@ -19,6 +20,7 @@ import Numeric from '../classes/field-types/numeric/Numeric.vue';
|
|||
import Date from '../classes/field-types/date/Date.vue';
|
||||
import Relationship from '../classes/field-types/relationship/Relationship.vue';
|
||||
|
||||
import FilterRange from '../classes/filter-types/range/Range.vue';
|
||||
|
||||
Vue.customElement('tainacan-text', Text);
|
||||
eventBus.registerComponent( 'tainacan-text' );
|
||||
|
@ -44,4 +46,11 @@ eventBus.registerComponent( 'tainacan-date' );
|
|||
Vue.customElement('tainacan-relationship', Relationship);
|
||||
eventBus.registerComponent( 'tainacan-relationship' );
|
||||
|
||||
eventBus.listener();
|
||||
eventBus.listener();
|
||||
|
||||
/* Filters */
|
||||
|
||||
Vue.customElement('tainacan-filter-range', FilterRange);
|
||||
eventFilterBus.registerComponent( 'tainacan-filter-range' );
|
||||
|
||||
eventFilterBus.listener();
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
import axios from '../../../axios/axios';
|
||||
import qs from 'qs';
|
||||
|
||||
export const do_query = ({ commit, state }) => {
|
||||
return new Promise((resolve, reject) =>{
|
||||
axios.get('/collections/' + state.collection + '/items?' + qs.stringify( state.query ))
|
||||
.then(res => {
|
||||
|
||||
})
|
||||
.catch(error => {
|
||||
|
||||
})
|
||||
});
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
export const getQuery = state => {
|
||||
return state.query;
|
||||
}
|
||||
|
||||
export const getCollection = state => {
|
||||
return state.collection;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
import * as actions from './actions';
|
||||
import * as getters from './getters';
|
||||
import * as mutations from './mutations';
|
||||
|
||||
const state = {
|
||||
query: {},
|
||||
collection: null
|
||||
};
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state,
|
||||
mutations,
|
||||
actions,
|
||||
getters
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
export const setQuery = ( state, query ) => {
|
||||
state.query = query;
|
||||
}
|
||||
|
||||
export const setCollection = ( state, collection ) => {
|
||||
state.query = collection;
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Tests;
|
||||
|
||||
/**
|
||||
* @group api
|
||||
*/
|
||||
class TAINACAN_REST_Field_Types_Controller extends TAINACAN_UnitApiTestCase {
|
||||
|
||||
public function test_get_field_types(){
|
||||
|
||||
$ftype_request = new \WP_REST_Request('GET', $this->namespace . '/field-types');
|
||||
|
||||
$ftype_response = $this->server->dispatch($ftype_request);
|
||||
|
||||
$data = $ftype_response->get_data();
|
||||
|
||||
global $Tainacan_Fields;
|
||||
|
||||
$field_types = $Tainacan_Fields->fetch_field_types('NAME');
|
||||
|
||||
$this->assertEquals(count($field_types), count($data));
|
||||
|
||||
foreach ($data as $ftype){
|
||||
$this->assertContains($ftype['name'], $field_types);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue