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 d49c407..36230dc 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 @@ -35,6 +35,7 @@ class Wiass_REST_Delivery_Process_API { register_rest_route( self::$namespace, 'customer-acceptance/(?P\d+)', array( 'methods' => 'GET', 'callback' => array(__CLASS__, 'get_customer_acceptance'), + 'permission_callback' => 'is_user_logged_in' ) ); /* @@ -57,11 +58,13 @@ class Wiass_REST_Delivery_Process_API { register_rest_route( self::$namespace, 'customer-acceptance/(?P\d+)', array( 'methods' => 'POST', 'callback' => array(__CLASS__, 'submit_customer_acceptance'), + 'permission_callback' => 'is_user_logged_in' ) ); register_rest_route( self::$namespace, 'customer-acceptance/(?P\d+)/upload-file' , array( 'methods' => 'POST', 'callback' => array(__CLASS__, 'upload_file'), + 'permission_callback' => 'is_user_logged_in' ) ); } @@ -112,10 +115,6 @@ class Wiass_REST_Delivery_Process_API { } public static function get_customer_acceptance(WP_REST_Request $request){ - if (!is_user_logged_in()){ - return self::generate_error('You don\'t have permission to read this entry', 401); - } - $entry = GFAPI::get_entry($request['entry_id']); if (is_wp_error($entry)){ return self::generate_error('Customer acceptance entry not found', 404); @@ -125,10 +124,18 @@ class Wiass_REST_Delivery_Process_API { $uploaded_files = json_decode($entry[self::UPLOADED_FILES_FIELD_ID]); foreach($uploaded_files as $file_url){ - $info = pathinfo($file_url); + //example of decoded url : + //http://localhost/wp/index.php?gf-download=2018/08/rokovi-1535378841.docx&form-id=1&field-id=12&hash=1be6c30f0eeff93563b352d15fe459d5ded12ee06c2c8f36fed66b42dedf2534 + + $decoded_url = urldecode($file_url); + $url_parts = explode('?', $decoded_url); + $file_name_base_parts = explode('&', $url_parts[1]); + $file_name_parts = explode('/', $file_name_base_parts[0]); + $file_name_with_extension_parts = explode('.', $file_name_parts[2]); + $acceptance_documents_entry = array( - 'name' => $info['filename'], - 'extension' => $info['extension'], + 'name' => $file_name_with_extension_parts[0], + 'extension' => $file_name_with_extension_parts[1], 'url' => $file_url ); array_push($acceptance_documents, $acceptance_documents_entry); @@ -162,34 +169,50 @@ class Wiass_REST_Delivery_Process_API { $status = $request['actionType']; $reason = $request['declineReason']; - $installation_declined = ($status === self::DECLINE_STATUS_LABEL); - if (!in_array($status, self::ACCEPTABLE_STATUS)){ return self::generate_wiaas_response('ACCEPTANCE_STATUS_MISSING', 'error'); } + $installation_declined = ($status === self::DECLINE_STATUS_LABEL); + + $uploaded_files = json_decode($entry[self::UPLOADED_FILES_FIELD_ID]); + if ($installation_declined && $reason === ''){ return self::generate_wiaas_response('DECLINE_REASON_EMPTY', 'error'); } + if (!$installation_declined && (count($uploaded_files)===0)){ + return self::generate_wiaas_response('ACCEPTANCE_NOT_UPLOADED', 'error'); + } + $entry[self::DECLINE_REASON_FIELD_ID] = $reason; $entry[self::ACCEPTANCE_STATUS_FIELD_ID] = $status; - - $gf_api = new Gravity_Flow_API($entry['form_id']); - $current_step = $gf_api->get_current_step($entry); - $current_step->update_step_status('complete'); - $gf_api->send_to_step($entry, $current_step->get_id() + 1); - - 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{ + if (!GFAPI::update_entry( $entry )){ return self::generate_wiaas_response('INTERNAL_SERVER_ERROR', 'error'); } + + $gf_api = new Gravity_Flow_API($entry['form_id']); + $current_step = $gf_api->get_current_step($entry); + if ( $current_step ) { + $current_step->purge_assignees(); + $current_step->update_step_status( 'complete' ); + } + $entry_id = $entry['id']; + $new_step_id = $current_step->get_id() + 1; + $new_step = $gf_api->get_step( $new_step_id, $entry ); + $feedback = sprintf( esc_html__( 'Sent to step: %s', 'gravityflow' ), $new_step->get_name() ); + $gf_api->add_timeline_note( $entry_id, $feedback ); + $gf_api->log_activity( 'workflow', 'sent_to_step', $gf_api->form_id, $entry_id, $step_id ); + gform_update_meta( $entry_id, 'workflow_final_status', 'pending' ); + $new_step->start(); + $gf_api->process_workflow( $entry_id ); + + + if ($installation_declined){ + return self::generate_wiaas_response('INSTALLATION_DECLINED', 'success'); + } + return self::generate_wiaas_response('INSTALLATION_ACCEPTED', 'success'); } public static function upload_file(WP_REST_Request $request){ @@ -253,13 +276,14 @@ class Wiass_REST_Delivery_Process_API { } $file_url = self::BASE_NAME . $relative_path . $new_file_name; - + $url_for_download = $upload_file_field->get_download_url($file_url); + $uploaded_files = json_decode($entry[self::UPLOADED_FILES_FIELD_ID]); if ($uploaded_files === NULL){ $uploaded_files = []; } - array_push($uploaded_files, $file_url); + array_push($uploaded_files, $url_for_download); $entry[self::UPLOADED_FILES_FIELD_ID] = json_encode($uploaded_files); @@ -270,27 +294,6 @@ class Wiass_REST_Delivery_Process_API { 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(