diff --git a/src/admin/class-tainacan-admin.php b/src/admin/class-tainacan-admin.php index 8cafede8f..a1da50238 100644 --- a/src/admin/class-tainacan-admin.php +++ b/src/admin/class-tainacan-admin.php @@ -220,6 +220,13 @@ class Admin { $class = new $metadata_type; $settings['i18n']['helpers_label'][$class->get_component()] = $class->get_form_labels(); } + + $filter_types = $Tainacan_Filters->fetch_filter_types(); + + foreach ( $filter_types as $index => $filter_type){ + $class = new $filter_type; + $settings['i18n']['helpers_label'][$class->get_component()] = $class->get_form_labels(); + } $settings['form_hooks'] = Admin_Hooks::get_instance()->get_registered_hooks(); diff --git a/src/admin/components/edition/filter-edition-form.vue b/src/admin/components/edition/filter-edition-form.vue index 4d47c5291..5647b51b4 100644 --- a/src/admin/components/edition/filter-edition-form.vue +++ b/src/admin/components/edition/filter-edition-form.vue @@ -149,11 +149,10 @@ + v-model="editForm.filter_type_object.options"/>
diff --git a/src/admin/pages/lists/items-page.vue b/src/admin/pages/lists/items-page.vue index 787231f8e..add0a2fc4 100644 --- a/src/admin/pages/lists/items-page.vue +++ b/src/admin/pages/lists/items-page.vue @@ -619,7 +619,7 @@ placement: 'auto', }"> - {{ `${$i18n.get('label_all_published_items')}` }} + {{ $i18n.get('label_all_published_items') }}  {{ collection && collection.total_items ? ` (${Number(collection.total_items.private) + Number(collection.total_items.publish)})` : (isRepositoryLevel && repositoryTotalItems) ? ` (${ repositoryTotalItems.private + repositoryTotalItems.publish })` : '' }} @@ -635,7 +635,7 @@ - {{ `${$i18n.get('label_publish_items')}` }} + {{ $i18n.get('label_publish_items') }}  {{ collection && collection.total_items ? ` (${collection.total_items.publish})` : (isRepositoryLevel && repositoryTotalItems) ? ` (${ repositoryTotalItems.publish })` : '' }} @@ -653,7 +653,7 @@ - {{ `${$i18n.get('label_private_items')}` }} + {{ $i18n.get('label_private_items') }}  {{ collection && collection.total_items ? ` (${collection.total_items.private})` : (isRepositoryLevel && repositoryTotalItems) ? ` (${ repositoryTotalItems.private })` : '' }} @@ -669,7 +669,7 @@ - {{ `${$i18n.get('label_draft_items')}` }} + {{ $i18n.get('label_draft_items') }}  {{ collection && collection.total_items ? ` (${collection.total_items.draft})` : (isRepositoryLevel && repositoryTotalItems) ? ` (${ repositoryTotalItems.draft })` : '' }} @@ -686,7 +686,7 @@ - {{ `${$i18n.get('label_trash_items')}` }} + {{ $i18n.get('label_trash_items') }}  {{ collection && collection.total_items ? ` (${collection.total_items.trash})` : (isRepositoryLevel && repositoryTotalItems) ? ` (${ repositoryTotalItems.trash })` : '' }} diff --git a/src/admin/tainacan-admin-i18n.php b/src/admin/tainacan-admin-i18n.php index c3034922a..50513ddd1 100644 --- a/src/admin/tainacan-admin-i18n.php +++ b/src/admin/tainacan-admin-i18n.php @@ -453,6 +453,8 @@ return apply_filters( 'tainacan-admin-i18n', [ 'instruction_click_or_drag_metadatum_create' => __( 'Click or drag and drop to create a new metadatum', 'tainacan' ), 'instruction_drag_and_drop_filter_sort' => __( 'Drag and drop to change filter order', 'tainacan' ), 'instruction_drag_and_drop_metadatum_sort' => __( 'Drag and drop to change metadatum order', 'tainacan' ), + 'instruction_select_step_options_to_show' => __( 'Select which Step values to show', 'tainacan' ), + 'instruction_select_maximum_options_to_show' => __( 'Select which Maximum of options to show', 'tainacan' ), // Info. Other feedback to user. 'info_items_tab_all' => __( 'Every published item of this collection, including those visible only to editors.', 'tainacan' ), diff --git a/src/classes/entities/class-tainacan-filter.php b/src/classes/entities/class-tainacan-filter.php index 2700a706b..b3883abf9 100644 --- a/src/classes/entities/class-tainacan-filter.php +++ b/src/classes/entities/class-tainacan-filter.php @@ -124,12 +124,12 @@ class Filter extends Entity { function get_filter_type_object(){ $class_name = $this->get_filter_type(); - if( !class_exists( $class_name ) ){ - return false; - } + if (empty($class_name)) { + return null; + } $object_type = new $class_name(); - $object_type->set_options( $this->get_filter_options() ); + $object_type->set_options( $this->get_filter_type_options() ); return $object_type; } @@ -147,7 +147,7 @@ class Filter extends Entity { * * @return array Configurations for the filter type object */ - function get_filter_options(){ + function get_filter_type_options(){ return $this->get_mapped_property('filter_type_options'); } @@ -258,6 +258,18 @@ class Filter extends Entity { $this->add_error($metadatum, $message); } + $this->add_error('filter_type_options', $is_valid); + return false; } + + /** + * Set Filter type options + * + * @param [string || integer] $value + * @return void + */ + function set_filter_type_options( $value ){ + $this->set_mapped_property('filter_type_options', $value); + } } \ No newline at end of file diff --git a/src/classes/filter-types/filter-type/class-tainacan-filter-type.php b/src/classes/filter-types/filter-type/class-tainacan-filter-type.php index c7cc814e4..c9eef012e 100644 --- a/src/classes/filter-types/filter-type/class-tainacan-filter-type.php +++ b/src/classes/filter-types/filter-type/class-tainacan-filter-type.php @@ -5,11 +5,41 @@ use Tainacan\Metadata_Types; defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); +/** + * Class TainacanFilterType + */ abstract class Filter_Type { private $supported_types = []; + + /** + * Array of options specific to this filter type. Stored in filter_type_options property of the Filter object + * @var array + */ private $options = []; + + /** + * The default values for the filter type options array + * @var array + */ + private $default_options = []; + + /** + * The name of the web component used by this filter type + * @var string + */ private $component; + + /** + * The name of the web component used by the Form + * @var bool | string + */ + private $form_component = false; + + /** + * The html template featuring a preview of how this metadata type componenet + * @var string + */ private $preview_template = ''; protected $use_max_options = true; @@ -72,11 +102,13 @@ abstract class Filter_Type { public function _toArray(){ $attributes = []; - $attributes['className'] = get_class($this); - $attributes['component'] = $this->get_component(); - $attributes['supported_types'] = $this->get_supported_types(); + $attributes['className'] = get_class($this); + $attributes['component'] = $this->get_component(); + $attributes['options'] = $this->get_options(); + $attributes['supported_types'] = $this->get_supported_types(); $attributes['preview_template'] = $this->get_preview_template(); - $attributes['use_max_options'] = $this->get_use_max_options(); + $attributes['use_max_options'] = $this->get_use_max_options(); + $attributes['form_component'] = $this->get_form_component(); return $attributes; } @@ -88,6 +120,10 @@ abstract class Filter_Type { $this->options = ( is_array( $options ) ) ? $options : (!is_array(unserialize( $options )) ? [] : unserialize( $options )); } + public function set_default_options(Array $options) { + $this->default_options = $options; + } + /** * Validates the options Array * @@ -121,11 +157,13 @@ abstract class Filter_Type { $this->component = $component; } - /** - * @return mixed - */ - public function get_options() { - return $this->options; + /** + * Gets the options for this filter types including default values for options + * that were not set yet. + * @return array Filter type options + */ + public function get_options() { + return array_merge($this->default_options, $this->options); } public function set_use_max_options($use_max_options) { @@ -135,4 +173,38 @@ abstract class Filter_Type { public function get_use_max_options() { return $this->use_max_options; } + + /** + * Gets one option from the options array. + * + * Checks if option exist or if it have a default value. Otherwise return an empty string + * + * @param string $key the desired option + * @return mixed the option value, the default value or an empty string + */ + public function get_option($key) { + $options = $this->get_options(); + return isset($options[$key]) ? $options[$key] : ''; + } + + /** + * allow i18n from messages + */ + public function get_form_labels(){ + return []; + } + + /** + * @return string + */ + public function get_form_component() { + return $this->form_component; + } + + /** + * @param $form_component The web component that will render the filter options form + */ + public function set_form_component($form_component){ + $this->form_component = $form_component; + } } \ No newline at end of file diff --git a/src/classes/filter-types/numeric/FormNumeric.vue b/src/classes/filter-types/numeric/FormNumeric.vue index e741bc7e5..3578cc88b 100644 --- a/src/classes/filter-types/numeric/FormNumeric.vue +++ b/src/classes/filter-types/numeric/FormNumeric.vue @@ -1,54 +1,102 @@ \ No newline at end of file diff --git a/src/classes/filter-types/numeric/Numeric.vue b/src/classes/filter-types/numeric/Numeric.vue index 5c61193d4..840fd87b9 100644 --- a/src/classes/filter-types/numeric/Numeric.vue +++ b/src/classes/filter-types/numeric/Numeric.vue @@ -62,7 +62,7 @@
@@ -111,6 +111,7 @@ filter: { type: Object // concentrate all attributes metadatum id and type }, + step: [Number, String], metadatum_id: [Number], // not required, but overrides the filter metadatum id if is set collection_id: [Number], // not required, but overrides the filter metadatum id if is set labelId: '', diff --git a/src/classes/filter-types/numeric/class-tainacan-numeric.php b/src/classes/filter-types/numeric/class-tainacan-numeric.php index f2908e924..00db35c64 100644 --- a/src/classes/filter-types/numeric/class-tainacan-numeric.php +++ b/src/classes/filter-types/numeric/class-tainacan-numeric.php @@ -12,8 +12,11 @@ class Numeric extends Filter_Type { function __construct(){ $this->set_supported_types(['float']); $this->set_component('tainacan-filter-numeric'); - // $this->set_form_component('tainacan-filter-form-numeric'); + $this->set_form_component('tainacan-filter-form-numeric'); $this->set_use_max_options(false); + $this->set_default_options([ + 'step' => 1 + ]); $this->set_preview_template('
@@ -66,18 +69,46 @@ class Numeric extends Filter_Type { '); } + /** + * @inheritdoc + */ + public function get_form_labels(){ + return [ + 'step' => [ + 'title' => __( 'Step', 'tainacan' ), + 'description' => __( 'The amount to be increased or decreased when clicking on filter control buttons.', 'tainacan' ), + ] + ]; + } /** * @param $filter * @return string * @internal param $metadatum */ public function render( $filter ){ - $options = $this->get_option('options'); - return ''; } + + + /** + * @param \Tainacan\Entities\Filter $filter + * @return array|bool true if is validate or array if has error + */ + public function validate_options(\Tainacan\Entities\Filter $filter) { + if ( !in_array($filter->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) + return true; + + if ( empty($this->get_option('step')) ) { + return [ + 'step' => __('Required step','tainacan') + ]; + } + + return true; + } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index cbc11e83a..ae803ebce 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -39,7 +39,7 @@ class Filters extends Repository { 'order' => [ 'map' => 'menu_order', 'title' => __( 'Order', 'tainacan' ), - 'type' => 'string', + 'type' => 'string/integer', 'description' => __( 'Filter order. This metadata is used if filters were manually ordered.', 'tainacan' ), 'validation' => '' ], @@ -76,14 +76,14 @@ class Filters extends Repository { 'color' => [ 'map' => 'meta', 'title' => __( 'Color', 'tainacan' ), - 'type' => 'integer', + 'type' => 'integer/string', 'description' => __( 'Filter color', 'tainacan' ), 'validation' => '' ], 'metadatum' => [ 'map' => 'meta', 'title' => __( 'Metadata', 'tainacan' ), - 'type' => 'integer', + 'type' => 'array/object/string', 'description' => __( 'Filter metadata', 'tainacan' ), 'validation' => '' ],