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

@@ -0,0 +1,38 @@
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
class Wiaas_Dates_List_Field extends GF_Field_List {
public $type = 'wiaas_date_list';
public function get_form_editor_field_title() {
return esc_attr__( 'Date List', 'wiaas' );
}
public function sanitize_settings() {
parent::sanitize_settings();
//$this->inputType = 'date';
}
public function get_list_input($has_columns, $column, $value, $form_id) {
$tabindex = $this->get_tabindex();
$disabled = $this->is_form_editor() ? 'disabled' : '';
return "<input type='date' name='input_{$this->id}[]' value='" . esc_attr( $value ) . "' {$tabindex} {$disabled}/>";
}
public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
return $this->get_value_entry_detail($value, '', false, 'html', 'screen' );
}
}
GF_Fields::register( new Wiaas_Dates_List_Field() );

View File

@@ -0,0 +1,84 @@
<?php
class Wiaas_Delivery_Process_Action {
public static function init() {
}
/**
* @param Wiaas_Delivery_Process_Step $step
*/
public static function get_step_action_form(Wiaas_Delivery_Process_Step $step) {
if (empty($step->target_form_id)) {
return null;
}
$action_form = GFAPI::get_form($step->target_form_id);
if (! $action_form) {
return null;
}
}
public static function get_step_action_entries(Wiaas_Delivery_Process_Step $step) {
$action_form = self::get_step_action_form($step);
if (!$action_form) {
return array();
}
$search_criteria = array(
'status' => 'active',
'field_filters' => array(
array( 'key' => 'wiaas_delivery_process_id',
'value' => $step->get_entry_id()
),
),
);
$sorting = array( 'key' => 'date_created', 'direction' => 'DESC' );
return GFAPI::get_entries( $action_form, $search_criteria, $sorting );
}
public static function get_action_forms() {
$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;
}
public static function is_action_form($form) {
$delivery_settings = rgar($form, 'wiaas_delivery_process');
return ! empty($delivery_settings) && $delivery_settings['delivery_form_type'] === 'action';
}
public static function get_customer_step_actions(Wiaas_Delivery_Process_Step $step) {
}
}
Wiaas_Delivery_Process_Action::init();

View File

@@ -0,0 +1,420 @@
<?php
if (! class_exists( 'GFForms' ) ) {
die();
}
class Wiaas_Delivery_Process_Addon extends Gravity_Flow_Extension {
private static $_instance = null;
protected $_slug = 'wiaas_delivery_process';
protected $_title = 'Delivery Process';
protected $_short_title = 'Delivery Process';
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new Wiaas_Delivery_Process_Addon();
}
return self::$_instance;
}
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' ) );
}
public function post_file_upload($path_info, $form_id) {
$wp_uploads = wp_upload_dir();
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'];
$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'] );
}
error_log($path_info);
return $path_info;
}
public function ajax_get_action_entry() {
$form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0;
$entry_id = absint( rgget( 'entry_id' ) );
}
/**
* Extends Gravity Form entry metadata with 'wiaas_delivery_process_id'
*
* @param array $entry_meta
* @param int $form_id
*
* @return array
*/
public function get_entry_meta( $entry_meta, $form_id ) {
$entry_meta[ 'wiaas_delivery_process_id' ] = array(
'label' => 'Wiaas Delivery Process Id',
'is_numeric' => true,
'update_entry_meta_callback' => array( __CLASS__, 'update_entry_delivery_process_id' ),
'is_default_column' => false, // this column will be displayed by default on the entry list
'filter' => array(
'operators' => array( 'is' ),
),
);
$entry_meta[ 'wiaas_delivery_order_id' ] = array(
'label' => 'Wiaas Delivery Process Order Id',
'is_numeric' => true,
'update_entry_meta_callback' => null,
'is_default_column' => false, // this column will be displayed by default on the entry list
'filter' => array(
'operators' => array( 'is' ),
),
);
$entry_meta[ 'wiaas_delivery_step_name' ] = array(
'label' => 'Wiaas Delivery Step name',
'is_numeric' => false,
'update_entry_meta_callback' => null,
'is_default_column' => false, // this column will be displayed by default on the entry list
'filter' => array(
'operators' => array( 'is' ),
),
);
return $entry_meta;
}
public static function update_entry_delivery_process_id($key, $entry, $form) {
if ( isset( $_REQUEST['wiaas_delivery_process_id'] ) ) {
return absint( $_REQUEST['wiaas_delivery_process_id'] );
}
if ( isset( $entry[ $key ] ) ) {
return $entry[ $key ];
}
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() {
$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );
$scripts = array(
array(
'handle' => 'wiaas_form_editor_js',
'src' => $plugin_url . '/assets/js/wiaas-form-editor.js',
'enqueue' => array(
array(
'admin_page' => array('form_editor'),
),
),
)
);
return array_merge( parent::scripts(), $scripts );
}
/**
* Add settings menu for form delivery process
*
* @param $tabs
* @param $form_id
*
* @return array
*/
public function add_form_settings_menu( $tabs, $form_id ) {
$tabs[] = array(
'name' => $this->_slug,
'label' => esc_html__( 'Delivery Process', 'wiaas' ),
'query' => array( 'fid' => null )
);
return $tabs;
}
/**
* Add settings field for delivery process settings menu
*
* @param $form
*
* @return array
*/
public function form_settings_fields($form) {
return array(
array(
'title' => esc_html__( 'Delivery Process', 'wiaas' ),
'fields' => array(
array(
'name' => 'delivery_form_type',
'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',
)
)
)
);
}
public function settings_delivery_form_type() {
$this->settings_select(array(
'name' => 'delivery_form_type',
'choices' => array(
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() {
$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',
'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>',
)
));
}
}

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;
}
}

