SpreadsheetService
in package
Spreadsheet service for parsing and importing menu data from uploaded files.
Supports CSV, XLSX, XLS, and ODS formats. Provides a multi-step workflow: upload -> parse headers -> map columns -> preview -> import.
Tags
Table of Contents
Constants
- ALLOWED_EXTENSIONS = ['csv', 'xlsx', 'xls', 'ods']
- COLUMN_MAP = ['name' => ['name', 'item name', 'item', 'product', 'product name', 'menu item'], 'description' => ['description', 'desc', 'details', 'item description'], 'price' => ['price', 'cost', 'amount', 'unit price'], 'category' => ['category', 'cat', 'group', 'menu category', 'section'], 'availability' => ['availability', 'available', 'when'], 'spice_level' => ['spice level', 'spice', 'heat', 'heat level'], 'prep_time' => ['prep time', 'preparation time', 'time', 'minutes']]
- MAX_FILE_SIZE = 10 * 1024 * 1024
Methods
- generateTemplate() : string
- Generate a CSV template file for download.
- import() : array<string|int, mixed>
- Import all rows from a spreadsheet into the menu system.
- parseHeaders() : array<string|int, mixed>
- Parse headers from an uploaded spreadsheet.
- previewRows() : array<string|int, mixed>
- Preview rows from the spreadsheet using a given column mapping.
- suggestMapping() : array<string|int, mixed>
- Suggest column mappings based on header names.
- upload() : string
- Upload a spreadsheet file to the storage directory.
- extractRowData() : array<string|int, mixed>
- Extract row data from a spreadsheet using the column mapping.
- generateSlug() : string
- Generate a simple slug from a string.
- mapAvailability() : string
- Map availability text to ENUM value.
- validateFile() : void
- Validate an uploaded spreadsheet file.
Constants
ALLOWED_EXTENSIONS
private
array<string|int, mixed>
ALLOWED_EXTENSIONS
= ['csv', 'xlsx', 'xls', 'ods']
Allowed file extensions
COLUMN_MAP
private
array<string|int, mixed>
COLUMN_MAP
= ['name' => ['name', 'item name', 'item', 'product', 'product name', 'menu item'], 'description' => ['description', 'desc', 'details', 'item description'], 'price' => ['price', 'cost', 'amount', 'unit price'], 'category' => ['category', 'cat', 'group', 'menu category', 'section'], 'availability' => ['availability', 'available', 'when'], 'spice_level' => ['spice level', 'spice', 'heat', 'heat level'], 'prep_time' => ['prep time', 'preparation time', 'time', 'minutes']]
Known column names mapped to database fields
MAX_FILE_SIZE
private
int
MAX_FILE_SIZE
= 10 * 1024 * 1024
Maximum upload file size in bytes (10MB)
Methods
generateTemplate()
Generate a CSV template file for download.
public
generateTemplate() : string
Return values
string —Path to the generated template
import()
Import all rows from a spreadsheet into the menu system.
public
import(string $fileName, array<string|int, mixed> $mapping, int $businessId[, string $mode = 'merge' ]) : array<string|int, mixed>
Parameters
- $fileName : string
-
Name of the stored file
- $mapping : array<string|int, mixed>
-
Associative array of db_field => column_index
- $businessId : int
-
Business ID
- $mode : string = 'merge'
-
Import mode: 'overwrite' or 'merge'
Return values
array<string|int, mixed> —Import result summary
parseHeaders()
Parse headers from an uploaded spreadsheet.
public
parseHeaders(string $fileName) : array<string|int, mixed>
Parameters
- $fileName : string
-
Name of the stored file
Tags
Return values
array<string|int, mixed> —Array of header column names
previewRows()
Preview rows from the spreadsheet using a given column mapping.
public
previewRows(string $fileName, array<string|int, mixed> $mapping[, int $limit = 20 ]) : array<string|int, mixed>
Parameters
- $fileName : string
-
Name of the stored file
- $mapping : array<string|int, mixed>
-
Associative array of db_field => column_index
- $limit : int = 20
-
Maximum number of rows to preview
Return values
array<string|int, mixed> —Array of parsed row data
suggestMapping()
Suggest column mappings based on header names.
public
suggestMapping(array<string|int, mixed> $headers) : array<string|int, mixed>
Parameters
- $headers : array<string|int, mixed>
-
Array of header column names from the spreadsheet
Return values
array<string|int, mixed> —Associative array of db_field => header_index
upload()
Upload a spreadsheet file to the storage directory.
public
upload(array<string|int, mixed> $file) : string
Parameters
- $file : array<string|int, mixed>
-
The $_FILES entry
Tags
Return values
string —The stored file path
extractRowData()
Extract row data from a spreadsheet using the column mapping.
private
extractRowData(Worksheet $sheet, int $row, array<string|int, mixed> $mapping) : array<string|int, mixed>
Parameters
- $sheet : Worksheet
-
Worksheet
- $row : int
-
Row number
- $mapping : array<string|int, mixed>
-
Column mapping
Return values
array<string|int, mixed> —Extracted data
generateSlug()
Generate a simple slug from a string.
private
generateSlug(string $text) : string
Parameters
- $text : string
-
Input text
Return values
stringmapAvailability()
Map availability text to ENUM value.
private
mapAvailability(string $value) : string
Parameters
- $value : string
-
Raw availability text
Return values
string —Valid ENUM value
validateFile()
Validate an uploaded spreadsheet file.
private
validateFile(array<string|int, mixed> $file) : void
Parameters
- $file : array<string|int, mixed>
-
The $_FILES entry