Files
old-new-wiaas/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-delivery-process-api.php
2018-08-29 09:12:46 +02:00

273 lines
7.8 KiB
PHP

<?php
class Wiass_REST_Delivery_Process_API {
private static $BASE_NAME = 'http://localhost/';
private static $ACCEPTANCE_STATUS_FIELD_ID = 8;
private static $EXPIRATION_DATE_FIELD_ID = 9;
private static $DECLINE_REASON_FIELD_ID = 10;
private static $UPLOADED_FILES_FIELD_ID = 12;
/**
* 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'),
) );
/*
Used for some fast test and check
register_rest_route( self::$namespace, 'gravity-form-entry/(?P<entry_id>\d+)', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'get_form_entry'),
) );
register_rest_route( self::$namespace, 'gravity-form-entry/(?P<entry_id>\d+)/field/(?P<field_id>\d+(.\d+)?)', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'get_field_value_from_entry'),
) );
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__, 'update_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){
//TODO: check for permissions
$entry = GFAPI::get_entry($data['entry_id']);
if (!$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 update_customer_acceptance($data){
//TODO : check for permissions
$entry = GFAPI::get_entry($data['entry_id']);
$entry[self::$DECLINE_REASON_FIELD_ID] = $_POST['declineReason'];
$entry[self::$ACCEPTANCE_STATUS_FIELD_ID] = $_POST['actionType'];
$result = GFAPI::update_entry( $entry );
return new WP_REST_Response ($result);
}
public static function upload_file($data){
//TODO : Check permissions
$input_name = "file";
if (!$_FILES[$input_name]){
return self::generate_error("No file", 400);
}
$entry = GFAPI::get_entry($data['entry_id']);
if (!$entry['form_id']){
return self::generate_error("Entry not found", 404);
}
$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_error("File extension is not allowed", 400);
}
$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_error("File extension is not allowed", 400);
}
}
$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_error("Internal server 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 new WP_REST_Response($uploaded_files);
}
return self::generate_error('Error updating entry');
}
//Used for testing and checking
/*
public static function get_form_entry($data) {
//TODO: check for permissions
return GFAPI::get_entry($data['entry_id']);
}
public static function get_field_value_from_entry($data){
//TODO : check for permissions
$entry = GFAPI::get_entry($data['entry_id']);
$result = $entry[$data['field_id']] ?: '';
return new WP_REST_Response ($result);
}
public static function update_entry($data){
//TODO : check for permissions
$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;
}
}