use constants; send message objects instead of strings
This commit is contained in:
@@ -2,12 +2,17 @@
|
||||
|
||||
class Wiass_REST_Delivery_Process_API {
|
||||
|
||||
private static $BASE_NAME = WP_HOME . "/";
|
||||
const BASE_NAME = WP_HOME . '/';
|
||||
|
||||
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;
|
||||
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.
|
||||
@@ -110,7 +115,7 @@ class Wiass_REST_Delivery_Process_API {
|
||||
|
||||
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);
|
||||
return self::generate_error('You don\'t have permission to read this entry', 401);
|
||||
}
|
||||
|
||||
$entry = GFAPI::get_entry($data['entry_id']);
|
||||
@@ -119,7 +124,7 @@ class Wiass_REST_Delivery_Process_API {
|
||||
}
|
||||
|
||||
$acceptance_documents = array();
|
||||
$uploaded_files = json_decode($entry[self::$UPLOADED_FILES_FIELD_ID]);
|
||||
$uploaded_files = json_decode($entry[self::UPLOADED_FILES_FIELD_ID]);
|
||||
|
||||
foreach($uploaded_files as $file_url){
|
||||
$info = pathinfo($file_url);
|
||||
@@ -132,15 +137,15 @@ class Wiass_REST_Delivery_Process_API {
|
||||
}
|
||||
|
||||
$acceptance_status = 0;
|
||||
if ($entry[self::$ACCEPTANCE_STATUS_FIELD_ID]){
|
||||
$acceptance_status = ($entry[self::$ACCEPTANCE_STATUS_FIELD_ID] === 'accept') ? 1 : -1;
|
||||
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],
|
||||
'expiration' => $entry[self::EXPIRATION_DATE_FIELD_ID],
|
||||
'status' => $acceptance_status,
|
||||
'decline_reason' => $entry[self::$DECLINE_REASON_FIELD_ID]
|
||||
'decline_reason' => $entry[self::DECLINE_REASON_FIELD_ID]
|
||||
);
|
||||
|
||||
return new WP_REST_Response($result);
|
||||
@@ -148,54 +153,79 @@ class Wiass_REST_Delivery_Process_API {
|
||||
|
||||
public static function update_customer_acceptance($data){
|
||||
if (!is_user_logged_in()){
|
||||
return self::generate_error("You don't have permission to read this entry", 401);
|
||||
return self::generate_error('You don\'t have permission to read this entry', 401);
|
||||
}
|
||||
|
||||
$entry = GFAPI::get_entry($data['entry_id']);
|
||||
if (!$entry){
|
||||
return self::generate_error('Customer acceptance entry not found', 404);
|
||||
return self::generate_wiaas_response('INTERNAL_SERVER_ERROR', 'error');
|
||||
}
|
||||
|
||||
$entry[self::$DECLINE_REASON_FIELD_ID] = $_POST['declineReason'];
|
||||
$entry[self::$ACCEPTANCE_STATUS_FIELD_ID] = $_POST['actionType'];
|
||||
$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();
|
||||
|
||||
$result = GFAPI::update_entry( $entry );
|
||||
return new WP_REST_Response ($result);
|
||||
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);
|
||||
return self::generate_error('You don\'t have permission to read this entry', 401);
|
||||
}
|
||||
|
||||
$input_name = "file";
|
||||
$input_name = 'file';
|
||||
if (!$_FILES[$input_name]){
|
||||
return self::generate_error("No file", 400);
|
||||
return self::generate_wiaas_response('NO_FILES_UPLOADED', 'error');
|
||||
}
|
||||
|
||||
$entry = GFAPI::get_entry($data['entry_id']);
|
||||
if (!$entry['form_id']){
|
||||
return self::generate_error("Entry not found", 404);
|
||||
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") . '/';
|
||||
$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);
|
||||
$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);
|
||||
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_error("File extension is not allowed", 400);
|
||||
return self::generate_wiaas_response('INVALID_FILE_ACCEPTANCE', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,36 +234,36 @@ class Wiass_REST_Delivery_Process_API {
|
||||
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");
|
||||
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);
|
||||
$path_parts = explode('/', $target_path);
|
||||
|
||||
$relative_path = "";
|
||||
$relative_path = '';
|
||||
$i = count($path_parts) - 7;
|
||||
while($i < count($path_parts)-1){
|
||||
$relative_path = $relative_path . $path_parts[$i] . "/";
|
||||
$relative_path = $relative_path . $path_parts[$i] . '/';
|
||||
$i++;
|
||||
}
|
||||
|
||||
$file_url = self::$BASE_NAME . $relative_path . $new_file_name;
|
||||
$file_url = self::BASE_NAME . $relative_path . $new_file_name;
|
||||
|
||||
$uploaded_files = json_decode($entry[self::$UPLOADED_FILES_FIELD_ID]);
|
||||
$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);
|
||||
$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_wiaas_response('FILE_UPLOADED','success');
|
||||
}
|
||||
|
||||
return self::generate_error('Error updating entry');
|
||||
return self::generate_wiaas_response('NOT_UPLOADED', 'error');
|
||||
}
|
||||
|
||||
//Used for testing and checking
|
||||
@@ -285,5 +315,19 @@ class Wiass_REST_Delivery_Process_API {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user