feat: add action `tainacan-insert-tainacan-item` and `tainacan-pre-insert-tainacan-item` #144

This commit is contained in:
vnmedeiros 2020-10-26 16:51:32 -03:00
parent d9da6bae70
commit f855a57cee
3 changed files with 128 additions and 131 deletions

View File

@ -344,7 +344,6 @@ class Metadata extends Repository {
$args = apply_filters( 'tainacan_fetch_args', $args, 'metadata' );
error_log(json_encode($args));
$wp_query = new \WP_Query( $args );

View File

@ -128,9 +128,10 @@ abstract class Repository {
$old = '';
$diffs = [];
do_action( 'tainacan-pre-insert', $obj );
do_action( 'tainacan-pre-insert-' . $obj->get_post_type(), $obj );
$obj_post_type = $obj->get_post_type();
if( $obj_post_type != false )
do_action( "tainacan-pre-insert-$obj_post_type", $obj );
$map = $this->get_map();
@ -159,6 +160,8 @@ abstract class Repository {
$post_t = $collection->get_db_identifier();
$obj->WP_Post->post_type = $post_t;
$obj_post_type = 'tainacan-item';
do_action( "tainacan-pre-insert-$obj_post_type", $obj );
}
// TODO verificar se salvou mesmo
@ -176,7 +179,9 @@ abstract class Repository {
update_post_meta( $id, '_user_edit_lastr', get_current_user_id() );
do_action( 'tainacan-insert', $obj, $diffs, $is_update );
do_action( 'tainacan-insert-' . $obj->get_post_type(), $obj );
if( $obj_post_type != false ) {
do_action( "tainacan-insert-$obj_post_type", $obj );
}
// return a brand new object
return new $this->entities_type( $obj->WP_Post );

View File

@ -12,159 +12,152 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
*/
class Control extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->set_primitive_type('control');
$this->set_component('tainacan-text');
$this->set_name( __('Control Type', 'tainacan') );
$this->set_description( __('A special metadata type, used to map certain item properties such as collection id and document type to metadata in order to easily create filters.', 'tainacan') );
$this->set_default_options([
'control_metadatum_options' => ['document_type', 'collection_id'],
'control_metadatum' => 'document_type'
]);
add_action( 'tainacan-api-item-updated', [&$this, 'update_control_metadatum'], 10, 2 );
}
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->set_primitive_type('control');
$this->set_component('tainacan-text');
$this->set_name( __('Control Type', 'tainacan') );
$this->set_description( __('A special metadata type, used to map certain item properties such as collection id and document type to metadata in order to easily create filters.', 'tainacan') );
$this->set_default_options([
'control_metadatum_options' => ['document_type', 'collection_id'],
'control_metadatum' => 'document_type'
]);
add_action( 'tainacan-insert-tainacan-item', array( $this, 'update_control_metadatum' ), 10, 1 );
}
public function update_control_metadatum( \Tainacan\Entities\Item $item, $attributes) {
function update_control_metadatum( $item ) {
if ( $item instanceof \Tainacan\Entities\Item ) {
$metadata = $item->get_metadata();
foreach ($metadata as $item_metadatum) {
if ( $item_metadatum->get_metadatum()->get_metadata_type_object() instanceof \Tainacan\Metadata_Types\Control &&
$item_metadatum->get_metadatum()->get_metadata_type_options()['control_metadatum'] == $this->get_option('control_metadatum')) {
$update_item_metadatum = new \Tainacan\Entities\Item_Metadata_Entity( $item, $item_metadatum->get_metadatum() );
switch ( $this->get_option('control_metadatum') ) {
case 'document_type':
$update_item_metadatum->set_value( $item->get_document_type() );
break;
if ( $item instanceof \Tainacan\Entities\Item ) {
case 'collection_id':
$update_item_metadatum->set_value( $item->get_collection_id() );
break;
default:
// What the hell am I doing here?
break;
}
$metadata = $item->get_metadata();
foreach ($metadata as $item_metadatum) {
if ( $update_item_metadatum->validate() )
\Tainacan\Repositories\Item_Metadata::get_instance()->insert( $update_item_metadatum );
else
$errors[] = $update_item_metadatum->get_errors();
if ( $item_metadatum->get_metadatum()->get_metadata_type_object() instanceof \Tainacan\Metadata_Types\Control && $item_metadatum->get_metadatum()->get_metadata_type_options()['control_metadatum'] == $this->get_option('control_metadatum')) {
$update_item_metadatum = new \Tainacan\Entities\Item_Metadata_Entity( $item, $item_metadatum->get_metadatum() );
switch ( $this->get_option('control_metadatum') ) {
case 'document_type':
$update_item_metadatum->set_value( $item->get_document_type() );
break;
break; // Ends foreach in case we already found the related metadata
}
}
}
}
case 'collection_id':
$update_item_metadatum->set_value( $item->get_collection_id() );
break;
default:
// What the hell am I doing here?
break;
}
if ( $update_item_metadatum->validate() )
\Tainacan\Repositories\Item_Metadata::get_instance()->insert( $update_item_metadatum );
else
$errors[] = $update_item_metadatum->get_errors();
break; // Ends foreach in case we already found the related metadata
}
}
}
}
public function validate_options( Metadatum $metadatum ) {
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('control_metadatum')) ) {
return [
'control_metadatum' => __('Required control metadatum.','tainacan')
];
}
return true;
if ( empty($this->get_option('control_metadatum')) ) {
return [
'control_metadatum' => __('Required control metadatum.','tainacan')
];
}
return true;
}
public function get_control_metadatum_value($value, $control_metadatum, $format) {
$return = '';
}
switch ( $control_metadatum ) {
case 'document_type':
$return = ($format == 'html' ? $this->get_document_as_html( $value ) : $this->get_document_as_string( $value ));
break;
public function get_control_metadatum_value($value, $control_metadatum, $format) {
$return = '';
switch ( $control_metadatum ) {
case 'document_type':
$return = ($format == 'html' ? $this->get_document_as_html( $value ) : $this->get_document_as_string( $value ));
break;
case 'collection_id':
$return = ($format == 'html' ? $this->get_collection_as_html( $value ) : $this->get_collection_as_string( $value ));
break;
default:
// What the hell am I doing here?
break;
}
case 'collection_id':
$return = ($format == 'html' ? $this->get_collection_as_html( $value ) : $this->get_collection_as_string( $value ));
break;
default:
// What the hell am I doing here?
break;
}
return $return;
}
/**
}
/**
* 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();
$control_metadatum = $this->get_option('control_metadatum');
return $this->get_control_metadatum_value($value, $control_metadatum, 'html');
}
$value = $item_metadata->get_value();
$control_metadatum = $this->get_option('control_metadatum');
return $this->get_control_metadatum_value($value, $control_metadatum, 'html');
}
/**
/**
* Return the value of an Item_Metadata_Entity using a metadatum of this metadatum type as a string
* @param Item_Metadata_Entity $item_metadata
* @return string The String representation of the value, containing one or multiple items names, linked to the item page
*/
public function get_value_as_string(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$control_metadatum = $this->get_option('control_metadatum');
$value = $item_metadata->get_value();
$control_metadatum = $this->get_option('control_metadatum');
return $this->get_control_metadatum_value($value, $control_metadatum, 'string');
}
}
private function get_document_as_html( $value ) {
return $this->get_document_as_string( $value );
}
private function get_document_as_html( $value ) {
return $this->get_document_as_string( $value );
}
public function get_document_as_string( $value ) {
switch ($value) {
case 'attachment':
return __( 'File', 'tainacan' );
break;
case 'text':
return __( 'Text', 'tainacan' );
break;
case 'url':
return __( 'URL', 'tainacan' );
break;
public function get_document_as_string( $value ) {
switch ($value) {
case 'attachment':
return __( 'File', 'tainacan' );
break;
case 'text':
return __( 'Text', 'tainacan' );
break;
case 'url':
return __( 'URL', 'tainacan' );
break;
default:
return $value;
}
}
private function get_collection_as_html( $value ) {
$collection = \Tainacan\Repositories\Collections::get_instance()->fetch( (int) $value );
if ( $collection instanceof \Tainacan\Entities\Collection ) {
$label = $collection->get_name();
$link = $collection->get_url();
$return = "<a data-linkto='collection' data-id='$value' href='$link'>";
$return.= $label;
$return .= "</a>";
return $return;
}
default:
return $value;
}
}
private function get_collection_as_html( $value ) {
$collection = \Tainacan\Repositories\Collections::get_instance()->fetch( (int) $value );
if ( $collection instanceof \Tainacan\Entities\Collection ) {
$label = $collection->get_name();
$link = $collection->get_url();
$return = "<a data-linkto='collection' data-id='$value' href='$link'>";
$return.= $label;
$return .= "</a>";
return $return;
}
return null;
}
}
private function get_collection_as_string( $value ) {
$collection = \Tainacan\Repositories\Collections::get_instance()->fetch( (int) $value );
if ( $collection instanceof \Tainacan\Entities\Collection ) {
$label = $collection->get_name();
return $label;
}
return null;
}
private function get_collection_as_string( $value ) {
$collection = \Tainacan\Repositories\Collections::get_instance()->fetch( (int) $value );
if ( $collection instanceof \Tainacan\Entities\Collection ) {
$label = $collection->get_name();
return $label;
}
return null;
}
}