diff options
| author | Lester Caine <lester@lsces.co.uk> | 2026-05-28 16:50:46 +0100 |
|---|---|---|
| committer | Lester Caine <lester@lsces.co.uk> | 2026-05-28 16:50:46 +0100 |
| commit | 124baae9bdd250afbd4af4f898337b6aa44f4fd9 (patch) | |
| tree | 55a024e6c1dd15dd87f28da7cb917a2eb5158712 /import | |
| parent | 639fd9f85e189cf98977af0caf20108bb557eb2a (diff) | |
| download | stock-124baae9bdd250afbd4af4f898337b6aa44f4fd9.tar.gz stock-124baae9bdd250afbd4af4f898337b6aa44f4fd9.tar.bz2 stock-124baae9bdd250afbd4af4f898337b6aa44f4fd9.zip | |
Fix kitlocker groups import: correct parent assembly_id, always ensure link
Parent assembly_id corrected from 375 to 21. isValid() alone was not
sufficient to detect a missing DB row — add mContentId check.
Restructure skip logic so parent link is checked and inserted for all
rows, making the script safe to re-run after the original silent failure.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'import')
| -rw-r--r-- | import/load_kitlocker_groups.php | 83 |
1 files changed, 48 insertions, 35 deletions
diff --git a/import/load_kitlocker_groups.php b/import/load_kitlocker_groups.php index 5bc4795..d3b6055 100644 --- a/import/load_kitlocker_groups.php +++ b/import/load_kitlocker_groups.php @@ -1,12 +1,13 @@ <?php /** - * Phase 1: Import KitlockerGroups.csv as child assemblies of assembly 375. + * Phase 1: Import KitlockerGroups.csv as child assemblies of a parent assembly. * * CSV columns: KLID, Title * Each group assembly is tagged with a KLID xref so load_kitlocker_assemblies.php * can locate the correct parent by KLID lookup. * - * Already-existing groups (matched by KLID xref) are skipped. + * Safe to re-run: existing groups (matched by KLID xref) are skipped but their + * parent link is always checked and added if missing. * * @package stock */ @@ -23,18 +24,19 @@ $gBitSystem->verifyPermission( 'p_stock_admin' ); require_once STOCK_PKG_CLASS_PATH.'StockAssembly.php'; $csvFile = __DIR__.'/data/KitlockerGroups.csv'; -$parentAssemblyId = 375; -$loaded = $skipped = 0; +$parentAssemblyId = 21; +$loaded = $skipped = $linked = 0; $errors = []; -// Load parent assembly +// Load parent assembly and verify it actually exists in the DB $parent = new StockAssembly( $parentAssemblyId, null ); $parent->load(); -if( !$parent->isValid() ) { - $errors[] = "Parent assembly $parentAssemblyId not found."; +if( !$parent->isValid() || empty( $parent->mContentId ) ) { + $errors[] = "Parent assembly $parentAssemblyId not found or has no content_id — check the assembly_id."; } elseif( !file_exists( $csvFile ) ) { $errors[] = "File not found: $csvFile"; } else { + $parentContentId = $parent->mContentId; $fh = fopen( $csvFile, 'r' ); $rowNum = 0; while( ($cols = fgetcsv( $fh, 0, ',', '"', '' )) !== false ) { @@ -45,46 +47,57 @@ if( !$parent->isValid() ) { $title = trim( $cols[1] ?? '' ); if( $klid === '' || $title === '' ) continue; - // Skip if KLID xref already exists - if( $gBitDb->getOne( + // Get existing content_id from KLID xref, or create new assembly + $contentId = (int)$gBitDb->getOne( "SELECT `content_id` FROM `".BIT_DB_PREFIX."liberty_xref` WHERE `item`=? AND `xkey`=?", [ 'KLID', $klid ] - ) ) { + ); + + if( $contentId ) { $skipped++; - continue; - } + } else { + $assembly = new StockAssembly(); + $pHash = [ 'title' => $title, 'edit' => '', 'format_guid' => 'bithtml' ]; + if( !$assembly->store( $pHash ) ) { + $errors[] = "Row $rowNum: failed to create '$title'"; + continue; + } + $contentId = $assembly->mContentId; - $assembly = new StockAssembly(); - $pHash = [ 'title' => $title, 'edit' => '', 'format_guid' => 'bithtml' ]; - if( !$assembly->store( $pHash ) ) { - $errors[] = "Row $rowNum: failed to create '$title'"; - continue; + // Tag with KLID xref + $xrefId = $gBitDb->GenID( 'liberty_xref_seq' ); + $gBitDb->associateInsert( BIT_DB_PREFIX.'liberty_xref', [ + 'xref_id' => $xrefId, + 'content_id' => $contentId, + 'item' => 'KLID', + 'xorder' => 0, + 'xkey' => $klid, + 'last_update_date' => $gBitDb->NOW(), + ] ); + $loaded++; } - $contentId = $assembly->mContentId; - - // Tag with KLID xref - $xrefId = $gBitDb->GenID( 'liberty_xref_seq' ); - $gBitDb->associateInsert( BIT_DB_PREFIX.'liberty_xref', [ - 'xref_id' => $xrefId, - 'content_id' => $contentId, - 'item' => 'KLID', - 'xorder' => 0, - 'xkey' => $klid, - 'last_update_date' => $gBitDb->NOW(), - ] ); - - // Add as child of parent assembly 375 - $parent->addItem( $contentId ); - - $loaded++; + // Always ensure parent link exists (safe to re-run) + $alreadyLinked = (int)$gBitDb->getOne( + "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."stock_assembly_component_map` + WHERE `assembly_content_id`=? AND `item_content_id`=?", + [ $parentContentId, $contentId ] + ); + if( !$alreadyLinked ) { + $gBitDb->getOne( + "INSERT INTO `".BIT_DB_PREFIX."stock_assembly_component_map` + (`assembly_content_id`, `item_content_id`, `item_position`) VALUES (?,?,NULL)", + [ $parentContentId, $contentId ] + ); + $linked++; + } } fclose( $fh ); } $gBitSmarty->assign( 'loaded', $loaded ); $gBitSmarty->assign( 'skipped', $skipped ); -$gBitSmarty->assign( 'deleted', 0 ); +$gBitSmarty->assign( 'deleted', $linked ); $gBitSmarty->assign( 'errors', $errors ); $gBitSmarty->assign( 'csvFile', $csvFile ); $gBitSmarty->assign( 'movement', null ); |
