Handle process navigation better

This commit is contained in:
Almira Krdzic
2018-11-03 11:15:52 +01:00
parent 26f997abbc
commit 8d4f75cc41
8 changed files with 168 additions and 38 deletions

View File

@@ -8,14 +8,26 @@ class Wiaas_Admin_Delivery_Process {
add_action( 'gravityflow_title_entry_detail', array( __CLASS__, 'process_title' ), 10, 3 );
add_action('add_meta_boxes', array(__CLASS__, 'add_delivery_process_metabox'), 100);
add_action( 'wp_ajax_wiaas_create_order_delivery_process', array(__CLASS__, 'wiaas_ajax_create_order_delivery_process') );
add_filter('gravityflow_admin_actions_workflow_detail', array (__CLASS__, 'filter_process_send_to_step_options'), 10, 5);
add_action('gravityflow_entry_detail', array(__CLASS__, 'maybe_display_delivery_process_navigation'), 9, 3);
add_filter('gravityflow_admin_action_feedback', array(__CLASS__, 'maybe_process_admin_step_change_action'), 10, 4);
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
}
public static function enqueue_scripts() {
$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );
wp_enqueue_script( 'wiaas_admin_delivery_process', $plugin_url . '/assets/js/wiaas-admin-delivery-process.js' );
wp_enqueue_style( 'wiaas_admin_delivery_process', $plugin_url . '/assets/css/wiaas-admin-delivery-process.css' );
}
public static function add_delivery_process_metabox() {
add_meta_box(
@@ -29,45 +41,84 @@ class Wiaas_Admin_Delivery_Process {
}
public static function filter_process_send_to_step_options($admin_actions, $current_step, $steps, $form, $entry) {
public static function maybe_process_admin_step_change_action($feedback, $admin_action, $form, $entry) {
$delivery_process_actions = array();
$admin_action = rgpost( 'wiaas_delivery_process_navigation_action' );
if ( $current_step ) {
list( $base_admin_action, $action_id ) = rgexplode( '|', $admin_action, 2 );
$previous_step_id = null;
// get previous step id for current step
foreach ($steps as $index => $step) {
if ( $base_admin_action == 'send_to_step' ) {
$step_id = $action_id;
$api = new Gravity_Flow_API( $form['id'] );
$api->send_to_step( $entry, $step_id );
$entry = GFAPI::get_entry( $entry['id'] );
$new_step = $api->get_current_step( $entry );
$feedback = $new_step ?
sprintf( esc_html__( 'Sent to step: %s', 'wiaas' ), $new_step->get_name() ) :
esc_html__( 'Workflow Complete', 'wiaas' );
}
$next_step = gravity_flow()->get_next_step($step, $entry, $form);
return $feedback;
}
if ($next_step && $next_step->get_id() === $current_step->get_id()) {
$previous_step_id = $step->get_id();
}
}
public static function maybe_display_delivery_process_navigation($form, $entry, $current_step) {
if (! empty($previous_step_id)) {
if(! GFAPI::current_user_can_any( 'gravityflow_workflow_detail_admin_actions' ) || empty( $current_step ) ) {
return;
}
$delivery_process_actions[] = array(
'label' => esc_html__( 'Previous step', 'wiaas' ),
'value' => 'send_to_step|' . $previous_step_id
);
}
$steps = gravity_flow()->get_steps($form['id'], $entry);
// get next step id for current step
$next_step = gravity_flow()->get_next_step($current_step, $entry, $form);
if ($next_step) {
// get next step id
$next_step = gravity_flow()->get_next_step($current_step, $entry, $form);
$next_step_id = empty($next_step) ? null : $next_step->get_id();
$delivery_process_actions[] = array(
'label' => esc_html__( 'Next step', 'wiaas' ),
'value' => 'send_to_step|' . $next_step->get_id()
);
}
}
// get previous step id
foreach ($steps as $step) {
return $delivery_process_actions;
}
$next = gravity_flow()->get_next_step($step, $entry, $form);
if ($next && $next->get_id() === $current_step->get_id()) {
$previous_step = $step;
}
}
$previous_step_id = empty($previous_step) ? null : $previous_step->get_id();
// bail out if none exist
if ( empty($next_step_id) && empty($previous_step_id) ) {
return;
}
/**
* @reference Gravity_Flow::maybe_process_admin_action for used field names
* which are being checked there
*
*/
?>
<div style="padding:10px; height:40px">
<input id="wiaas_delivery_process_navigation_action" type="hidden" name="wiaas_delivery_process_navigation_action">
<input type="hidden" name="_gravityflow_admin_action" value="1">
<input
data-step="send_to_step|<?php esc_attr_e($next_step_id, 'wiaas') ?>"
type="submit"
<?php disabled(empty($next_step_id), true, true) ?>
class="button wiaas_delivery_step_nav"
style="float:right; margin-left: 20px;" value="NEXT STEP">
<input
data-step="send_to_step|<?php esc_attr_e($previous_step_id, 'wiaas') ?>"
type="submit"
<?php disabled(empty($previous_step_id), true, true) ?>
class="button wiaas_delivery_step_nav"
style="float:right;" value="PREV STEP">
</div>
<?php
}
public static function wiaas_ajax_create_order_delivery_process() {