View File

@@ -0,0 +1,55 @@
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
class Wiaas_Field_Order_Bundle_Document extends GF_Field_FileUpload {
public $type ='wiaas_order_bundle_document';
public $wiaasDocTypeFilter = 'config';
public function get_form_editor_field_settings() {
return array(
'wiaas_doc_type_filter',
'conditional_logic_field_setting',
'error_message_setting',
'label_setting',
'label_placement_setting',
'admin_label_setting',
'rules_setting',
'file_extensions_setting',
'file_size_setting',
'visibility_setting',
'description_setting',
'css_class_setting',
);
}
public function get_form_editor_field_title() {
return esc_attr__( 'Bundle Document', 'wiaas' );
}
public function get_download_url( $file, $force_download = false ) {
$upload_root = GFFormsModel::get_upload_url( $this->formId );
$upload_root = trailingslashit( $upload_root );
// handle download for order documents not uploaded by gravity forms
if ( strpos( $file, $upload_root ) === false ) {
return admin_url() . '?gf-wiaas-order-doc=' . urlencode($file);
}
return parent::get_download_url( $file, $force_download );
}
public function sanitize_settings() {
parent::sanitize_settings();
$this->wiaasDocTypeFilter = sanitize_key($this->wiaasDocTypeFilter);
}
}
GF_Fields::register( new Wiaas_Field_Order_Bundle_Document() );

View File

@@ -0,0 +1,109 @@
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
class Wiaas_Field_Order_Bundle_Select extends GF_Field_Select {
public $type = 'wiaas_order_bundle';
public function get_form_editor_field_title() {
return esc_attr__( 'Bundle Select', 'wiaas' );
}
public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
return $this->get_selected_bundle_display_name($value);
}
public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br ) {
return $this->get_selected_bundle_display_name($value);
}
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
return $this->get_selected_bundle_display_name($value);
}
public function get_field_input( $form, $value = '', $entry = null ) {
$this->choices = array();
$order_id = null;
if (! empty($entry)) {
$order_field = GFCommon::get_fields_by_type($form, array( 'wiaas_order' ) )[0];
$order_id = ! empty($order_field) ? $entry[$order_field->id] : null;
} else if( ! empty($value)) {
list ($order_id, $item_id) = explode('|', $value);
}
if (! empty($order_id)) {
$this->choices = $this->get_selected_bundle_display_name($order_id);
}
return parent::get_field_input( $form, $value, $entry );
}
/**
* Adds bundles as choices
*/
public function post_convert_field() {
if ($this->is_form_editor()) {
$this->choices = array();
}
if ( ! $this->is_form_editor() && ! empty( rgget('order_id') )) {
$this->choices = $this->get_bundles_as_choices(absint(rgget('order_id')));
}
}
public function get_selected_bundle_display_name($value) {
list ($order_id, $item_id) = explode('|', $value);
if (! empty($order_id) && ! empty($item_id) && $order = wc_get_order($order_id)) {
$item = $order->get_item($item_id);
return $item->get_name();
}
return '';
}
public function get_bundles_as_choices($order_id) {
$choices = array();
if (! empty ($order_id) && $order = wc_get_order($order_id)) {
$standard_bundle_items = wiaas_get_order_standard_bundle_items($order);
foreach ($standard_bundle_items as $item) {
$choices[] = array(
'value' => $order_id . '|' . $item->get_id(),
'text' => $item->get_name()
);
}
}
return $choices;
}
}
GF_Fields::register( new Wiaas_Field_Order_Bundle_Select() );

View File

@@ -0,0 +1,53 @@
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
class Wiaas_Field_Order_Number extends GF_Field_Number {
public $type = 'wiaas_order';
function get_form_editor_field_settings() {
return array(
'conditional_logic_field_setting',
'prepopulate_field_setting',
'error_message_setting',
'label_setting',
'label_placement_setting',
'admin_label_setting',
'size_setting',
'rules_setting',
'visibility_setting',
'duplicate_setting',
'default_value_setting',
'placeholder_setting',
'description_setting',
'css_class_setting',
);
}
public function sanitize_settings() {
parent::sanitize_settings();
}
public function get_form_editor_field_title() {
return esc_attr__( 'Order Number', 'wiaas' );
}
public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
return "#100000$value";
}
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
return "#100000$value";
}
public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br ) {
return "#100000$value";
}
}
GF_Fields::register( new Wiaas_Field_Order_Number() );

