PublishedMapController.php
This handles the storage, deletion, and retrieval of published maps, allowing users to store and manage JSON data representing published maps.
storePublishedMap
storePublishedMappublic function storePublishedMap(Request $request)
{
// Retrieve JSON data from the request
$jsonData = $request->input('jsonData');
// Decode the JSON data into an associative array
$jsonData = json_decode($jsonData, true);
// Extract a unique identifier (ID) from the JSON data
$uniqueHash = $jsonData['ID'];
// Define the directory where the published maps will be stored
$directory = 'PublishedMap';
// Construct the file path using the unique ID and set the file extension to '.json'
$filePublishedMap = $directory . '/' . $uniqueHash . '.json';
// Check if the directory exists; if not, create it recursively with full permissions
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
// Convert the JSON data back to a pretty-printed JSON format
$prettyPrintedJson = json_encode($jsonData, JSON_PRETTY_PRINT);
// Open the file for writing; if the file does not exist, it will be created
$file = fopen($filePublishedMap, 'w');
// Check if the file was successfully opened
if ($file) {
// Write the pretty-printed JSON data to the file
fwrite($file, $prettyPrintedJson);
// Close the file
fclose($file);
}
// Prepare a success response
$response = [
'SUCCESS' => 1,
];
// Return the response
return $response;
}To explain what this storePublishedMap method does:
Retrieve JSON Data:
It extracts the JSON data from the incoming HTTP request using
$request->input('jsonData').
Decode JSON Data:
It decodes the JSON data into an associative array using
json_decode($jsonData, true).
Extract Unique Identifier (ID):
It retrieves a unique identifier (ID) from the JSON data using
$jsonData['ID'].
Define Directory and File Path:
It defines a directory named 'PublishedMap' where the published maps will be stored.
It constructs the file path using the unique ID and sets the file extension to '.json'.
Create Directory if Not Exists:
It checks if the directory exists, and if not, it creates it recursively with full permissions using
mkdir.
Convert to Pretty-Printed JSON:
It converts the JSON data back to a pretty-printed JSON format using
json_encode($jsonData, JSON_PRETTY_PRINT).
Open File for Writing:
It opens the file for writing using
fopen($filePublishedMap, 'w'). If the file doesn't exist, it will be created.
Write to File:
It writes the pretty-printed JSON data to the file using
fwrite($file, $prettyPrintedJson).
Close File:
It closes the file using
fclose($file).
Prepare Success Response:
It prepares a success response array with the key
'SUCCESS'set to1.
Return Response:
It returns the success response array.
deletePublishedMap
deletePublishedMappublic function deletePublishedMap(Request $request)
{
// Define the directory where the published maps are stored
$directory = 'PublishedMap';
// Construct the file path to the map that needs to be deleted
$fileToDelete = $directory . '/' . $request->input('fileName') . '.json';
// Delete the specified file using the unlink function
unlink($fileToDelete);
// Prepare a success response
$response = [
'SUCCESS' => 1,
];
// Return the response
return $response;
}To explain what this deletePublishedMap method does:
Define Directory:
It defines the directory where the published maps are stored as
$directory = 'PublishedMap'.
Construct File Path:
It constructs the file path to the map that needs to be deleted by appending the filename and '.json' extension.
Delete File:
It uses the
unlinkfunction to delete the specified file ($fileToDelete).
Prepare Success Response:
It prepares a success response array with the key
'SUCCESS'set to1.
Return Response:
It returns the success response array.
getPublishedMap
getPublishedMappublic function getPublishedMap()
{
// Define the directory where the published maps are stored
$directory = 'PublishedMap';
// Get the list of files in the directory
$files = scandir($directory);
// Remove '.' and '..' entries from the list of files
$files = array_diff($files, ['.', '..']);
// Initialize an array to store the JSON data of each published map
$jsonData = [];
// Iterate through each file in the directory
foreach ($files as $file) {
// Construct the full file path
$filePath = $directory . '/' . $file;
// Read the content of the file
$fileContent = file_get_contents($filePath);
// Check if reading the file content was successful
if ($fileContent !== false) {
// Decode the JSON content into an associative array and add it to the result array
$jsonData[] = json_decode($fileContent, true);
}
}
// Prepare a success response with the retrieved JSON data
$response = [
'SUCCESS' => 1,
'DATA' => $jsonData
];
// Return the response
return $response;
}To explain what this getPublishedMap method does:
Define Directory:
It defines the directory where the published maps are stored as
$directory = 'PublishedMap'.
List Files in Directory:
It uses
scandir($directory)to get the list of files in the specified directory.
Filter System Entries:
It removes the '.' and '..' entries from the list of files using
array_diff.
Initialize JSON Data Array:
It initializes an empty array named
$jsonDatato store the JSON data of each published map.
Iterate Through Files:
It iterates through each file in the directory.
Construct File Path:
It constructs the full file path by appending the filename to the directory.
Read File Content:
It reads the content of each file using
file_get_contents($filePath).
Decode JSON Content:
It decodes the JSON content into an associative array using
json_decode($fileContent, true).
Add JSON Data to Array:
It adds the decoded JSON data to the
$jsonDataarray.
Prepare Success Response:
It prepares a success response array with the keys
'SUCCESS'set to1and'DATA'set to the array of JSON data.
Return Response:
It returns the success response array containing the retrieved JSON data.
Last updated