handle order actions

This commit is contained in:
Almira Krdzic
2018-10-31 10:23:59 +01:00
parent 5aca4e8572
commit 308c836460
17 changed files with 834 additions and 649 deletions

View File

@@ -27,39 +27,42 @@ class Wiaas_Delivery_Process_Addon extends Gravity_Flow_Extension {
public function init() {
parent::init();
add_action( 'gravityflow_entry_detail', array( $this, 'display_process_steps_details' ), 10, 3 );
add_action('gravityflow_title_entry_detail', array( $this, 'process_title' ), 10, 3);
add_filter( 'gform_upload_path', array( $this, 'post_file_upload' ), 10, 2 );
}
public function init_ajax() {
parent::init_ajax();
add_action( 'wp_ajax_wiaas_get_action_entry', array( $this, 'ajax_get_action_entry' ) );
// this AJAX action is here and not in /admin folder because of Gravity Forms extension logic
add_action( 'wp_ajax_wiaas_delivery_get_form', array( $this, 'ajax_get_form' ) );
}
public function ajax_get_form() {
public function post_file_upload($path_info, $form_id) {
$form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0;
$wp_uploads = wp_upload_dir();
$field_id = sanitize_text_field( rgget( 'field_id' ) );
if ( empty( $wp_uploads['subdir'] ) ) {
$path_info['path'] = $wp_uploads['path'] . wiaas_documents_base_dir();
$path_info['url'] = $wp_uploads['url'] . wiaas_documents_base_dir();
$path_info['subdir'] = wiaas_documents_base_dir();
} else {
$new_subdir = wiaas_documents_base_dir() . $wp_uploads['subdir'];
$entry_id = absint( rgget( 'entry_id' ) );
$path_info['path'] = str_replace( $wp_uploads['subdir'], $new_subdir, $wp_uploads['path'] );
$path_info['url'] = str_replace( $wp_uploads['subdir'], $new_subdir, $wp_uploads['url'] );
$path_info['subdir'] = str_replace( $wp_uploads['subdir'], $new_subdir, $wp_uploads['subdir'] );
}
$field_values = array( $field_id => $entry_id );
error_log($path_info);
gravity_form_enqueue_scripts( $form_id, true );
return $path_info;
$is_admin = isset( $_GET['is_admin'] );
if ( $is_admin ) {
wp_enqueue_style( 'common', site_url() . '/wp-admin/css/common.css', array(), $this->_version );
} else {
wp_enqueue_style( 'common', get_stylesheet_directory_uri() . '/style.css', array(), $this->_version );
}
wp_print_styles();
wp_print_scripts();
// Render an AJAX-enabled form.
// https://www.gravityhelp.com/documentation/article/embedding-a-form/#function-call
$html = gravity_form( $form_id, true, false, false, $field_values, true, 1, false );
printf( "<div id='wiaas-delivery-process-form' style='padding:10px;'>%s</div>", $html );
die();
}
@@ -125,170 +128,6 @@ class Wiaas_Delivery_Process_Addon extends Gravity_Flow_Extension {
return '';
}
public function display_process_steps_details($form, $entry, $current_step) {
$delivery_settings = rgar($form, 'wiaas_delivery_process');
if ($delivery_settings['delivery_form_type'] === 'action') {
return;
}
$workflow_api = new Gravity_Flow_API($form['id']);
$steps = $workflow_api->get_steps();
foreach ($steps as $index => $step) {
if (! $step->is_active()) {
continue;
}
$is_step_running = $step->get_status() === 'pending';
$is_current_step = $step->get_id() === $current_step->get_id();
$disabled_style = $is_step_running ? '' : 'opacity: 0.5';
?>
<div class="postbox" style="<?php esc_attr_e($disabled_style, 'wiaas') ?>">
<h3>
<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=gravityflowparentchild_get_form&order_id=65&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) {
$this->_display_step_action_entry($action_form, $action_entry);
}
?>
</tbody>
</table>
</div>
<?php
}
?>
</div>
<?php
}
}
private 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><td colspan="2">' . $field->get_value_entry_detail($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>';
}
if (! empty($current_action_step)) {
?>
<tfoot>
<tr>
<td colspan="2">
<a class="button" href="<?php echo $current_action_step->get_entry_url() ?>">
View
<?php echo $current_action_step->get_status_label($current_action_step->get_status()) ?>
</a>
</td>
</tr>
</tfoot>
<?php
}
?>
</table>
<hr>
<?php
}
public function scripts() {
@@ -346,75 +185,85 @@ class Wiaas_Delivery_Process_Addon extends Gravity_Flow_Extension {
'title' => esc_html__( 'Delivery Process', 'wiaas' ),
'fields' => array(
array(
'name' => 'delivery_form_type',
'name' => 'delivery_process',
'label' => esc_html__( 'Delivery Form Type', 'wiaas' ),
'type' => 'delivery_form_type',
),
array(
'name' => 'delivery_action_code',
'label' => esc_html__( 'Action code', 'wiaas' ),
'type' => 'delivery_action_code',
),
array(
'name' => 'delivery_action_form_type',
'label' => esc_html__( 'Automatic?', 'wiaas' ),
'type' => 'delivery_action_form_automatic',
'type' => 'delivery_process',
)
)
)
);
}
public function settings_delivery_form_type() {
public function settings_delivery_process() {
$this->settings_select(array(
'name' => 'delivery_form_type',
'choices' => array(
array( 'value' => 'process', 'label' => 'Process Form' ),
array( 'value' => 'action', 'label' => 'Action Form' ),
array( 'value' => 'process', 'label' => 'Process Form' )
),
'after_select' => '<p class="description"> Choose if this form will be used as process form or action form.</p>' .
'<p class="description"> <strong>Process form</strong> defines order delivery process workflow.</p>' .
'<p class="description"> <strong>Action form</strong> defines custom order data that is collected from delivery process participants.</p>'
));
}
public function settings_delivery_action_code() {
$this->settings_select(array(
'name' => 'delivery_action_code',
'choices' => array(
array( 'value' => '', 'label' => 'Select action code ...' ),
array( 'value' => 'customer-acceptance', 'label' => 'Customer acceptance' ),
array( 'value' => 'validate-questionnaire', 'label' => 'Validate Questionnaire' ),
array( 'value' => 'schedule-meeting', 'label' => 'Schedule meeting' )
),
'after_select' => '<p class="description"> Choose action code for action form.</p>'
));
}
public function settings_delivery_action_form_automatic() {
?>
<br /> <br /> <br />
<?php
$this->settings_checkbox_and_select(array(
'checkbox' => array(
'label' => esc_html__( 'Enable', 'wiaas' ),
'name' => 'automatic_action_entries_enabled',
'defeault_value' => '0',
),
'select' => array(
'name' => 'automatic_action_entries_type',
$settings = $this->get_current_settings();
if ($settings['delivery_form_type'] !== 'process') {
$this->settings_select(array(
'name' => 'delivery_action_code',
'choices' => array(
array(
'value' => 'single',
'label' => esc_html__( 'Single entry', 'wiaas' ),
),
array(
'value' => 'bundle',
'label' => esc_html__( 'Entry per bundle', 'wiaas' ),
)
array( 'value' => '', 'label' => 'Select action code ...' ),
array( 'value' => 'customer-acceptance', 'label' => 'Customer acceptance' ),
array( 'value' => 'validate-questionnaire', 'label' => 'Validate Questionnaire' ),
array( 'value' => 'schedule-meeting', 'label' => 'Schedule meeting' )
),
'after_select' => '<p class="description">Automatic entries can be created once per order or per every bundle in order.</p>' .
'<p class="description">Automatic entry will not be created if any required field cannot be populated.</p>',
)
'after_select' => '<p class="description"> Choose action code for action form.</p>'
));
$this->settings_checkbox_and_select(array(
'checkbox' => array(
'label' => esc_html__( 'Automatic', 'wiaas' ),
'name' => 'automatic_action_entries_enabled',
'default_value' => '0',
),
'select' => array(
'name' => 'automatic_action_entries_type',
'choices' => array(
array(
'value' => 'single',
'label' => esc_html__( 'Single entry', 'wiaas' ),
),
array(
'value' => 'bundle',
'label' => esc_html__( 'Entry per bundle', 'wiaas' ),
)
),
'after_select' => '<p class="description">Automatic entries can be created once per order or per every bundle in order.</p>' .
'<p class="description">Automatic entry will not be created if any required field cannot be populated.</p>',
)
));
return;
}
$this->settings_select(array(
'name' => 'delivery_country',
'choices' => array(
array( 'value' => 'se', 'label' => 'Sweden' ),
array( 'value' => 'dk', 'label' => 'Denmark' ),
array( 'value' => 'fi', 'label' => 'Finland' )
),
'after_select' => '<p class="description"> Choose country for which this process is defined.</p>'
));
}
}