diff --git a/src/packages/com_mokojoomstorelocator/admin/src/Controller/ImportController.php b/src/packages/com_mokojoomstorelocator/admin/src/Controller/ImportController.php index 6f8f0f3..6100b45 100644 --- a/src/packages/com_mokojoomstorelocator/admin/src/Controller/ImportController.php +++ b/src/packages/com_mokojoomstorelocator/admin/src/Controller/ImportController.php @@ -61,7 +61,11 @@ class ImportController extends BaseController $geocodeOnImport = (bool) $input->getInt('geocode', 0); $updateExisting = (bool) $input->getInt('update_existing', 0); - $result = $this->processCSV($file['tmp_name'], $geocodeOnImport, $updateExisting); + // Column mapping from the enhanced import UI (JSON string: {"0":"title","1":"address",...}) + $columnMapJson = $input->getString('column_map', ''); + $columnMap = $columnMapJson ? json_decode($columnMapJson, true) : null; + + $result = $this->processCSV($file['tmp_name'], $geocodeOnImport, $updateExisting, $columnMap); $app->enqueueMessage( Text::sprintf('COM_MOKOJOOMSTORELOCATOR_IMPORT_RESULT', $result['imported'], $result['updated'], $result['skipped']), @@ -77,15 +81,16 @@ class ImportController extends BaseController * Expected columns: title, address, city, state, postcode, country, latitude, longitude, * phone, email, website, hours, description * - * @param string $filePath Path to the CSV file. - * @param bool $geocodeOnImport Whether to geocode missing coordinates. - * @param bool $updateExisting Whether to update existing records by title match. + * @param string $filePath Path to the CSV file. + * @param bool $geocodeOnImport Whether to geocode missing coordinates. + * @param bool $updateExisting Whether to update existing records by title match. + * @param array|null $columnMap Column mapping from UI: {"csv_index":"field_name",...} * * @return array ['imported' => int, 'updated' => int, 'skipped' => int] * * @since 1.0.0 */ - private function processCSV(string $filePath, bool $geocodeOnImport, bool $updateExisting): array + private function processCSV(string $filePath, bool $geocodeOnImport, bool $updateExisting, ?array $columnMap = null): array { $handle = fopen($filePath, 'r'); @@ -104,7 +109,17 @@ class ImportController extends BaseController return ['imported' => 0, 'updated' => 0, 'skipped' => 0]; } - $headers = array_map('strtolower', array_map('trim', $headers)); + // If column map provided from UI, use it; otherwise fall back to header-based mapping + if ($columnMap) + { + // columnMap is {"csv_index": "field_name", ...} + $useColumnMap = true; + } + else + { + $headers = array_map('strtolower', array_map('trim', $headers)); + $useColumnMap = false; + } $db = Factory::getContainer()->get(\Joomla\Database\DatabaseInterface::class); $geocoder = $geocodeOnImport ? new Geocoder() : null; @@ -114,13 +129,25 @@ class ImportController extends BaseController while (($row = fgetcsv($handle)) !== false) { - if (count($row) < count($headers)) + if (count($row) < 2) { $skipped++; continue; } - $data = array_combine($headers, $row); + if ($useColumnMap) + { + $data = []; + + foreach ($columnMap as $csvIndex => $fieldName) + { + $data[$fieldName] = $row[(int) $csvIndex] ?? ''; + } + } + else + { + $data = @array_combine($headers, $row) ?: []; + } if (empty($data['title'])) { diff --git a/src/packages/com_mokojoomstorelocator/admin/tmpl/import/default.php b/src/packages/com_mokojoomstorelocator/admin/tmpl/import/default.php index 9b34cb5..517d656 100644 --- a/src/packages/com_mokojoomstorelocator/admin/tmpl/import/default.php +++ b/src/packages/com_mokojoomstorelocator/admin/tmpl/import/default.php @@ -11,79 +11,288 @@ defined('_JEXEC') or die; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\Router\Route; +use Joomla\CMS\Session\Session; + +$locationFields = [ + '' => '— Skip —', + 'title' => 'Title *', + 'address' => 'Address', + 'city' => 'City', + 'state' => 'State', + 'postcode' => 'Postal Code', + 'country' => 'Country', + 'latitude' => 'Latitude', + 'longitude' => 'Longitude', + 'phone' => 'Phone', + 'email' => 'Email', + 'website' => 'Website', + 'hours' => 'Hours', + 'description' => 'Description', + 'video_url' => 'Video URL', +]; ?> -