summaryrefslogtreecommitdiff
path: root/list_kitlocker.php
blob: 05313bab9f579656598a85f2fda4662fe708594c (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
<?php
/**
 * Combined kitlocker listing — assemblies and components filtered by stgrp tag.
 * @package stock
 */

namespace Bitweaver\Stock;

use Bitweaver\KernelTools;
use Bitweaver\Liberty\LibertyContent;
use Bitweaver\BitBase;

require_once '../kernel/includes/setup_inc.php';

$gBitSystem->verifyPackage( 'stock' );

global $gBitSystem, $gBitSmarty, $gBitDb;

$stgrp = !empty( $_REQUEST['stgrp'] ) ? trim( $_REQUEST['stgrp'] ) : null;

$X        = BIT_DB_PREFIX;
$bindVars = [];
$whereSql = "AND lc.`content_type_guid` IN ('stockassembly','stockcomponent')";

if( $stgrp ) {
	$whereSql .= " AND EXISTS (SELECT 1 FROM `{$X}liberty_xref` sx WHERE sx.`content_id` = lc.`content_id` AND sx.`item` = ?)";
	$bindVars[] = $stgrp;
	$groupTitle = $gBitDb->getOne(
		"SELECT `cross_ref_title` FROM `{$X}liberty_xref_item` WHERE `item` = ? AND `x_group` = 'stgrp'",
		[ $stgrp ]
	);
} else {
	$whereSql .= " AND EXISTS (SELECT 1 FROM `{$X}liberty_xref` sx
		INNER JOIN `{$X}liberty_xref_item` xi ON xi.`item` = sx.`item` AND xi.`x_group` = 'stgrp'
		WHERE sx.`content_id` = lc.`content_id`)";
	$groupTitle = null;
}

$maxRecords = max( 1, (int)( $_REQUEST['max_records'] ?? 20 ) );
$page       = max( 1, (int)( $_REQUEST['page'] ?? 1 ) );
$offset     = ( $page - 1 ) * $maxRecords;

$totalCount = (int)$gBitDb->getOne(
	"SELECT COUNT(*) FROM `{$X}liberty_content` lc WHERE 1=1 $whereSql",
	$bindVars
);

$rs = $gBitDb->query(
	"SELECT lc.`content_id`, lc.`title`, lc.`data`, lc.`format_guid`, lc.`content_type_guid`,
		(SELECT FIRST 1 x.`xkey` FROM `{$X}liberty_xref` x
		 WHERE x.`content_id` = lc.`content_id` AND x.`item` = 'KLID') AS klid,
		(SELECT FIRST 1 x.`xkey` FROM `{$X}liberty_xref` x
		 WHERE x.`content_id` = lc.`content_id` AND x.`item` = 'KLPR') AS klpr
	 FROM `{$X}liberty_content` lc
	 WHERE 1=1 $whereSql
	 ORDER BY lc.`title`",
	$bindVars, $maxRecords, $offset
);

$items = [];
while( $row = $rs->fetchRow() ) {
	$parseHash = [ 'data' => $row['data'], 'format_guid' => $row['format_guid'] ?? 'bithtml' ];
	$row['parsed_data'] = LibertyContent::parseDataHash( $parseHash );
	$items[] = $row;
}

$listHash = [
	'cant'         => $totalCount,
	'max_records'  => $maxRecords,
	'offset'       => $offset,
	'page'         => $page,
	'page_records' => count( $items ),
];
if( $stgrp ) {
	$listHash['listInfo']['parameters']['stgrp'] = $stgrp;
}
BitBase::postGetList( $listHash );

$gBitSmarty->assign( 'kitlockerItems', $items );
$gBitSmarty->assign( 'stgrp',          $stgrp );
$gBitSmarty->assign( 'groupTitle',     $groupTitle );
$gBitSmarty->assign( 'listInfo',       $listHash['listInfo'] );

$pageTitle = $groupTitle ?: KernelTools::tra( 'Kitlocker' );
$gBitSystem->setBrowserTitle( $pageTitle );
$gBitSystem->display( 'bitpackage:stock/list_kitlocker.tpl', null, [ 'display_mode' => 'list' ] );