From ed88d12629d4b7bee6be910c10dc81aa089896d4 Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Wed, 15 Jan 2020 14:08:08 -0300 Subject: [PATCH] Temporaly keeps copy of filter and metadata types on classes folder. --- .../class-tainacan-autocomplete.php | 54 +++ .../checkbox/class-tainacan-checkbox.php | 71 ++++ .../class-tainacan-date-interval.php | 55 ++++ .../filter-types/date/class-tainacan-date.php | 89 +++++ .../class-tainacan-filter-type.php | 235 +++++++++++++ .../class-tainacan-numeric-interval.php | 87 +++++ .../class-tainacan-numeric-list-interval.php | 70 ++++ .../numeric/class-tainacan-numeric.php | 113 +++++++ .../selectbox/class-tainacan-selectbox.php | 39 +++ .../taginput/class-tainacan-taginput.php | 69 ++++ .../class-tainacan-taxonomycheckbox.php | 71 ++++ .../class-tainacan-taxonomytaginput.php | 69 ++++ .../compound/class-tainacan-compound.php | 100 ++++++ .../class-tainacan-core-description.php | 113 +++++++ .../core-title/class-tainacan-core-title.php | 85 +++++ .../date/class-tainacan-date.php | 108 ++++++ .../class-tainacan-metadata-type.php | 274 +++++++++++++++ .../numeric/class-tainacan-numeric.php | 53 +++ .../class-tainacan-relationship.php | 236 +++++++++++++ .../selectbox/class-tainacan-selectbox.php | 75 +++++ .../taxonomy/class-tainacan-taxonomy.php | 311 ++++++++++++++++++ .../text/class-tainacan-text.php | 67 ++++ .../textarea/class-tainacan-textarea.php | 77 +++++ 23 files changed, 2521 insertions(+) create mode 100644 src/classes/filter-types/autocomplete/class-tainacan-autocomplete.php create mode 100644 src/classes/filter-types/checkbox/class-tainacan-checkbox.php create mode 100644 src/classes/filter-types/date-interval/class-tainacan-date-interval.php create mode 100644 src/classes/filter-types/date/class-tainacan-date.php create mode 100644 src/classes/filter-types/filter-type/class-tainacan-filter-type.php create mode 100644 src/classes/filter-types/numeric-interval/class-tainacan-numeric-interval.php create mode 100644 src/classes/filter-types/numeric-list-interval/class-tainacan-numeric-list-interval.php create mode 100644 src/classes/filter-types/numeric/class-tainacan-numeric.php create mode 100644 src/classes/filter-types/selectbox/class-tainacan-selectbox.php create mode 100644 src/classes/filter-types/taginput/class-tainacan-taginput.php create mode 100644 src/classes/filter-types/taxonomy/class-tainacan-taxonomycheckbox.php create mode 100644 src/classes/filter-types/taxonomy/class-tainacan-taxonomytaginput.php create mode 100644 src/classes/metadata-types/compound/class-tainacan-compound.php create mode 100644 src/classes/metadata-types/core-description/class-tainacan-core-description.php create mode 100644 src/classes/metadata-types/core-title/class-tainacan-core-title.php create mode 100644 src/classes/metadata-types/date/class-tainacan-date.php create mode 100644 src/classes/metadata-types/metadata-type/class-tainacan-metadata-type.php create mode 100644 src/classes/metadata-types/numeric/class-tainacan-numeric.php create mode 100644 src/classes/metadata-types/relationship/class-tainacan-relationship.php create mode 100644 src/classes/metadata-types/selectbox/class-tainacan-selectbox.php create mode 100644 src/classes/metadata-types/taxonomy/class-tainacan-taxonomy.php create mode 100644 src/classes/metadata-types/text/class-tainacan-text.php create mode 100644 src/classes/metadata-types/textarea/class-tainacan-textarea.php diff --git a/src/classes/filter-types/autocomplete/class-tainacan-autocomplete.php b/src/classes/filter-types/autocomplete/class-tainacan-autocomplete.php new file mode 100644 index 000000000..13db2a442 --- /dev/null +++ b/src/classes/filter-types/autocomplete/class-tainacan-autocomplete.php @@ -0,0 +1,54 @@ +set_name( __('Autocomplete', 'tainacan') ); + $this->set_supported_types(['string','long_string','item']); + $this->set_component('tainacan-filter-autocomplete'); + $this->set_use_max_options(false); + $this->set_preview_template(' +
+ +
+ '); + } + + /** + * @param $filter + * @return string + */ + + public function render( $filter ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/filter-types/checkbox/class-tainacan-checkbox.php b/src/classes/filter-types/checkbox/class-tainacan-checkbox.php new file mode 100644 index 000000000..ebad4f1d4 --- /dev/null +++ b/src/classes/filter-types/checkbox/class-tainacan-checkbox.php @@ -0,0 +1,71 @@ +set_name( __('Checkbox List', 'tainacan') ); + $this->set_supported_types(['string','long_string','item']); + $this->set_component('tainacan-filter-checkbox'); + $this->set_preview_template(' +
+
+

'. __('Selected values') . ':

+
+
+
+ '. __('Value') . ' 2 + +
+
+
+
+ '. __('Value') . ' 3 + +
+
+
+
+ +
+
+
+ +
+
+ +
+
+ '. __('View all') . ' +
+ '); + } + + /** + * @param $filter + * @return string + */ + + public function render( $filter ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/filter-types/date-interval/class-tainacan-date-interval.php b/src/classes/filter-types/date-interval/class-tainacan-date-interval.php new file mode 100644 index 000000000..d9d8716ca --- /dev/null +++ b/src/classes/filter-types/date-interval/class-tainacan-date-interval.php @@ -0,0 +1,55 @@ +set_name( __('Date Interval', 'tainacan') ); + $this->set_supported_types(['date']); + $this->set_component('tainacan-filter-date-interval'); + $this->set_use_max_options(false); + $this->set_preview_template(' +
+
+ +
+

until

+
+ +
+
+ '); + } + + /** + * @param $filter + * @return string + * @internal param $metadatum + */ + public function render( $filter ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/filter-types/date/class-tainacan-date.php b/src/classes/filter-types/date/class-tainacan-date.php new file mode 100644 index 000000000..eec010199 --- /dev/null +++ b/src/classes/filter-types/date/class-tainacan-date.php @@ -0,0 +1,89 @@ +set_name( __('Date', 'tainacan') ); + $this->set_supported_types(['date']); + $this->set_component('tainacan-filter-date'); + // $this->set_form_component('tainacan-filter-form-date'); + $this->set_use_max_options(false); + // $this->set_default_options([ + // 'type' => 'day' + // ]); + $this->set_preview_template(' +
+
+ +
+
+ '); + } + + // /** + // * @inheritdoc + // */ + // public function get_form_labels(){ + // return [ + // 'type' => [ + // 'title' => __( 'Type', 'tainacan' ), + // 'description' => __( 'The type of the date picker, may be for day, month or year.', 'tainacan' ), + // ] + // ]; + // } + + /** + * @param $filter + * @return string + * @internal param $metadatum + */ + public function render( $filter ){ + return ''; + // type="' . $this->get_option('type') . '" + } + +} \ 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 new file mode 100644 index 000000000..b4a5b48a4 --- /dev/null +++ b/src/classes/filter-types/filter-type/class-tainacan-filter-type.php @@ -0,0 +1,235 @@ +name = $name; + } + + /** + * @return string + */ + public function get_name() { + return $this->name; + } + + /** + * @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; + } + + /** + * @return mixed + */ + public function get_component() { + return $this->component; + } + + /** + * specifies the preview template for the filter type + * + * @param string $preview_template for the filter type + */ + public function set_preview_template($preview_template){ + $this->preview_template = $preview_template; + } + + /** + * @return string + */ + public function get_preview_template() { + return $this->preview_template; + } + + + /** + * @return array + */ + public function _toArray(){ + $attributes = []; + + $attributes['className'] = get_class($this); + $attributes['name'] = $this->get_name(); + $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['form_component'] = $this->get_form_component(); + + return $attributes; + } + + /** + * @param $options + */ + public function set_options( $options ){ + $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 + * + * This method should be declared by each filter type sub classes + * + * @param \Tainacan\Entities\Filter $filter The metadatum object that is beeing validated + * + * @return true|array True if options are valid. If invalid, returns an array where keys are the metadatum keys and values are error messages. + * @throws \Exception + */ + public function validate_options(\Tainacan\Entities\Filter $filter) { + $metadata_type = $filter->get_metadatum()->get_metadata_type(); + //if there is no metadatum to validate + if( !$metadata_type ){ + return true; + } + + $class = ( is_object( $metadata_type ) ) ? $metadata_type : new $metadata_type(); + + if(in_array( $class->get_primitive_type(), $this->supported_types )){ + return true; + } else { + return ['unsupported_type' => __('The metadata primitive type is not supported by this filter', 'tainacan')]; + } + } + + /** + * @param mixed $component + */ + public function set_component( $component ) { + $this->component = $component; + } + + /** + * 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) { + $this->use_max_options = $use_max_options; + } + + 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; + } + + public function get_filter_type() { + return str_replace( 'Tainacan\Filter_Types\\', '', get_class($this) ); + } +} \ No newline at end of file diff --git a/src/classes/filter-types/numeric-interval/class-tainacan-numeric-interval.php b/src/classes/filter-types/numeric-interval/class-tainacan-numeric-interval.php new file mode 100644 index 000000000..ff17a4c9e --- /dev/null +++ b/src/classes/filter-types/numeric-interval/class-tainacan-numeric-interval.php @@ -0,0 +1,87 @@ +set_name( __('Numeric Interval', 'tainacan') ); + $this->set_supported_types(['float']); + $this->set_component('tainacan-filter-numeric-interval'); + $this->set_form_component('tainacan-filter-form-numeric-interval'); + $this->set_use_max_options(false); + $this->set_default_options([ + 'step' => 1 + ]); + $this->set_preview_template(' +
+
+

+ +

+
+ +
+

+ +

+
+

until

+
+

+ +

+
+ +
+

+ +

+
+
+ '); + } + + public function get_form_labels(){ + return [ + 'step' => [ + 'title' => __( 'Step', 'tainacan' ), + 'description' => __( 'The amount to be increased or decreased when clicking on filter control buttons. This alo defines whether the input accepts decimal numbers.', 'tainacan' ), + ], + 'custom' => ['title' => __('Custom interval','tainacan')], + ]; + } + + /** + * @param $filter + * @return string + * @internal param $metadatum + */ + public function render( $filter ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/filter-types/numeric-list-interval/class-tainacan-numeric-list-interval.php b/src/classes/filter-types/numeric-list-interval/class-tainacan-numeric-list-interval.php new file mode 100644 index 000000000..c4ee5943c --- /dev/null +++ b/src/classes/filter-types/numeric-list-interval/class-tainacan-numeric-list-interval.php @@ -0,0 +1,70 @@ +set_name( __('Numeric Interval List', 'tainacan') ); + $this->set_supported_types(['float']); + $this->set_component('tainacan-filter-numeric-list-interval'); + $this->set_form_component('tainacan-filter-form-numeric-list-interval'); + $this->set_use_max_options(false); + $this->set_default_options([ + 'intervals' => [], + 'showIntervalOnTag' => false + ]); + $this->set_preview_template(' +
+ +
+ '); + } + + public function get_form_labels() { + return [ + 'intervals' => [ + 'title' => __('Predefined intervals','tainacan'), + 'description' => __('A list of predefined intervals that the filter will offer on a select box.','tainacan') + ], + 'showIntervalOnTag' => [ + 'title' => __('Interval on tags', 'tainacan'), + 'description' => __('Whether the applyed interval values should appear on filter tags.') + ] + ]; + } + + /** + * @param $filter + * @return string + * @internal param $metadatum + */ + public function render( $filter ) { + return ''; + } +} \ No newline at end of file diff --git a/src/classes/filter-types/numeric/class-tainacan-numeric.php b/src/classes/filter-types/numeric/class-tainacan-numeric.php new file mode 100644 index 000000000..4860b7804 --- /dev/null +++ b/src/classes/filter-types/numeric/class-tainacan-numeric.php @@ -0,0 +1,113 @@ +set_name( __('Numeric', 'tainacan') ); + $this->set_supported_types(['float']); + $this->set_component('tainacan-filter-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(' +
+
+ +
+ '); + } + + /** + * @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 ){ + 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' => __('"Step" value is required','tainacan') + ]; + } + + return true; + } +} \ No newline at end of file diff --git a/src/classes/filter-types/selectbox/class-tainacan-selectbox.php b/src/classes/filter-types/selectbox/class-tainacan-selectbox.php new file mode 100644 index 000000000..57cf4002b --- /dev/null +++ b/src/classes/filter-types/selectbox/class-tainacan-selectbox.php @@ -0,0 +1,39 @@ +set_name( __('Select Box', 'tainacan') ); + $this->set_supported_types(['string', 'long_string']); + $this->set_component('tainacan-filter-selectbox'); + $this->set_use_max_options(false); + $this->set_preview_template(' +
+
+ + + +
+
+ '); + } + + /** + * @param $filter + * @return string + */ + + public function render( $filter ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/filter-types/taginput/class-tainacan-taginput.php b/src/classes/filter-types/taginput/class-tainacan-taginput.php new file mode 100644 index 000000000..086a3b5ea --- /dev/null +++ b/src/classes/filter-types/taginput/class-tainacan-taginput.php @@ -0,0 +1,69 @@ +set_name( __('Tag Input', 'tainacan') ); + $this->set_supported_types(['string','long_string','item']); + $this->set_component('tainacan-filter-taginput'); + $this->set_use_max_options(false); + $this->set_preview_template(' +
+

'. __('Selected values') . ':

+
+
+
+ '. __('Collection') . ' 2 '._('item') . ' 13 + +
+
+
+
+ '. __('Collection') . ' 3 '._('item') . ' 2 + +
+
+
+ +
+ '); + } + + /** + * @param $filter + * @return string + */ + + public function render( $filter ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/filter-types/taxonomy/class-tainacan-taxonomycheckbox.php b/src/classes/filter-types/taxonomy/class-tainacan-taxonomycheckbox.php new file mode 100644 index 000000000..b327ca54a --- /dev/null +++ b/src/classes/filter-types/taxonomy/class-tainacan-taxonomycheckbox.php @@ -0,0 +1,71 @@ +set_name( __('Taxonomy Checkbox List', 'tainacan') ); + $this->set_supported_types(['term']); + $this->set_component('tainacan-filter-taxonomy-checkbox'); + $this->set_preview_template(' +
+
+

'. __('Selected values') . ':

+
+
+
+ '. __('Value') . ' 21 + +
+
+
+
+ '. __('Value') . ' 7 + +
+
+
+
+ +
+
+
+ +
+
+ +
+
+ '. __('View all') . ' +
+ '); + } + + /** + * @param $filter + * @return string + */ + + public function render( $filter ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/filter-types/taxonomy/class-tainacan-taxonomytaginput.php b/src/classes/filter-types/taxonomy/class-tainacan-taxonomytaginput.php new file mode 100644 index 000000000..0be31c430 --- /dev/null +++ b/src/classes/filter-types/taxonomy/class-tainacan-taxonomytaginput.php @@ -0,0 +1,69 @@ +set_name( __('Taxonomy Tag Input', 'tainacan') ); + $this->set_supported_types(['term']); + $this->set_component('tainacan-filter-taxonomy-taginput'); + $this->set_use_max_options(false); + $this->set_preview_template(' +
+

'. __('Selected values') . ':

+
+
+
+ '. __('Value') . ' 21 + +
+
+
+
+ '. __('Value') . ' 7 + +
+
+
+ +
+ '); + } + + /** + * @param $filter + * @return string + */ + + public function render( $filter ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/metadata-types/compound/class-tainacan-compound.php b/src/classes/metadata-types/compound/class-tainacan-compound.php new file mode 100644 index 000000000..66a18f54c --- /dev/null +++ b/src/classes/metadata-types/compound/class-tainacan-compound.php @@ -0,0 +1,100 @@ +set_primitive_type('compound'); + $this->set_component('tainacan-compound'); + } + + /** + * @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata + * @return string + */ + + public function render( $itemMetadata ){ + return 'get_value() ).'\' + name="'.$itemMetadata->get_metadatum()->get_name().'">'; + } + + /** + * generate the metadata for this metadatum type + */ + public function form(){ + + } + + + /** + * Return the value of an Item_Metadata_Entity using a metadatum of this metadatum type as an html string + * @param Item_Metadata_Entity $item_metadata + * @return string The HTML representation of the value, each HTML representation of the value of each metadatum composing this metadata + */ + public function get_value_as_html(Item_Metadata_Entity $item_metadata) { + + $value = $item_metadata->get_value(); + + $return = ''; + + if ( $item_metadata->is_multiple() ) { + + $return .= '
'; + + foreach ( $value as $compound_element ) { + + $return .= '
'; + + foreach ( $compound_element as $meta ) { + if ( $meta instanceof Item_Metadata_Entity ) { + $return .= '

' . $meta->get_metadatum()->get_name() . "

\n"; + $return .= '

' . $meta->get_value_as_html() . '

' . "\n\n"; + } + } + + $return .= '
' . "\n\n"; + + } + + $return .= '
' . "\n\n"; + + + } else { + + foreach ( $value as $meta ) { + + $return .= '
'; + + if ( $meta instanceof Item_Metadata_Entity ) { + $return .= '

' . $meta->get_metadatum()->get_name() . "

\n"; + $return .= '

' . $meta->get_value_as_html() . '

'; + } + + $return .= '
' . "\n\n"; + + } + + } + + return $return; + + } + + + +} \ No newline at end of file diff --git a/src/classes/metadata-types/core-description/class-tainacan-core-description.php b/src/classes/metadata-types/core-description/class-tainacan-core-description.php new file mode 100644 index 000000000..8544d92a5 --- /dev/null +++ b/src/classes/metadata-types/core-description/class-tainacan-core-description.php @@ -0,0 +1,113 @@ +set_primitive_type('string'); + $this->set_core(true); + $this->set_related_mapped_prop('description'); + $this->set_component('tainacan-textarea'); + $this->set_name( __('Core Description', 'tainacan') ); + $this->set_description( __('The "Core Description" is a compulsory metadata automatically created for all collections by default. This is the main description displayed in items lists and where the basic research tools will do their searches.', 'tainacan') ); + } + + /** + * @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata + * @return string + */ + + public function render( $itemMetadata ){ + return 'get_value() ).'\' + name="'.$itemMetadata->get_metadatum()->get_name().'">'; + } + + /** + * generate the metadata for this metadatum type + */ + public function form(){ + + } + + /** + * Description core Metadatum type is stored as the item content (description) + * + * Lets validate it as the item description + * + * @param TainacanEntitiesItem_Metadata_Entity $item_metadata + * @return bool Valid or not + * + * Quarantine - Core metadata should be validated as any other metadata + * and item title is no longer mandatory + + public function validate(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { + + $item = $item_metadata->get_item(); + + if ( !in_array($item->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) + return true; + + $item->set_description($item_metadata->get_value()); + + return $item->validate_prop('description'); + + } + + */ + + public function validate_options( Metadatum $metadatum ) { + + if ( !in_array($metadatum->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) + return true; + + if ( $metadatum->get_multiple() != 'no') { + return ['multiple' => __('Core Metadata can not accept multiple values', 'tainacan')]; + } + + return true; + + } + + /** + * Get the value as a HTML string with links and breakline tag. + * @return string + */ + public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { + $value = $item_metadata->get_value(); + $return = ''; + if ( $item_metadata->is_multiple() ) { + $total = sizeof($value); + $count = 0; + $prefix = $item_metadata->get_multivalue_prefix(); + $suffix = $item_metadata->get_multivalue_suffix(); + $separator = $item_metadata->get_multivalue_separator(); + foreach ( $value as $el ) { + $return .= $prefix; + $return .= nl2br($this->make_clickable_links($el)); + $return .= $suffix; + $count ++; + if ($count < $total) + $return .= $separator; + } + } else { + $return = nl2br($this->make_clickable_links($value)); + } + return $return; + } + +} \ No newline at end of file diff --git a/src/classes/metadata-types/core-title/class-tainacan-core-title.php b/src/classes/metadata-types/core-title/class-tainacan-core-title.php new file mode 100644 index 000000000..e0c2ba997 --- /dev/null +++ b/src/classes/metadata-types/core-title/class-tainacan-core-title.php @@ -0,0 +1,85 @@ +set_primitive_type('string'); + $this->set_core(true); + $this->set_related_mapped_prop('title'); + $this->set_component('tainacan-text'); + $this->set_name( __('Core Title', 'tainacan') ); + $this->set_description( __('The "Core Title" is a compulsory metadata automatically created for all collections by default. It is the main metadatum of the item and where the basic research tools will do their searches.', 'tainacan') ); + } + + /** + * @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata + * @return string + */ + + public function render( $itemMetadata ){ + return 'get_value() ).'\' + name="'.$itemMetadata->get_metadatum()->get_name().'">'; + } + + /** + * generate the metadata for this metadatum type + */ + public function form(){ + + } + + /** + * Core title metadatum type is stored as the item title + * + * Lets validate it as the item title + * + * @param TainacanEntitiesItem_Metadata_Entity $item_metadata + * @return bool Valid or not + * + * Quarantine - Core metadata should be validated as any other metadata + * and item title is no longer mandatory + + public function validate(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { + + $item = $item_metadata->get_item(); + + if ( !in_array($item->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) + return true; + + $item->set_title($item_metadata->get_value()); + + return $item->validate_prop('title'); + + } + */ + + public function validate_options( Metadatum $metadatum ) { + + if ( !in_array($metadatum->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) + return true; + + if ( $metadatum->get_multiple() != 'no') { + return ['multiple' => __('Core Metadata can not accept multiple values', 'tainacan')]; + } + + return true; + + } + +} \ No newline at end of file diff --git a/src/classes/metadata-types/date/class-tainacan-date.php b/src/classes/metadata-types/date/class-tainacan-date.php new file mode 100644 index 000000000..6946822b9 --- /dev/null +++ b/src/classes/metadata-types/date/class-tainacan-date.php @@ -0,0 +1,108 @@ +set_primitive_type('date'); + $this->set_component('tainacan-date'); + $this->set_name( __('Date', 'tainacan') ); + $this->set_description( __('Exact date type, with day, month and year.', 'tainacan') ); + $this->set_preview_template(' +
+
+ +
+
+ '); + } + + /** + * @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata + * @return string + */ + + public function render( $itemMetadata ){ + return 'get_value() ).'\' + name="'.$itemMetadata->get_metadatum()->get_name().'">'; + } + + + public function validate( Item_Metadata_Entity $item_metadata) { + $value = $item_metadata->get_value(); + $format = 'Y-m-d'; + + if (is_array($value)) { + foreach ($value as $date_value) { + $d = \DateTime::createFromFormat($format, $date_value); + if (!$d || $d->format($format) !== $date_value) { + $this->add_error( + sprintf( + __('Invalid date format. Expected format is YYYY-MM-DD, got %s.', 'tainacan'), + $date_value + ) + ); + return false; + } + } + return True; + } + + $d = \DateTime::createFromFormat($format, $value); + + if (!$d || $d->format($format) !== $value) { + $this->add_error( + sprintf( + __('Invalid date format. Expected format is YYYY-MM-DD, got %s.', 'tainacan'), + $value + ) + ); + return false; + } + return true; + + } + + /** + * Get the value as a HTML string with proper date format set in admin + * @return string + */ + public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { + + $value = $item_metadata->get_value(); + $return = ''; + if ( $item_metadata->is_multiple() ) { + $total = sizeof($value); + $count = 0; + $prefix = $item_metadata->get_multivalue_prefix(); + $suffix = $item_metadata->get_multivalue_suffix(); + $separator = $item_metadata->get_multivalue_separator(); + foreach ( $value as $el ) { + $return .= $prefix; + $return .= mysql2date(get_option('date_format'), ($el)); + $return .= $suffix; + $count ++; + if ($count < $total) + $return .= $separator; + } + } else { + $return = mysql2date(get_option('date_format'), ($value)); + } + return $return; + + } + + +} \ No newline at end of file diff --git a/src/classes/metadata-types/metadata-type/class-tainacan-metadata-type.php b/src/classes/metadata-types/metadata-type/class-tainacan-metadata-type.php new file mode 100644 index 000000000..9504cb511 --- /dev/null +++ b/src/classes/metadata-types/metadata-type/class-tainacan-metadata-type.php @@ -0,0 +1,274 @@ +related_mapped_prop; + } + + public function set_related_mapped_prop($related_mapped_prop){ + $this->related_mapped_prop = $related_mapped_prop; + } + + public function get_validation_errors() { + return []; + } + + public function get_primitive_type(){ + return $this->primitive_type; + } + + public function set_primitive_type($primitive_type){ + $this->primitive_type = $primitive_type; + } + + public function get_errors() { + return $this->errors; + } + + public function get_component() { + return $this->component; + } + + public function set_component($component){ + $this->component = $component; + } + + public function get_form_component() { + return $this->form_component; + } + + public function set_form_component($form_component){ + $this->form_component = $form_component; + } + + public function get_preview_template() { + return $this->preview_template; + } + + public function set_preview_template($preview_template){ + $this->preview_template = $preview_template; + } + + public function get_name(){ + return $this->name; + } + + public function set_name($name){ + $this->name = $name; + } + + public function get_description(){ + return $this->description; + } + + public function set_description($description){ + $this->description = $description; + } + + public function add_error($msg) { + $this->errors[] = $msg; + } + + /** + * @param $options + */ + public function set_options( $options ){ + $this->options = ( is_array( $options ) ) ? $options : (!is_array(unserialize( $options )) ? [] : unserialize( $options )); + } + + public function set_default_options(Array $options) { + $this->default_options = $options; + } + + /** + * Gets the options for this metadatum types, including default values for options + * that were not set yet. + * @return array Metadatum type options + */ + public function get_options() { + return array_merge($this->default_options, $this->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 []; + } + /** + * generate the metadata for this metadatum type + */ + public function form(){ + + } + + public function _toArray(){ + $attributes = []; + + $attributes['name'] = $this->get_name(); + $attributes['description'] = $this->get_description(); + $attributes['errors'] = $this->get_errors(); + $attributes['related_mapped_prop'] = $this->get_related_mapped_prop(); + $attributes['options'] = $this->get_options(); + $attributes['className'] = get_class($this); + $attributes['core'] = $this->get_core(); + $attributes['component'] = $this->get_component(); + $attributes['primitive_type'] = $this->get_primitive_type(); + $attributes['form_component'] = $this->get_form_component(); + $attributes['preview_template'] = $this->get_preview_template(); + + return $attributes; + + } + + /** + * Validates the options Array + * + * This method should be declared by each metadatum type sub classes + * + * @param \Tainacan\Entities\Metadatum $metadatum The metadatum object that is beeing validated + * @return true|Array True if optinos are valid. If invalid, returns an array where keys are the metadatum keys and values are error messages. + */ + public function validate_options(\Tainacan\Entities\Metadatum $metadatum) { + return true; + } + + /** + * @return mixed + */ + public function get_core() { + return $this->core; + } + + public function set_core($core){ + $this->core = $core; + } + + public function get_repository() { + return $this->repository; + } + + public function set_repository($repository){ + $this->repository = $repository; + } + + /** + * Gets a slug based on the class name to represent the metadata type + */ + public function get_slug() { + $classname = get_class($this); + return strtolower( substr($classname, strrpos($classname, '\\') + 1) ); + } +} \ No newline at end of file diff --git a/src/classes/metadata-types/numeric/class-tainacan-numeric.php b/src/classes/metadata-types/numeric/class-tainacan-numeric.php new file mode 100644 index 000000000..2112aa130 --- /dev/null +++ b/src/classes/metadata-types/numeric/class-tainacan-numeric.php @@ -0,0 +1,53 @@ +set_name( __('Numeric', 'tainacan') ); + $this->set_primitive_type('float'); + $this->set_component('tainacan-numeric'); + $this->set_form_component('tainacan-form-numeric'); + $this->set_description( __('A numeric value, integer or float', 'tainacan') ); + $this->set_preview_template(' +
+
+ +
+
+ '); + } + + /** + * @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 $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata + * @return string + */ + + public function render( $itemMetadata ){ + return 'get_value() ).'\' + name="'.$itemMetadata->get_metadatum()->get_name().'">'; + } +} \ No newline at end of file diff --git a/src/classes/metadata-types/relationship/class-tainacan-relationship.php b/src/classes/metadata-types/relationship/class-tainacan-relationship.php new file mode 100644 index 000000000..698d5d88a --- /dev/null +++ b/src/classes/metadata-types/relationship/class-tainacan-relationship.php @@ -0,0 +1,236 @@ +set_primitive_type('item'); + $this->set_repository( \Tainacan\Repositories\Items::get_instance() ); + $this->set_component('tainacan-relationship'); + $this->set_form_component('tainacan-form-relationship'); + $this->set_name( __('Relationship', 'tainacan') ); + $this->set_description( __('A relationship with another item', 'tainacan') ); + $this->set_preview_template(' +
+ +
+ '); + } + + /** + * @inheritdoc + */ + public function get_form_labels(){ + return [ + 'collection_id' => [ + 'title' => __( 'Related Collection', 'tainacan' ), + 'description' => __( 'Select the collection to fetch items', 'tainacan' ), + ], + 'search' => [ + 'title' => __( 'Related Metadatum', 'tainacan' ), + 'description' => __( 'Select the metadata to use as search criteria in the target collection and as a label when representing the relationship', 'tainacan' ), + ], + 'repeated' => [ + 'title' =>__( 'Allow repeated items', 'tainacan' ), + 'description' => __( 'Allows different items to be related to the same item selected in another collection.', 'tainacan' ), + ] + ]; + } + + /** + * @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata + * @return string + */ + + public function render( $itemMetadata ){ + return 'get_value() ).'\' + name="'.$itemMetadata->get_metadatum()->get_name().'">'; + } + + public function validate_options(\Tainacan\Entities\Metadatum $metadatum) { + if ( !in_array($metadatum->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) + return true; + + if (!empty($this->get_option('collection_id')) && !is_numeric($this->get_option('collection_id'))) { + return [ + 'collection_id' => __('Invalid collection ID','tainacan') + ]; + } else if( empty($this->get_option('collection_id'))) { + return [ + 'collection_id' => __('The related collection is required','tainacan') + ]; + } + // empty is ok + if ( !empty($this->get_option('search')) && !is_numeric($this->get_option('search')) ) { + return [ + 'search' => __('Search option must be a numeric Metadatum ID','tainacan') + ]; + } + + return true; + } + + /** + * Return the value of an Item_Metadata_Entity using a metadatum of this metadatum type as an html string + * @param Item_Metadata_Entity $item_metadata + * @return string The HTML representation of the value, containing one or multiple items names, linked to the item page + */ + public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { + + $value = $item_metadata->get_value(); + + $return = ''; + + if ( $item_metadata->is_multiple() ) { + + $count = 1; + $total = sizeof($value); + $prefix = $item_metadata->get_multivalue_prefix(); + $suffix = $item_metadata->get_multivalue_suffix(); + $separator = $item_metadata->get_multivalue_separator(); + + foreach ( $value as $item_id ) { + + try { + + //$item = new \Tainacan\Entities\Item($item_id); + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + $item = $Tainacan_Items->fetch( (int) $item_id); + + + $count ++; + + if ( $item instanceof \Tainacan\Entities\Item ) { + + $return .= $prefix; + + $return .= $this->get_item_html($item); + + $return .= $suffix; + + if ( $count <= $total ) { + $return .= $separator; + } + + } + + + } catch (\Exception $e) { + // item not found + } + + } + + } else { + + try { + + $item = new \Tainacan\Entities\Item($value); + + if ( $item instanceof \Tainacan\Entities\Item ) { + $return .= $this->get_item_html($item); + } + + } catch (\Exception $e) { + // item not found + } + + } + + return $return; + + } + + private function get_item_html($item) { + + $return = ''; + $id = $item->get_id(); + + $search_meta_id = $this->get_option('search'); + + if ( $id && $search_meta_id ) { + + $link = get_permalink( (int) $id ); + + $search_meta_id = $this->get_option('search'); + + $metadatum = \Tainacan\Repositories\Metadata::get_instance()->fetch((int) $search_meta_id); + + $label = ''; + + if ($metadatum instanceof \Tainacan\Entities\Metadatum) { + $item_meta = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadatum); + $label = $item_meta->get_value_as_string(); + } + + if ( empty($label) ) { + $label = $item->get_title(); + } + + if (is_string($link)) { + + $return = ""; + $return.= $label; + $return .= ""; + + } + + } + + return $return; + + } + + /** + * Get related Collection object + * @return \Tainacan\Entities\Collection|false The Collection object or false + */ + public function get_collection() { + + $collection_id = $this->get_option('collection_id'); + + if ( is_numeric($collection_id) ) { + $collection = \Tainacan\Repositories\Collections::get_instance()->fetch( (int) $collection_id ); + if ( $collection instanceof \Tainacan\Entities\Collection ) { + return $collection; + } + } + + return false; + + } + +} \ No newline at end of file diff --git a/src/classes/metadata-types/selectbox/class-tainacan-selectbox.php b/src/classes/metadata-types/selectbox/class-tainacan-selectbox.php new file mode 100644 index 000000000..69bafe381 --- /dev/null +++ b/src/classes/metadata-types/selectbox/class-tainacan-selectbox.php @@ -0,0 +1,75 @@ +set_primitive_type('string'); + $this->set_component('tainacan-selectbox'); + $this->set_form_component('tainacan-form-selectbox'); + $this->set_name( __('Selectbox', 'tainacan') ); + $this->set_description( __('A selectbox with a fixed list of value to choose one from', 'tainacan') ); + $this->set_preview_template(' +
+
+ + + +
+
+ '); + } + + /** + * @inheritdoc + */ + public function get_form_labels(){ + return [ + 'options' => [ + 'title' => __( 'Options', 'tainacan' ), + 'description' => __( 'Creates options for what is selected. Hit to add a new one.', 'tainacan' ), + ] + ]; + } + /** + * @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata + * @return string + */ + + public function render( $itemMetadata ){ + $options = $this->get_option('options'); + return 'get_value() ).'\' + name="'.$itemMetadata->get_metadatum()->get_name().'">'; + } + + /** + * @param \Tainacan\Entities\Metadatum $metadatum + * @return array|bool true if is validate or array if has error + */ + public function validate_options(\Tainacan\Entities\Metadatum $metadatum) { + if ( !in_array($metadatum->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) + return true; + + if ( empty($this->get_option('options')) ) { + return [ + 'options' => __('Required options','tainacan') + ]; + } + + return true; + } +} diff --git a/src/classes/metadata-types/taxonomy/class-tainacan-taxonomy.php b/src/classes/metadata-types/taxonomy/class-tainacan-taxonomy.php new file mode 100644 index 000000000..cb2bec569 --- /dev/null +++ b/src/classes/metadata-types/taxonomy/class-tainacan-taxonomy.php @@ -0,0 +1,311 @@ +set_primitive_type('term'); + $this->set_repository( \Tainacan\Repositories\Terms::get_instance() ); + + $this->set_default_options([ + 'allow_new_terms' => 'no' + ]); + + $this->set_form_component('tainacan-form-taxonomy'); + $this->set_component('tainacan-taxonomy'); + $this->set_name( __('Taxonomy', 'tainacan') ); + $this->set_description( __('A metadatum to use a taxonomy in this collection', 'tainacan') ); + $this->set_preview_template(' +
+
+

'. __('Selected terms') . ':

+
+
+
+ '. __('Term') . ' 2 + +
+
+
+
+ '. __('Term') . ' 3 + +
+
+
+
+ +
+
+
+ +
+
+ +
+
+ '. __('View all') . ' +
+ '); + } + + /** + * @inheritdoc + */ + public function get_form_labels(){ + return [ + 'taxonomy_id' => [ + 'title' => __( 'Related Collection', 'tainacan' ), + 'description' => __( 'Select the collection to fetch items', 'tainacan' ), + ], + 'input_type' => [ + 'title' => __( 'Input type', 'tainacan' ), + 'description' => __( 'The html type of the terms list ', 'tainacan' ), + ], + 'allow_new_terms' => [ + 'title' => __( 'Allow new terms', 'tainacan' ), + 'description' => __( 'Allows to create new terms', 'tainacan' ), + ] + ]; + } + + /** + * @param $itemMetadata Item_Metadata_Entity The instace of the entity itemMetadata + * @return string + */ + + public function render( $itemMetadata ){ + $options = ( isset( $this->get_options()['options'] ) ) ? $this->get_options()['options'] : ''; + return 'get_value() ).'\' + name="'.$itemMetadata->get_metadatum()->get_name().'">'; + } + + public function validate_options( Metadatum $metadatum) { + + if ( !in_array($metadatum->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) + return true; + + if (empty($this->get_option('taxonomy_id'))) + return ['taxonomy_id' => __('Please select a taxonomy', 'tainacan')]; + + $Tainacan_Metadata = Metadata::get_instance(); + + // Check taxonomy visibility + $status = get_post_status( $this->get_option('taxonomy_id') ); + $post_status_obj = get_post_status_object($status); + if ( ! $post_status_obj->public ) { + $meta_status_obj = get_post_status_object($metadatum->get_status()); + if ( $meta_status_obj->public ) { + return ['taxonomy_id' => __('This metadatum can not be public because chosen taxonomy is not.', 'tainacan')]; + } + } + + if ( $this->get_option('allow_new_terms') == 'yes' ) { + $taxonomy = \tainacan_taxonomies()->fetch( (int) $this->get_option('taxonomy_id') ); + if ( $taxonomy instanceof \Tainacan\Entities\Taxonomy ) { + if ( $taxonomy->get_allow_insert() != 'yes' ) { + return ['allow_new_terms' => __('This metadatum can not allow new terms to be created because the chosen taxonomy does not allow it.', 'tainacan')]; + } + } + } + + $taxonomy_metadata = $Tainacan_Metadata->fetch([ + 'collection_id' => $metadatum->get_collection_id(), + 'metadata_type' => 'Tainacan\\Metadata_Types\\Taxonomy' + ], 'OBJECT'); + + $taxonomy_metadata = array_map(function ($metadatum_map) { + $fto = $metadatum_map->get_metadata_type_object(); + return [ $metadatum_map->get_id() => $fto->get_option('taxonomy_id') ]; + }, $taxonomy_metadata); + + if( is_array( $taxonomy_metadata ) ){ + foreach ($taxonomy_metadata as $metadatum_id => $taxonomy_metadatum) { + if ( is_array( $taxonomy_metadatum ) && key($taxonomy_metadatum) != $metadatum->get_id() + && in_array($this->get_option('taxonomy_id'), $taxonomy_metadatum)) { + return ['taxonomy_id' => __('You can not have 2 taxonomy metadata using the same taxonomy in a collection.', 'tainacan')]; + } + } + } + + return true; + + } + + /** + * Validate item based on metadatum type taxonomies options + * + * @param Item_Metadata_Entity $item_metadata + * + * @return bool Valid or not + */ + public function validate( Item_Metadata_Entity $item_metadata) { + + $item = $item_metadata->get_item(); + + if ( !in_array($item->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) + return true; + + $valid = true; + + if ('no' === $this->get_option('allow_new_terms') || false === $this->get_option('allow_new_terms')) { //support legacy bug when it was saved as false + $terms = $item_metadata->get_value(); + + if (false === $terms) + return true; + + if (!is_array($terms)) + $terms = array($terms); + + foreach ($terms as $term) { + if (is_object($term) && $term instanceof \Tainacan\Entities\Term) { + $term = $term->get_id(); + } + + // TODO term_exists is not fully reliable. Use $terms_repository->term_exists. see issue #159 + if (!term_exists($term)) { + $valid = false; + $this->add_error(__('term not found.', 'tainacan')); + break; + } + } + + } + + return $valid; + + } + + /** + * Return the value of an Item_Metadata_Entity using a metadatum of this metadatum type as an html string + * @param Item_Metadata_Entity $item_metadata + * @return string The HTML representation of the value, containing one or multiple terms, separated by comma, linked to term page + */ + public function get_value_as_html(Item_Metadata_Entity $item_metadata) { + + $value = $item_metadata->get_value(); + + $return = ''; + + if ( $item_metadata->is_multiple() ) { + + $count = 1; + $total = sizeof($value); + $prefix = $item_metadata->get_multivalue_prefix(); + $suffix = $item_metadata->get_multivalue_suffix(); + $separator = $item_metadata->get_multivalue_separator(); + + foreach ( $value as $term ) { + + $count ++; + + if ( is_integer($term) ) { + $term = \Tainacan\Repositories\Terms::get_instance()->fetch($term, $this->get_option('taxonomy_id')); + } + + if ( $term instanceof \Tainacan\Entities\Term ) { + $return .= $prefix; + + $return .= $this->get_term_hierarchy_html($term); + + $return .= $suffix; + + if ( $count <= $total ) { + $return .= $separator; + } + + } + + } + + } else { + + if ( $value instanceof \Tainacan\Entities\Term ) { + $return .= $this->get_term_hierarchy_html($value); + } + + } + + return $return; + + } + + private function get_term_hierarchy_html( \Tainacan\Entities\Term $term ) { + + $terms = []; + + $terms[] = $term->_toHtml(); + + while ($term->get_parent() > 0) { + $term = \Tainacan\Repositories\Terms::get_instance()->fetch( (int) $term->get_parent(), $term->get_taxonomy() ); + $terms[] = $term->_toHtml(); + } + + $terms = \array_reverse($terms); + + $glue = apply_filters('tainacan-terms-hierarchy-html-separator', ' > '); + + return \implode($glue, $terms); + + } + + public function _toArray() { + + $array = parent::_toArray(); + + if ( isset($array['options']['taxonomy_id']) ) { + $array['options']['taxonomy'] = \Tainacan\Repositories\Taxonomies::get_instance()->get_db_identifier_by_id( $array['options']['taxonomy_id'] ); + } + + return $array; + + } + + /** + * Get related taxonomy object + * @return \Tainacan\Entities\Taxonomy|false The Taxonomy object or false + */ + public function get_taxonomy() { + + $taxonomy_id = $this->get_option('taxonomy_id'); + + if ( is_numeric($taxonomy_id) ) { + $taxonomy = \Tainacan\Repositories\Taxonomies::get_instance()->fetch( (int) $taxonomy_id ); + if ( $taxonomy instanceof \Tainacan\Entities\Taxonomy ) { + return $taxonomy; + } + } + + return false; + + } + +} diff --git a/src/classes/metadata-types/text/class-tainacan-text.php b/src/classes/metadata-types/text/class-tainacan-text.php new file mode 100644 index 000000000..552da64a7 --- /dev/null +++ b/src/classes/metadata-types/text/class-tainacan-text.php @@ -0,0 +1,67 @@ +set_name( __('Text', 'tainacan') ); + $this->set_description( __('A simple, one line, text input', 'tainacan') ); + $this->set_preview_template(' +
+
+ +
+
+ '); + } + + /** + * @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata + * @return string + */ + + public function render( $itemMetadata ){ + return 'get_value() ).'\' + name="'.$itemMetadata->get_metadatum()->get_name().'">'; + } + + /** + * Get the value as a HTML string with links + * @return string + */ + public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { + $value = $item_metadata->get_value(); + $return = ''; + if ( is_array($value) && $item_metadata->is_multiple() ) { + $total = sizeof($value); + $count = 0; + $prefix = $item_metadata->get_multivalue_prefix(); + $suffix = $item_metadata->get_multivalue_suffix(); + $separator = $item_metadata->get_multivalue_separator(); + foreach ( $value as $el ) { + $return .= $prefix; + $return .= $this->make_clickable_links($el); + $return .= $suffix; + $count ++; + if ($count < $total) + $return .= $separator; + } + } else { + $return = $this->make_clickable_links($value); + } + return $return; + } +} \ No newline at end of file diff --git a/src/classes/metadata-types/textarea/class-tainacan-textarea.php b/src/classes/metadata-types/textarea/class-tainacan-textarea.php new file mode 100644 index 000000000..a71d7f549 --- /dev/null +++ b/src/classes/metadata-types/textarea/class-tainacan-textarea.php @@ -0,0 +1,77 @@ +set_primitive_type('long_string'); + $this->set_component('tainacan-textarea'); + $this->set_name( __('Textarea', 'tainacan') ); + $this->set_description( __('A textarea with multiple lines', 'tainacan') ); + $this->set_preview_template(' +
+
+ +
+
+ '); + + } + + /** + * @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata + * @return string + */ + + public function render( $itemMetadata ) { + return 'get_value() ).'\' + name="'.$itemMetadata->get_metadatum()->get_name().'">'; + } + + public function get_multivalue_prefix() { + return '

'; + } + + public function get_multivalue_suffix() { + return '

'; + } + + /** + * Get the value as a HTML string with links and breakline tag. + * @return string + */ + public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { + $value = $item_metadata->get_value(); + $return = ''; + if ( $item_metadata->is_multiple() ) { + $total = sizeof($value); + $count = 0; + $prefix = $item_metadata->get_multivalue_prefix(); + $suffix = $item_metadata->get_multivalue_suffix(); + $separator = $item_metadata->get_multivalue_separator(); + foreach ( $value as $el ) { + $return .= $prefix; + $return .= nl2br($this->make_clickable_links($el)); + $return .= $suffix; + $count ++; + if ($count < $total) + $return .= $separator; + } + } else { + $return = nl2br($this->make_clickable_links($value)); + } + return $return; + } +} \ No newline at end of file