Merge branch 'develop' of https://github.com/tainacan/tainacan into develop

This commit is contained in:
mateuswetah 2018-02-01 14:52:27 -02:00
commit b24c08c471
33 changed files with 671 additions and 424 deletions

View File

@ -25,6 +25,7 @@ import Checkbox from '../../classes/field-types/checkbox/Checkbox.vue';
import Radio from '../../classes/field-types/radio/Radio.vue'; import Radio from '../../classes/field-types/radio/Radio.vue';
import Numeric from '../../classes/field-types/numeric/Numeric.vue'; import Numeric from '../../classes/field-types/numeric/Numeric.vue';
import Date from '../../classes/field-types/date/Date.vue'; import Date from '../../classes/field-types/date/Date.vue';
import Relationship from '../../classes/field-types/relationship/Relationship.vue';
import TaincanFormItem from '../../classes/field-types/tainacan-form-item.vue'; import TaincanFormItem from '../../classes/field-types/tainacan-form-item.vue';
@ -36,6 +37,7 @@ Vue.component('tainacan-checkbox', Checkbox);
Vue.component('tainacan-radio', Radio); Vue.component('tainacan-radio', Radio);
Vue.component('tainacan-numeric', Numeric); Vue.component('tainacan-numeric', Numeric);
Vue.component('tainacan-date', Date); Vue.component('tainacan-date', Date);
Vue.component('tainacan-relationship', Relationship);
Vue.component('tainacan-form-item', TaincanFormItem); Vue.component('tainacan-form-item', TaincanFormItem);

View File

@ -13,11 +13,20 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
$entity_prepared = [ $entity_prepared = [
'id' => $entity->get_id(), 'id' => $entity->get_id(),
'description' => $entity->get_description(), 'description' => $entity->get_description(),
'author_id' => $entity->get_author_id(),
'creation_date' => $entity->get_creation_date(),
'modification_date' => $entity->get_modification_date(),
]; ];
if(array_key_exists('modification_date', $map)){
$entity_prepared['modification_date'] = $entity->get_modification_date();
}
if(array_key_exists('creation_date', $map)){
$entity_prepared['creation_date'] = $entity->get_creation_date();
}
if(array_key_exists('author_id', $map)){
$entity_prepared['author_id'] = $entity->get_author_id();
}
if(array_key_exists('name', $map)){ if(array_key_exists('name', $map)){
$entity_prepared['name'] = $entity->get_name(); $entity_prepared['name'] = $entity->get_name();
} elseif(array_key_exists('title', $map)){ } elseif(array_key_exists('title', $map)){
@ -35,6 +44,24 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
return $entity_prepared; return $entity_prepared;
} }
/**
* @param $filters
* @param $map
*
* @return array
*/
protected function unmap_filters($filters, $map){
$unmapped = [];
if(!empty($filters)) {
foreach ( $filters as $filter => $value ) {
$unmapped[ $map[ $filter ]['map'] ] = $value;
}
}
return $unmapped;
}
} }
?> ?>

View File

