Files
old-new-wiaas/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-delivery-process-addon.php
2018-10-30 17:20:56 +01:00

420 lines
11 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_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>',
)
));
}
}