. */ use Models\Module; use Modules\FileAdapters\FileAdapter; use Modules\Importazione\Import; include_once __DIR__.'/../../core.php'; $modulo_import = Module::find($id_module); switch (filter('op')) { case 'add': $id_import = filter('id_import'); $import = Import::find($id_import); $id_adapter = FileAdapter::getLocalConnector()->id; $id_record = $import->id; Uploads::upload($_FILES['file'], [ 'id_module' => $id_module, 'id_record' => $id_record, 'id_adapter' => $id_adapter, ]); break; case 'example': $id_import = filter('id_import'); $import = Import::find($id_import); $import_manager = $import->class; if (!empty($import_manager)) { // Generazione percorso $file = $modulo_import->upload_directory.'/example-'.strtolower($import->getTranslation('name')).'.csv'; $filepath = base_dir().'/'.$file; // Generazione del file $import_manager::createExample($filepath); echo base_path().'/'.$file; } break; case 'import': // Individuazione del modulo $import = Import::find($id_record); $import_manager = $import->class; // Dati indicati $include_first_row = post('include_first_row'); $fields = (array) post('fields'); $page = post('page'); $limit = 500; // Inizializzazione del lettore CSV $csv = new $import_manager($record->filepath); foreach ($fields as $key => $value) { $csv->setColumnAssociation($key, (int) $value - 1); } // Generazione offset sulla base della pagina $offset = isset($page) ? $page * $limit : 0; // Ignora la prima riga se composta da header if ($offset == 0 && empty($include_first_row)) { ++$offset; } // Gestione automatica dei valori convertiti $primary_key = post('primary_key'); $csv->setPrimaryKey($primary_key - 1); // Operazioni di inizializzazione per l'importazione if (!isset($page) || empty($page)) { $csv->init(); } $count = $csv->importRows($offset, $limit); $more = $count == $limit; // Operazioni di finalizzazione per l'importazione if (!$more) { $csv->complete(); } echo json_encode([ 'more' => $more, 'count' => $count, ]); break; }