Skip to main content

Excel Import

The Excel Import functionality is a decoupled, modular system designed to centralize complex file parsing while delegating business logic (database operations) to individual modules. This architecture ensures high performance, full localization, and easy maintenance.


๐Ÿš€ Overviewโ€‹

The system consists of three main layers:

  1. Parser (magic addon): Handles file uploads and converts Excel rows into a standardized PHP array based on a provided mapping.
  2. Orchestrator (direction addon): Authenticates requests and routes the parsed data to the correct module.
  3. Handler (Specific Module): Receives the cleaned data and performs specific tasks like database insertion.

๐Ÿ› ๏ธ Usage & Configurationโ€‹

To enable Excel import for a module, you need to configure the $conf array in your moduleโ€™s initialization method (e.g., at_cpv.module.php).

1. Enable the UIโ€‹

Add the import_excel flag and define the column mapping.

$conf['import_excel'] = true;

// Define which Excel columns map to which database fields
$conf['import_mapping'] = [
['column' => 'cpv-code', 'required' => true],
['column' => 'cpv-description', 'required' => false],
['column' => 'cpv-unit', 'required' => false]
];

// (Optional) Define where to go after a successful import
$conf['redirect'] = $this->lib->url->url('direction', 'cpv');

๐Ÿ“ฅ Backend Handlerโ€‹

Every module that supports Excel import MUST implement the following method:

import_excel_data(array $data): arrayโ€‹

This method receives the parsed array from the Excel file. Each element in the array is a row mapped according to your $conf['import_mapping'].

๐Ÿ“‹ Example Implementation:โ€‹

public function import_excel_data(array $data): array
{
foreach ($data as $row) {
$code = $row['cpv-code'];
$desc = $row['cpv-description'];

// Your database logic here...
$this->model->save_item($code, $desc);
}

return ['success' => 1, 'message' => 'Successfully imported ' . count($data) . ' items.'];
}

๐Ÿ“‹ Technical Parametersโ€‹

ParameterTypeDescription
import_excelboolEnables the "Import Excel" button in the Autotable UI.
import_mappingarrayA list of objects defining column (field name) and required (boolean).
redirectstringThe URL the browser will navigate to upon successful import.
additional_dataarray(Optional) Hidden key-value pairs passed from the UI to the handler.

๐ŸŒ Localizationโ€‹

All error messages (e.g., "Missing required field", "Empty file") are managed via the framework's language system (.ini files).

  • Dynamic Variables: Use %s for dynamic values like row numbers or field names.
  • Example Key: missing_required_field_on_row=%s ีฟีธีฒีซ %s ีบีกึ€ีฟีกีคีซึ€ ีคีกีทีฟีจ ีขีกึีกีฏีกีตีธึ‚ีด ีง

โš ๏ธ Best Practicesโ€‹

Resource Limits

Large imports automatically increase the PHP memory_limit to 512M and max_execution_time to 600s (10 minutes) during the orchestration phase.

Validation

Always perform final security validation within your import_excel_data() method to ensure data integrity before database insertion.