-
Notifications
You must be signed in to change notification settings - Fork 0
The Log Ecobrick Page
The log.php
file is the cornerstone of the GoBrik Ecobrick logging system. It allows users to log ecobricks to the GoBrik platform. The page handles both user authentication, interaction with the database to log an ecobrick's data and the geo tagging of the ecobrick record using the user's information. The script also includes various safety checks, error handling, and user feedback.
The script starts by importing helper functions for authentication from earthenAuth_helper.php
. It checks if the user is logged in and manages the session securely:
- Uses
startSecureSession()
to regenerate sessions for security. - Retrieves the user's
buwana_id
from the session to personalize the interaction.
If the user is authenticated, the script retrieves their location data from the database using buwana_id
:
- Location Data: Fetches the user's latitude, longitude, continent, watershed, and full location.
- Community and Status: Retrieves the user’s community name and GEA (Global Ecobrick Alliance) status.
The system also connects to two databases (gobrikconn_env.php
and buwanaconn_env.php
):
- Buwana Database: For user information.
- GoBrik Database: For ecobrick logging and related actions.
If a retry parameter is provided in the URL, the script triggers the retry process:
- This allows users to resubmit ecobricks that may have failed during a previous submission.
When the form is submitted via a POST request, the following occurs:
- Data Validation: The form collects details about the ecobrick (e.g., maker, weight, volume, type of sequestration, plastic source, brand name, location, etc.).
- Data Processing: The script calculates important values, such as the density of the ecobrick and the CO2 sequestration equivalent.
-
Serial Generation: Unique identifiers (serial number and ecobrick ID) are generated using
setSerialNumber()
. - Country and Community IDs: The script uses the location to extract the country and community IDs from the database.
The script checks if the ecobrick already exists using its unique ID:
- If an existing ecobrick is found, the record is updated.
- If no record exists, a new one is created in the
tb_ecobricks
table.
The SQL queries used here are dynamic and handle errors using exception handling and logging.
The script provides real-time feedback to the user:
- Form Validation: JavaScript ensures that mandatory fields (e.g., ecobrick maker, volume, weight) are correctly filled before submission.
- Autocomplete: Location fields offer suggestions using the OpenStreetMap API.
- River Suggestions: For the watershed, it fetches nearby rivers using the Overpass API.
- Default Settings: Users can save their ecobrick settings to local storage and restore them for future submissions.
The script logs extensive information for debugging and feedback:
- Database Interaction: Logs the query execution process, affected rows, and any warnings or errors.
- Error Feedback: If something goes wrong (e.g., user data missing, database errors), the user is informed, and details are logged for developers.
Several JavaScript features improve the user experience:
- Form Submission Validation: The script checks for invalid characters, missing fields, and other form issues before submission.
- Autocomplete: Provides location and community suggestions to the user.
- Local Storage: Ecobrick form data can be saved and restored for convenience.
- earthenAuth_helper.php: Handles session and authentication-related logic.
- gobrikconn_env.php: Database connection for GoBrik (for ecobrick-related data).
- buwanaconn_env.php: Database connection for Buwana (for user-related data).
The script includes detailed error handling for:
-
Database Errors: Uses
try-catch
blocks to catch and log any errors that occur during the database operations. - Validation Errors: Ensures that all required data fields are present and valid before submitting to the database.
- Fetching location data:
$sql_location = "SELECT location_lat, location_long FROM users_tb WHERE buwana_id = ?";
$stmt_location = $buwana_conn->prepare($sql_location);
$stmt_location->bind_param("i", $buwana_id);
$stmt_location->execute();
$stmt_location->bind_result($user_location_lat, $user_location_long);
$stmt_location->fetch();
- Inserting a new ecobrick:
$sql = "INSERT INTO tb_ecobricks (...) VALUES (...)";
$stmt = $gobrik_conn->prepare($sql);
$stmt->bind_param(...);
$stmt->execute();
The log.php
page is a crucial part of the GoBrik system, responsible for allowing users to log their ecobricks with detailed information. It provides a secure, user-friendly interface, manages user data effectively, and ensures that all ecobricks are properly recorded in the database for posterity.
In the GoBrik table we write table names with tb_ at the start of the table name (using a plural noun i.e. ecobricks, ecobrickers, etc) if it is only used on GoBrik. On the buwana database we add the _tb at the end of the name (using a plural nound i.e. users, countries, watershed, etc.). On the GoBrik database, if a table is mirrored on Buwana, we name it the same as on the Buwana database (i.e. communities_tb, languages_tb, countries_tb etc.)