> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getlecker.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Helper_Backend

> Utility trait for backend operations in a PHP application

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.

<ResponseField name="dbName" type="string">
  The name of the database to be dumped and imported.
</ResponseField>

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

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

### executeSqlChunk($localDb, $sqlChunk)

Executes a chunk of SQL commands on the local database.

<ResponseField name="localDb" type="PDO">
  PDO connection to the local database.
</ResponseField>

<ResponseField name="sqlChunk" type="string">
  A string containing multiple SQL commands separated by semicolons.
</ResponseField>

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.

<ResponseField name="Returns">
  The result of matching 'is\_unique\[metatag.name]' against the 'is\_unique' pattern.
</ResponseField>

## CSV Upload Functions

### csv\_upload(\$itemId)

Displays the CSV upload form.

<ResponseField name="itemId" type="string|int">
  The ID of the item associated with the CSV upload.
</ResponseField>

### upload\_file()

Handles the file upload process for CSV files.

<ResponseField name="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
</ResponseField>

### csvUpdateWhereId()

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

<ResponseField name="Returns">
  JSON response with:

  * status: 'success' if the operation was successful
  * returndata: An array of updated data
</ResponseField>

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

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

## Configuration Variables

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

<ResponseField name="$divider" type="string" default=",">
  The delimiter used in the CSV file.
</ResponseField>

<ResponseField name="$keyOfItemId" type="string" default="event_id">
  The key in the CSV file that corresponds to the item ID in the database.
</ResponseField>

<ResponseField name="$tableName" type="string" default="table">
  The name of the database table to be updated.
</ResponseField>

<ResponseField name="$keyArray" type="array">
  An array of keys in the CSV file, in the order they appear. These keys should correspond to database column names.
</ResponseField>
