function lw_handle_client_request_document_upload( $request_id, $file_input = null, $document_name = '' ) { $user_id = get_current_user_id(); if ( ! $user_id ) { return new WP_Error( 'lw_no_user', 'You must be logged in to upload documents.', array( 'status' => 401 ) ); } $request_id = trim( (string) $request_id ); if ( ! $request_id ) { return new WP_Error( 'lw_invalid_id', 'Request ID is required.', array( 'status' => 400 ) ); } $requests = lw_get_client_requests( $user_id ); $request_exists = false; $matched_request = null; foreach ( $requests as $existing_request ) { if ( ( $existing_request['requestId'] ?? '' ) === $request_id ) { $request_exists = true; $matched_request = $existing_request; break; } } if ( ! $request_exists ) { return new WP_Error( 'lw_not_found', 'Request not found for this user.', array( 'status' => 404 ) ); } if ( ! $file_input || ! is_array( $file_input ) || ! isset( $file_input['tmp_name'] ) ) { return new WP_Error( 'lw_missing_file', 'No document file was provided.', array( 'status' => 400 ) ); } if ( ! empty( $file_input['error'] ) ) { return new WP_Error( 'lw_upload_error', 'Upload failed. Please try a different file.', array( 'status' => 400 ) ); } require_once ABSPATH . 'wp-admin/includes/file.php'; $upload_overrides = array( 'test_form' => false, ); $uploaded = wp_handle_upload( $file_input, $upload_overrides ); if ( ! is_array( $uploaded ) || ! empty( $uploaded['error'] ) ) { return new WP_Error( 'lw_upload_failed', ! empty( $uploaded['error'] ) ? (string) $uploaded['error'] : 'Unable to store the uploaded document.', array( 'status' => 500 ) ); } $current_user = wp_get_current_user(); $resolved_name = sanitize_text_field( (string) $document_name ); if ( ! $resolved_name ) { $resolved_name = sanitize_file_name( (string) ( $file_input['name'] ?? 'document' ) ); } $config = function_exists( 'clientPortalLoadConfig' ) ? clientPortalLoadConfig() : array(); $zoho_config = is_array( $config['zoho'] ?? null ) ? $config['zoho'] : array(); $integration_mode = sanitize_key( (string) ( $zoho_config['integration_mode'] ?? 'workdrive_api' ) ); $zoho_sync = array( 'synced' => false, 'folderId' => '', 'folderCreated' => false, 'fileId' => '', 'fileUrl' => '', 'status' => null, 'crmSynced' => false, 'crmRecordId' => '', 'fallbackUsed' => false, 'warning' => '', ); if ( 'workdrive_function' === $integration_mode ) { require_once __DIR__ . '/ZohoFunctionSyncService.php'; try { $function_sync = new ZohoFunctionSyncService( $config ); $function_result = $function_sync->syncUploadedDocument( (string) ( $current_user->user_email ?? '' ), array( 'document_title' => $resolved_name, 'request_id' => $request_id, 'request_title' => (string) ( $matched_request['title'] ?? '' ), 'original_name' => sanitize_file_name( (string) ( $file_input['name'] ?? $resolved_name ) ), 'public_url' => (string) ( $uploaded['url'] ?? '' ), 'local_path' => (string) ( $uploaded['file'] ?? '' ), ) ); $zoho_sync['synced'] = true; $zoho_sync['status'] = (string) ( $function_result['status'] ?? '' ); $function_body = is_array( $function_result['body'] ?? null ) ? $function_result['body'] : array(); $function_payload = is_array( $function_body['zoho_response'] ?? null ) ? $function_body['zoho_response'] : $function_body; $details = is_array( $function_payload['details'] ?? null ) ? $function_payload['details'] : array(); $zoho_sync['folderId'] = sanitize_text_field( (string) ( $details['folder_id'] ?? $details['folderId'] ?? '' ) ); $zoho_sync['fileId'] = sanitize_text_field( (string) ( $details['file_id'] ?? $details['fileId'] ?? '' ) ); $zoho_sync['fileUrl'] = ! empty( $zoho_sync['fileId'] ) ? 'https://workdrive.zoho.com/file/' . $zoho_sync['fileId'] : ''; } catch ( Throwable $exception ) { $zoho_sync['warning'] = 'WF_DEBUG_NEW_PATH: ' . $exception->getMessage(); } } else { $contacts_root_folder_id = lw_get_zoho_workdrive_contacts_folder_id(); $folder_result = lw_get_or_create_client_workdrive_folder( $current_user ); $target_folders = array(); if ( ! is_wp_error( $folder_result ) ) { $target_folders[] = sanitize_text_field( (string) ( $folder_result['id'] ?? '' ) ); $zoho_sync['folderCreated'] = ! empty( $folder_result['created'] ); } if ( $contacts_root_folder_id ) { $target_folders[] = sanitize_text_field( (string) $contacts_root_folder_id ); } $target_folders = array_values( array_unique( array_filter( $target_folders ) ) ); $last_sync_error_message = ''; foreach ( $target_folders as $candidate_folder_id ) { $candidate_file_name = $resolved_name; if ( $contacts_root_folder_id && $candidate_folder_id === $contacts_root_folder_id ) { $email_prefix = sanitize_title( (string) ( $current_user->user_email ?? '' ) ); if ( $email_prefix ) { $candidate_file_name = $email_prefix . '__' . sanitize_file_name( $resolved_name ); } } $zoho_upload = lw_workdrive_upload_file( $candidate_folder_id, $uploaded['file'], $candidate_file_name ); if ( is_wp_error( $zoho_upload ) ) { $last_sync_error_message = $zoho_upload->get_error_message(); continue; } $zoho_sync['synced'] = true; $zoho_sync['folderId'] = $candidate_folder_id; $zoho_sync['fileId'] = $zoho_upload['fileId'] ?? ''; $zoho_sync['fileUrl'] = ! empty( $zoho_sync['fileId'] ) ? 'https://workdrive.zoho.com/file/' . $zoho_sync['fileId'] : ''; $zoho_sync['status'] = $zoho_upload['status'] ?? null; $zoho_sync['fallbackUsed'] = ( $contacts_root_folder_id && $candidate_folder_id === $contacts_root_folder_id ); $zoho_sync['warning'] = ''; break; } if ( ! $zoho_sync['synced'] ) { if ( is_wp_error( $folder_result ) ) { $zoho_sync['warning'] = 'Portal upload succeeded but CRM sync failed while preparing folder: ' . $folder_result->get_error_message(); } elseif ( $last_sync_error_message ) { $zoho_sync['warning'] = 'Portal upload succeeded but CRM sync failed while uploading file: ' . $last_sync_error_message; } else { $zoho_sync['warning'] = 'Portal upload succeeded but CRM sync could not find a valid target folder.'; } } elseif ( $zoho_sync['fallbackUsed'] ) { $zoho_sync['warning'] = 'Uploaded to Contacts root folder because client folder creation/reuse was unavailable.'; } } if ( $zoho_sync['synced'] ) { $crm_sync = lw_create_zoho_client_document_record( $current_user, array( 'name' => $resolved_name, 'file_name' => sanitize_file_name( (string) ( $file_input['name'] ?? $resolved_name ) ), 'file_url' => $uploaded['url'] ?? '', 'workdrive_file_id' => $zoho_sync['fileId'] ?? '', 'workdrive_file_url' => $zoho_sync['fileUrl'] ?? '', ) ); if ( is_wp_error( $crm_sync ) ) { $crm_warning = 'CRM record sync failed: ' . $crm_sync->get_error_message(); $zoho_sync['warning'] = $zoho_sync['warning'] ? $zoho_sync['warning'] . ' ' . $crm_warning : $crm_warning; } else { $zoho_sync['crmSynced'] = true; $zoho_sync['crmRecordId'] = sanitize_text_field( (string) ( $crm_sync['recordId'] ?? '' ) ); } } $file_size = 0; if ( ! empty( $uploaded['file'] ) && file_exists( $uploaded['file'] ) ) { $file_size = (int) filesize( $uploaded['file'] ); } lw_upsert_request_document_record( array( 'user_id' => (int) $user_id, 'request_uid' => $request_id, 'request_row_id' => (int) ( $matched_request['dbRowId'] ?? 0 ), 'document_name' => $resolved_name, 'original_file_name' => sanitize_file_name( (string) ( $file_input['name'] ?? '' ) ), 'wp_file_url' => $uploaded['url'] ?? '', 'wp_file_path' => $uploaded['file'] ?? '', 'mime_type' => $uploaded['type'] ?? '', 'file_size' => $file_size, 'zoho_synced' => ! empty( $zoho_sync['synced'] ), 'zoho_status' => isset( $zoho_sync['status'] ) ? (string) $zoho_sync['status'] : '', 'zoho_folder_id' => $zoho_sync['folderId'] ?? '', 'zoho_file_id' => $zoho_sync['fileId'] ?? '', 'zoho_fallback_used' => ! empty( $zoho_sync['fallbackUsed'] ), 'sync_status' => lw_get_request_document_sync_status( $zoho_sync ), 'sync_message' => $zoho_sync['warning'] ?? '', ) ); return rest_ensure_response( array( 'uploaded' => true, 'message' => $zoho_sync['synced'] ? ( $zoho_sync['crmSynced'] ? 'Document uploaded and synced to Zoho WorkDrive and CRM.' : 'Document uploaded and synced to Zoho WorkDrive.' ) : 'Document uploaded in portal. Zoho CRM sync is pending.', 'document' => array( 'name' => $resolved_name, 'url' => $uploaded['url'] ?? '', 'mimeType' => $uploaded['type'] ?? '', 'requestId' => $request_id, ), 'zoho' => $zoho_sync, ) ); } ledgerworx.me https://validator.w3.org/feed/docs/rss2.html Registration Login Blog-Expanding Your UAE Business into Saudi Arabia Blog Elementor #3194 Client Portal Home About Blog-RTA Fines and Visa Renewal User Account Password Reset User Membership Pricing ThankYou Lost Password Contact Company Registration PRO Services Privacy Policy FAQ’s License Services Services VAT REGISTRATION Accounting & Bookkeeping Corporate Sponsorship Services Members Hello world! Sample Page Members