Delivery setup
This commit is contained in:
@@ -2,8 +2,14 @@
|
||||
|
||||
class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
|
||||
|
||||
/**
|
||||
* Code for Wiaas Delivery Process Step type
|
||||
* @var string
|
||||
*/
|
||||
public $_step_type = 'wiaas_delivery_step';
|
||||
|
||||
private static $delivery_action_form_title_prefix = 'DELIVERY ACTION TYPE:';
|
||||
|
||||
private static $delivery_action_types = array(
|
||||
'DELIVERY ACTION TYPE: Customer acceptance' => 'customer-acceptance',
|
||||
'DELIVERY ACTION TYPE: Validate Questionnaire' => 'validate-questionnaire',
|
||||
@@ -11,15 +17,31 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
|
||||
'DELIVERY ACTION TYPE: Schedule meeting' => 'schedule-meeting'
|
||||
);
|
||||
|
||||
public static function get_delivery_action_types() {
|
||||
return array_keys(self::$delivery_action_types);
|
||||
}
|
||||
|
||||
public static function get_delivery_action_type_prefix() {
|
||||
return self::$delivery_action_form_title_prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns label for Wiass Delivery Process Step
|
||||
* @return string
|
||||
*/
|
||||
public function get_label() {
|
||||
return esc_html__( 'Wiaas Delivery Step', 'wiaas' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns settings for wiaas delivery process step
|
||||
* @return array
|
||||
*/
|
||||
public function get_settings() {
|
||||
|
||||
$settings_api = $this->get_common_settings_api();
|
||||
|
||||
$forms = $this->get_forms();
|
||||
$forms = $this->get_target_forms_choices();
|
||||
$form_choices[] = array( 'label' => esc_html__( 'Select a Form', 'wiaas' ), 'value' => '' );
|
||||
foreach ( $forms as $form ) {
|
||||
$form_choices[] = array( 'label' => $form->title, 'value' => $form->id );
|
||||
@@ -45,7 +67,7 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
|
||||
),
|
||||
array(
|
||||
'name' => 'target_form_id',
|
||||
'label' => esc_html__( 'Form', 'wiaas' ),
|
||||
'label' => esc_html__( 'Action', 'wiaas' ),
|
||||
'type' => 'select',
|
||||
'onchange' => "jQuery(this).closest('form').submit();",
|
||||
'choices' => $form_choices,
|
||||
@@ -53,48 +75,40 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
|
||||
),
|
||||
);
|
||||
|
||||
$settings['fields'][] = array(
|
||||
'name' => 'store_new_entry_id',
|
||||
'label' => esc_html__( 'Store New Entry ID', 'wiaas' ),
|
||||
'type' => 'checkbox_and_container',
|
||||
'checkbox' => array(
|
||||
'label' => esc_html__( 'Store the ID of the new entry.', 'wiaas' ),
|
||||
),
|
||||
'settings' => array(
|
||||
array(
|
||||
'name' => 'new_entry_id_field',
|
||||
'type' => 'field_select',
|
||||
'args' => array(
|
||||
'input_types' => array(
|
||||
'hidden',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process Wiass Delivery Process Step
|
||||
*
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function process() {
|
||||
|
||||
$form = $this->get_form();
|
||||
$entry = $this->get_entry();
|
||||
$target_form = GFAPI::get_form( $this->target_form_id );
|
||||
|
||||
# if target form is not valid just finish the step
|
||||
if (!$target_form) {
|
||||
return true;
|
||||
}
|
||||
|
||||
# create new entry for target form with populated value for customer-id from parent entry
|
||||
$new_entry = array(
|
||||
'form_id' => $this->target_form_id,
|
||||
'wiaas_delivery_process_id' => $this->get_entry_id(),
|
||||
'wiaas_delivery_order_id' => $entry['wiaas_delivery_order_id'],
|
||||
);
|
||||
|
||||
$customer_id_value = null;
|
||||
|
||||
if ( is_array( $form['fields'] ) ) {
|
||||
foreach ( $form['fields'] as $field ) {
|
||||
if (GFCommon::get_label( $field ) === 'order-id') {
|
||||
$new_entry['wiaas_delivery_order_id'] = $entry[$field->id];
|
||||
}
|
||||
if (GFCommon::get_label( $field ) === 'customer-id') {
|
||||
$customer_id_value = $entry[$field->id];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,6 +117,7 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
|
||||
foreach ( $target_form['fields'] as $field ) {
|
||||
if (GFCommon::get_label( $field ) === 'customer-id') {
|
||||
$new_entry[$field->id] = $customer_id_value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,36 +132,62 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
|
||||
|
||||
$note = $this->get_name() . ': ' . esc_html__( 'started.', 'wiaas' );
|
||||
$this->add_note( $note );
|
||||
|
||||
# return false since we wait for workflow of target entry to be complete first
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates current step status based on target form entry status
|
||||
*
|
||||
* If target form entry has associated workflow current step will complete its status
|
||||
* only when target form entry workflow is completed
|
||||
* @return string
|
||||
*/
|
||||
public function status_evaluation() {
|
||||
|
||||
# retrieve target form entry to check its workflow status
|
||||
$target_form_entry = $this->get_target_form_entry();
|
||||
|
||||
if(is_wp_error($target_form_entry)) {
|
||||
# if there is no target form entry just complete the step
|
||||
if(!$target_form_entry) {
|
||||
return 'complete';
|
||||
}
|
||||
|
||||
# retrieve target form entry workflow status
|
||||
$api = new Gravity_Flow_API( $this->target_form_id );
|
||||
$status = $api->get_status($target_form_entry);
|
||||
|
||||
if ( $status === 'complete' || $status === 'approved' ) {
|
||||
return 'complete';
|
||||
}
|
||||
|
||||
return 'pending';
|
||||
# status is complete only if target entry flow is complete
|
||||
return $status === 'complete' || $status === 'approved' ? 'complete' : 'pending';
|
||||
}
|
||||
|
||||
/**
|
||||
* Expands step entry with additional metadata to track created target entry id
|
||||
* @param array $entry_meta
|
||||
* @param int $form_id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_entry_meta($entry_meta, $form_id) {
|
||||
$entry_meta['wiaas_delivery_step_' . $this->get_id() .'_entry_id'] = null;
|
||||
if ($form_id === $this->get_form_id()) {
|
||||
$entry_meta['wiaas_delivery_step_' . $this->get_id() .'_entry_id'] = null;
|
||||
}
|
||||
return $entry_meta;
|
||||
}
|
||||
|
||||
public function get_forms() {
|
||||
$forms = GFFormsModel::get_forms();
|
||||
return $forms;
|
||||
/**
|
||||
* Retrieves forms that are valid options for delivery step action
|
||||
* @return array
|
||||
*/
|
||||
public function get_target_forms_choices() {
|
||||
return GFFormsModel::search_forms(self::$delivery_action_form_title_prefix, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves delivery action type that is executed with this step
|
||||
* @return string
|
||||
*/
|
||||
public function get_delivery_action_type() {
|
||||
$target_form = GFAPI::get_form( $this->target_form_id );
|
||||
return self::$delivery_action_types[$target_form['title']];
|
||||
@@ -183,11 +224,26 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
|
||||
return $comments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves target form entry created when step was started
|
||||
* @return array|null
|
||||
*/
|
||||
public function get_target_form_entry() {
|
||||
return GFAPI::get_entry($this->get_target_form_entry_id());
|
||||
$entry = GFAPI::get_entry($this->get_target_form_entry_id());
|
||||
if(is_wp_error($entry)) {
|
||||
return null;
|
||||
}
|
||||
return $entry;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves target form entry id created when step was started
|
||||
* @return int
|
||||
*/
|
||||
public function get_target_form_entry_id() {
|
||||
return gform_get_meta($this->get_entry_id(), 'wiaas_delivery_step_' . $this->get_id() .'_entry_id');
|
||||
$value = gform_get_meta($this->get_entry_id(), 'wiaas_delivery_step_' . $this->get_id() .'_entry_id');
|
||||
|
||||
return absint($value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user