From aa4a3daaf66639cb47ab729d9ec00d15c92c108e Mon Sep 17 00:00:00 2001 From: GotPPay Date: Tue, 14 Aug 2018 23:02:25 +0200 Subject: [PATCH] add custom endpoints for customer acceptance --- .../class-wiaas-rest-delivery-process-api.php | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-delivery-process-api.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-delivery-process-api.php index 5a8d493..9b8a11c 100644 --- a/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-delivery-process-api.php +++ b/backend/app/plugins/wiaas/includes/api/class-wiaas-rest-delivery-process-api.php @@ -1,6 +1,14 @@ 'GET', 'callback' => array(__CLASS__, 'get_next_actions_for_user'), ) ); + + register_rest_route( self::$namespace, 'customer-acceptance/(?P\d+)', array( + 'methods' => 'GET', + 'callback' => array(__CLASS__, 'get_customer_acceptance'), + ) ); + + register_rest_route( self::$namespace, 'gravity-form-entry/(?P\d+)', array( + 'methods' => 'GET', + 'callback' => array(__CLASS__, 'get_form_entry'), + ) ); + + register_rest_route( self::$namespace, 'gravity-form-entry/(?P\d+)/field/(?P\d+(.\d+)?)', array( + 'methods' => 'GET', + 'callback' => array(__CLASS__, 'get_field_value_from_entry'), + ) ); + + register_rest_route( self::$namespace, 'gravity-form-entry/(?P\d+)', array( + 'methods' => 'PUT', + 'callback' => array(__CLASS__, 'update_entry'), + ) ); + + register_rest_route( self::$namespace, 'customer-acceptance/(?P\d+)', array( + 'methods' => 'POST', + 'callback' => array(__CLASS__, 'update_customer_acceptance'), + ) ); + + register_rest_route( self::$namespace, 'customer-acceptance/(?P\d+)/upload-file' , array( + 'methods' => 'POST', + 'callback' => array(__CLASS__, 'upload_file'), + ) ); } @@ -62,4 +100,161 @@ class Wiass_REST_Delivery_Process_API { return $response; } + + public static function get_form_entry($data) { + //TODO: check for permissions + return GFAPI::get_entry($data['entry_id']); + } + + 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 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); + } + + 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'); + } + + public 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; + } + + } \ No newline at end of file