The Helper_Backend trait provides utility functions for backend operations, including database management, CSV handling, and regex testing.

Database Functions

createSqlDump($dbName)

Creates a SQL dump of an external database and imports it into a local database.

dbName
string

The name of the database to be dumped and imported.

This function performs the following steps:

  1. Connects to an external database
  2. Retrieves all tables from the external database
  3. Creates a local database if it doesn’t exist
  4. For each table:
    • Generates CREATE TABLE syntax
    • Exports data in chunks
    • Handles BLOB data separately
    • Imports data into the local database
  5. Adjusts character encoding for ‘name’ columns

This function uses hardcoded database credentials. In a production environment, these should be stored securely and not in the source code.

executeSqlChunk(localDb,localDb, sqlChunk)

Executes a chunk of SQL commands on the local database.

localDb
PDO

PDO connection to the local database.

sqlChunk
string

A string containing multiple SQL commands separated by semicolons.

This function:

  1. Temporarily disables foreign key checks
  2. Executes each SQL command in the chunk
  3. Catches and reports any PDOExceptions
  4. Re-enables foreign key checks after execution

Regex Functions

regex_test()

Tests the regex_match function with a specific regex pattern.

Returns

The result of matching ‘is_unique[metatag.name]’ against the ‘is_unique’ pattern.

CSV Upload Functions

csv_upload($itemId)

Displays the CSV upload form.

itemId
string|int

The ID of the item associated with the CSV upload.

upload_file()

Handles the file upload process for CSV files.

Returns

JSON response with:

  • success: Boolean indicating if the upload was successful
  • path: The server path where the file was saved
  • filename: The new filename of the uploaded file

csvUpdateWhereId()

Processes an uploaded CSV file and updates database records based on its contents.

Returns

JSON response with:

  • status: ‘success’ if the operation was successful
  • returndata: An array of updated data

This function:

  1. Reads the CSV file
  2. Maps CSV columns to database fields
  3. Updates database records based on a specified ID column
  4. Deletes the CSV file after processing

The function uses hardcoded values for CSV structure and database mapping. These should be configurable for flexibility in different use cases.

Configuration Variables

The csvUpdateWhereId() function uses several configuration variables that should be customized based on the CSV structure and database schema:

$divider
string
default: ","

The delimiter used in the CSV file.

$keyOfItemId
string
default: "event_id"

The key in the CSV file that corresponds to the item ID in the database.

$tableName
string
default: "table"

The name of the database table to be updated.

$keyArray
array

An array of keys in the CSV file, in the order they appear. These keys should correspond to database column names.