PublishedMapController.php

This handles the storage, deletion, and retrieval of published maps, allowing users to store and manage JSON data representing published maps.

storePublishedMap

public 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:

  1. Retrieve JSON Data:

    • It extracts the JSON data from the incoming HTTP request using $request->input('jsonData').

  2. Decode JSON Data:

    • It decodes the JSON data into an associative array using json_decode($jsonData, true).

  3. Extract Unique Identifier (ID):

    • It retrieves a unique identifier (ID) from the JSON data using $jsonData['ID'].

  4. 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'.

  5. Create Directory if Not Exists:

    • It checks if the directory exists, and if not, it creates it recursively with full permissions using mkdir.

  6. Convert to Pretty-Printed JSON:

    • It converts the JSON data back to a pretty-printed JSON format using json_encode($jsonData, JSON_PRETTY_PRINT).

  7. Open File for Writing:

    • It opens the file for writing using fopen($filePublishedMap, 'w'). If the file doesn't exist, it will be created.

  8. Write to File:

    • It writes the pretty-printed JSON data to the file using fwrite($file, $prettyPrintedJson).

  9. Close File:

    • It closes the file using fclose($file).

  10. Prepare Success Response:

    • It prepares a success response array with the key 'SUCCESS' set to 1.

  11. Return Response:

    • It returns the success response array.

deletePublishedMap

public 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:

  1. Define Directory:

    • It defines the directory where the published maps are stored as $directory = 'PublishedMap'.

  2. Construct File Path:

    • It constructs the file path to the map that needs to be deleted by appending the filename and '.json' extension.

  3. Delete File:

    • It uses the unlink function to delete the specified file ($fileToDelete).

  4. Prepare Success Response:

    • It prepares a success response array with the key 'SUCCESS' set to 1.

  5. Return Response:

    • It returns the success response array.

getPublishedMap

public 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:

  1. Define Directory:

    • It defines the directory where the published maps are stored as $directory = 'PublishedMap'.

  2. List Files in Directory:

    • It uses scandir($directory) to get the list of files in the specified directory.

  3. Filter System Entries:

    • It removes the '.' and '..' entries from the list of files using array_diff.

  4. Initialize JSON Data Array:

    • It initializes an empty array named $jsonData to store the JSON data of each published map.

  5. Iterate Through Files:

    • It iterates through each file in the directory.

  6. Construct File Path:

    • It constructs the full file path by appending the filename to the directory.

  7. Read File Content:

    • It reads the content of each file using file_get_contents($filePath).

  8. Decode JSON Content:

    • It decodes the JSON content into an associative array using json_decode($fileContent, true).

  9. Add JSON Data to Array:

    • It adds the decoded JSON data to the $jsonData array.

  10. Prepare Success Response:

    • It prepares a success response array with the keys 'SUCCESS' set to 1 and 'DATA' set to the array of JSON data.

  11. Return Response:

    • It returns the success response array containing the retrieved JSON data.

Last updated