add action `set_comments` in bulk edit #171

This commit is contained in:
Vinícius Nunes 2019-12-18 17:04:32 -03:00
parent fb567051e7
commit ef9e6ecced
2 changed files with 27 additions and 1 deletions

View File

@ -281,6 +281,10 @@ class REST_Bulkedit_Controller extends REST_Controller {
return $this->generic_action('replace_value', $request, ['old_value', 'new_value']);
}
public function set_comments($request) {
return $this->generic_action('set_comments', $request, []);
}
public function get_item($request) {
$group_id = $request['group_id'];
@ -314,7 +318,7 @@ class REST_Bulkedit_Controller extends REST_Controller {
], 400);
}
if ($method != 'set_status' && !isset($body['metadatum_id'])) {
if ( !in_array($method, ['set_status', 'set_comments']) && !isset($body['metadatum_id'])) {
return new \WP_REST_Response([
'error_message' => __('You must specify a Metadatum ID.', 'tainacan'),
], 400);

View File

@ -413,4 +413,26 @@ class Bulk_Edit_Process extends Generic_Process {
return false;
}
private function set_comments( \Tainacan\Entities\Item $item) {
$value = $this->bulk_edit_data['value'];
if ( ! in_array( $value, array( 'open', 'closed' ) ) ) {
$this->add_error_log( __( "the status of comments must be 'open' or 'closed'", 'tainacan' ) );
return false;
}
$item->set_comment_status($value);
if($item->validate()) {
$this->items_repository->update($item);
return true;
}
$this->add_error_log( sprintf( __( 'Please verify, invalid value(s) to edit item ID: "%d"', 'tainacan' ), $item->get_id() ) );
$serealize_erro = (object) array('err' => array());
array_walk_recursive($item->get_errors(), create_function('&$v, $k, &$t', '$t->err[] = $v;'), $serealize_erro);
$this->add_error_log( __('errors: ', 'tainacan') . implode(", ", $serealize_erro->err) );
return false;
}
}