@ -41,20 +41,20 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'), 'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'), 'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_item_schema() //'args' => $this->get_item_schema()
), ),
array( array(
'methods' => WP_REST_Server::CREATABLE, 'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'), 'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check'), 'permission_callback' => array($this, 'create_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE), //'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE),
), ),
)); ));
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<collection_id>[\d]+)', array( register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<collection_id>[\d]+)', array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'), 'callback' => array($this, 'get_item'),
'args' => $this->get_collection_params(), //'args' => $this->get_collection_params(),
'permission_callback' => array($this, 'get_item_permissions_check'), 'permission_callback' => array($this, 'get_item_permissions_check'),
), ),
array( array(
@ -78,7 +78,19 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
*/ */
public function get_items($request){ public function get_items($request){
$collections = $this->collections_repository->fetch(); $args = [];
$map = $this->collections_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$collections = $this->collections_repository->fetch($args);
$response = $this->prepare_item_for_response($collections, $request); $response = $this->prepare_item_for_response($collections, $request);

View File

@ -3,11 +3,12 @@
use Tainacan\Entities; use Tainacan\Entities;
use Tainacan\Repositories; use Tainacan\Repositories;
class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller { class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
private $field; private $field;
private $item_metadata_repository; private $item_metadata_repository;
private $item_repository; private $item_repository;
private $collection_repository; private $collection_repository;
private $field_repository;
public function __construct() { public function __construct() {
$this->namespace = 'tainacan/v2'; $this->namespace = 'tainacan/v2';
@ -22,7 +23,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
*/ */
public function init_objects() { public function init_objects() {
$this->field = new Entities\Field(); $this->field = new Entities\Field();
$this->metadata_repository = new Repositories\Fields(); $this->field_repository = new Repositories\Fields();
$this->item_metadata_repository = new Repositories\Item_Metadata(); $this->item_metadata_repository = new Repositories\Item_Metadata();
$this->item_repository = new Repositories\Items(); $this->item_repository = new Repositories\Items();
$this->collection_repository = new Repositories\Collections(); $this->collection_repository = new Repositories\Collections();
@ -58,7 +59,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'), 'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'), 'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_collection_params(), //'args' => $this->get_collection_params(),
), ),
array( array(
'methods' => WP_REST_Server::CREATABLE, 'methods' => WP_REST_Server::CREATABLE,
@ -103,7 +104,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
} }
if($this->field->validate()) { if($this->field->validate()) {
$this->metadata_repository->insert( $this->field ); $this->field_repository->insert( $this->field );
$items = $this->item_repository->fetch([], $collection_id, 'WP_Query'); $items = $this->item_repository->fetch([], $collection_id, 'WP_Query');
@ -173,9 +174,21 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
public function get_items( $request ) { public function get_items( $request ) {
$collection_id = $request['collection_id']; $collection_id = $request['collection_id'];
$args = [];
$map = $this->field_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$collection = new Entities\Collection($collection_id); $collection = new Entities\Collection($collection_id);
$collection_metadata = $this->metadata_repository->fetch_by_collection($collection, [], 'OBJECT'); $collection_metadata = $this->field_repository->fetch_by_collection($collection, $args, 'OBJECT');
$prepared_item = $this->prepare_item_for_response($collection_metadata, $request); $prepared_item = $this->prepare_item_for_response($collection_metadata, $request);
@ -259,9 +272,9 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
$attributes[$att] = $value; $attributes[$att] = $value;
} }
$field = $this->metadata_repository->fetch($field_id); $field = $this->field_repository->fetch($field_id);
$updated_metadata = $this->metadata_repository->update($field, $attributes); $updated_metadata = $this->field_repository->update($field, $attributes);
if(!($updated_metadata instanceof Entities\Field)){ if(!($updated_metadata instanceof Entities\Field)){
return new WP_REST_Response($updated_metadata, 400); return new WP_REST_Response($updated_metadata, 400);

View File

@ -207,13 +207,19 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
$body = json_decode($request->get_body(), true); $body = json_decode($request->get_body(), true);
if(!empty($body)){ if(!empty($body)){
$attributes = ['ID' => $filter_id]; $attributes = [];
foreach ($body as $att => $value){ foreach ($body as $att => $value){
$attributes[$att] = $value; $attributes[$att] = $value;
} }
$updated_filter = $this->filter_repository->update($attributes); $filter = $this->filter_repository->fetch($filter_id);
$updated_filter = $this->filter_repository->update($filter, $attributes);
if(!($updated_filter instanceof Entities\Filter)){
return new WP_REST_Response($updated_filter, 400);
}
return new WP_REST_Response($updated_filter->__toArray(), 200); return new WP_REST_Response($updated_filter->__toArray(), 200);
} }
@ -267,7 +273,19 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$filters = $this->filter_repository->fetch([], 'OBJECT'); $args = [];
$map = $this->filter_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$filters = $this->filter_repository->fetch($args, 'OBJECT');
$response = $this->prepare_item_for_response($filters, $request); $response = $this->prepare_item_for_response($filters, $request);

View File

@ -8,6 +8,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
private $item_metadata_repository; private $item_metadata_repository;
private $item_repository; private $item_repository;
private $collection_repository; private $collection_repository;
private $field_repository;
public function __construct() { public function __construct() {
$this->namespace = 'tainacan/v2'; $this->namespace = 'tainacan/v2';
@ -53,7 +54,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'), 'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'), 'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_collection_params(), //'args' => $this->get_collection_params(),
) )
) )
); );

View File

@ -47,15 +47,15 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'), 'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'), 'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_collection_params(), //'args' => $this->get_collection_params(),
), ),
array( array(
'methods' => WP_REST_Server::CREATABLE, 'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'), 'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check'), 'permission_callback' => array($this, 'create_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE), //'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE),
), ),
'schema' => array($this, 'get_public_item_schema'), //'schema' => array($this, 'get_public_item_schema'),
)); ));
register_rest_route( register_rest_route(
$this->namespace, '/' . $this->rest_base . '/(?P<item_id>[\d]+)', $this->namespace, '/' . $this->rest_base . '/(?P<item_id>[\d]+)',
@ -105,7 +105,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
return $items; return $items;
} elseif(!empty($item)){ } elseif(!empty($item)){
return $this->get_only_needed_attributes($item, $map); return $item->__toArray();
} }
return $item; return $item;
@ -132,8 +132,20 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$args = [];
$map = $this->items_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$collection_id = $request['collection_id']; $collection_id = $request['collection_id'];
$items = $this->items_repository->fetch([], $collection_id, 'WP_Query'); $items = $this->items_repository->fetch($args, $collection_id, 'WP_Query');
$response = $this->prepare_item_for_response($items, $request); $response = $this->prepare_item_for_response($items, $request);
@ -169,6 +181,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
* @param WP_REST_Request $request * @param WP_REST_Request $request
* *
* @return object|Entities\Item|WP_Error * @return object|Entities\Item|WP_Error
* @throws Exception
*/ */
public function prepare_item_for_database( $request ) { public function prepare_item_for_database( $request ) {
@ -206,6 +219,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
* @param WP_REST_Request $request * @param WP_REST_Request $request
* *
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
* @throws Exception
*/ */
public function create_item( $request ) { public function create_item( $request ) {
$collection_id = $request['collection_id']; $collection_id = $request['collection_id'];

View File

@ -0,0 +1,141 @@
<?php
use Tainacan\Entities;
use Tainacan\Repositories;
class TAINACAN_REST_Logs_Controller extends TAINACAN_REST_Controller {
private $logs_repository;
private $log;
/**
* TAINACAN_REST_Logs_Controller constructor.
*/
public function __construct() {
$this->namespace = 'tainacan/v2';
$this->rest_base = 'logs';
add_action('rest_api_init', array($this, 'register_routes'));
add_action('init', array($this, 'init_objects'));
}
public function init_objects(){
$this->logs_repository = new Repositories\Logs();
$this->log = new Entities\Log();
}
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'),
)
)
);
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<log_id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_item_permissions_check'),
)
)
);
}
/**
* @param mixed $item
* @param WP_REST_Request $request
*
* @return array|WP_Error|WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
$prepared = [];
$map = $this->logs_repository->get_map();
if(is_array($item)){
foreach ($item as $it){
$prepared[] = $this->get_only_needed_attributes($it, $map);
}
return $prepared;
}
return $item->__toArray();
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$args = [];
$map = $this->logs_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
$logs = $this->logs_repository->fetch($args, 'OBJECT');
if(!empty($logs)) {
$prepared_logs = $this->prepare_item_for_response( $logs, $request );
return new WP_REST_Response($prepared_logs, 200);
}
return new WP_REST_Response($logs, 200);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function get_items_permissions_check( $request ) {
return $this->logs_repository->can_read($this->log);
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
$log_id = $request['log_id'];
$log = $this->logs_repository->fetch($log_id);
if(!empty($log)) {
$prepared_log = $this->prepare_item_for_response( $log, $request );
return new WP_REST_Response($prepared_log, 200);
}
return new WP_REST_Response($log, 200);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function get_item_permissions_check( $request ) {
$log = $this->logs_repository->fetch($request['log_id']);
if($log instanceof Entities\Log){
return $log->can_read();
}
return false;
}
}
?>

View File

@ -62,6 +62,16 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_REST_Controller {
) )
) )
); );
register_rest_route(
$this->namespace, '/' . $this->rest_base . '/(?P<taxonomy_id>[\d]+)/collection/(?P<collection_id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check')
)
)
);
} }
/** /**
@ -194,7 +204,19 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$taxonomies = $this->taxonomy_repository->fetch([], 'OBJECT'); $args = [];
$map = $this->taxonomy_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$taxonomies = $this->taxonomy_repository->fetch($args, 'OBJECT');
$taxonomies_prepared = $this->prepare_item_for_response($taxonomies, $request); $taxonomies_prepared = $this->prepare_item_for_response($taxonomies, $request);
@ -259,14 +281,26 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_REST_Controller {
$body = json_decode($request->get_body(), true); $body = json_decode($request->get_body(), true);
if(!empty($body)){ if(!empty($body) || isset($request['collection_id'])){
$attributes = ['ID' => $taxonomy_id]; $attributes = [];
foreach ($body as $att => $value){ if(isset($request['collection_id'])) {
$attributes[$att] = $value; $collection_id = $request['collection_id'];
$attributes = [ 'collection' => $collection_id ];
} else {
foreach ( $body as $att => $value ) {
$attributes[ $att ] = $value;
}
} }
$updated_taxonomy = $this->taxonomy_repository->update($attributes); $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
$updated_taxonomy = $this->taxonomy_repository->update($taxonomy, $attributes);
if(!($updated_taxonomy instanceof Entities\Taxonomy)){
return new WP_REST_Response($updated_taxonomy, 400);
}
return new WP_REST_Response($updated_taxonomy->__toArray(), 200); return new WP_REST_Response($updated_taxonomy->__toArray(), 200);
} }

View File

@ -190,12 +190,8 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_REST_Controller {
$body = json_decode($request->get_body(), true); $body = json_decode($request->get_body(), true);
if(!empty($body)){ if(!empty($body)){
$taxonomy_name = $this->taxonomy_repository->fetch($taxonomy_id)->get_db_identifier(); $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
$tax_name = $taxonomy->get_db_identifier();
$identifiers = [
'term_id' => $term_id,
'tax_name' => $taxonomy_name
];
$attributes = []; $attributes = [];
@ -203,7 +199,13 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_REST_Controller {
$attributes[$att] = $value; $attributes[$att] = $value;
} }
$updated_term = $this->terms_repository->update([$attributes, $identifiers]); $term = $this->terms_repository->fetch($term_id, $taxonomy);
$updated_term = $this->terms_repository->update([$term, $tax_name], $attributes);
if(!($updated_term instanceof Entities\Term)){
return new WP_REST_Response($updated_term, 400);
}
return new WP_REST_Response($updated_term->__toArray(), 200); return new WP_REST_Response($updated_term->__toArray(), 200);
} }
@ -260,7 +262,17 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_REST_Controller {
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id); $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
$args = json_decode($request->get_body(), true); $args = [];
$map = $this->terms_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$terms = $this->terms_repository->fetch($args, $taxonomy); $terms = $this->terms_repository->fetch($args, $taxonomy);

View File

@ -3,11 +3,12 @@
$rest_controller = new TAINACAN_REST_Controller(); $rest_controller = new TAINACAN_REST_Controller();
$rest_collections_controller = new TAINACAN_REST_Collections_Controller(); $rest_collections_controller = new TAINACAN_REST_Collections_Controller();
$rest_items_controller = new TAINACAN_REST_Items_Controller(); $rest_items_controller = new TAINACAN_REST_Items_Controller();
$rest_metadata_controller = new TAINACAN_REST_Metadata_Controller(); $rest_fields_controller = new TAINACAN_REST_Fields_Controller();
$rest_taxonomies_controller = new TAINACAN_REST_Taxonomies_Controller(); $rest_taxonomies_controller = new TAINACAN_REST_Taxonomies_Controller();
$rest_terms_controller = new TAINACAN_REST_Terms_Controller(); $rest_terms_controller = new TAINACAN_REST_Terms_Controller();
$rest_filters_controller = new TAINACAN_REST_Filters_Controller(); $rest_filters_controller = new TAINACAN_REST_Filters_Controller();
$rest_item_metadata_controller = new TAINACAN_REST_Item_Metadata_Controller(); $rest_item_metadata_controller = new TAINACAN_REST_Item_Metadata_Controller();
$rest_logs_controller = new TAINACAN_REST_Logs_Controller();
// Add here other endpoints imports // Add here other endpoints imports
?> ?>

View File

@ -1,83 +1,47 @@
<template> <template>
<div class="component"> <div>
<label <el-checkbox-group v-model="checked">
v-for="option in getOptions" <el-checkbox
:for="option.replace(' ','-') + '-checkbox'"> v-for="option,index in getOptions"
<input :key="index"
type="checkbox" v-model="checked"
:id="option.replace(' ','-') + '-checkbox'" @change="onChecked(option)"
:value="option" :label="option"
:checked="isChecked(option)" border>{{ option }}</el-checkbox>
@change="sendValue($event)"> {{ option }} <br> </el-checkbox-group>
</label>
</div> </div>
</template> </template>
<script> <script>
import store from '../../../js/store/store';
export default { export default {
store,
data(){ data(){
return { return {
checked:[] checked:[]
} }
}, },
props: { props: {
name: { field: {
type: String type: Object
}, }
options: {
type: String
},
item_id: {
type: Number
},
field_id: {
type: Number
},
value: {
type: [ String,Number ]
},
},
created(){
this.setInitValueOnStore();
}, },
computed: { computed: {
getOptions(){ getOptions(){
const values = ( this.options ) ? this.options.split("\n") : ''; if (this.field) {
return values; const fields = this.field.field.field_type_options.options;
return ( fields ) ? fields.split("\n") : [];
}
return [];
} }
}, },
methods: { methods: {
setInitValueOnStore (){ onChecked(option) {
const array_values = JSON.parse( this.value ); this.$emit('blur');
if ( array_values.length > 0 ){ this.onInput(this.checked)
this.checked = array_values;
this.$store.dispatch('item/setSingleMetadata', { item_id: this.item_id, field_id: this.field_id, values: array_values });
}
}, },
sendValue ( event ){ onInput($event) {
let index = this.checked.indexOf( event.target.value ); this.inputValue = $event;
if( index >= 0 ){ this.$emit('input', this.inputValue);
this.checked.splice(index,1);
}else{
this.checked.push( event.target.value );
}
this.$store.dispatch('item/sendMetadata', { item_id: this.item_id, field_id: this.field_id, values: this.checked });
},
isChecked ( value ){
let index = this.checked.indexOf( value );
return index >= 0;
} }
} }
} }
</script> </script>
<style scoped="">
#postcustomstuff table input, #postcustomstuff table select, #postcustomstuff table textarea {
width: auto;
margin: 8px;
}
</style>

View File

@ -1,15 +1,11 @@
<template> <template>
<div class="block">
<el-date-picker <el-date-picker
v-model="valueDate"
@blur="changeValue()"
type="date" type="date"
format="dd/MM/yyyy" format="dd/MM/yyyy"
value-format="dd/MM/yyyy" value-format="dd/MM/yyyy"
placeholder="Selecione a data..."> :value="dateValue"
</el-date-picker> @blur="onBlur"
<div class="demonstration">{{ valueDate }}</div> @input="onInput($event)"></el-date-picker>
</div>
</template> </template>
<script> <script>
@ -19,40 +15,18 @@
locale.use(lang) locale.use(lang)
export default { export default {
props: { data() {
name: { type: String },
item_id: { type: Number },
field_id: { type: Number },
value: { type: [ String,Number ] },
errorsMsg: { type: [ String,Number ] },
},
data(){
return { return {
valueDate:'' dateValue: ''
} }
}, },
created(){
this.getValue();
},
methods: { methods: {
changeValue(){ onBlur() {
this.$emit('input', { item_id: this.item_id, field_id: this.field_id, values: event.target.value } ); this.$emit('blur');
}, },
getValue(){ onInput($event) {
try{ this.dateValue = $event;
let val = JSON.parse( this.value ); this.$emit('input', this.dateValue);
this.valueDate = val;
}catch(e){
console.log('invalid json value');
}
},
getErrors(){
try{
return JSON.parse( this.errorsMsg );
}catch(e){
console.log('invalid json error');
}
return this.errorsMsg;
} }
} }
} }

View File

@ -1,59 +1,27 @@
<template> <template>
<div class="component"> <el-input-number
<p>{{ name }}</p> controls-position="right"
<input type="text" @blur="changeValue($event)" :value="getValue()"> :value="inputValue"
<p v-for="error in getErrors()"> @blur="onBlur"
{{ error }} @change="onBlur"
</p> @input="onInput($event)"></el-input-number>
</div>
</template> </template>
<script> <script>
export default { export default {
props: { data() {
name: { type: String }, return {
item_id: { type: Number }, inputValue: ''
field_id: { type: Number }, }
value: { type: [ String,Number ] },
errorsMsg: { type: [ String,Number ] },
}, },
methods: { methods: {
changeValue( event ){ onBlur() {
this.$emit('changeValue', { item_id: this.item_id, field_id: this.field_id, values: event.target.value } ); this.$emit('blur');
}, },
getValue(){ onInput($event) {
try{ this.inputValue = $event;
return JSON.parse( this.value ); this.$emit('input', this.inputValue);
}catch(e){
console.log('invalid json value');
}
return this.value;
},
getErrors(){
try{
return JSON.parse( this.errorsMsg );
}catch(e){
console.log('invalid json error msg');
}
return this.errorsMsg;
} }
} }
} }
</script> </script>
<style scoped>
input[type="text"] {
display: block;
margin: 0;
width: 100%;
border-radius: 6px;
font-family: sans-serif;
font-size: 18px;
appearance: none;
box-shadow: none;
color:green;
}
input[type="text"]:focus {
outline: none;
}
</style>

View File

@ -1,14 +1,12 @@
<template> <template>
<div class="component"> <div>
<label <el-radio
v-for="option in getOptions" v-for="option,index in getOptions"
:for="option.replace(' ','-') + '-checkbox'"> :key="index"
<input v-model="checked"
type="radio" @change="onChecked(option)"
:id="option.replace(' ','-') + '-checkbox'" :label="option"
:value="option" border>{{ option }}</el-radio>
v-model="checked"> {{ option }}<br>
</label>
</div> </div>
</template> </template>
@ -20,25 +18,28 @@
} }
}, },
props: { props: {
name: { field: {
type: String type: Object
},
options: {
type: String
} }
}, },
computed: { computed: {
getOptions(){ getOptions(){
const values = ( this.options ) ? this.options.split("\n") : ''; if (this.field) {
return values; const fields = this.field.field.field_type_options.options;
return ( fields ) ? fields.split("\n") : [];
}
return [];
}
},
methods: {
onChecked(option) {
this.$emit('blur');
this.onInput(this.checked)
},
onInput($event) {
this.inputValue = $event;
this.$emit('input', this.inputValue);
} }
} }
} }
</script> </script>
<style scoped="">
#postcustomstuff table input, #postcustomstuff table select, #postcustomstuff table textarea {
width: auto;
margin: 8px;
}
</style>

View File

@ -22,7 +22,11 @@ class Radio extends Field_Type {
public function render( $itemMetadata ){ public function render( $itemMetadata ){
$options = ( isset( $this->options['options'] ) ) ? $this->options['options'] : ''; $options = ( isset( $this->options['options'] ) ) ? $this->options['options'] : '';
return '<tainacan-radio options="'.$options.'" name="'.$itemMetadata->get_field()->get_name().'"></tainacan-radio>'; return '<tainacan-radio
field_id ="'.$itemMetadata->get_field()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
options="'.$options.'" name="'.$itemMetadata->get_field()->get_name().'"></tainacan-radio>';
} }
/** /**

View File

@ -1,77 +1,76 @@
<template> <template>
<div class="component"> <div>
<p>{{ name }}</p> <el-select
<select v-model="selected"
class="form-control" multiple
:disabled="!getOptions" filterable
v-model="manageValue"> remote
<option reserve-keyword
v-for="option in getOptions" :remote-method="search"
:selected="option == ''"> :loading="loading"
{{ option }} @change="onChecked()">
</option> <el-option
</select> v-for="option,index in options"
:key="option.value"
:label="option.label"
:value="option.value"
></el-option>
</el-select>
</div> </div>
</template> </template>
<script> <script>
import store from '../../../js/store/store'; import axios from '../../../js/axios/axios'
export default { export default {
store,
data(){ data(){
return { return {
selected:'' selected:[],
options: [],
loading: false,
collectionId: 0,
inputValue: null
} }
}, },
props: { props: {
name: { field: {
type: String type: Object
},
options: {
type: String
},
item_id: {
type: Number
},
field_id: {
type: Number
},
value: {
type: [ String,Number ]
},
},
created(){
this.setInitValueOnStore();
},
computed: {
getOptions(){
const values = ( this.options ) ? this.options.split("\n") : false;
return values;
},
manageValue : {
get(){
let field = this.$store.getters['item/getMetadata'].find(field => field.field_id === this.field_id );
if( field ){
return field.values;
}else if( this.value ){
return JSON.parse( this.value );
}
},
set( value ){
this.$store.dispatch('item/sendMetadata', { item_id: this.item_id, field_id: this.field_id, values: value });
}
} }
}, },
methods: { methods: {
setInitValueOnStore(){ onChecked() {
if ( this.value ){ this.$emit('blur');
this.$store.dispatch('item/setSingleMetadata', { item_id: this.item_id, field_id: this.field_id, values: JSON.parse( this.value ) }); this.onInput(this.selected)
},
onInput($event) {
this.inputValue = $event;
this.$emit('input', this.inputValue);
},
search(query) {
if (query !== '') {
this.loading = true;
this.options = [];
let collectionId = this.field.field.field_type_options.collection_id;
axios.get('/collection/'+collectionId+'/items')
.then( res => {
let result = [];
this.loading = false;
result = res.data.filter(item => {
return item.title.toLowerCase()
.indexOf(query.toLowerCase()) > -1;
});
for (let item of result) {
this.options.push({ label: item.title, value: item.id })
}
})
.catch(error => {
console.log(error);
});
} else {
this.options = [];
} }
} }
} }
} }
</script> </script>
<style scoped>
</style>

View File

@ -23,7 +23,11 @@ class Relationship extends Field_Type {
*/ */
public function render( $itemMetadata ){ public function render( $itemMetadata ){
return '<tainacan-relationship name="'.$itemMetadata->get_field()->get_name().'"></tainacan-relationship>'; return '<tainacan-relationship
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-relationship>';
} }
/** /**

View File

@ -1,77 +1,46 @@
<template> <template>
<div class="component"> <div>
<p>{{ name }}</p> <el-select v-model="selected" @change="onChecked()">
<select <el-option
class="form-control" v-for="option,index in getOptions"
:disabled="!getOptions" :key="index"
v-model="manageValue"> :label="option"
<option :value="option"
v-for="option in getOptions" border>{{ option }}</el-option>
:selected="option == ''"> </el-select>
{{ option }}
</option>
</select>
</div> </div>
</template> </template>
<script> <script>
import store from '../../../js/store/store';
export default { export default {
store,
data(){ data(){
return { return {
selected:'' selected:''
} }
}, },
props: { props: {
name: { field: {
type: String type: Object
}, }
options: {
type: String
},
item_id: {
type: Number
},
field_id: {
type: Number
},
value: {
type: [ String,Number ]
},
},
created(){
this.setInitValueOnStore();
}, },
computed: { computed: {
getOptions(){ getOptions(){
const values = ( this.options ) ? this.options.split("\n") : false; if (this.field && this.field.field.field_type_options.options) {
return values; const fields = this.field.field.field_type_options.options;
}, return ( fields ) ? fields.split("\n") : [];
manageValue : {
get(){
let field = this.$store.getters['item/getMetadata'].find(field => field.field_id === this.field_id );
if( field ){
return field.values;
}else if( this.value ){
return JSON.parse( this.value );
}
},
set( value ){
this.$store.dispatch('item/sendMetadata', { item_id: this.item_id, field_id: this.field_id, values: value });
} }
return [];
} }
}, },
methods: { methods: {
setInitValueOnStore(){ onChecked(option) {
if ( this.value ){ this.$emit('blur');
this.$store.dispatch('item/updateMetadata', { item_id: this.item_id, field_id: this.field_id, values: JSON.parse( this.value ) }); this.onInput(this.selected)
} },
onInput($event) {
this.$emit('input', $event);
} }
} }
} }
</script> </script>
<style scoped>
</style>

View File

@ -22,7 +22,7 @@ class Selectbox extends Field_Type {
public function render( $itemMetadata ){ public function render( $itemMetadata ){
$options = ( isset( $this->options['options'] ) ) ? $this->options['options'] : ''; $options = ( isset( $this->options['options'] ) ) ? $this->options['options'] : '';
return '<tainacan-selectbox options="'.$options.'" return '<tainacan-selectbox
field_id ="'.$itemMetadata->get_field()->get_id().'" field_id ="'.$itemMetadata->get_field()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'" item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\' value=\''.json_encode( $itemMetadata->get_value() ).'\'

View File

@ -1,10 +1,10 @@
<template> <template>
<div> <div>
<el-form-item :label="field.field.name" :prop="validateObject()"> <el-form-item :label="field.field.name" :prop="validateObject()">
<component :is="extractFieldType(field.field.field_type)" v-model="inputs[0]" @blur="changeValue()"></component> <component :is="extractFieldType(field.field.field_type)" v-model="inputs[0]" :field="field" @blur="changeValue()"></component>
<div v-if="field.field.multiple == 'yes'"> <div v-if="field.field.multiple == 'yes'">
<div v-if="index > 0" v-for="(input, index) in inputsList " v-bind:key="index" class="multiple-inputs"> <div v-if="index > 0" v-for="(input, index) in inputsList " v-bind:key="index" class="multiple-inputs">
<component :is="extractFieldType(field.field.field_type)" v-model="inputs[index]" @blur="changeValue()"></component><el-button v-if="index > 0" @click="removeInput(index)">-</el-button> <component :is="extractFieldType(field.field.field_type)" v-model="inputs[index]" :field="field" @blur="changeValue()"></component><el-button v-if="index > 0" @click="removeInput(index)">-</el-button>
</div> </div>
<el-button @click="addInput">+</el-button> <el-button @click="addInput">+</el-button>
</div> </div>

View File

@ -1,31 +1,26 @@
<template> <template>
<div class="component"> <el-input type="textarea"
<p>{{ name }}</p> :autosize="{ minRows: 2, maxRows: 4}"
<textarea cols="30" rows="10"></textarea> :value="inputValue"
</div> @blur="onBlur"
@input="onInput($event)"></el-input>
</template> </template>
<script> <script>
export default { export default {
props: { data() {
name: { type: String } return {
inputValue: ''
}
},
methods: {
onBlur() {
this.$emit('blur');
},
onInput($event) {
this.inputValue = $event;
this.$emit('input', this.inputValue);
}
} }
} }
</script> </script>
<style scoped>
textarea {
display: block;
margin: 0;
width: 100%;
border-radius: 6px;
font-family: sans-serif;
font-size: 18px;
appearance: none;
box-shadow: none;
color:green;
}
textarea:focus {
outline: none;
}
</style>

View File

@ -21,7 +21,11 @@ class Textarea extends Field_Type {
*/ */
public function render( $itemMetadata ){ public function render( $itemMetadata ){
return '<tainacan-textarea name="'.$itemMetadata->get_field()->get_name().'"></tainacan-textarea>'; return '<tainacan-textarea
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-textarea>';
} }
/** /**

View File

@ -228,7 +228,9 @@ class Fields extends Repository {
* *
* @param array $args WP_Query args || int $args the field id * @param array $args WP_Query args || int $args the field id
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
*
* @return \WP_Query|Array an instance of wp query OR array of entities; * @return \WP_Query|Array an instance of wp query OR array of entities;
* @throws \Exception
*/ */
public function fetch( $args, $output = null ) { public function fetch( $args, $output = null ) {
@ -259,7 +261,9 @@ class Fields extends Repository {
* @param Entities\Collection $collection * @param Entities\Collection $collection
* @param array $args * @param array $args
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) * @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){ public function fetch_by_collection(Entities\Collection $collection, $args = [], $output = null){
$collection_id = $collection->get_id(); $collection_id = $collection->get_id();

View File

@ -169,19 +169,20 @@ class Filters extends Repository {
} }
public function update($object, $new_values = null){ public function update($object, $new_values = null){
$map = $this->get_map(); foreach ($new_values as $key => $value) {
try {
$entity = []; $set_ = 'set_' . $key;
$object->$set_( $value );
foreach ($object as $key => $value) { } catch (\Error $error){
if($key != 'ID') { return $error->getMessage();
$entity[$map[$key]['map']] = $value ;
} elseif ($key == 'ID'){
$entity[$key] = (int) $value;
} }
} }
return new Entities\Filter(wp_update_post($entity)); if($object->validate()){
return $this->insert($object);
}
return $object->get_errors();
} }
/** /**

View File

@ -78,7 +78,10 @@ abstract class Repository {
} }
} }
$obj->WP_Post->post_type = $obj::get_post_type(); $obj->WP_Post->post_type = $obj::get_post_type();
//$obj->WP_Post->post_status = 'publish';
if($obj instanceof Entities\Log) {
$obj->WP_Post->post_status = 'publish';
}
// TODO verificar se salvou mesmo // TODO verificar se salvou mesmo
$id = wp_insert_post($obj->WP_Post); $id = wp_insert_post($obj->WP_Post);

View File

@ -109,7 +109,8 @@ class Taxonomies extends Repository {
/** /**
* @param Entities\Taxonomy $taxonomy * @param Entities\Taxonomy $taxonomy
* @return int *
* @return Entities\Entity
*/ */
public function insert($taxonomy) { public function insert($taxonomy) {
@ -185,22 +186,20 @@ class Taxonomies extends Repository {
} }
public function update($object, $new_values = null){ public function update($object, $new_values = null){
$map = $this->get_map(); foreach ($new_values as $key => $value) {
try {
$entity = []; $set_ = 'set_' . $key;
$object->$set_( $value );
foreach ($object as $key => $value) { } catch (\Error $error){
if($key != 'ID') { return $error->getMessage();
$entity[$map[$key]['map']] = $value ;
} elseif ($key == 'ID'){
$entity[$key] = (int) $value;
} }
} }
$updated_taxonomy = new Entities\Taxonomy(wp_update_post($entity)); if($object->validate()){
$updated_taxonomy->register_taxonomy(); return $this->insert($object);
}
return $updated_taxonomy; return $object->get_errors();
} }
public function delete($args){ public function delete($args){

View File

@ -61,6 +61,10 @@ class Terms extends Repository {
'on_error' => __('The user is empty or invalid', 'tainacan'), 'on_error' => __('The user is empty or invalid', 'tainacan'),
'validation' => v::numeric()->positive(), 'validation' => v::numeric()->positive(),
], ],
'hide_empty' => [
'map' => 'hide_empty',
'type' => 'bool'
]
]); ]);
} }
@ -156,20 +160,23 @@ class Terms extends Repository {
} }
public function update($object, $new_values = null){ public function update($object, $new_values = null){
$map = $this->get_map(); $tax_name = $object[1];
$object = $object[0];
$entity = []; foreach ($new_values as $key => $value) {
try {
foreach ($object[0] as $key => $value) { $set_ = 'set_' . $key;
if($key != 'ID') { $object->$set_( $value );
$entity[$map[$key]['map']] = $value ; } catch (\Error $error){
return $error->getMessage();
} }
} }
$term_tax_ids = wp_update_term($object[1]['term_id'], $object[1]['tax_name'], $entity); if($object->validate()){
$term_id = (int) $term_tax_ids['term_id']; return new Entities\Term($this->insert($object), $tax_name);
}
return new Entities\Term($term_id, $object[1]['tax_name']); return $object->get_errors();
} }
public function delete($args){ public function delete($args){

View File

@ -48,4 +48,12 @@ trait Entity_Collections_Relation {
$this->set_collections_ids($collections_ids); $this->set_collections_ids($collections_ids);
} }
public function set_collection($new_collection_id){
$this->collections = $this->get_mapped_property('collections_ids');
$collections[] = $new_collection_id;
$this->set_collections_ids($collections);
}
} }

