add a main methods to bulk edit in bg processo #200

This commit is contained in:
Vinícius Nunes 2019-12-05 00:42:50 -03:00
parent daa74e795a
commit 1d70f55d83
2 changed files with 193 additions and 31 deletions

View File

@ -181,15 +181,15 @@ class REST_Bulkedit_Controller extends REST_Controller {
}
public function bulk_edit_permissions_check($request) {
$collection = $this->collections_repository->fetch($request['collection_id']);
public function bulk_edit_permissions_check($request) {
$collection = $this->collections_repository->fetch($request['collection_id']);
if ($collection instanceof Entities\Collection) {
return current_user_can($collection->get_items_capabilities()->edit_others_posts);
}
return current_user_can($collection->get_items_capabilities()->edit_others_posts);
}
return false;
}
return false;
}
public function create_item($request) {
@ -364,28 +364,46 @@ class REST_Bulkedit_Controller extends REST_Controller {
}
}
$group_id = $request['group_id'];
$args = ['id' => $group_id];
$bulk = new \Tainacan\Bulk_Edit($args);
$bulk_id = $request['group_id'];
$metadatum = $this->metadatum_repository->fetch($body['metadatum_id']);
if ( $metadatum instanceof Entities\Metadatum ) {
$value = isset($body['new_value']) ? $body['new_value'] : $body['value'];
$old_value = isset($body['old_value']) ? $body['old_value'] : null;
$action = $bulk->$method($metadatum, $value, $old_value);
if ( is_wp_error($action) ) {
return new \WP_REST_Response([
'error_message' => $action->get_error_message(),
], 400);
} else {
return new \WP_REST_Response($action, 200);
}
} else {
return new \WP_REST_Response([
'error_message' => __('Metadatum not found.', 'tainacan'),
], 400);
global $Tainacan_Generic_Process_Handler;
$process = $Tainacan_Generic_Process_Handler->get_process_instance_by_session_id($bulk_id);
if ($process !== false) {
$bulk_edit_data = [
"value" => isset($body['new_value']) ? $body['new_value'] : $body['value'],
"method" => $method,
"old_value" => isset($body['old_value']) ? $body['old_value'] : null,
"metadatum_id" => $body['metadatum_id'],
];
$process->set_bulk_edit_data($bulk_edit_data);
$bg_bulk = $Tainacan_Generic_Process_Handler->add_to_queue($process);
//$Tainacan_Generic_Process_Handler->delete_process_instance($process);
}
return new \WP_REST_Response(["bg_process_id"=>$bulk_id, "method" => $method], 200);
//$bulk_id = $request['group_id'];
//$args = ['id' => $bulk_id];
//$bulk = new \Tainacan\Bulk_Edit($args);
// $metadatum = $this->metadatum_repository->fetch($body['metadatum_id']);
// if ( $metadatum instanceof Entities\Metadatum ) {
// $value = isset($body['new_value']) ? $body['new_value'] : $body['value'];
// $old_value = isset($body['old_value']) ? $body['old_value'] : null;
// $action = $bulk->$method($metadatum, $value, $old_value);
// if ( is_wp_error($action) ) {
// return new \WP_REST_Response([
// 'error_message' => $action->get_error_message(),
// ], 400);
// } else {
// return new \WP_REST_Response($action, 200);
// }
// } else {
// return new \WP_REST_Response([
// 'error_message' => __('Metadatum not found.', 'tainacan'),
// ], 400);
// }
}
public function get_item_in_sequence($request) {

View File

@ -5,14 +5,158 @@ use Tainacan\Entities;
class Bulk_Edit_Process extends Generic_Process {
public function __construct($id) {
parent::__construct();
$this->id = $id;
private $meta_key = '_tnc_bulk';
public function __construct($attributes = array()) {
$this->array_attributes = array_merge($this->array_attributes, [
'bulk_id',
'bulk_edit_data'
]);
parent::__construct($attributes);
$this->init_objects();
}
public function main_process() {
$this->add_log("log");
public function init_objects() {
$this->items_repository = \Tainacan\Repositories\Items::get_instance();
$this->metadatum_repository = \Tainacan\Repositories\Metadata::get_instance();
$this->item_metadata_repository = \Tainacan\Repositories\Item_Metadata::get_instance();
}
public function set_bulk_id($bulk_id) { //TODO bulk_id or ID?
$this->bulk_id = $bulk_id;
$this->id = $this->bulk_id;
}
public function get_bulk_id( ) {
return $this->bulk_id;
}
public function get_output() {
$message = __('Bulk-edit end', 'tainacan');
return $message;
}
public function set_bulk_edit_data($bulk_edit_data = false) {
$this->bulk_edit_data = $bulk_edit_data;
}
public function get_bulk_edit_data() {
return $this->bulk_edit_data;
}
private function bulk_list_remove_item($item, $meta_key, $meta_value) {
return delete_post_meta( $item->get_id(), $meta_key, $meta_value);
}
private function bulk_list_get_item() {
$args = [
'perpage' => 1,
'meta_query' => array(
array(
'key' => $this->meta_key,
'value' => $this->bulk_id,
'compare' => '=',
)
)
];
$item = $this->items_repository->fetch($args, [], 'OBJECT');
if (is_array($item) && !empty($item))
$item = $item[0];
if ($item instanceof \Tainacan\Entities\Item)
return $item;
return false;
}
public function main_process() {
$method = $this->bulk_edit_data['method'];
if ( !method_exists($this, $method) ) {
$this->add_error_log(__('method not exists', 'tainacan'));
$this->abort();
return false;
}
$item = $this->bulk_list_get_item();
if($item == false) {
return false;
}
$this->add_log( sprintf( __('bulk edit item ID: "%d"', 'tainacan'), $item->get_id() ) );
$this->$method($item);
$this->bulk_list_remove_item($item, $this->meta_key, $this->bulk_id);
return $item->get_id();
}
private function save_item_metadata(\Tainacan\Entities\Item_Metadata_Entity $item_metadata, \Tainacan\Entities\Item $item) {
if ( $item_metadata->validate() ) {
if( $item->can_edit() ) {
$updated_item_metadata = $this->item_metadata_repository->update( $item_metadata );
} else {
$this->add_error_log( sprintf( __('do not have permission to edit item ID: "%d"', 'tainacan'), $item->get_id() ) );
return false;
}
} else {
$this->add_error_log( sprintf( __( 'Please verify, invalid value(s) to edit item ID: "%d"', 'tainacan' ), $item->get_id() ) );
$this->add_error_log($item_metadata->get_errors());
return false;
}
return true;
}
private function set_value(\Tainacan\Entities\Item $item) {
$metadatum = $this->metadatum_repository->fetch($this->bulk_edit_data['metadatum_id']);
$value = $this->bulk_edit_data['value'];
$item_metadata = new Entities\Item_Metadata_Entity( $item, $metadatum );
if($item_metadata->is_multiple()) {
$item_metadata->set_value( $value );
} elseif(is_array($value)) {
$item_metadata->set_value(implode(' ', $value));
} else {
$item_metadata->set_value($value);
}
return $this->save_item_metadata($item_metadata, $item);
}
private function add_value(\Tainacan\Entities\Item $item) {
$metadatum_id = $this->bulk_edit_data['metadatum_id'];
$metadatum = $this->metadatum_repository->fetch($metadatum_id);
$value = $this->bulk_edit_data['value'];
if (!$metadatum->is_multiple()) {
$this->add_error_log( __( 'Unable to add a value to a metadata if it does not accept multiple values', 'tainacan' ) );
return false;
}
if ($metadatum->is_collection_key()) {
$this->add_error_log( __( 'Unable to add a value to a metadata set to be a collection key', 'tainacan' ) );
return false;
}
//$item_metadata = new Entities\Item_Metadata_Entity( $item, $metadatum );
$items_metadata = $item->get_metadata();
foreach ($items_metadata as $item_metadata){
$metadatum = $item_metadata->get_metadatum();
if($metadatum->get_id() == $metadatum_id) {
$values = is_array($item_metadata->get_value()) ? $item_metadata->get_value() : [$item_metadata->get_value()];
$values = array_merge($values, [$value]);
$item_metadata->set_value( $values );
return $this->save_item_metadata($item_metadata, $item);
}
}
return false;
}
private function remove_value(\Tainacan\Entities\Item $item) {
}
private function replace_value(\Tainacan\Entities\Item $item) {
}
}