summaryrefslogtreecommitdiff
path: root/import/ImportSimpleAssembly.php
blob: 5b0e6af28b147e0058c6a5a30a4c08fbe7389a47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
 * Simplified assembly CSV importer — title, description, KLPR, KLURL only.
 *
 * CSV column layout (0-based, no header row):
 *   0  title        Assembly / kit name (used as the liberty content title)
 *   1  description  Plain-text description (stored as content body)
 *   2  KLPR         Kitlocker price (stored in xkey, max 32 chars)
 *   3  KLURL        Kitlocker datasheet URL (stored in xkey_ext, max 250 chars)
 *
 * Existing assemblies (matched by title) are skipped unless cleared first.
 * KLPR / KLURL xrefs are only inserted when the column is non-empty.
 *
 * @package stock
 */

use Bitweaver\Stock\StockAssembly;

/**
 * Delete an assembly (and all related rows) by title. Returns true if deleted, false if not found.
 */
function stockExpungeAssemblyByTitle( string $title ): bool {
	global $gBitDb;

	$contentId = $gBitDb->getOne(
		"SELECT lc.`content_id` FROM `".BIT_DB_PREFIX."liberty_content` lc
		 WHERE lc.`content_type_guid` = '".STOCKASSEMBLY_CONTENT_TYPE_GUID."' AND lc.`title` = ?",
		[ $title ]
	);
	if( !$contentId ) {
		return false;
	}

	$assembly = new StockAssembly( (int)$contentId );
	$assembly->expunge();
	return true;
}

function stockImportSimpleAssembly( array $data, int $rowNum ): array {
	global $gBitDb;

	$result = [ 'loaded' => 0, 'skipped' => 0, 'errors' => [] ];

	$title = trim( $data[0] ?? '' );
	if( empty( $title ) ) {
		$result['skipped']++;
		$result['errors'][] = "Row $rowNum: empty title, skipped.";
		return $result;
	}

	// Skip if already exists
	$exists = $gBitDb->getOne(
		"SELECT lc.`content_id` FROM `".BIT_DB_PREFIX."liberty_content` lc
		 WHERE lc.`content_type_guid` = '".STOCKASSEMBLY_CONTENT_TYPE_GUID."' AND lc.`title` = ?",
		[ $title ]
	);
	if( $exists ) {
		$result['skipped']++;
		$result['errors'][] = "Row $rowNum: '$title' already exists, skipped.";
		return $result;
	}

	$description = trim( $data[1] ?? '' );
	$klpr        = trim( $data[2] ?? '' );
	$klurl       = trim( $data[3] ?? '' );

	$assembly = new StockAssembly();
	$pHash = [
		'title'       => $title,
		'edit'        => $description,
		'format_guid' => 'bithtml',
	];
	if( !$assembly->store( $pHash ) ) {
		$result['skipped']++;
		$result['errors'][] = "Row $rowNum: failed to create assembly '$title'.";
		return $result;
	}

	$contentId = $assembly->mContentId;

	if( !empty( $klpr ) ) {
		$xrefId = $gBitDb->GenID( 'liberty_xref_seq' );
		$gBitDb->associateInsert( BIT_DB_PREFIX.'liberty_xref', [
			'xref_id'          => $xrefId,
			'content_id'       => $contentId,
			'item'             => 'KLPR',
			'xorder'           => 0,
			'xkey'             => substr( $klpr, 0, 32 ),
			'last_update_date' => $gBitDb->NOW(),
		] );
	}

	if( !empty( $klurl ) ) {
		$xrefId = $gBitDb->GenID( 'liberty_xref_seq' );
		$gBitDb->associateInsert( BIT_DB_PREFIX.'liberty_xref', [
			'xref_id'          => $xrefId,
			'content_id'       => $contentId,
			'item'             => 'KLURL',
			'xorder'           => 0,
			'xkey_ext'         => substr( $klurl, 0, 250 ),
			'last_update_date' => $gBitDb->NOW(),
		] );
	}

	$result['loaded']++;
	return $result;
}