Merge pull request #522 from tainacan/feature/506

Feature/506
This commit is contained in:
Vinícius Nunes Medeiros 2021-04-09 10:56:15 -03:00 committed by GitHub
commit 251c900d86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 135 additions and 87 deletions

View File

@ -191,6 +191,9 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
if ($status == 'finished') {
$params['progress_label'] = __('Process completed','tainacan');
$params['progress_value'] = 100;
} else if ($status == 'finished-errors') {
$params['progress_label'] = __('Process completed with errors','tainacan');
$params['progress_value'] = 100;
}
$wpdb->update(
$this->table,
@ -282,11 +285,11 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
$batch = $this->get_batch();
if($this->process_lock_in_time != get_site_transient( $this->identifier . '_process_lock' )) {
$this->write_log($batch->key, ['New request has ignored']);
$this->write_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'New request has ignored']]);
wp_die();
}
$this->write_log($batch->key, ['New Request']);
$this->write_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'New Request']]);
register_shutdown_function(function() use($batch) {
$error = error_get_last();
@ -306,8 +309,8 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
$this->debug('Shutdown with Fatal error captured');
$this->debug($error_str);
$this->write_error_log($batch->key, ['Fatal Error: ' . $error_str]);
$this->write_error_log($batch->key, ['Process aborted']);
$this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Fatal Error: ' . $error_str]] );
$this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Process aborted']]);
$this->close( $batch->key, 'errored' );
$this->debug('Batch closed due to captured error');
$this->unlock_process();
@ -321,8 +324,8 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
$task = $this->task( $task );
} catch (\Exception $e) {
// TODO: Add Stacktrace
$this->write_error_log($batch->key, ['Fatal Error: ' . $e->getMessage()]);
$this->write_error_log($batch->key, ['Process aborted']);
$this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Fatal Error: ' . $e->getMessage()]]);
$this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Process aborted']]);
$task = false;
$close_status = 'errored';
}
@ -350,7 +353,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
} else {
$this->debug('Complete');
$this->complete();
$this->write_log($batch->key, ['Process Finished']);
$this->write_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Process Finished']] );
}
$this->debug('dying');
@ -434,18 +437,21 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
private function recursive_stingify_log_array(array $log, $break = true) {
$return = '';
foreach ($log as $k => $l) {
$l_datetime = $l['datetime'];
$l_message = $l['message'];
$return .= "[$l_datetime] ";
if (!is_numeric($k)) {
$return .= $k . ': ';
}
if (is_array($l)) {
//$return .= $this->recursive_stingify_log_array($l, false);
$return .= print_r($l, true);
} elseif (is_string($l)) {
$return .= $l;
if (is_array($l_message)) {
//$return .= $this->recursive_stingify_log_array($l_message, false);
$return .= print_r($l_message, true);
} elseif (is_string($l_message)) {
$return .= $l_message;
}
$return .="\n";
//$return .= $break ? "\n" : ', ';
}
return $return;

View File

@ -470,7 +470,7 @@ class Item_Metadata_Entity extends Entity {
return false;
}
if (empty($value)) {
if (empty($value) && $value !== '0' && $value !== 0) {
if ($this->is_required()) {
$validation_statuses = ['publish', 'future', 'private'];
if (in_array($item->get_status(), apply_filters( 'tainacan-status-require-validation', $validation_statuses) )) {

View File

@ -168,7 +168,7 @@ class Metadatum extends Entity {
/**
* Return the an object child of \Tainacan\metadatum_Types\Metadata_Type with options
*
* @return \Tainacan\Metadata_Types\Metadata_Type The metadatum type class with filled options
* @return \Tainacan\Metadata_Types\Metadata_Type|Object The metadatum type class with filled options
*/
function get_metadata_type_object(){
$class_name = $this->get_metadata_type();

View File

@ -60,6 +60,19 @@ class CSV extends Exporter {
$line[] = $rel;
} elseif ($meta->get_metadatum()->get_metadata_type() == 'Tainacan\Metadata_Types\Compound') {
$line[] = $this->get_compound_metadata_cell($meta);
} elseif ($meta->get_metadatum()->get_metadata_type() == 'Tainacan\Metadata_Types\Date' ) {
$metadatum = $meta->get_metadatum();
$date_value = 'ERROR ON FORMATING DATE';
if (is_object($metadatum)) {
$fto = $metadatum->get_metadata_type_object();
if (is_object($fto)) {
if ( method_exists($fto, 'get_value_as_html') ) {
$fto->output_date_format = 'Y-m-d';
$date_value = $fto->get_value_as_html($meta);
}
}
}
$line[] = $date_value;
} else {
$line[] = $meta->get_value_as_string();
}

View File

@ -355,15 +355,11 @@ abstract class Exporter {
}
public function add_log($message ) {
$count = count($this->log);
$date_key = sprintf("[%' 9d | %s]", $count, date("Y-m-d H:i:s"));
$this->log[$date_key] = $message;
$this->log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message];
}
public function add_error_log($message ) {
$count = count($this->log);
$date_key = sprintf("[%' 9d | %s]", $count, date("Y-m-d H:i:s"));
$this->error_log[$date_key] = $message;
$this->error_log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message];
}
public function is_finished() {

View File

@ -146,14 +146,11 @@ abstract class Generic_Process {
}
public function add_log($message ) {
$count = count($this->log);
$date_key = sprintf("[%' 9d | %s]", $count, date("Y-m-d H:i:s"));
$this->log[$date_key] = $message;
$this->log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message];
}
public function add_error_log($message ){
$date_key = '[' . date("Y-m-d H:i:s") . '] ';
$this->error_log[$date_key] = $message;
public function add_error_log($message ) {
$this->error_log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message];
}
public function is_finished() {

View File

@ -12,7 +12,8 @@ class CSV extends Importer {
'delimiter' => ',',
'multivalued_delimiter' => '||',
'encode' => 'utf8',
'enclosure' => ''
'enclosure' => '',
'escape_empty_value' => '[empty value]'
]);
}
@ -385,6 +386,31 @@ class CSV extends Importer {
</div>
</div>
</div>
<div class="column">
<div class="field">
<label class="label"><?php _e('Empty value', 'tainacan'); ?></label>
<span class="help-wrapper">
<a class="help-button has-text-secondary">
<span class="icon is-small">
<i class="tainacan-icon tainacan-icon-help" ></i>
</span>
</a>
<div class="help-tooltip">
<div class="help-tooltip-header">
<h5><?php _e('Empty value', 'tainacan'); ?></h5>
</div>
<div class="help-tooltip-body">
<p><?php _e('The string representing a value not informed for the metadata. (e.g. \EMPTY)', 'tainacan'); ?></p>
</div>
</div>
</span>
<div class="control is-clearfix">
<input class="input" type="text" name="escape_empty_value" value="<?php echo $this->get_option('escape_empty_value'); ?>">
</div>
</div>
</div>
</div>
<div class="columns">
@ -738,7 +764,9 @@ class CSV extends Importer {
if ($metadatum instanceof Entities\Metadatum) {
$singleItemMetadata = new Entities\Item_Metadata_Entity( $item, $metadatum); // *empty item will be replaced by inserted in the next foreach
if( $metadatum->get_metadata_type() == 'Tainacan\Metadata_Types\Taxonomy' ) {
if ($this->is_clear_value($values)) {
$singleItemMetadata->set_value("");
} else if( $metadatum->get_metadata_type() == 'Tainacan\Metadata_Types\Taxonomy' ) {
if( !is_array( $values ) ) {
$tmp = $this->insert_hierarchy( $metadatum, $values);
if ($tmp !== false) {
@ -874,6 +902,16 @@ class CSV extends Importer {
}
}
/**
* @param $value
* @return bool
*/
private function is_clear_value($value) {
if( !empty($value) && is_array($value) )
return false;
return $this->get_option('escape_empty_value') == $value;
}
/**
* @param $metadatum the metadata
* @param $values the categories names

View File

@ -345,14 +345,10 @@ abstract class Importer {
* @param $messagelog
*/
public function add_log($message ) {
$count = count($this->log);
$date_key = sprintf("[%' 9d | %s]", $count, date("Y-m-d H:i:s"));
$this->log[$date_key] = $message;
$this->log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message];
}
public function add_error_log($message ){
$count = count($this->log);
$date_key = sprintf("[%' 9d | %s]", $count, date("Y-m-d H:i:s"));
$this->error_log[$date_key] = $message;
public function add_error_log($message ) {
$this->error_log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message];
}
public function add_collection(array $collection) {

View File

@ -11,56 +11,58 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
*/
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>
');
}
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>
');
public function validate( Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$format = 'Y-m-d';
$this->output_date_format = get_option('date_format');
}
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;
}
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
@ -80,7 +82,7 @@ class Date extends Metadata_Type {
if( empty( $el ) )
continue;
$return .= $prefix;
$return .= mysql2date(get_option('date_format'), ($el));
$return .= mysql2date($this->output_date_format, ($el));
$return .= $suffix;
$count ++;
if ($count < $total)
@ -89,7 +91,7 @@ class Date extends Metadata_Type {
} else {
if( empty( $value ) )
return "";
$return = mysql2date(get_option('date_format'), ($value));
$return = mysql2date($this->output_date_format, ($value));
}
return $return;