136 lines
5.3 KiB
PHP
136 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Mail\RfpSubmittedMail;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use GuzzleHttp\Client;
|
|
use App\Services\FormService;
|
|
|
|
class FormsController extends Controller
|
|
{
|
|
public function create(Request $request)
|
|
{
|
|
dd("hola!");
|
|
// Automatically determine the view from the URL segment
|
|
$formType = $request->segment(1); // Gets 'submit-xx' part of the URL
|
|
dd("hola!");
|
|
// Construct the view name dynamically based on the form type
|
|
$view = "forms.{$formType}";
|
|
|
|
// Return the dynamically determined view
|
|
return view($view);
|
|
}
|
|
|
|
// Method to show the dynamic form based on the route parameter
|
|
public function create_dynamic($formName, FormService $formService)
|
|
{
|
|
try {
|
|
$formFields = $formService->getFormFields($formName);
|
|
} catch (\Exception $e) {
|
|
// Handle the error if the JSON file does not exist
|
|
abort(404, 'Form configuration not found.');
|
|
}
|
|
return view('forms.submit-form', ['formFields' => $formFields]);
|
|
}
|
|
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$formData = $request->all();
|
|
$jsonData = json_encode($formData);
|
|
|
|
// Check if web3Signature is provided
|
|
if (!empty($formData['web3Signature'])) {
|
|
$ipfsResponse = $this->addToIPFS($jsonData);
|
|
|
|
// Include the IPFS response (CID or error message) in the form data for the email
|
|
if (!empty($ipfsResponse['cid'])) {
|
|
$formData['IPFS_CID'] = $ipfsResponse['cid'];
|
|
} else {
|
|
// Include the error message explaining why the CID was not retrieved
|
|
$formData['IPFS_Error'] = $ipfsResponse['error'];
|
|
}
|
|
}
|
|
|
|
// Determine the recipient email address from the form data
|
|
$recipientEmail = !empty($formData['contactEmail']) ? $formData['contactEmail'] : 'contacto.hadox@gmail.com'; // Fallback email if 'contactEmail' is not provided
|
|
|
|
// Send the email with all form data, potentially including the IPFS response
|
|
Mail::to($recipientEmail)->send(new RfpSubmittedMail($formData));
|
|
|
|
// Redirect back or to another page with an appropriate message
|
|
if (!empty($ipfsResponse['cid'])) {
|
|
return redirect()->route('rfp.create')->with('success', 'RFP submitted successfully, emailed to ' . $recipientEmail . ', and stored on IPFS with CID: ' . $ipfsResponse['cid']);
|
|
} else if (isset($ipfsResponse['error'])) {
|
|
return redirect()->route('rfp.create')->with('warning', 'RFP submitted successfully and emailed to ' . $recipientEmail . ', but storing on IPFS failed: ' . $ipfsResponse['error']);
|
|
} else {
|
|
return redirect()->route('rfp.create')->with('success', 'RFP submitted successfully and emailed to ' . $recipientEmail);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
protected function addToIPFS($jsonData)
|
|
{
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
// Pinata API endpoint for pinning files
|
|
$pinataUrl = 'https://api.pinata.cloud/pinning/pinFileToIPFS';
|
|
|
|
// Pinata JWT for authentication
|
|
$pinataJWT = env('PINATA_JWT'); // Replace with your actual JWT from Pinata
|
|
|
|
try {
|
|
// Prepare multipart form data
|
|
$multipartData = [
|
|
[
|
|
'name' => 'file',
|
|
'contents' => $jsonData,
|
|
'filename' => 'data.json'
|
|
],
|
|
[
|
|
'name' => 'pinataOptions',
|
|
'contents' => json_encode(['cidVersion' => 1]),
|
|
],
|
|
[
|
|
'name' => 'pinataMetadata',
|
|
'contents' => json_encode(['name' => 'Your Metadata Name']),
|
|
]
|
|
];
|
|
|
|
// Make the request to Pinata
|
|
$response = $client->post($pinataUrl, [
|
|
'headers' => [
|
|
'Authorization' => "Bearer {$pinataJWT}",
|
|
],
|
|
'multipart' => $multipartData,
|
|
]);
|
|
|
|
// Parse the response
|
|
$responseBody = json_decode($response->getBody()->getContents());
|
|
|
|
if (isset($responseBody->IpfsHash)) {
|
|
return ['cid' => $responseBody->IpfsHash]; // Use 'IpfsHash' for Pinata
|
|
} else {
|
|
$message = $responseBody->error->message ?? 'No CID returned, and no error message provided by Pinata.';
|
|
return ['error' => $message];
|
|
}
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
|
// Handle client exceptions (4xx errors)
|
|
$responseBody = json_decode($e->getResponse()->getBody()->getContents(), true);
|
|
$errorMessage = $responseBody['error']['reason'] ?? 'Client error when adding data to IPFS.';
|
|
\Log::error('Client error when adding data to IPFS: ' . $errorMessage);
|
|
return ['error' => 'Client error when adding data to IPFS: ' . $errorMessage];
|
|
} catch (\Exception $e) {
|
|
// Handle other exceptions
|
|
\Log::error('Failed to add data to IPFS: ' . $e->getMessage());
|
|
return ['error' => 'Failed to add data to IPFS: ' . $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|