307 lines
8.8 KiB
PHP
307 lines
8.8 KiB
PHP
<?php
|
|
|
|
class Wiass_REST_Delivery_Process_API {
|
|
|
|
const BASE_NAME = WP_HOME . '/';
|
|
|
|
const ACCEPTANCE_STATUS_FIELD_ID = 8;
|
|
const EXPIRATION_DATE_FIELD_ID = 9;
|
|
const DECLINE_REASON_FIELD_ID = 10;
|
|
const UPLOADED_FILES_FIELD_ID = 12;
|
|
|
|
const ACCEPT_STATUS_LABEL = 'accept';
|
|
const DECLINE_STATUS_LABEL = 'decline';
|
|
|
|
const ACCEPTABLE_STATUS = [self::ACCEPT_STATUS_LABEL, self::DECLINE_STATUS_LABEL];
|
|
|
|
/**
|
|
* Endpoint namespace.
|
|
*
|
|
* @var string
|
|
*/
|
|
private static $namespace = 'wiaas';
|
|
|
|
|
|
public static function register_routes() {
|
|
register_rest_route( self::$namespace, 'next-delivery-steps', array(
|
|
'methods' => 'GET',
|
|
'callback' => array(__CLASS__, 'get_next_actions_for_user'),
|
|
) );
|
|
|
|
register_rest_route( self::$namespace, 'customer-acceptance/(?P<entry_id>\d+)', array(
|
|
'methods' => 'GET',
|
|
'callback' => array(__CLASS__, 'get_customer_acceptance'),
|
|
) );
|
|
|
|
register_rest_route( self::$namespace, 'gravity-form-entry/(?P<entry_id>\d+)', array(
|
|
'methods' => 'GET',
|
|
'callback' => array(__CLASS__, 'get_form_entry'),
|
|
) );
|
|
|
|
//Used for testing
|
|
register_rest_route( self::$namespace, 'gravity-form-entry/(?P<entry_id>\d+)', array(
|
|
'methods' => 'PUT',
|
|
'callback' => array(__CLASS__, 'update_entry'),
|
|
) );
|
|
|
|
|
|
register_rest_route( self::$namespace, 'customer-acceptance/(?P<entry_id>\d+)', array(
|
|
'methods' => 'POST',
|
|
'callback' => array(__CLASS__, 'submit_customer_acceptance'),
|
|
) );
|
|
|
|
register_rest_route( self::$namespace, 'customer-acceptance/(?P<entry_id>\d+)/upload-file' , array(
|
|
'methods' => 'POST',
|
|
'callback' => array(__CLASS__, 'upload_file'),
|
|
) );
|
|
}
|
|
|
|
|
|
public static function get_next_actions_for_user() {
|
|
$current_user = wp_get_current_user();
|
|
|
|
$field_filters = array();
|
|
$field_filters[] = array(
|
|
'key' => 'workflow_user_id_' . $current_user->ID,
|
|
'value' => 'pending',
|
|
);
|
|
|
|
$user_roles = gravity_flow()->get_user_roles();
|
|
foreach ( $user_roles as $user_role ) {
|
|
$field_filters[] = array(
|
|
'key' => 'workflow_role_' . $user_role,
|
|
'value' => 'pending',
|
|
);
|
|
}
|
|
|
|
$field_filters['mode'] = 'any';
|
|
|
|
$search_criteria = array();
|
|
$search_criteria['field_filters'] = $field_filters;
|
|
$search_criteria['status'] = 'active';
|
|
|
|
$form_ids = gravity_flow()->get_workflow_form_ids();
|
|
|
|
$total_count = 7;
|
|
|
|
|
|
$entries = GFAPI::get_entries( $form_ids, $search_criteria, null, null, $total_count );
|
|
|
|
$data = array();
|
|
foreach ($entries as $entry) {
|
|
$step = gravity_flow()->get_step( $entry['workflow_step'] );
|
|
$data[] = array(
|
|
'order_id' => $entry['wiaas_delivery_order_id'],
|
|
'order_number' => $entry['wiaas_delivery_order_id'],
|
|
'status' => $entry['workflow_final_status'],
|
|
'step_action' => $step->get_name(),
|
|
);
|
|
}
|
|
|
|
$response = new WP_REST_Response( $data );
|
|
|
|
return $response;
|
|
}
|
|
|
|
public static function get_customer_acceptance($data){
|
|
if (!is_user_logged_in()){
|
|
return self::generate_error('You don\'t have permission to read this entry', 401);
|
|
}
|
|
|
|
$entry = GFAPI::get_entry($data['entry_id']);
|
|
if (is_wp_error($entry)){
|
|
return self::generate_error('Customer acceptance entry not found', 404);
|
|
}
|
|
|
|
$acceptance_documents = array();
|
|
$uploaded_files = json_decode($entry[self::UPLOADED_FILES_FIELD_ID]);
|
|
|
|
foreach($uploaded_files as $file_url){
|
|
$info = pathinfo($file_url);
|
|
$acceptance_documents_entry = array(
|
|
'name' => $info['filename'],
|
|
'extension' => $info['extension'],
|
|
'url' => $file_url
|
|
);
|
|
array_push($acceptance_documents, $acceptance_documents_entry);
|
|
}
|
|
|
|
$acceptance_status = 0;
|
|
if ($entry[self::ACCEPTANCE_STATUS_FIELD_ID]){
|
|
$acceptance_status = ($entry[self::ACCEPTANCE_STATUS_FIELD_ID] === 'accept') ? 1 : -1;
|
|
}
|
|
|
|
$result = array(
|
|
'documents' => $acceptance_documents,
|
|
'expiration' => $entry[self::EXPIRATION_DATE_FIELD_ID],
|
|
'status' => $acceptance_status,
|
|
'decline_reason' => $entry[self::DECLINE_REASON_FIELD_ID]
|
|
);
|
|
|
|
return new WP_REST_Response($result);
|
|
}
|
|
|
|
public static function submit_customer_acceptance($data){
|
|
if (!is_user_logged_in()){
|
|
return self::generate_error('You don\'t have permission to update this entry', 401);
|
|
}
|
|
|
|
$entry = GFAPI::get_entry($data['entry_id']);
|
|
if (is_wp_error($entry)){
|
|
return self::generate_error('Customer acceptance entry not found', 404);
|
|
}
|
|
|
|
$status = $_POST['actionType'];
|
|
$reason = $_POST['declineReason'];
|
|
$installation_declined = ($status === self::DECLINE_STATUS_LABEL);
|
|
|
|
if (!in_array($status, self::ACCEPTABLE_STATUS)){
|
|
return self::generate_wiaas_response('ACCEPTANCE_STATUS_MISSING', 'error');
|
|
}
|
|
|
|
if ($installation_declined && $reason === ''){
|
|
return self::generate_wiaas_response('DECLINE_REASON_EMPTY', 'error');
|
|
}
|
|
|
|
$entry[self::DECLINE_REASON_FIELD_ID] = $reason;
|
|
$entry[self::ACCEPTANCE_STATUS_FIELD_ID] = $status;
|
|
|
|
//submit step
|
|
$entry['workflow_step'] = 2;
|
|
$entry['workflow_step_status_1'] = 'complete';
|
|
$entry['workflow_step_status_2'] = 'pending';
|
|
$entry['workflow_timestamp'] = time();
|
|
|
|
if (GFAPI::update_entry( $entry )){
|
|
if ($installation_declined){
|
|
return self::generate_wiaas_response('INSTALLATION_DECLINED', 'success');
|
|
}else{
|
|
return self::generate_wiaas_response('INSTALLATION_ACCEPTED', 'success');
|
|
}
|
|
}else{
|
|
return self::generate_wiaas_response('INTERNAL_SERVER_ERROR', 'error');
|
|
}
|
|
}
|
|
|
|
public static function upload_file($data){
|
|
if (!is_user_logged_in()){
|
|
return self::generate_error('You don\'t have permission to read this entry', 401);
|
|
}
|
|
|
|
$input_name = 'file';
|
|
if (!$_FILES[$input_name]){
|
|
return self::generate_wiaas_response('NO_FILES_UPLOADED', 'error');
|
|
}
|
|
|
|
$entry = GFAPI::get_entry($data['entry_id']);
|
|
if (!$entry['form_id']){
|
|
return self::generate_wiaas_response('NOT_UPLOADED', 'error');
|
|
}
|
|
|
|
$form = GFAPI::get_form($entry['form_id']);
|
|
$form_upload_path = GFFormsModel::get_upload_path( $form['id'] );
|
|
|
|
$target_path = $form_upload_path . '/' . date('Y') . '/' . date('m') . '/';
|
|
wp_mkdir_p( $target_path );
|
|
GFCommon::recursive_add_index_file( $target_path );
|
|
|
|
$upload_file_field = GFAPI::get_field($form['id'], self::UPLOADED_FILES_FIELD_ID);
|
|
$file_name = $_FILES[$input_name]['name'];
|
|
$file_path_details = pathinfo($file_name);
|
|
|
|
if ( GFCommon::file_name_has_disallowed_extension( $file_name ) ) {
|
|
return self::generate_wiaas_response('INVALID_FILE_ACCEPTANCE', 'error');
|
|
}
|
|
$allowed_extensions = ! empty( $upload_file_field->allowedExtensions ) ? GFCommon::clean_extensions( explode( ',', strtolower( $upload_file_field->allowedExtensions ) ) ) : array();
|
|
if ( ! empty( $allowed_extensions ) ) {
|
|
if ( ! GFCommon::match_file_extension( $file_name, $allowed_extensions ) ) {
|
|
return self::generate_wiaas_response('INVALID_FILE_ACCEPTANCE', 'error');
|
|
}
|
|
}
|
|
|
|
$new_file_name = $file_path_details['filename'] . '-' . time() . '.' . $file_path_details['extension'];
|
|
|
|
if ( move_uploaded_file( $_FILES[$input_name]['tmp_name'], $target_path . $new_file_name ) ) {
|
|
GFFormsModel::set_permissions( $target_path . $new_file_name );
|
|
} else {
|
|
return self::generate_wiaas_response('INTERNAL_SERVER_ERROR', 'error');
|
|
}
|
|
|
|
//Extract path relative to the root
|
|
//Last 6 strings (excluding last empty) are path relative to the root
|
|
$path_parts = explode('/', $target_path);
|
|
|
|
$relative_path = '';
|
|
$i = count($path_parts) - 7;
|
|
while($i < count($path_parts)-1){
|
|
$relative_path = $relative_path . $path_parts[$i] . '/';
|
|
$i++;
|
|
}
|
|
|
|
$file_url = self::BASE_NAME . $relative_path . $new_file_name;
|
|
|
|
$uploaded_files = json_decode($entry[self::UPLOADED_FILES_FIELD_ID]);
|
|
|
|
if ($uploaded_files === NULL){
|
|
$uploaded_files = [];
|
|
}
|
|
array_push($uploaded_files, $file_url);
|
|
|
|
$entry[self::UPLOADED_FILES_FIELD_ID] = json_encode($uploaded_files);
|
|
|
|
if (GFAPI::update_entry( $entry )) {
|
|
return self::generate_wiaas_response('FILE_UPLOADED','success');
|
|
}
|
|
|
|
return self::generate_wiaas_response('NOT_UPLOADED', 'error');
|
|
}
|
|
|
|
//Used for testing
|
|
public static function get_form_entry($data) {
|
|
return GFAPI::get_entry($data['entry_id']);
|
|
}
|
|
|
|
|
|
//Used for testing
|
|
public static function update_entry($data){
|
|
$entry = GFAPI::get_entry($data['entry_id']);
|
|
$new_values = json_decode($data->get_body());
|
|
if ($new_values === NULL) {
|
|
return new WP_REST_Response ();
|
|
}
|
|
$keys = get_object_vars($new_values);
|
|
foreach($keys as $key => $value){
|
|
$entry[$key] = $value;
|
|
}
|
|
$result = GFAPI::update_entry( $entry );
|
|
return new WP_REST_Response ($result);
|
|
}
|
|
|
|
//Helper function
|
|
private static function generate_error($message, $code = 500){
|
|
$error = array(
|
|
'status' => $code,
|
|
'message' => $message,
|
|
);
|
|
|
|
$result = new WP_REST_Response($error);
|
|
$result->set_status($code);
|
|
return $result;
|
|
}
|
|
|
|
private static function generate_wiaas_response($message, $code, $data = NULL){
|
|
$response = array(
|
|
'messages' => [
|
|
array(
|
|
'code' => $code,
|
|
'message' => $message
|
|
)
|
|
],
|
|
'data' => $data
|
|
);
|
|
|
|
return new WP_REST_Response($response);
|
|
}
|
|
|
|
|
|
} |