Files
old-new-wiaas/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-delivery-process-step.php

297 lines
7.5 KiB
PHP
Raw Normal View History

<?php
class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
2018-08-06 17:36:57 +02:00
/**
* Code for Wiaas Delivery Process Step type
* @var string
*/
public $_step_type = 'wiaas_delivery_step';
2018-08-06 17:36:57 +02:00
/**
* Returns label for Wiass Delivery Process Step
* @return string
*/
public function get_label() {
2018-10-30 17:20:56 +01:00
return esc_html__( 'Delivery Step', 'wiaas' );
}
2018-08-06 17:36:57 +02:00
/**
* Returns settings for wiaas delivery process step
* @return array
*/
public function get_settings() {
$settings_api = $this->get_common_settings_api();
2018-11-18 22:10:01 +01:00
$forms = Wiaas_Delivery_Process_Action::get_action_forms();
$form_choices[] = array( 'label' => esc_html__( 'Select a Form', 'wiaas' ), 'value' => '' );
foreach ( $forms as $form ) {
$form_choices[] = array( 'label' => $form['title'], 'value' => $form['id'] );
}
$settings = array(
'title' => esc_html__( 'Wiaas Delivery Step', 'wiaas' ),
'fields' => array(
$settings_api->get_setting_instructions(),
array (
'name' => 'is_visible_to_customer',
'label' => __( 'Visible to customer', 'wiaas' ),
'type' => 'checkbox',
'choices' => array(
array(
'label' => __( 'Show to customer', 'wiaas' ),
'name' => 'is_visible_to_customer',
'default_value' => true,
),
),
'tooltip' => __( 'Determines if this step will be shown to customer.', 'wiaas' ),
),
array(
'name' => 'target_form_id',
2018-08-06 17:36:57 +02:00
'label' => esc_html__( 'Action', 'wiaas' ),
'type' => 'select',
'onchange' => "jQuery(this).closest('form').submit();",
'choices' => $form_choices,
2018-10-30 17:20:56 +01:00
)
),
);
return $settings;
}
public function start() {
parent::start();
$this->update_step_status('pending');
return false;
}
public function evaluate_status() {
return $this->get_status();
}
public function get_status_label($status) {
2018-10-30 17:20:56 +01:00
$label = parent::get_status_label($status);
2018-10-30 17:20:56 +01:00
if (empty($label)) {
2018-10-31 10:23:59 +01:00
$label = __('Not started', 'wiaas');
}
return $label;
}
2018-11-18 22:10:01 +01:00
/**
* Update status
*
* @param bool $status
*/
public function update_step_status($status = false) {
2018-11-18 22:10:01 +01:00
/**
* If status is being updated after manual step completion ('Next Step' or 'Previous Step') change default
* behavior of settings status to cancelled if manual action is sending flow to next step.
* Instead of default cancelled status set complete status for this step.
*/
2018-11-03 11:15:52 +01:00
if ($status === 'cancelled' && $admin_action = rgpost( 'wiaas_delivery_process_navigation_action' )) {
list( $base_admin_action, $step_id ) = rgexplode( '|', $admin_action, 2 );
2018-10-31 10:23:59 +01:00
$next_step = gravity_flow()->get_next_step($this, $this->get_entry(), $this->get_form());
// going to next step
if ($base_admin_action === 'send_to_step' && $next_step && $next_step->get_id() == $step_id) {
2018-10-31 10:23:59 +01:00
$status = 'complete';
}
2018-10-30 17:20:56 +01:00
}
parent::update_step_status($status);
}
2018-08-06 17:36:57 +02:00
/**
2018-11-18 22:10:01 +01:00
*
* Process Wiaas Delivery Process Step
2018-08-06 17:36:57 +02:00
*
2018-11-18 22:10:01 +01:00
* @return bool We will always return false because step will be completed only by manual user action
2018-08-06 17:36:57 +02:00
*/
function process() {
$form = $this->get_form();
$entry = $this->get_entry();
$target_form = GFAPI::get_form( $this->target_form_id );
2018-08-06 17:36:57 +02:00
2018-10-30 17:20:56 +01:00
# if target form is not set we are done
2018-08-06 17:36:57 +02:00
if (!$target_form) {
2018-10-30 17:20:56 +01:00
// return false since we wait for admin to process the step
return false;
2018-08-06 17:36:57 +02:00
}
2018-10-30 17:20:56 +01:00
$delivery_settings = rgar($target_form, 'wiaas_delivery_process');
2018-10-30 17:20:56 +01:00
if (! $delivery_settings['automatic_action_entries_enabled']) {
2018-10-30 17:20:56 +01:00
return false;
}
$action_entries_ids = gform_get_meta($this->get_entry_id(), 'wiaas_delivery_step_' . $this->get_id() . '_action_entry_ids');
// if action entries present this step is reprocessing and we should not be creating new action entries
if (! empty($action_entries_ids)) {
// return false since we wait for admin to process the step
return false;
}
2018-10-30 17:20:56 +01:00
// create new entries for step action forms
$order_field = GFCommon::get_fields_by_type($form, 'wiaas_order')[0];
$order_id = empty($order_field) ? null : absint($entry[$order_field->id]);
// if process has not order we cannot create actions
if (empty($order_id)) {
// return false since we wait for admin to process the step
return false;
}
2018-10-30 17:20:56 +01:00
$delivery_settings = rgar($target_form, 'wiaas_delivery_process');
switch ($delivery_settings['automatic_action_entries_type']) {
case 'single':
$action_entries_ids = $this->_create_single_action_entry($target_form, $order_id);
break;
case 'bundle':
$action_entries_ids = $this->_create_per_bundle_action_entries($target_form, $order_id);
}
2018-10-30 17:20:56 +01:00
gform_update_meta($this->get_entry_id(), 'wiaas_delivery_step_' . $this->get_id() . '_action_entry_ids', $action_entries_ids);
$note = $this->get_name() . ': ' . esc_html__( 'started.', 'wiaas' );
$this->add_note( $note );
2018-08-06 17:36:57 +02:00
2018-10-30 17:20:56 +01:00
// return false since we wait for admin to process the step
return false;
}
2018-10-30 17:20:56 +01:00
public function workflow_detail_box($form, $args) {
parent::workflow_detail_box($form, $args);
$target_form = GFAPI::get_form( $this->target_form_id );
if (empty( $target_form)) {
2018-10-30 17:20:56 +01:00
return;
}
2018-10-30 17:20:56 +01:00
?>
<h4>Step: <?php echo $this->get_name() ?></h4>
<h4>Action: <?php echo $target_form['title'] ?></h4>
<?php
}
2018-08-06 17:36:57 +02:00
/**
2018-10-30 17:20:56 +01:00
* Expands step entry with additional metadata to track created target actions entries id
*
2018-08-06 17:36:57 +02:00
* @param array $entry_meta
* @param int $form_id
*
* @return array
*/
public function get_entry_meta($entry_meta, $form_id) {
2018-08-06 17:36:57 +02:00
if ($form_id === $this->get_form_id()) {
2018-10-30 17:20:56 +01:00
$entry_meta['wiaas_delivery_step_' . $this->get_id() . '_action_entry_ids'] = array();
2018-08-06 17:36:57 +02:00
}
return $entry_meta;
}
2018-11-18 22:10:01 +01:00
2018-08-06 17:36:57 +02:00
/**
*
2018-11-18 22:10:01 +01:00
* PRIVATE
*
2018-08-06 17:36:57 +02:00
*/
2018-08-06 17:36:57 +02:00
/**
2018-11-18 22:10:01 +01:00
* Create action entry to trigger subworkflow
* @param $target_form
* @param $order_id
*
* @return array
2018-08-06 17:36:57 +02:00
*/
2018-10-30 17:20:56 +01:00
private function _create_single_action_entry($target_form, $order_id) {
$action_entries_ids = array();
$new_entry = Wiaas_Order_Fields::map_order_to_entry($order_id, $target_form);
if (empty($new_entry)) {
// entry cannot be created
return $action_entries_ids;
}
2018-10-31 10:23:59 +01:00
$new_entry['form_id'] = $target_form['id'];
$new_entry['wiaas_delivery_process_id'] = $this->get_entry_id();
$new_entry['wiaas_delivery_order_id'] = $order_id;
2018-10-30 17:20:56 +01:00
$entry_id = GFAPI::add_entry( $new_entry );
if ( is_wp_error( $entry_id ) ) {
$this->log_debug( __METHOD__ .'(): failed to add entry' );
} else {
// store entry id
$action_entries_ids[] = $entry_id;
}
return $action_entries_ids;
}
2018-11-18 22:10:01 +01:00
/**
* Create action form entries for every bundle in order to trigger subworkflows
*
* @param $target_form
* @param $order_id
*
* @return array
*/
2018-10-30 17:20:56 +01:00
private function _create_per_bundle_action_entries($target_form, $order_id) {
$action_entries_ids = array();
$bundle_items = wiaas_get_order_standard_bundle_items($order_id);
foreach ($bundle_items as $item) {
$new_entry = Wiaas_Order_Fields::map_order_to_entry($order_id, $target_form, $item->get_id());
if (empty($new_entry)) {
// entry cannot be created
continue;
}
$new_entry['form_id'] = $target_form['id'];
$new_entry['wiaas_delivery_process_id'] = $this->get_entry_id();
$new_entry['wiaas_delivery_order_id'] = $order_id;
$entry_id = GFAPI::add_entry( $new_entry );
if ( is_wp_error( $entry_id ) ) {
$this->log_debug( __METHOD__ .'(): failed to add entry' );
} else {
// store entry id
$action_entries_ids[] = $entry_id;
}
}
return $action_entries_ids;
}
}