Temporaly keeps copy of filter and metadata types on classes folder.

This commit is contained in:
Mateus Machado Luna 2020-01-15 14:08:08 -03:00
parent 5cb8183ee0
commit ed88d12629
23 changed files with 2521 additions and 0 deletions

View File

@ -0,0 +1,54 @@
<?php
namespace Tainacan\Filter_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Autocomplete extends Filter_Type {
function __construct(){
$this->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('
<div>
<div class="taginput control is-expanded has-selected">
<div class="taginput-container is-focusable">
<div class="autocomplete control">
<div class="control has-icon-right is-loading is-clearfix">
<input type="text" class="input" value="'. __('Item') . ' 9" >
</div>
<div class="dropdown-menu" style="">
<div class="dropdown-content">
<a class="dropdown-item is-hovered">
<span>'. __('Collection') . ' 2 <strong>'._('item') . ' 9</strong>9</span>
</a>
<a class="dropdown-item">
<span>'. __('Collection') . ' 3 <strong>'._('item') . ' 9</strong>9</span>
</a>
<a class="dropdown-item">
<span>'. __('Collection') . ' 3 <strong>'._('item') . ' 9</strong>8</span>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
');
}
/**
* @param $filter
* @return string
*/
public function render( $filter ){
return '<tainacan-filter-autocomplete name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum()->get_id().'"></tainacan-filter-autocomplete>';
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace Tainacan\Filter_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Checkbox extends Filter_Type {
function __construct(){
$this->set_name( __('Checkbox List', 'tainacan') );
$this->set_supported_types(['string','long_string','item']);
$this->set_component('tainacan-filter-checkbox');
$this->set_preview_template('
<div>
<div>
<p class="has-text-gray">'. __('Selected values') . ': </p>
<div class="field selected-tags is-grouped-multiline is-grouped">
<div>
<div class="tags has-addons">
<span class="tag"><span>'. __('Value') . ' 2</span></span>
<a class="tag is-delete"></a>
</div>
</div>
<div>
<div class="tags has-addons">
<span class="tag"><span>'. __('Value') . ' 3</span></span>
<a class="tag is-delete"></a>
</div>
</div>
</div>
<div>
<label class="b-checkbox checkbox" border="" style="padding-left: 8px;">
<input type="checkbox" value="option1">
<span class="check"></span>
<span class="control-label">'. __('Value') . ' 1</span>
</label>
<br>
</div>
<div>
<label class="b-checkbox checkbox" border="" style="padding-left: 8px;">
<input type="checkbox" checked value="option2">
<span class="check"></span>
<span class="control-label">'. __('Value') . ' 2</span>
</label>
</div>
<div>
<label class="b-checkbox checkbox" border="" style="padding-left: 8px;">
<input type="checkbox" checked value="option3">
<span class="check"></span>
<span class="control-label">'. __('Value') . ' 3</span>
</label>
</div>
</div>
<a class="add-new-term">'. __('View all') . '</a>
</div>
');
}
/**
* @param $filter
* @return string
*/
public function render( $filter ){
return '<tainacan-filter-checkbox name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-selectbox>';
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace Tainacan\Filter_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Date_Interval extends Filter_Type {
function __construct(){
$this->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('
<div>
<div class="datepicker control is-small">
<div class="dropdown is-bottom-left is-mobile-modal">
<div role="button" class="dropdown-trigger">
<div class="control has-icons-left is-small is-clearfix">
<input type="text" autocomplete="off" placeholder=" '. __('Select a date', 'tainacan') .'" class="input is-small">
<span class="icon is-left is-small"><i class="mdi mdi-calendar-today"></i></span>
</div>
</div>
</div>
</div>
<p class="is-size-7 has-text-centered is-marginless">until</p>
<div class="datepicker control is-small">
<div class="dropdown is-bottom-left is-mobile-modal">
<div role="button" class="dropdown-trigger">
<div class="control has-icons-left is-small is-clearfix">
<input type="text" autocomplete="off" placeholder=" '. __('Select a date', 'tainacan') .'" class="input is-small">
<span class="icon is-left is-small"><i class="mdi mdi-calendar-today"></i></span>
</div>
</div>
</div>
</div>
</div>
');
}
/**
* @param $filter
* @return string
* @internal param $metadatum
*/
public function render( $filter ){
return '<tainacan-filter-date-interval
name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum()->get_id().'"></tainacan-filter-date-interval>';
}
}

View File

@ -0,0 +1,89 @@
<?php
namespace Tainacan\Filter_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanFilterType
*/
class Date extends Filter_Type {
function __construct(){
$this->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('
<div>
<div>
<div class="date-filter-container">
<div class="dropdown is-active">
<div role="button" class="dropdown-trigger">
<button class="button is-white">
<span class="icon is-small">
<i>=</i>
</span>
<span class="icon">
<i class="tainacan-icon tainacan-icon-20px tainacan-icon-arrowdown"></i>
</span>
</button>
</div>
<div class="background" style="display: none;"></div>
<div class="dropdown-menu" style="display: none;">
<div role="list" class="dropdown-content">
<a class="dropdown-item is-active">=&nbsp; ' . __('Equal', 'tainacan') .'</a>
<a class="dropdown-item">&nbsp; '. __('Not equal', 'tainacan') .'</a>
<a class="dropdown-item">&gt;&nbsp; '. __('After', 'tainacan') .'</a>
<a class="dropdown-item">&nbsp; '. __('After (inclusive)', 'tainacan') .'</a>
<a class="dropdown-item">&lt;&nbsp; '. __('Before', 'tainacan') .'</a>
<a class="dropdown-item">&nbsp; '. __('Before (inclusive)', 'tainacan') .'</a>
</div>
</div>
</div>
<div class="datepicker control is-small">
<div class="dropdown is-bottom-left is-mobile-modal">
<div role="button" class="dropdown-trigger">
<div class="control has-icons-left is-small is-clearfix">
<input type="text" autocomplete="off" placeholder=" '. __('Select a date', 'tainacan') .'" class="input is-small">
<span class="icon is-left is-small"><i class="mdi mdi-calendar-today"></i></span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
');
}
// /**
// * @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 '<tainacan-filter-date
name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-date>';
// type="' . $this->get_option('type') . '"
}
}

View File

@ -0,0 +1,235 @@
<?php
namespace Tainacan\Filter_Types;
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 filter type that will be rendered on labels
* @var string
*/
private $name;
/**
* 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;
public function __construct(){
add_action('register_filter_types', array(&$this, 'register_filter_type'));
}
abstract function render( $metadatum );
/**
* generate the metadata for this metadatum type
*/
public function form(){
}
/**
* @param string $name for the filter type
*/
public function set_name($name){
$this->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) );
}
}

View File

@ -0,0 +1,87 @@
<?php
namespace Tainacan\Filter_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Numeric_Interval extends Filter_Type {
function __construct(){
$this->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('
<div>
<div class="b-numberinput field is-grouped">
<p class="control">
<button type="button" class="button is-primary is-small">
<span class="icon is-small">
<i class="mdi mdi-minus"></i>
</span>
</button>
</p>
<div class="control is-small is-clearfix">
<input type="number" step="0.01" class="input is-small" value="6">
</div>
<p class="control">
<button type="button" class="button is-primary is-small">
<span class="icon is-small">
<i class="mdi mdi-plus"></i>
</span>
</button>
</p>
</div>
<p class="is-size-7 has-text-centered is-marginless">until</p>
<div class="b-numberinput field is-grouped">
<p class="control">
<button type="button" class="button is-primary is-small">
<span class="icon is-small">
<i class="mdi mdi-minus"></i>
</span>
</button>
</p>
<div class="control is-small is-clearfix">
<input type="number" step="0.01" class="input is-small" value="10">
</div>
<p class="control">
<button type="button" class="button is-primary is-small">
<span class="icon is-small">
<i class="mdi mdi-plus"></i>
</span>
</button>
</p>
</div>
</div>
');
}
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 '<tainacan-filter-numeric-interval
name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum()->get_id().'"></tainacan-filter-numeric-interval>';
}
}

View File

@ -0,0 +1,70 @@
<?php
namespace Tainacan\Filter_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Numeric_List_Interval extends Filter_Type {
function __construct(){
$this->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('
<div class="collapse show">
<div class="dropdown is-active">
<div role="button" class="dropdown-trigger">
<button class="button is-white">
List
<span class="icon">
<i class="tainacan-icon tainacan-icon-20px tainacan-icon-arrowdown"></i>
</span>
</button>
</div>
<div class="background"></div>
<div class="dropdown-menu">
<div role="list" class="dropdown-content">
<a class="dropdown-item is-active">Top 10</a>
<a class="dropdown-item">Top 20</a>
<a class="dropdown-item">Top 30</a>
</div>
</div>
</div>
</div>
');
}
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 '<tainacan-filter-numeric-list-interval
name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum()->get_id().'"></tainacan-filter-form-numeric-list-interval>';
}
}

View File

@ -0,0 +1,113 @@
<?php
namespace Tainacan\Filter_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Numeric extends Filter_Type {
function __construct(){
$this->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('
<div>
<div>
<div class="numeric-filter-container">
<div class="dropdown is-active">
<div role="button" class="dropdown-trigger">
<button class="button is-white">
<span class="icon is-small">
<i>=</i>
</span>
<span class="icon">
<i class="tainacan-icon tainacan-icon-20px tainacan-icon-arrowdown"></i>
</span>
</button>
</div>
<div class="background" style="display: none;"></div>
<div class="dropdown-menu" style="display: none;">
<div role="list" class="dropdown-content">
<a class="dropdown-item is-active">=&nbsp; ' . __('Equal', 'tainacan') .'</a>
<a class="dropdown-item">&nbsp; '. __('Not equal', 'tainacan') .'</a>
<a class="dropdown-item">&gt;&nbsp; '. __('Greater than', 'tainacan') .'</a>
<a class="dropdown-item">&nbsp; '. __('Greater than or equal to', 'tainacan') .'</a>
<a class="dropdown-item">&lt;&nbsp; '. __('Less than', 'tainacan') .'</a>
<a class="dropdown-item">&nbsp; '. __('Less than or equal to', 'tainacan') .'</a>
</div>
</div>
</div>
<div class="b-numberinput field is-grouped">
<p class="control">
<button type="button" class="button is-primary is-small">
<span class="icon is-small">
<i class="mdi mdi-minus"></i>
</span>
</button>
</p>
<div class="control is-small is-clearfix">
<input type="number" step="0.01" class="input is-small" value="1.5">
</div>
<p class="control">
<button type="button" class="button is-primary is-small">
<span class="icon is-small">
<i class="mdi mdi-plus"></i>
</span>
</button>
</p>
</div>
</div>
</div>
');
}
/**
* @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 '<tainacan-filter-numeric
step="' . $this->get_option('step') . '"
name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-numeric>';
}
/**
* @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;
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace Tainacan\Filter_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Selectbox extends Filter_Type {
function __construct(){
$this->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('
<div>
<div class="control is-expanded">
<span class="select is-fullwidth">
<select>
<option value="someValue">' . __('Select here...') . '</option>
</select>
</span>
</div>
</div>
');
}
/**
* @param $filter
* @return string
*/
public function render( $filter ){
return '<tainacan-filter-selectbox name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-selectbox>';
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace Tainacan\Filter_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Taginput extends Filter_Type {
function __construct(){
$this->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('
<div>
<p class="has-text-gray">'. __('Selected values') . ': </p>
<div class="field selected-tags is-grouped-multiline is-grouped">
<div>
<div class="tags has-addons">
<span class="tag"><span>'. __('Collection') . ' 2 '._('item') . ' 13</span></span>
<a class="tag is-delete"></a>
</div>
</div>
<div>
<div class="tags has-addons">
<span class="tag"><span>'. __('Collection') . ' 3 '._('item') . ' 2</span></span>
<a class="tag is-delete"></a>
</div>
</div>
</div>
<div class="taginput control is-expanded has-selected">
<div class="taginput-container is-focusable">
<div class="autocomplete control">
<div class="control has-icon-right is-loading is-clearfix">
<input type="text" class="input" value="'. __('Item') . ' 9" >
</div>
<div class="dropdown-menu" style="">
<div class="dropdown-content">
<a class="dropdown-item is-hovered">
<span>'. __('Collection') . ' 2 <strong>'._('item') . ' 9</strong>9</span>
</a>
<a class="dropdown-item">
<span>'. __('Collection') . ' 3 <strong>'._('item') . ' 9</strong>9</span>
</a>
<a class="dropdown-item">
<span>'. __('Collection') . ' 3 <strong>'._('item') . ' 9</strong>8</span>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
');
}
/**
* @param $filter
* @return string
*/
public function render( $filter ){
return '<tainacan-filter-taginput name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-taginput>';
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace Tainacan\Filter_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class TaxonomyCheckbox extends Filter_Type {
function __construct(){
$this->set_name( __('Taxonomy Checkbox List', 'tainacan') );
$this->set_supported_types(['term']);
$this->set_component('tainacan-filter-taxonomy-checkbox');
$this->set_preview_template('
<div>
<div>
<p class="has-text-gray">'. __('Selected values') . ': </p>
<div class="field selected-tags is-grouped-multiline is-grouped">
<div>
<div class="tags has-addons">
<span class="tag"><span>'. __('Value') . ' 21</span></span>
<a class="tag is-delete"></a>
</div>
</div>
<div>
<div class="tags has-addons">
<span class="tag"><span>'. __('Value') . ' 7</span></span>
<a class="tag is-delete"></a>
</div>
</div>
</div>
<div>
<label class="b-checkbox checkbox" border="" style="padding-left: 8px;">
<input type="checkbox" value="option1">
<span class="check"></span>
<span class="control-label">'. __('Value') . ' 1</span>
</label>
<br>
</div>
<div>
<label class="b-checkbox checkbox" border="" style="padding-left: 8px;">
<input type="checkbox" checked value="option2">
<span class="check"></span>
<span class="control-label">'. __('Value') . ' 2</span>
</label>
</div>
<div>
<label class="b-checkbox checkbox" border="" style="padding-left: 8px;">
<input type="checkbox" checked value="option3">
<span class="check"></span>
<span class="control-label">'. __('Value') . ' 3</span>
</label>
</div>
</div>
<a class="add-new-term">'. __('View all') . '</a>
</div>
');
}
/**
* @param $filter
* @return string
*/
public function render( $filter ){
return '<tainacan-filter-taxonomy-checkbox name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-taxonomy-checkbox>';
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace Tainacan\Filter_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class Taginput
*/
class TaxonomyTaginput extends Filter_Type {
function __construct(){
$this->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('
<div>
<p class="has-text-gray">'. __('Selected values') . ': </p>
<div class="field selected-tags is-grouped-multiline is-grouped">
<div>
<div class="tags has-addons">
<span class="tag"><span>'. __('Value') . ' 21</span></span>
<a class="tag is-delete"></a>
</div>
</div>
<div>
<div class="tags has-addons">
<span class="tag"><span>'. __('Value') . ' 7</span></span>
<a class="tag is-delete"></a>
</div>
</div>
</div>
<div class="taginput control is-expanded has-selected">
<div class="taginput-container is-focusable">
<div class="autocomplete control">
<div class="control has-icon-right is-loading is-clearfix">
<input type="text" class="input" value="'. __('Value') . ' 9" >
</div>
<div class="dropdown-menu" style="">
<div class="dropdown-content">
<a class="dropdown-item is-hovered">
<span><strong>'._('Value') . ' 9</strong>9</span>
</a>
<a class="dropdown-item">
<span><strong>'._('Value') . ' 9</strong>9</span>
</a>
<a class="dropdown-item">
<span><strong>'._('Value') . ' 9</strong>8</span>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
');
}
/**
* @param $filter
* @return string
*/
public function render( $filter ){
return '<tainacan-filter-taxonomy-taginput name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-taxonomy-taginput>';
}
}

View File

@ -0,0 +1,100 @@
<?php
namespace Tainacan\Metadata_Types;
use Tainacan\Entities\Metadatum;
use Tainacan\Entities\Item_Metadata_Entity;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Compound extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->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 '<tainacan-text
id="tainacan-text-' . $itemMetadata->get_item()->WP_Post->post_name . '"
metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-text>';
}
/**
* 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 .= '<div class="tainacan-compund-group">';
foreach ( $value as $compound_element ) {
$return .= '<div class="tainacan-compund-metadatum">';
foreach ( $compound_element as $meta ) {
if ( $meta instanceof Item_Metadata_Entity ) {
$return .= '<h4>' . $meta->get_metadatum()->get_name() . "</h4>\n";
$return .= '<p>' . $meta->get_value_as_html() . '</p>' . "\n\n";
}
}
$return .= '</div>' . "\n\n";
}
$return .= '</div>' . "\n\n";
} else {
foreach ( $value as $meta ) {
$return .= '<div class="tainacan-compund-metadatum">';
if ( $meta instanceof Item_Metadata_Entity ) {
$return .= '<h4>' . $meta->get_metadatum()->get_name() . "</h4>\n";
$return .= '<p>' . $meta->get_value_as_html() . '</p>';
}
$return .= '</div>' . "\n\n";
}
}
return $return;
}
}

View File

@ -0,0 +1,113 @@
<?php
namespace Tainacan\Metadata_Types;
use Tainacan\Entities\Metadatum;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Core_Description extends Metadata_Type {
use \Tainacan\Traits\Formatter_Text;
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->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 '<tainacan-textarea
id="tainacan-textarea-' . $itemMetadata->get_item()->WP_Post->post_name . '"
metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-textarea>';
}
/**
* 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;
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace Tainacan\Metadata_Types;
use Tainacan\Entities\Metadatum;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Core_Title extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->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 '<tainacan-text
id="tainacan-text-' . $itemMetadata->get_item()->WP_Post->post_name . '"
metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-text>';
}
/**
* 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;
}
}

View File

@ -0,0 +1,108 @@
<?php
namespace Tainacan\Metadata_Types;
use Tainacan\Entities\Item_Metadata_Entity;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Date extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->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('
<div>
<div class="control is-inline">
<input type="text" placeholder="' . __('mm/dd/yyyy') . '" class="input"></input>
</div>
</div>
');
}
/**
* @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata
* @return string
*/
public function render( $itemMetadata ){
return '<tainacan-date metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-date>';
}
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;
}
}

View File

@ -0,0 +1,274 @@
<?php
namespace Tainacan\Metadata_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
abstract class Metadata_Type {
/**
* Indicates the type of variable that this metadatum type handles.
*
* This is used to relate Metadatum types and filter types, so we know which filter types
* will be available to be used for each metadatum based on its Metadatum Type
*
* For instance, the Filter Type "input text" may be used to search in any metadatum that has
* a Metadatum Type with a string primitive type.
*
* @var string
*/
private $primitive_type;
/**
* When primitive type points to an entity, such as item or term, this indidicates which repository to use
* to fetch that entity
*
* default is false: no repository
*
* @var bool | \Tainacan\Repositories\Repository
*/
private $repository = false;
/**
* Array of options specific to this metadatum type. Stored in metadata_type_options property of the Metadatum object
* @var array
*/
private $options = [];
/**
* The default values for the metadatum type options array
* @var array
*/
private $default_options = [];
private $errors;
/**
* Indicates whether this is a core Metadatum Type or not
*
* Core metadatum types are used by Title and description metadata. These metadata:
* * Can only be used once, they belong to the repository and can not be deleted
* * Its values are saved in th wp_post table, and not as post_meta
*
*/
private $core = false;
/**
* Used by core metadatum types to indicate where it should be saved
*/
private $related_mapped_prop = false;
/**
* The name of the web component used by this metadatum type
* @var string
*/
private $component;
/**
* The name of the web component used by the Form
* @var bool | string
*/
private $form_component = false;
/**
* The Metadata type name. Metadata type classes must set an internationalized string for this property
*/
private $name;
/**
* The Metadata type description. Metadata type classes must set an internationalized string for this property
*/
private $description;
/**
* The html template featuring a preview of how this metadata type componenet
* @var string
*/
private $preview_template;
abstract function render( $itemMetadata );
public function __construct(){
}
public function validate(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
return true;
}
public function get_related_mapped_prop(){
return $this->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) );
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace Tainacan\Metadata_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Numeric extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->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('
<div>
<div class="control is-clearfix">
<input type="number" placeholder="3,1415" class="input">
</div>
</div>
');
}
/**
* @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 '<tainacan-numeric
metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-numeric>';
}
}

View File

@ -0,0 +1,236 @@
<?php
namespace Tainacan\Metadata_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Relationship extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->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('
<div>
<div class="taginput control is-expanded has-selected">
<div class="taginput-container is-focusable">
<div class="autocomplete control">
<div class="control has-icon-right is-loading is-clearfix">
<input type="text" class="input" value="'. __('Item') . ' 9" >
</div>
<div class="dropdown-menu" style="">
<div class="dropdown-content">
<a class="dropdown-item is-hovered">
<span>'. __('Collection') . ' 2 <strong>'._('item') . ' 9</strong>9</span>
</a>
<a class="dropdown-item">
<span>'. __('Collection') . ' 3 <strong>'._('item') . ' 9</strong>9</span>
</a>
<a class="dropdown-item">
<span>'. __('Collection') . ' 3 <strong>'._('item') . ' 9</strong>8</span>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
');
}
/**
* @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 '<tainacan-relationship
collection_id="' . $this->get_options()['collection_id'] . '"
metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-relationship>';
}
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 = "<a data-linkto='item' data-id='$id' href='$link'>";
$return.= $label;
$return .= "</a>";
}
}
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;
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace Tainacan\Metadata_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Selectbox extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->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('
<div>
<div class="control is-expanded">
<span class="select is-fullwidth">
<select>
<option value="someValue">' . __('Select here...') . '</option>
</select>
</span>
</div>
</div>
');
}
/**
* @inheritdoc
*/
public function get_form_labels(){
return [
'options' => [
'title' => __( 'Options', 'tainacan' ),
'description' => __( 'Creates options for what is selected. Hit <enter> 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 '<tainacan-selectbox
options="' . $options . '"
metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-selectbox>';
}
/**
* @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;
}
}

View File

@ -0,0 +1,311 @@
<?php
namespace Tainacan\Metadata_Types;
use Tainacan\Entities\Metadatum;
use Tainacan\Entities\Item_Metadata_Entity;
use Tainacan\Repositories\Metadata;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Taxonomy extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->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('
<div>
<div>
<p class="has-text-gray">'. __('Selected terms') . ': </p>
<div class="field selected-tags is-grouped-multiline is-grouped">
<div>
<div class="tags has-addons">
<span class="tag"><span>'. __('Term') . ' 2</span></span>
<a class="tag is-delete"></a>
</div>
</div>
<div>
<div class="tags has-addons">
<span class="tag"><span>'. __('Term') . ' 3</span></span>
<a class="tag is-delete"></a>
</div>
</div>
</div>
<div>
<label class="b-checkbox checkbox" border="" style="padding-left: 8px;">
<input type="checkbox" value="option1">
<span class="check"></span>
<span class="control-label">'. __('Term') . ' 1</span>
</label>
<br>
</div>
<div>
<label class="b-checkbox checkbox" border="" style="padding-left: 8px;">
<input type="checkbox" checked value="option2">
<span class="check"></span>
<span class="control-label">'. __('Term') . ' 2</span>
</label>
</div>
<div>
<label class="b-checkbox checkbox" border="" style="padding-left: 8px;">
<input type="checkbox" checked value="option3">
<span class="check"></span>
<span class="control-label">'. __('Term') . ' 3</span>
</label>
</div>
</div>
<a class="add-new-term">'. __('View all') . '</a>
</div>
');
}
/**
* @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 '<tainacan-selectbox
options="' . $options . '"
metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-selectbox>';
}
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', '<span class="hierarchy-separator"> > </span>');
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;
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace Tainacan\Metadata_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Text extends Metadata_Type {
use \Tainacan\Traits\Formatter_Text;
function __construct(){
// call metadatum type constructor
parent::__construct();
parent::set_primitive_type('string');
parent::set_component('tainacan-text');
$this->set_name( __('Text', 'tainacan') );
$this->set_description( __('A simple, one line, text input', 'tainacan') );
$this->set_preview_template('
<div>
<div class="control is-clearfix">
<input type="text" placeholder="' . __('Type here...') . '" class="input">
</div>
</div>
');
}
/**
* @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata
* @return string
*/
public function render( $itemMetadata ){
return '<tainacan-text metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-text>';
}
/**
* 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;
}
}

View File

@ -0,0 +1,77 @@
<?php
namespace Tainacan\Metadata_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Textarea extends Metadata_Type {
use \Tainacan\Traits\Formatter_Text;
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->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('
<div>
<div class="control is-clearfix">
<textarea rows="3" placeholder="' . __('Type some long text here...') . '" class="input"></textarea>
</div>
</div>
');
}
/**
* @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata
* @return string
*/
public function render( $itemMetadata ) {
return '<tainacan-textarea
metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-textarea>';
}
public function get_multivalue_prefix() {
return '<p>';
}
public function get_multivalue_suffix() {
return '</p>';
}
/**
* 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;
}
}