Files
old-new-wiaas/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-delivery-process.php
2018-11-02 10:30:25 +01:00

396 lines
12 KiB
PHP

<?php
class Wiaas_Admin_Delivery_Process {
public static function init() {
add_action( 'gravityflow_entry_detail', array( __CLASS__, 'display_process_steps_details' ), 10, 3 );
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);
}
public static function add_delivery_process_metabox() {
add_meta_box(
'order_delivery_process_meta_box',
__('Delivery Process', 'cmb'),
array(__CLASS__, 'order_delivery_process_meta_box'),
'shop_order',
'side',
'high'
);
}
public static function filter_process_send_to_step_options($admin_actions, $current_step, $steps, $form, $entry) {
$delivery_process_actions = array();
if ( $current_step ) {
$previous_step_id = null;
// get previous step id for current step
foreach ($steps as $index => $step) {
$next_step = gravity_flow()->get_next_step($step, $entry, $form);
if ($next_step && $next_step->get_id() === $current_step->get_id()) {
$previous_step_id = $step->get_id();
}
}
if (! empty($previous_step_id)) {
$delivery_process_actions[] = array(
'label' => esc_html__( 'Previous step', 'wiaas' ),
'value' => 'send_to_step|' . $previous_step_id
);
}
// get next step id for current step
$next_step = gravity_flow()->get_next_step($current_step, $entry, $form);
if ($next_step) {
$delivery_process_actions[] = array(
'label' => esc_html__( 'Next step', 'wiaas' ),
'value' => 'send_to_step|' . $next_step->get_id()
);
}
}
return $delivery_process_actions;
}
public static function wiaas_ajax_create_order_delivery_process() {
check_ajax_referer('wiaas_create_order_delivery_process');
$error = new WP_Error('-1', 'Failed to create order delivery process');
if (!isset($_POST['order']) || !isset($_POST['form'])){
wp_send_json_error($error);
}
$order_id = intval( $_POST['order'] );
$form_id = intval( $_POST['form'] );
if ($process_entry_id = Wiaas_Delivery_Process::create_delivery_process_for_order($order_id, $form_id)){
$workflow = new Gravity_Flow_API($form_id);
$step = $workflow->get_current_step(GFAPI::get_entry($process_entry_id));
$entry_url = $step->get_entry_url();
wp_send_json_success(array(
'url' => $entry_url
));
}
wp_send_json_error($error);
}
public static function order_delivery_process_meta_box() {
global $post;
$order_id = $post->ID;
$process_entry = Wiaas_Delivery_Process::get_order_delivery_process_entry($order_id);
if ( empty($process_entry) ){
$order = wc_get_order($order_id);
$list_of_delivery_processes = Wiaas_Delivery_Process::get_available_process_list_for_country(
Wiaas_Countries::get_country_code_by_currency($order->get_currency())
);
?>
<div>
<select id="delivery-process-selector" style="width: 100%;">
<option value="" disabled selected>Assign process to order ... </option>
<?php
foreach($list_of_delivery_processes as $index => $process){
echo '<option value=' . $process['id'] . '>' . $process['title'] . '</option>';
}
?>
</select>
<a id="assign_delivery_process" class="button" style="margin-top: 10px;">Assign</a>
</div>
<script type="text/javascript" >
jQuery(document).ready(function($) {
$('#assign_delivery_process').click(function(e){
e.preventDefault();
var data = {
action: 'wiaas_create_order_delivery_process',
_ajax_nonce: '<?php echo wp_create_nonce( "wiaas_create_order_delivery_process" ) ?>',
order: '<?php echo $order_id ?>',
form: $('#delivery-process-selector').val()
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
if (response.success){
location.reload();
}else{
alert(response.data[0].message);
}
});
});
});
</script>
<?php
} else{
$entry_url = add_query_arg( array(
'page' => 'gravityflow-inbox',
'view' => 'entry',
'id' => $process_entry['form_id'],
'lid' => $process_entry['id']
), admin_url() );
?>
<a href="<?php esc_attr_e($entry_url, 'wiaas') ?>"> Delivery Process </a>
<?php
}
}
public static function display_process_steps_details($form, $entry, $current_step) {
$delivery_settings = rgar($form, 'wiaas_delivery_process');
if ($delivery_settings['delivery_form_type'] === 'action') {
return;
}
// get process order ID
$order_id = absint($entry['wiaas_delivery_order_id']);
if (empty($order_id)) {
$order_field = GFCommon::get_fields_by_type($form, array( 'wiaas_order') )[0];
if ( ! empty($order_field)) {
$order_id = $entry[$order_field->id];
}
}
// display process steps
$workflow_api = new Gravity_Flow_API($form['id']);
$steps = $workflow_api->get_steps();
?>
<div class="postbox">
<h3>
<i class="fa fa-circle" style="font-size: 22px; margin-right: 10px;color: #34C388; "></i>
<span>Order placed </span>
</h3>
</div>
<div class="postbox">
<h3>
<i class="fa fa-circle" style="font-size: 22px; margin-right: 10px;color: #34C388; "></i>
<span> Assign process </span>
</h3>
</div>
<?php
foreach ($steps as $index => $step) {
if (! $step->is_active()) {
continue;
}
$is_step_completed = $step->get_status() === 'complete' || $step->get_status() === 'approved';
$is_current_step = $current_step && $step->get_id() === $current_step->get_id();
if ($is_current_step) {
$style = 'color: #FD8049;';
} else if ($is_step_completed) {
$style = 'color: #34C388;';
} else {
$style = 'opacity: 0.5; color: #CCC;';
}
?>
<div class="postbox">
<h3>
<i class="fa fa-circle" style="font-size: 22px; margin-right: 10px; <?php esc_attr_e($style, 'wiaas') ?>"></i>
<span><?php esc_html_e($step->get_name(), 'wiaas') ?></span>
</h3>
<?php
if ($is_current_step) {
Gravity_Flow_Entry_Detail::maybe_show_instructions(true, true, $current_step, $form, $entry);
$action_form = GFAPI::get_form( $step->target_form_id );
if (empty($action_form)) {
echo '</div>';
continue;
}
$action_delivery_settings = rgar($action_form, 'wiaas_delivery_process');
?>
<div class="submitbox" style="padding: 10px;">
<table>
<tbody>
<?php
if (! $action_delivery_settings['automatic_action_entries_enabled']) {
$form_url = admin_url( 'admin-ajax.php' ) .
'?action=wiaas_delivery_get_form&order_id=' . $order_id .
'&form_id=' . $action_form['id'] .
'&wiaas_delivery_process_id=' . $entry['id'];
$form_url .= '&is_admin=1';
$form_link = sprintf(
'<a href="%s&TB_iframe=true&width=600&height=550" class="button button-primary thickbox">' .
'<i class="fa fa-plus"></i> ' . $action_form['title'] . '</a>',
$form_url );
echo $form_link;
}
echo '<br><br><br>';
$page_size = 20;
$search_criteria = array(
'status' => 'active',
'field_filters' => array(
array( 'key' => 'wiaas_delivery_process_id',
'value' => $entry['id']
),
),
);
$sorting = array( 'key' => 'date_created', 'direction' => 'DESC' );
$paging = array( 'offset' => 0, 'page_size' => $page_size );
$entries = GFAPI::get_entries( $action_form, $search_criteria, $sorting, $paging );
foreach ($entries as $action_entry) {
self::_display_step_action_entry($action_form, $action_entry);
}
?>
</tbody>
</table>
</div>
<?php
}
?>
</div>
<?php
}
add_thickbox();
}
private static function _display_step_action_entry($action_form, $action_entry) {
$workflow_api = new Gravity_Flow_API($action_entry['form_id']);
$current_action_step = $workflow_api->get_current_step($action_entry);
?>
<table>
<?php
foreach ($action_form['fields'] as $field) {
if ($field->type === 'wiaas_order') {
continue;
}
if ($field->type === 'workflow_discussion') {
echo '<tr style="padding: 20px;"><td colspan="2">' . $field->format_discussion_value($action_entry[$field->id]) . '</td></tr>';
continue;
}
$value = $field->get_value_entry_detail($action_entry[$field->id]);
$label = $field->get_field_label(false, $action_entry[$field->id]);
echo '<tr>' .
'<td><strong>' . $label . ' : </strong></td>' .
'<td>' . $value . '</td>' .
'</tr>';
}
?>
<tfoot>
<tr>
<td colspan="2" style="text-transform: uppercase;font-size: 11px; letter-spacing: 0.4px; margin:10px;">
<strong>
<?php
if (! empty($current_action_step)) {
echo $current_action_step->get_status_label($current_action_step->get_status()) . ': ' . $current_action_step->get_name();
echo '<a target="_blank" href="' . $current_action_step->get_entry_url() . '">' .
' <i class="fa fa-external-link" style="font-size: 16px;"></i>' .
'</a>';
} else {
echo $workflow_api->get_status($action_entry);
}
?>
</strong>
</td>
</tr>
</tfoot>
<?php
?>
</table>
<hr />
<?php
}
}
Wiaas_Admin_Delivery_Process::init();