288 lines
7.5 KiB
PHP
288 lines
7.5 KiB
PHP
<?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_filter('gravityflow_get_users_args', array ($this, 'allow_only_administrator_to_be_assigned_to_step'));
|
|
|
|
add_filter('gravityflow_assignee_choices', array ($this, 'add_orders_assignee_choices'));
|
|
|
|
add_filter('gravityflow_step_assignees', array ($this, 'maybe_assign_organization_to_step'), 10, 2);
|
|
}
|
|
|
|
/**
|
|
* Handle organization assignee for step (Map users from organization as assignees to step)
|
|
*
|
|
* @param $assignees
|
|
* @param Gravity_Flow_Step $step
|
|
*
|
|
* @return array
|
|
*/
|
|
public function maybe_assign_organization_to_step($assignees, Gravity_Flow_Step $step) {
|
|
|
|
$mapped_assignees = array();
|
|
|
|
foreach ($assignees as $assignee) {
|
|
|
|
if (strpos($assignee->get_type(), 'wiaas_organization_order_') !== false) {
|
|
|
|
$organization_id = $assignee->get_id();
|
|
|
|
$user_ids = wiaas_get_organization_user_ids($organization_id);
|
|
|
|
if (empty($user_ids)) {
|
|
|
|
continue;
|
|
}
|
|
|
|
$user_id = $user_ids[0];
|
|
|
|
$mapped_assignees[] = $step->get_assignee( array(
|
|
'id' => $user_id,
|
|
'type' => 'user_id',
|
|
'editable_fields' => $assignee->get_editable_fields()
|
|
) );
|
|
|
|
continue;
|
|
}
|
|
|
|
$mapped_assignees[] = $assignee;
|
|
}
|
|
|
|
return $mapped_assignees;
|
|
}
|
|
|
|
|
|
public function allow_only_administrator_to_be_assigned_to_step($args) {
|
|
|
|
$args['role'] = 'administrator';
|
|
|
|
//$args['exclude'] = array( 1 ); // exclude super admin user
|
|
|
|
return $args;
|
|
}
|
|
|
|
public static function add_orders_assignee_choices($choices) {
|
|
|
|
return $choices;
|
|
}
|
|
|
|
/**
|
|
* 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 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_process',
|
|
'label' => esc_html__( 'Delivery Form Type', 'wiaas' ),
|
|
'type' => 'delivery_process',
|
|
)
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
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' ),
|
|
),
|
|
'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>'
|
|
));
|
|
|
|
|
|
?>
|
|
<br /> <br /> <br />
|
|
<?php
|
|
|
|
|
|
$settings = $this->get_current_settings();
|
|
|
|
if ($settings['delivery_form_type'] !== 'process') {
|
|
|
|
$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>'
|
|
));
|
|
|
|
|
|
$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>'
|
|
));
|
|
}
|
|
}
|