delivery step actions

This commit is contained in:
Almira Krdzic
2018-10-30 17:20:56 +01:00
parent f5766cda99
commit 5aca4e8572
23 changed files with 1651 additions and 218 deletions

View File

@@ -30,7 +30,7 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
* @return string
*/
public function get_label() {
return esc_html__( 'Wiaas Delivery Step', 'wiaas' );
return esc_html__( 'Delivery Step', 'wiaas' );
}
/**
@@ -41,13 +41,12 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
$settings_api = $this->get_common_settings_api();
$forms = $this->get_target_forms_choices();
$forms = $this->get_action_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 );
$form_choices[] = array( 'label' => $form['title'], 'value' => $form['id'] );
}
$settings = array(
'title' => esc_html__( 'Wiaas Delivery Step', 'wiaas' ),
'fields' => array(
@@ -71,13 +70,23 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
'type' => 'select',
'onchange' => "jQuery(this).closest('form').submit();",
'choices' => $form_choices,
),
)
),
);
return $settings;
}
public function update_step_status($status = false) {
if ($status === 'cancelled') {
$status = 'complete';
}
parent::update_step_status($status);
}
/**
* Process Wiass Delivery Process Step
*
@@ -90,51 +99,54 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
$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 is not set we are done
if (!$target_form) {
return true;
// return false since we wait for admin to process the step
return false;
}
# 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'],
'wiaas_delivery_step_name' => $this->get_name(),
);
$delivery_settings = rgar($target_form, 'wiaas_delivery_process');
$customer_id_value = null;
if (! $delivery_settings['automatic_action_entries_enabled']) {
if ( is_array( $form['fields'] ) ) {
foreach ( $form['fields'] as $field ) {
if (GFCommon::get_label( $field ) === 'customer-id') {
$customer_id_value = $entry[$field->id];
break;
}
}
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;
}
if ( is_array( $target_form['fields'] ) ) {
foreach ( $target_form['fields'] as $field ) {
if (GFCommon::get_label( $field ) === 'customer-id') {
$new_entry[$field->id] = $customer_id_value;
break;
}
}
// 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;
}
$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
gform_update_meta($this->get_entry_id(), 'wiaas_delivery_step_' . $this->get_id() .'_entry_id', $entry_id);
$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);
}
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 );
# return false since we wait for workflow of target entry to be complete first
// return false since we wait for admin to process the step
return false;
}
@@ -147,24 +159,29 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
*/
public function status_evaluation() {
# retrieve target form entry to check its workflow status
$target_form_entry = $this->get_target_form_entry();
// return 'pending';
}
# if there is no target form entry just complete the step
if(!$target_form_entry) {
return 'complete';
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)) {
return;
}
# retrieve target form entry workflow status
$api = new Gravity_Flow_API( $this->target_form_id );
$status = $api->get_status($target_form_entry);
?>
<h4>Step: <?php echo $this->get_name() ?></h4>
<h4>Action: <?php echo $target_form['title'] ?></h4>
<?php
# 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
* Expands step entry with additional metadata to track created target actions entries id
*
* @param array $entry_meta
* @param int $form_id
*
@@ -172,7 +189,7 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
*/
public function get_entry_meta($entry_meta, $form_id) {
if ($form_id === $this->get_form_id()) {
$entry_meta['wiaas_delivery_step_' . $this->get_id() .'_entry_id'] = null;
$entry_meta['wiaas_delivery_step_' . $this->get_id() . '_action_entry_ids'] = array();
}
return $entry_meta;
}
@@ -181,8 +198,24 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
* 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);
public function get_action_forms_choices() {
$forms = GFAPI::get_forms();
$action_forms = array();
foreach ( $forms as $form ) {
$delivery_settings = rgar($form, 'wiaas_delivery_process');
if ( ! empty($delivery_settings) && $delivery_settings['delivery_form_type'] === 'action'){
$action_forms[] = $form;
}
}
return $action_forms;
}
/**
@@ -194,37 +227,6 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
return self::$delivery_action_types[$target_form['title']];
}
public function get_target_actual_date() {
$target_entry = $this->get_target_form_entry();
$target_form = GFAPI::get_form( $this->target_form_id );
if (!is_wp_error($target_entry) && is_array($target_form['fields'])) {
foreach ( $target_form['fields'] as $field ) {
if (GFCommon::get_label( $field ) === 'Actual Date') {
return $target_entry[$field->id];
}
}
}
return null;
}
public function get_target_step_comments() {
$notes = RGFormsModel::get_lead_notes( $this->get_target_form_entry_id() );
$comments = array();
foreach ( $notes as $key => $note ) {
if ( $note->note_type !== 'gravityflow' ) {
$comments[] = array(
'date' => $note->date_created,
'text' => $note->value,
'user' => $note->user_name
);
}
}
return $comments;
}
/**
* Retrieves target form entry created when step was started
* @return array|null
@@ -247,4 +249,70 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
return absint($value);
}
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;
}
$new_entry = array_merge($new_entry,array(
'form_id' => $this->target_form_id,
'wiaas_delivery_process_id' => $this->get_entry_id(),
'wiaas_delivery_order_id' => $order_id,
'wiaas_delivery_step_name' => $this->get_name(),
));
$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;
}
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;
}
}