View File

@@ -0,0 +1,78 @@
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
class Wiaas_Field_Order_Supplier_Select extends GF_Field_Select {
public $type = 'wiaas_order_supplier';
public function get_form_editor_field_title() {
return esc_attr__( 'Supplier Select', 'wiaas' );
}
public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
return $this->_get_selected_supplier_display_name($value);
}
public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br ) {
return $this->_get_selected_supplier_display_name($value);
}
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
return $this->_get_selected_supplier_display_name($value);
}
/**
* Adds bundles as choices
*/
public function post_convert_field() {
$this->choices = array();
if ( ! $this->is_form_editor() ) {
$this->choices = $this->_get_suppliers_as_choices();
}
}
private function _get_selected_supplier_display_name($value) {
list ($order_id, $supplier_id) = explode('|', $value);
if (! empty($order_id) && ! empty($supplier_id) && $order = wc_get_order($order_id)) {
return wiaas_get_organization_name($supplier_id);
}
return '';
}
private function _get_suppliers_as_choices() {
$choices = array();
$order_id = absint(rgget('order_id'));
if (! empty ($order_id) && $order = wc_get_order($order_id)) {
$suppliers = wiaas_get_order_suppliers($order);
foreach ($suppliers as $supplier_id => $supplier_name) {
$choices[] = array(
'value' => $order_id . '|' . $supplier_id,
'text' => $supplier_name
);
}
}
return $choices;
}
}
GF_Fields::register( new Wiaas_Field_Order_Supplier_Select() );

View File

@@ -0,0 +1,139 @@
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
class Wiaas_Order_Fields {
public static function init() {
add_action( 'gform_field_standard_settings', array( __CLASS__, 'field_settings' ) );
}
/**
* Add custom settings for form order fields
*
* @param int $position
*/
public static function field_settings( $position ) {
if ( $position === 20 ) {
// After Field description setting.
?>
<li class="wiaas_doc_type_filter field_setting">
<label for="wiaas-doc-type-filter" class="section_label">
<?php esc_html_e( 'Document Type', 'wiaas' ); ?>
</label>
<select id="wiaas-doc-type-filter" onchange="SetFieldProperty('wiaasDocTypeFilter',this.value);">
<option value="configuration"><?php esc_html_e( 'Configuration', 'wiaas' ); ?></option>
<option value="order_questionaire"><?php esc_html_e( 'Order Questionaire', 'wiaas' ); ?></option>
<option value="installation_protocol"><?php esc_html_e( 'Installation protocol', 'wiaas' ); ?></option>
<option value="customer_acceptance"><?php esc_html_e( 'Customer acceptance', 'wiaas' ); ?></option>
</select>
</li>
<?php
}
}
public static function get_value_from_order_field($entry, $field) {
switch ($field->type) {
case 'wiaas_order_bundle':
$value = $entry[$field->id];
list ($order_id, $item_id) = explode('|', $value);
if ( ! empty($order_id) && ! empty($item_id)) {
return array(
'id' => $item_id,
'name' => $field->get_selected_bundle_display_name($value)
);
}
return null;
case '':
}
}
public static function map_order_to_entry($order_id, $form, $bundle_item_id = null) {
if (empty($form['fields']) ||
empty(GFCommon::get_fields_by_type( $form, array('wiaas_order')) ) ) {
// form does not have order field so cannot be mapped
return false;
}
$order = wc_get_order($order_id);
$bundle_item = $order->get_item($bundle_item_id);
$entry = array();
foreach ($form['fields'] as $field) {
switch ($field->type) {
case 'wiaas_order':
$entry[(string) $field->id] = $order->get_id();
break;
case 'wiaas_order_bundle':
if ( empty($bundle_item) && $field->isRequired) {
// there is no data for required field so entry cannot be created
return false;
}
if (! empty($bundle_item)) {
$entry[(string) $field->id] = $order->get_id() . '|' . $bundle_item->get_id();
}
break;
case 'wiaas_order_bundle_document':
if ( empty($bundle_item) && $field->isRequired) {
// there is no data for required field so entry cannot be created
return false;
}
if (! empty($bundle_item)) {
$documents = wiaas_get_order_item_documents($bundle_item, $field->wiaasDocTypeFilter);
if ( empty($documents) && $field->isRequired) {
// there is no data for required field so entry cannot be created
return false;
}
if (! empty($documents)) {
$document = $documents[0];
$entry[$field->id] = $document['version'];
}
}
break;
}
}
return $entry;
}
}
Wiaas_Order_Fields::init();