create form for relationship metadata type

This commit is contained in:
Eduardo humberto 2017-12-07 12:10:22 -02:00
parent 139e93f191
commit 246d577c87
2 changed files with 107 additions and 50 deletions

View File

@ -4,59 +4,18 @@ namespace Tainacan\Field_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use Tainacan\Helpers;
/**
* Class TainacanFieldType
*/
class Relationship extends Field_Type {
public $collections;
public $search_metadata;
public $avoid_selected_items;
public $inverse;
function __construct(){
// call field type constructor
parent::__construct();
parent::set_primitive_type('');
}
/**
* get the collections that the metadata type is related
*
* @return mixed
*/
public function get_collections(){
return $this->collections;
}
/**
* get the metadata to search items
*
* @return mixed
*/
public function get_search_metadata(){
return $this->search_metadata;
}
/**
* avoid selected items
*
* @return mixed
*/
public function get_avoid_selected_items(){
return $this->avoid_selected_items;
}
/**
* verify inverse metadata
*
* @return mixed
*/
public function get_inverse(){
return $this->inverse;
}
/**
* @param $metadata
* @return string
@ -73,12 +32,61 @@ class Relationship extends Field_Type {
?>
<tr>
<td>
<label><?php echo __('Options','tainacan'); ?></label><br/>
<label><?php echo __('Collection related','tainacan'); ?></label><br/>
<small><?php echo __('Select the collection to fetch items','tainacan'); ?></small>
</td>
<td>
<textarea name="tnc_metadata_options"><?php echo ( $this->options ) ? $this->options : ''; ?></textarea>
<?php Helpers\HtmlHelpers::collections_dropdown( $this->options['collection_id'], 'field_type_relationship[collection_id]' ); ?>
</td>
</tr>
<?php if( $this->options['collection_id'] ): ?>
<tr>
<td>
<label><?php echo __('Metadata for search','tainacan'); ?></label><br/>
</td>
<td>
<?php Helpers\HtmlHelpers::metadata_checkbox_list(
$this->options['collection_id'],
( isset( $this->options['search'] ) ) ? $this->options['search'] : '',
'field_type_relationship[search][]'
) ?>
</td>
</tr>
<?php endif; ?>
<tr>
<td>
<label><?php echo __('Allow repeated items','tainacan'); ?></label><br/>
<small><?php echo __('Allow/Block selected items in this relationship','tainacan'); ?></small>
</td>
<td>
<textarea name="field_type_relationship[repeated]"><?php echo ( isset( $this->options['repeated'] ) ) ? $this->options['repeated'] : 'yes'; ?></textarea>
</td>
</tr>
<?php if( isset( $this->options['collection_id'] ) ): ?>
<?php
//filter only related metadata
$args = array( 'meta_query' => array ( array(
'key' => 'field_type',
'value' => 'Tainacan\Field_Types\Relationship',
) ) );
?>
<tr>
<td>
<label><?php echo __('Inverse','tainacan'); ?></label><br/>
<small><?php echo __('Select the relationship inverse for this metadata','tainacan'); ?></small>
</td>
<td>
<?php Helpers\HtmlHelpers::metadata_dropdown(
$this->options['collection_id'],
( isset( $this->options['inverse'] ) ) ? $this->options['inverse'] : '',
'field_type_relationship[inverse]',
$args
) ?>
</td>
</tr>
<?php endif; ?>
<?php
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace Tainacan\Helpers;
use Tainacan\Entities;
class HtmlHelpers {
@ -8,12 +9,13 @@ namespace Tainacan\Helpers;
* generates select field for all publish collections
*
* @param string $selected The collection id for the selected option
* @param string $name_field (optional) default 'tnc_prop_collections_id'
*/
public static function collections_dropdown($selected){
public static function collections_dropdown($selected, $name_field = 'tnc_prop_collection_id'){
global $Tainacan_Collections;
$collections = $Tainacan_Collections->fetch([], 'OBJECT');
?>
<select name="tnc_prop_collection_id">
<select name="<?php echo $name_field ?>">
<?php foreach ($collections as $col): ?>
<option value="<?php echo $col->get_id(); ?>" <?php selected($col->get_id(), $selected) ?>><?php echo $col->get_name(); ?></option>
<?php endforeach; ?>
@ -22,22 +24,69 @@ namespace Tainacan\Helpers;
}
/**
* generates select field for all publish collections
* generates checboxes field for all publish collections
*
* @param string(json) $selected json with an array with ids to be checked
* @param string $name_field (optional) default 'tnc_prop_collections_ids[]'
*/
public static function collections_checkbox_list($selected) {
public static function collections_checkbox_list($selected,$name_field = 'tnc_prop_collections_ids[]') {
global $Tainacan_Collections;
$collections = $Tainacan_Collections->fetch([], 'OBJECT');
$selected = json_decode($selected);
?>
<?php foreach ($collections as $col): ?>
<input type="checkbox" name="tnc_prop_collections_ids[]" value="<?php echo $col->get_id(); ?>" <?php checked(in_array($col->get_id(), $selected)); ?> style="width: 15px;">
<input type="checkbox" name="<?php echo $name_field ?>" value="<?php echo $col->get_id(); ?>" <?php checked(in_array($col->get_id(), $selected)); ?> style="width: 15px;">
<?php echo $col->get_name(); ?>
<br/>
<?php endforeach; ?>
<?php
}
/**
* generates select field for all publish metadata for a single collection
*
* @param Entities\Collection | integer $collection The collection id or the collection object
* @param string $selected The collection id for the selected option
* @param string $name_field (optional) default 'tnc_prop_collections_id'
* @param array $args (optional) filter the metadata list
*/
public static function metadata_dropdown( $collection , $selected, $name_field = 'tnc_prop_metadata_id', $args = []){
global $Tainacan_Metadatas;
$collection = ( is_numeric( $collection ) ) ? new Entities\Collection( $collection ) : $collection;
$metadata = $Tainacan_Metadatas->fetch_by_collection( $collection, $args, 'OBJECT');
?>
<select name="<?php echo $name_field ?>">
<option value=""><?php echo __('Select','tainacan') ?></option>
<?php foreach ($metadata as $col): ?>
<option value="<?php echo $col->get_id(); ?>" <?php selected($col->get_id(), $selected) ?>><?php echo $col->get_name(); ?></option>
<?php endforeach; ?>
</select>
<?php
}
/**
* generates checkboxes fields for all publish metadata for a single collection
*
* @param Entities\Collection | integer $collection The collection id or the collection object
* @param string(json) $selected string(json) | array json with an array or array of ids to be checked
* @param string $name_field (optional) default 'tnc_prop_tnc_metadata_ids[]' the checkbox field name
* @param array $args (optional) filter the metadata list
*/
public static function metadata_checkbox_list( $collection , $selected,$name_field = 'tnc_prop_tnc_metadata_ids[]', $args = []) {
global $Tainacan_Metadatas;
$collection = ( is_numeric( $collection ) ) ? new Entities\Collection( $collection ) : $collection;
$metadata = $Tainacan_Metadatas->fetch_by_collection( $collection, $args, 'OBJECT');
$selected = ( is_array( $selected) ) ? $selected : json_decode($selected);
$selected = ( $selected ) ? $selected : [];
?>
<?php foreach ($metadata as $col): ?>
<input type="checkbox" name="<?php echo $name_field ?>" value="<?php echo $col->get_id(); ?>" <?php checked(in_array($col->get_id(), $selected)); ?> style="width: 15px;">
<?php echo $col->get_name(); ?>
<br/>
<?php endforeach; ?>
<?php
}
}