View File

@ -22,16 +22,19 @@ export const eventBus = new Vue({
listener(){ listener(){
const components = this.getAllComponents(); const components = this.getAllComponents();
for (let eventElement of components){ for (let eventElement of components){
eventElement.addEventListener('changeValues', (event) => { eventElement.addEventListener('input', (event) => {
if ( event.detail[0] ){ if ( event.detail[0] ){
const promisse = this.$store.dispatch('item/updateMetadata', event.detail[0] ); const promisse = this.$store.dispatch('item/updateMetadata',
{ item_id: $(eventElement).attr("item_id"), field_id: $(eventElement).attr("field_id"), values: event.detail });
promisse.then( response => { promisse.then( response => {
eventElement.errorsMsg = JSON.stringify( [] ); // eventElement.errorsMsg = JSON.stringify( [] );
eventElement.value = response.value; // eventElement.value = response.value;
$(eventElement).val(response.value);
}, error => { }, error => {
const field = this.errors.find(error => error.field_id === event.detail[0].field_id ); const field = this.errors.find(error => error.field_id === event.detail[0].field_id );
eventElement.errorsMsg = JSON.stringify( field.error ); // eventElement.errorsMsg = JSON.stringify( field.error );
eventElement.value = event.detail[0].values; // eventElement.value = event.detail[0].values;
}); });
} }
}); });

View File

@ -5,7 +5,7 @@ import VueCustomElement from 'vue-custom-element';
import ElementUI from 'element-ui' import ElementUI from 'element-ui'
import { eventBus } from './event-bus-web-components'; import { eventBus } from './event-bus-web-components';
Vue.use(ElementUI) Vue.use(ElementUI);
Vue.use(VueCustomElement); Vue.use(VueCustomElement);
import Text from '../classes/field-types/text/Text.vue'; import Text from '../classes/field-types/text/Text.vue';

67
tests/test-api-logs.php Normal file
View File

@ -0,0 +1,67 @@
<?php
namespace Tainacan\Tests;
/**
*
* @group api
*
*/
class TAINACAN_REST_Logs_Controller extends TAINACAN_UnitApiTestCase {
public function test_get_logs(){
$this->tainacan_entity_factory->create_entity(
'log',
[
'title' => 'Log 1',
'description' => 'Log number 1',
],
true
);
$this->tainacan_entity_factory->create_entity(
'log',
[
'title' => 'Log 2',
'description' => 'Log number 2',
],
true
);
$request = new \WP_REST_Request(
'GET', $this->namespace . '/logs'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals('Log 1', $data[1]['title']);
$this->assertEquals('Log 2', $data[0]['title']);
}
public function test_get_a_log(){
$log = $this->tainacan_entity_factory->create_entity(
'log',
[
'title' => 'Log',
'description' => 'A description',
],
true
);
$request = new \WP_REST_Request(
'GET', $this->namespace . '/logs/' . $log->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals('Log', $data['title']);
$this->assertEquals($log->get_id(), $data['id']);
}
}
?>

View File

@ -148,15 +148,13 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
true true
); );
$show_empty = json_encode([
'hide_empty' => false
]);
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
'GET', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms' 'GET', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms'
); );
$request->set_body($show_empty); $request->set_query_params([
'hide_empty' => false
]);
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);