<?php
// This script and data application were generated by AppGini 5.81
// Download AppGini for free from https://bigprof.com/appgini/download/

	/* Configuration */
	/*************************************/

		$pcConfig = array(
			'customers' => array(
			),
			'employees' => array(
				'ReportsTo' => array(
					'parent-table' => 'employees',
					'parent-primary-key' => 'EmployeeID',
					'child-primary-key' => 'EmployeeID',
					'child-primary-key-index' => 0,
					'tab-label' => 'Subordinates <span class="hidden child-label-employees child-field-caption">(Reports To)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/administrator.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(2 => 'Photo', 3 => 'Last Name', 4 => 'First Name', 5 => 'Title', 7 => 'Age', 8 => 'Hire Date', 13 => 'Country', 17 => 'Reports To', 18 => 'Total Sales'),
					'display-field-names' => array(2 => 'Photo', 3 => 'LastName', 4 => 'FirstName', 5 => 'Title', 7 => 'Age', 8 => 'HireDate', 13 => 'Country', 17 => 'ReportsTo', 18 => 'TotalSales'),
					'sortable-fields' => array(0 => '`employees`.`EmployeeID`', 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6, 6 => '`employees`.`BirthDate`', 7 => '`employees`.`Age`', 8 => '`employees`.`HireDate`', 9 => 10, 10 => 11, 11 => 12, 12 => 13, 13 => 14, 14 => 15, 15 => 16, 16 => 17, 17 => 18, 18 => '`employees`.`TotalSales`'),
					'records-per-page' => 10,
					'default-sort-by' => 3,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-employees',
					'template-printable' => 'children-employees-printable',
					'query' => "SELECT `employees`.`EmployeeID` as 'EmployeeID', `employees`.`TitleOfCourtesy` as 'TitleOfCourtesy', `employees`.`Photo` as 'Photo', `employees`.`LastName` as 'LastName', `employees`.`FirstName` as 'FirstName', `employees`.`Title` as 'Title', if(`employees`.`BirthDate`,date_format(`employees`.`BirthDate`,'%m/%d/%Y'),'') as 'BirthDate', `employees`.`Age` as 'Age', if(`employees`.`HireDate`,date_format(`employees`.`HireDate`,'%m/%d/%Y'),'') as 'HireDate', `employees`.`Address` as 'Address', `employees`.`City` as 'City', `employees`.`Region` as 'Region', `employees`.`PostalCode` as 'PostalCode', `employees`.`Country` as 'Country', `employees`.`HomePhone` as 'HomePhone', `employees`.`Extension` as 'Extension', `employees`.`Notes` as 'Notes', IF(    CHAR_LENGTH(`employees1`.`LastName`) || CHAR_LENGTH(`employees1`.`FirstName`), CONCAT_WS('',   `employees1`.`LastName`, ', ', `employees1`.`FirstName`), '') as 'ReportsTo', `employees`.`TotalSales` as 'TotalSales' FROM `employees` LEFT JOIN `employees` as employees1 ON `employees1`.`EmployeeID`=`employees`.`ReportsTo` "
				),
			),
			'orders' => array(
				'CustomerID' => array(
					'parent-table' => 'customers',
					'parent-primary-key' => 'CustomerID',
					'child-primary-key' => 'OrderID',
					'child-primary-key-index' => 0,
					'tab-label' => 'Customer\'s Orders <span class="hidden child-label-orders child-field-caption">(Customer)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/cash_register.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(0 => 'Order ID', 1 => 'Status', 2 => 'Customer', 3 => 'Employee', 4 => 'Order Date', 7 => 'Ship Via', 14 => 'Ship Country', 15 => 'Total'),
					'display-field-names' => array(0 => 'OrderID', 1 => 'status', 2 => 'CustomerID', 3 => 'EmployeeID', 4 => 'OrderDate', 7 => 'ShipVia', 14 => 'ShipCountry', 15 => 'total'),
					'sortable-fields' => array(0 => '`orders`.`OrderID`', 1 => 2, 2 => '`customers1`.`CompanyName`', 3 => 4, 4 => '`orders`.`OrderDate`', 5 => '`orders`.`RequiredDate`', 6 => '`orders`.`ShippedDate`', 7 => '`shippers1`.`CompanyName`', 8 => '`orders`.`Freight`', 9 => '`customers1`.`CompanyName`', 10 => '`customers1`.`Address`', 11 => '`customers1`.`City`', 12 => '`customers1`.`Region`', 13 => '`customers1`.`PostalCode`', 14 => '`customers1`.`Country`', 15 => '`orders`.`total`'),
					'records-per-page' => 10,
					'default-sort-by' => 0,
					'default-sort-direction' => 'desc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-orders',
					'template-printable' => 'children-orders-printable',
					'query' => "SELECT `orders`.`OrderID` as 'OrderID', `orders`.`status` as 'status', IF(    CHAR_LENGTH(`customers1`.`CompanyName`), CONCAT_WS('',   `customers1`.`CompanyName`), '') as 'CustomerID', IF(    CHAR_LENGTH(`employees1`.`LastName`) || CHAR_LENGTH(`employees1`.`FirstName`), CONCAT_WS('',   `employees1`.`LastName`, ', ', `employees1`.`FirstName`), '') as 'EmployeeID', if(`orders`.`OrderDate`,date_format(`orders`.`OrderDate`,'%m/%d/%Y'),'') as 'OrderDate', if(`orders`.`RequiredDate`,date_format(`orders`.`RequiredDate`,'%m/%d/%Y'),'') as 'RequiredDate', if(`orders`.`ShippedDate`,date_format(`orders`.`ShippedDate`,'%m/%d/%Y'),'') as 'ShippedDate', IF(    CHAR_LENGTH(`shippers1`.`CompanyName`), CONCAT_WS('',   `shippers1`.`CompanyName`), '') as 'ShipVia', `orders`.`Freight` as 'Freight', IF(    CHAR_LENGTH(`customers1`.`CompanyName`), CONCAT_WS('',   `customers1`.`CompanyName`), '') as 'ShipName', IF(    CHAR_LENGTH(`customers1`.`Address`), CONCAT_WS('',   `customers1`.`Address`), '') as 'ShipAddress', IF(    CHAR_LENGTH(`customers1`.`City`), CONCAT_WS('',   `customers1`.`City`), '') as 'ShipCity', IF(    CHAR_LENGTH(`customers1`.`Region`), CONCAT_WS('',   `customers1`.`Region`), '') as 'ShipRegion', IF(    CHAR_LENGTH(`customers1`.`PostalCode`), CONCAT_WS('',   `customers1`.`PostalCode`), '') as 'ShipPostalCode', IF(    CHAR_LENGTH(`customers1`.`Country`), CONCAT_WS('',   `customers1`.`Country`), '') as 'ShipCountry', `orders`.`total` as 'total' FROM `orders` LEFT JOIN `customers` as customers1 ON `customers1`.`CustomerID`=`orders`.`CustomerID` LEFT JOIN `employees` as employees1 ON `employees1`.`EmployeeID`=`orders`.`EmployeeID` LEFT JOIN `shippers` as shippers1 ON `shippers1`.`ShipperID`=`orders`.`ShipVia` "
				),
				'EmployeeID' => array(
					'parent-table' => 'employees',
					'parent-primary-key' => 'EmployeeID',
					'child-primary-key' => 'OrderID',
					'child-primary-key-index' => 0,
					'tab-label' => 'Initiated orders <span class="hidden child-label-orders child-field-caption">(Employee)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/cash_register.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(0 => 'Order ID', 1 => 'Status', 2 => 'Customer', 3 => 'Employee', 4 => 'Order Date', 7 => 'Ship Via', 14 => 'Ship Country', 15 => 'Total'),
					'display-field-names' => array(0 => 'OrderID', 1 => 'status', 2 => 'CustomerID', 3 => 'EmployeeID', 4 => 'OrderDate', 7 => 'ShipVia', 14 => 'ShipCountry', 15 => 'total'),
					'sortable-fields' => array(0 => '`orders`.`OrderID`', 1 => 2, 2 => '`customers1`.`CompanyName`', 3 => 4, 4 => '`orders`.`OrderDate`', 5 => '`orders`.`RequiredDate`', 6 => '`orders`.`ShippedDate`', 7 => '`shippers1`.`CompanyName`', 8 => '`orders`.`Freight`', 9 => '`customers1`.`CompanyName`', 10 => '`customers1`.`Address`', 11 => '`customers1`.`City`', 12 => '`customers1`.`Region`', 13 => '`customers1`.`PostalCode`', 14 => '`customers1`.`Country`', 15 => '`orders`.`total`'),
					'records-per-page' => 10,
					'default-sort-by' => 0,
					'default-sort-direction' => 'desc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-orders',
					'template-printable' => 'children-orders-printable',
					'query' => "SELECT `orders`.`OrderID` as 'OrderID', `orders`.`status` as 'status', IF(    CHAR_LENGTH(`customers1`.`CompanyName`), CONCAT_WS('',   `customers1`.`CompanyName`), '') as 'CustomerID', IF(    CHAR_LENGTH(`employees1`.`LastName`) || CHAR_LENGTH(`employees1`.`FirstName`), CONCAT_WS('',   `employees1`.`LastName`, ', ', `employees1`.`FirstName`), '') as 'EmployeeID', if(`orders`.`OrderDate`,date_format(`orders`.`OrderDate`,'%m/%d/%Y'),'') as 'OrderDate', if(`orders`.`RequiredDate`,date_format(`orders`.`RequiredDate`,'%m/%d/%Y'),'') as 'RequiredDate', if(`orders`.`ShippedDate`,date_format(`orders`.`ShippedDate`,'%m/%d/%Y'),'') as 'ShippedDate', IF(    CHAR_LENGTH(`shippers1`.`CompanyName`), CONCAT_WS('',   `shippers1`.`CompanyName`), '') as 'ShipVia', `orders`.`Freight` as 'Freight', IF(    CHAR_LENGTH(`customers1`.`CompanyName`), CONCAT_WS('',   `customers1`.`CompanyName`), '') as 'ShipName', IF(    CHAR_LENGTH(`customers1`.`Address`), CONCAT_WS('',   `customers1`.`Address`), '') as 'ShipAddress', IF(    CHAR_LENGTH(`customers1`.`City`), CONCAT_WS('',   `customers1`.`City`), '') as 'ShipCity', IF(    CHAR_LENGTH(`customers1`.`Region`), CONCAT_WS('',   `customers1`.`Region`), '') as 'ShipRegion', IF(    CHAR_LENGTH(`customers1`.`PostalCode`), CONCAT_WS('',   `customers1`.`PostalCode`), '') as 'ShipPostalCode', IF(    CHAR_LENGTH(`customers1`.`Country`), CONCAT_WS('',   `customers1`.`Country`), '') as 'ShipCountry', `orders`.`total` as 'total' FROM `orders` LEFT JOIN `customers` as customers1 ON `customers1`.`CustomerID`=`orders`.`CustomerID` LEFT JOIN `employees` as employees1 ON `employees1`.`EmployeeID`=`orders`.`EmployeeID` LEFT JOIN `shippers` as shippers1 ON `shippers1`.`ShipperID`=`orders`.`ShipVia` "
				),
				'ShipVia' => array(
					'parent-table' => 'shippers',
					'parent-primary-key' => 'ShipperID',
					'child-primary-key' => 'OrderID',
					'child-primary-key-index' => 0,
					'tab-label' => 'Orders via shipper <span class="hidden child-label-orders child-field-caption">(Ship Via)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/cash_register.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(0 => 'Order ID', 1 => 'Status', 2 => 'Customer', 3 => 'Employee', 4 => 'Order Date', 7 => 'Ship Via', 14 => 'Ship Country', 15 => 'Total'),
					'display-field-names' => array(0 => 'OrderID', 1 => 'status', 2 => 'CustomerID', 3 => 'EmployeeID', 4 => 'OrderDate', 7 => 'ShipVia', 14 => 'ShipCountry', 15 => 'total'),
					'sortable-fields' => array(0 => '`orders`.`OrderID`', 1 => 2, 2 => '`customers1`.`CompanyName`', 3 => 4, 4 => '`orders`.`OrderDate`', 5 => '`orders`.`RequiredDate`', 6 => '`orders`.`ShippedDate`', 7 => '`shippers1`.`CompanyName`', 8 => '`orders`.`Freight`', 9 => '`customers1`.`CompanyName`', 10 => '`customers1`.`Address`', 11 => '`customers1`.`City`', 12 => '`customers1`.`Region`', 13 => '`customers1`.`PostalCode`', 14 => '`customers1`.`Country`', 15 => '`orders`.`total`'),
					'records-per-page' => 10,
					'default-sort-by' => 0,
					'default-sort-direction' => 'desc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-orders',
					'template-printable' => 'children-orders-printable',
					'query' => "SELECT `orders`.`OrderID` as 'OrderID', `orders`.`status` as 'status', IF(    CHAR_LENGTH(`customers1`.`CompanyName`), CONCAT_WS('',   `customers1`.`CompanyName`), '') as 'CustomerID', IF(    CHAR_LENGTH(`employees1`.`LastName`) || CHAR_LENGTH(`employees1`.`FirstName`), CONCAT_WS('',   `employees1`.`LastName`, ', ', `employees1`.`FirstName`), '') as 'EmployeeID', if(`orders`.`OrderDate`,date_format(`orders`.`OrderDate`,'%m/%d/%Y'),'') as 'OrderDate', if(`orders`.`RequiredDate`,date_format(`orders`.`RequiredDate`,'%m/%d/%Y'),'') as 'RequiredDate', if(`orders`.`ShippedDate`,date_format(`orders`.`ShippedDate`,'%m/%d/%Y'),'') as 'ShippedDate', IF(    CHAR_LENGTH(`shippers1`.`CompanyName`), CONCAT_WS('',   `shippers1`.`CompanyName`), '') as 'ShipVia', `orders`.`Freight` as 'Freight', IF(    CHAR_LENGTH(`customers1`.`CompanyName`), CONCAT_WS('',   `customers1`.`CompanyName`), '') as 'ShipName', IF(    CHAR_LENGTH(`customers1`.`Address`), CONCAT_WS('',   `customers1`.`Address`), '') as 'ShipAddress', IF(    CHAR_LENGTH(`customers1`.`City`), CONCAT_WS('',   `customers1`.`City`), '') as 'ShipCity', IF(    CHAR_LENGTH(`customers1`.`Region`), CONCAT_WS('',   `customers1`.`Region`), '') as 'ShipRegion', IF(    CHAR_LENGTH(`customers1`.`PostalCode`), CONCAT_WS('',   `customers1`.`PostalCode`), '') as 'ShipPostalCode', IF(    CHAR_LENGTH(`customers1`.`Country`), CONCAT_WS('',   `customers1`.`Country`), '') as 'ShipCountry', `orders`.`total` as 'total' FROM `orders` LEFT JOIN `customers` as customers1 ON `customers1`.`CustomerID`=`orders`.`CustomerID` LEFT JOIN `employees` as employees1 ON `employees1`.`EmployeeID`=`orders`.`EmployeeID` LEFT JOIN `shippers` as shippers1 ON `shippers1`.`ShipperID`=`orders`.`ShipVia` "
				),
			),
			'order_details' => array(
				'OrderID' => array(
					'parent-table' => 'orders',
					'parent-primary-key' => 'OrderID',
					'child-primary-key' => 'odID',
					'child-primary-key-index' => 0,
					'tab-label' => 'Order Items <span class="hidden child-label-order_details child-field-caption">(Order ID)</span>',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/application_form_magnify.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Order ID', 2 => 'Product', 3 => 'Category', 6 => 'Unit Price', 7 => 'Quantity', 8 => 'Discount', 9 => 'Subtotal'),
					'display-field-names' => array(1 => 'OrderID', 2 => 'ProductID', 3 => 'Category', 6 => 'UnitPrice', 7 => 'Quantity', 8 => 'Discount', 9 => 'Subtotal'),
					'sortable-fields' => array(0 => '`order_details`.`odID`', 1 => '`orders1`.`OrderID`', 2 => '`products1`.`ProductName`', 3 => 4, 4 => '`products1`.`UnitPrice`', 5 => '`products1`.`UnitsInStock`', 6 => '`order_details`.`UnitPrice`', 7 => '`order_details`.`Quantity`', 8 => '`order_details`.`Discount`', 9 => '`order_details`.`Subtotal`'),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-order_details',
					'template-printable' => 'children-order_details-printable',
					'query' => "SELECT `order_details`.`odID` as 'odID', IF(    CHAR_LENGTH(`orders1`.`OrderID`), CONCAT_WS('',   `orders1`.`OrderID`), '') as 'OrderID', IF(    CHAR_LENGTH(`products1`.`ProductName`), CONCAT_WS('',   `products1`.`ProductName`), '') as 'ProductID', IF(    CHAR_LENGTH(`categories1`.`CategoryName`) || CHAR_LENGTH(`suppliers1`.`CompanyName`), CONCAT_WS('',   `categories1`.`CategoryName`, ' / ', `suppliers1`.`CompanyName`), '') as 'Category', IF(    CHAR_LENGTH(`products1`.`UnitPrice`), CONCAT_WS('',   `products1`.`UnitPrice`), '') as 'CatalogPrice', IF(    CHAR_LENGTH(`products1`.`UnitsInStock`), CONCAT_WS('',   `products1`.`UnitsInStock`), '') as 'UnitsInStock', CONCAT('$', FORMAT(`order_details`.`UnitPrice`, 2)) as 'UnitPrice', `order_details`.`Quantity` as 'Quantity', CONCAT('$', FORMAT(`order_details`.`Discount`, 2)) as 'Discount', `order_details`.`Subtotal` as 'Subtotal' FROM `order_details` LEFT JOIN `orders` as orders1 ON `orders1`.`OrderID`=`order_details`.`OrderID` LEFT JOIN `products` as products1 ON `products1`.`ProductID`=`order_details`.`ProductID` LEFT JOIN `categories` as categories1 ON `categories1`.`CategoryID`=`products1`.`CategoryID` LEFT JOIN `suppliers` as suppliers1 ON `suppliers1`.`SupplierID`=`products1`.`SupplierID` "
				),
				'ProductID' => array(
					'parent-table' => 'products',
					'parent-primary-key' => 'ProductID',
					'child-primary-key' => 'odID',
					'child-primary-key-index' => 0,
					'tab-label' => 'Orders for this product <span class="hidden child-label-order_details child-field-caption">(Product)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/application_form_magnify.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Order ID', 2 => 'Product', 3 => 'Category', 6 => 'Unit Price', 7 => 'Quantity', 8 => 'Discount', 9 => 'Subtotal'),
					'display-field-names' => array(1 => 'OrderID', 2 => 'ProductID', 3 => 'Category', 6 => 'UnitPrice', 7 => 'Quantity', 8 => 'Discount', 9 => 'Subtotal'),
					'sortable-fields' => array(0 => '`order_details`.`odID`', 1 => '`orders1`.`OrderID`', 2 => '`products1`.`ProductName`', 3 => 4, 4 => '`products1`.`UnitPrice`', 5 => '`products1`.`UnitsInStock`', 6 => '`order_details`.`UnitPrice`', 7 => '`order_details`.`Quantity`', 8 => '`order_details`.`Discount`', 9 => '`order_details`.`Subtotal`'),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-order_details',
					'template-printable' => 'children-order_details-printable',
					'query' => "SELECT `order_details`.`odID` as 'odID', IF(    CHAR_LENGTH(`orders1`.`OrderID`), CONCAT_WS('',   `orders1`.`OrderID`), '') as 'OrderID', IF(    CHAR_LENGTH(`products1`.`ProductName`), CONCAT_WS('',   `products1`.`ProductName`), '') as 'ProductID', IF(    CHAR_LENGTH(`categories1`.`CategoryName`) || CHAR_LENGTH(`suppliers1`.`CompanyName`), CONCAT_WS('',   `categories1`.`CategoryName`, ' / ', `suppliers1`.`CompanyName`), '') as 'Category', IF(    CHAR_LENGTH(`products1`.`UnitPrice`), CONCAT_WS('',   `products1`.`UnitPrice`), '') as 'CatalogPrice', IF(    CHAR_LENGTH(`products1`.`UnitsInStock`), CONCAT_WS('',   `products1`.`UnitsInStock`), '') as 'UnitsInStock', CONCAT('$', FORMAT(`order_details`.`UnitPrice`, 2)) as 'UnitPrice', `order_details`.`Quantity` as 'Quantity', CONCAT('$', FORMAT(`order_details`.`Discount`, 2)) as 'Discount', `order_details`.`Subtotal` as 'Subtotal' FROM `order_details` LEFT JOIN `orders` as orders1 ON `orders1`.`OrderID`=`order_details`.`OrderID` LEFT JOIN `products` as products1 ON `products1`.`ProductID`=`order_details`.`ProductID` LEFT JOIN `categories` as categories1 ON `categories1`.`CategoryID`=`products1`.`CategoryID` LEFT JOIN `suppliers` as suppliers1 ON `suppliers1`.`SupplierID`=`products1`.`SupplierID` "
				),
			),
			'products' => array(
				'SupplierID' => array(
					'parent-table' => 'suppliers',
					'parent-primary-key' => 'SupplierID',
					'child-primary-key' => 'ProductID',
					'child-primary-key-index' => 0,
					'tab-label' => 'Products supplied <span class="hidden child-label-products child-field-caption">(Supplier)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/handbag.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Product Name', 2 => 'Supplier', 3 => 'Category', 4 => 'Quantity Per Unit', 5 => 'Unit Price', 9 => 'Discontinued'),
					'display-field-names' => array(1 => 'ProductName', 2 => 'SupplierID', 3 => 'CategoryID', 4 => 'QuantityPerUnit', 5 => 'UnitPrice', 9 => 'Discontinued'),
					'sortable-fields' => array(0 => '`products`.`ProductID`', 1 => 2, 2 => '`suppliers1`.`CompanyName`', 3 => '`categories1`.`CategoryName`', 4 => 5, 5 => '`products`.`UnitPrice`', 6 => '`products`.`UnitsInStock`', 7 => '`products`.`UnitsOnOrder`', 8 => '`products`.`ReorderLevel`', 9 => '`products`.`Discontinued`'),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-products',
					'template-printable' => 'children-products-printable',
					'query' => "SELECT `products`.`ProductID` as 'ProductID', `products`.`ProductName` as 'ProductName', IF(    CHAR_LENGTH(`suppliers1`.`CompanyName`), CONCAT_WS('',   `suppliers1`.`CompanyName`), '') as 'SupplierID', IF(    CHAR_LENGTH(`categories1`.`CategoryName`), CONCAT_WS('',   `categories1`.`CategoryName`), '') as 'CategoryID', `products`.`QuantityPerUnit` as 'QuantityPerUnit', CONCAT('$', FORMAT(`products`.`UnitPrice`, 2)) as 'UnitPrice', `products`.`UnitsInStock` as 'UnitsInStock', `products`.`UnitsOnOrder` as 'UnitsOnOrder', `products`.`ReorderLevel` as 'ReorderLevel', concat('<i class=\"glyphicon glyphicon-', if(`products`.`Discontinued`, 'check', 'unchecked'), '\"></i>') as 'Discontinued' FROM `products` LEFT JOIN `suppliers` as suppliers1 ON `suppliers1`.`SupplierID`=`products`.`SupplierID` LEFT JOIN `categories` as categories1 ON `categories1`.`CategoryID`=`products`.`CategoryID` "
				),
				'CategoryID' => array(
					'parent-table' => 'categories',
					'parent-primary-key' => 'CategoryID',
					'child-primary-key' => 'ProductID',
					'child-primary-key-index' => 0,
					'tab-label' => 'Products under this category <span class="hidden child-label-products child-field-caption">(Category)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/handbag.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Product Name', 2 => 'Supplier', 3 => 'Category', 4 => 'Quantity Per Unit', 5 => 'Unit Price', 9 => 'Discontinued'),
					'display-field-names' => array(1 => 'ProductName', 2 => 'SupplierID', 3 => 'CategoryID', 4 => 'QuantityPerUnit', 5 => 'UnitPrice', 9 => 'Discontinued'),
					'sortable-fields' => array(0 => '`products`.`ProductID`', 1 => 2, 2 => '`suppliers1`.`CompanyName`', 3 => '`categories1`.`CategoryName`', 4 => 5, 5 => '`products`.`UnitPrice`', 6 => '`products`.`UnitsInStock`', 7 => '`products`.`UnitsOnOrder`', 8 => '`products`.`ReorderLevel`', 9 => '`products`.`Discontinued`'),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-products',
					'template-printable' => 'children-products-printable',
					'query' => "SELECT `products`.`ProductID` as 'ProductID', `products`.`ProductName` as 'ProductName', IF(    CHAR_LENGTH(`suppliers1`.`CompanyName`), CONCAT_WS('',   `suppliers1`.`CompanyName`), '') as 'SupplierID', IF(    CHAR_LENGTH(`categories1`.`CategoryName`), CONCAT_WS('',   `categories1`.`CategoryName`), '') as 'CategoryID', `products`.`QuantityPerUnit` as 'QuantityPerUnit', CONCAT('$', FORMAT(`products`.`UnitPrice`, 2)) as 'UnitPrice', `products`.`UnitsInStock` as 'UnitsInStock', `products`.`UnitsOnOrder` as 'UnitsOnOrder', `products`.`ReorderLevel` as 'ReorderLevel', concat('<i class=\"glyphicon glyphicon-', if(`products`.`Discontinued`, 'check', 'unchecked'), '\"></i>') as 'Discontinued' FROM `products` LEFT JOIN `suppliers` as suppliers1 ON `suppliers1`.`SupplierID`=`products`.`SupplierID` LEFT JOIN `categories` as categories1 ON `categories1`.`CategoryID`=`products`.`CategoryID` "
				),
			),
			'categories' => array(
			),
			'suppliers' => array(
			),
			'shippers' => array(
			),
		);

	/*************************************/
	/* End of configuration */


	$currDir = dirname(__FILE__);
	include("{$currDir}/defaultLang.php");
	include("{$currDir}/language.php");
	include("{$currDir}/lib.php");
	@header('Content-Type: text/html; charset=' . datalist_db_encoding);

	handle_maintenance();

	/**
	* dynamic configuration based on current user's permissions
	* $userPCConfig array is populated only with parent tables where the user has access to
	* at least one child table
	*/
	$userPCConfig = array();
	foreach($pcConfig as $pcChildTable => $ChildrenLookups) {
		$permChild = getTablePermissions($pcChildTable);
		if($permChild[2]) { // user can view records of the child table, so proceed to check children lookups
			foreach($ChildrenLookups as $ChildLookupField => $ChildConfig) {
				$permParent = getTablePermissions($ChildConfig['parent-table']);
				if($permParent[2]) { // user can view records of parent table
					$userPCConfig[$pcChildTable][$ChildLookupField] = $pcConfig[$pcChildTable][$ChildLookupField];
					// show add new only if configured above AND the user has insert permission
					if($permChild[1] && $pcConfig[$pcChildTable][$ChildLookupField]['display-add-new']) {
						$userPCConfig[$pcChildTable][$ChildLookupField]['display-add-new'] = true;
					}else{
						$userPCConfig[$pcChildTable][$ChildLookupField]['display-add-new'] = false;
					}
				}
			}
		}
	}

	/* Receive, UTF-convert, and validate parameters */
	$ParentTable = $_REQUEST['ParentTable']; // needed only with operation=show-children, will be validated in the processing code
	$ChildTable = $_REQUEST['ChildTable'];
		if(!in_array($ChildTable, array_keys($userPCConfig))) {
			/* defaults to first child table in config array if not provided */
			$ChildTable = current(array_keys($userPCConfig));
		}
		if(!$ChildTable) { die('<!-- No tables accessible to current user -->'); }
	$SelectedID = strip_tags($_REQUEST['SelectedID']);
	$ChildLookupField = $_REQUEST['ChildLookupField'];
		if(!in_array($ChildLookupField, array_keys($userPCConfig[$ChildTable]))) {
			/* defaults to first lookup in current child config array if not provided */
			$ChildLookupField = current(array_keys($userPCConfig[$ChildTable]));
		}
	$Page = intval($_REQUEST['Page']);
		if($Page < 1) {
			$Page = 1;
		}
	$SortBy = ($_REQUEST['SortBy'] != '' ? abs(intval($_REQUEST['SortBy'])) : false);
		if(!in_array($SortBy, array_keys($userPCConfig[$ChildTable][$ChildLookupField]['sortable-fields']), true)) {
			$SortBy = $userPCConfig[$ChildTable][$ChildLookupField]['default-sort-by'];
		}
	$SortDirection = strtolower($_REQUEST['SortDirection']);
		if(!in_array($SortDirection, array('asc', 'desc'))) {
			$SortDirection = $userPCConfig[$ChildTable][$ChildLookupField]['default-sort-direction'];
		}
	$Operation = strtolower($_REQUEST['Operation']);
		if(!in_array($Operation, array('get-records', 'show-children', 'get-records-printable', 'show-children-printable'))) {
			$Operation = 'get-records';
		}

	/* process requested operation */
	switch($Operation) {
		/************************************************/
		case 'show-children':
			/* populate HTML and JS content with children tabs */
			$tabLabels = $tabPanels = $tabLoaders = '';
			foreach($userPCConfig as $ChildTable => $childLookups) {
				foreach($childLookups as $ChildLookupField => $childConfig) {
					if($childConfig['parent-table'] == $ParentTable) {
						$TableIcon = ($childConfig['table-icon'] ? "<img src=\"{$childConfig['table-icon']}\" border=\"0\" />" : '');
						$tabLabels .= sprintf('<li%s><a href="#panel_%s-%s" id="tab_%s-%s" data-toggle="tab">%s%s</a></li>' . "\n\t\t\t\t\t",($tabLabels ? '' : ' class="active"'), $ChildTable, $ChildLookupField, $ChildTable, $ChildLookupField, $TableIcon, $childConfig['tab-label']);
						$tabPanels .= sprintf('<div id="panel_%s-%s" class="tab-pane%s"><img src="loading.gif" align="top" />%s</div>' . "\n\t\t\t\t", $ChildTable, $ChildLookupField, ($tabPanels ? '' : ' active'), $Translation['Loading ...']);
						$tabLoaders .= sprintf('post("parent-children.php", { ChildTable: "%s", ChildLookupField: "%s", SelectedID: "%s", Page: 1, SortBy: "", SortDirection: "", Operation: "get-records" }, "panel_%s-%s");' . "\n\t\t\t\t", $ChildTable, $ChildLookupField, addslashes($SelectedID), $ChildTable, $ChildLookupField);
					}
				}
			}

			if(!$tabLabels) { die('<!-- no children of current parent table are accessible to current user -->'); }
			?>
			<div id="children-tabs">
				<ul class="nav nav-tabs">
					<?php echo $tabLabels; ?>
				</ul>
				<span id="pc-loading"></span>
			</div>
			<div class="tab-content"><?php echo $tabPanels; ?></div>

			<script>
				$j(function() {
					/* for iOS, avoid loading child tabs in modals */
					var iOS = /(iPad|iPhone|iPod)/g.test(navigator.userAgent);
					var embedded = ($j('.navbar').length == 0);
					if(iOS && embedded) {
						$j('#children-tabs').next('.tab-content').remove();
						$j('#children-tabs').remove();
						return;
					}

					/* ajax loading of each tab's contents */
					<?php echo $tabLoaders; ?>

					/* show child field caption on tab title in case the same child table appears more than once */
					$j('.child-field-caption').each(function() {
						var clss = $j(this).attr('class').split(/\s+/).reduce(function(rc, cc) {
							return (cc.match(/child-label-.*/) ? '.' + cc : rc);
						}, '');

						// if class occurs more than once, remove .hidden
						if($j(clss).length > 1) $j(clss).removeClass('hidden');
					})
				})
			</script>
			<?php
			break;

		/************************************************/
		case 'show-children-printable':
			/* populate HTML and JS content with children buttons */
			$tabLabels = $tabPanels = $tabLoaders = '';
			foreach($userPCConfig as $ChildTable => $childLookups) {
				foreach($childLookups as $ChildLookupField => $childConfig) {
					if($childConfig['parent-table'] == $ParentTable) {
						$TableIcon = ($childConfig['table-icon'] ? "<img src=\"{$childConfig['table-icon']}\" border=\"0\" />" : '');
						$tabLabels .= sprintf('<button type="button" class="btn btn-default" data-target="#panel_%s-%s" id="tab_%s-%s" data-toggle="collapse">%s %s</button>' . "\n\t\t\t\t\t", $ChildTable, $ChildLookupField, $ChildTable, $ChildLookupField, $TableIcon, $childConfig['tab-label']);
						$tabPanels .= sprintf('<div id="panel_%s-%s" class="collapse"><img src="loading.gif" align="top" />%s</div>' . "\n\t\t\t\t", $ChildTable, $ChildLookupField, $Translation['Loading ...']);
						$tabLoaders .= sprintf('post("parent-children.php", { ChildTable: "%s", ChildLookupField: "%s", SelectedID: "%s", Page: 1, SortBy: "", SortDirection: "", Operation: "get-records-printable" }, "panel_%s-%s");' . "\n\t\t\t\t", $ChildTable, $ChildLookupField, addslashes($SelectedID), $ChildTable, $ChildLookupField);
					}
				}
			}

			if(!$tabLabels) { die('<!-- no children of current parent table are accessible to current user -->'); }
			?>
			<div id="children-tabs" class="hidden-print">
				<div class="btn-group btn-group-lg">
					<?php echo $tabLabels; ?>
				</div>
				<span id="pc-loading"></span>
			</div>
			<div class="vspacer-lg"><?php echo $tabPanels; ?></div>

			<script>
				$j(function() {
					/* for iOS, avoid loading child tabs in modals */
					var iOS = /(iPad|iPhone|iPod)/g.test(navigator.userAgent);
					var embedded = ($j('.navbar').length == 0);
					if(iOS && embedded) {
						$j('#children-tabs').next('.tab-content').remove();
						$j('#children-tabs').remove();
						return;
					}

					/* ajax loading of each tab's contents */
					<?php echo $tabLoaders; ?>
				})
			</script>
			<?php
			break;

		/************************************************/
		case 'get-records-printable':
		default: /* default is 'get-records' */

			if($Operation == 'get-records-printable') {
				$userPCConfig[$ChildTable][$ChildLookupField]['records-per-page'] = 2000;
			}

			// build the user permissions limiter
			$permissionsWhere = $permissionsJoin = '';
			$permChild = getTablePermissions($ChildTable);
			if($permChild[2] == 1) { // user can view only his own records
				$permissionsWhere = "`$ChildTable`.`{$userPCConfig[$ChildTable][$ChildLookupField]['child-primary-key']}`=`membership_userrecords`.`pkValue` AND `membership_userrecords`.`tableName`='$ChildTable' AND LCASE(`membership_userrecords`.`memberID`)='".getLoggedMemberID()."'";
			}elseif($permChild[2] == 2) { // user can view only his group's records
				$permissionsWhere = "`$ChildTable`.`{$userPCConfig[$ChildTable][$ChildLookupField]['child-primary-key']}`=`membership_userrecords`.`pkValue` AND `membership_userrecords`.`tableName`='$ChildTable' AND `membership_userrecords`.`groupID`='".getLoggedGroupID()."'";
			}elseif($permChild[2] == 3) { // user can view all records
				/* that's the only case remaining ... no need to modify the query in this case */
			}
			$permissionsJoin = ($permissionsWhere ? ", `membership_userrecords`" : '');

			// build the count query
			$forcedWhere = $userPCConfig[$ChildTable][$ChildLookupField]['forced-where'];
			$query = 
				preg_replace('/^select .* from /i', 'SELECT count(1) FROM ', $userPCConfig[$ChildTable][$ChildLookupField]['query']) .
				$permissionsJoin . " WHERE " .
				($permissionsWhere ? "( $permissionsWhere )" : "( 1=1 )") . " AND " .
				($forcedWhere ? "( $forcedWhere )" : "( 2=2 )") . " AND " .
				"`$ChildTable`.`$ChildLookupField`='" . makeSafe($SelectedID) . "'";
			$totalMatches = sqlValue($query);

			// make sure $Page is <= max pages
			$maxPage = ceil($totalMatches / $userPCConfig[$ChildTable][$ChildLookupField]['records-per-page']);
			if($Page > $maxPage) { $Page = $maxPage; }

			// initiate output data array
			$data = array(
				'config' => $userPCConfig[$ChildTable][$ChildLookupField],
				'parameters' => array(
					'ChildTable' => $ChildTable,
					'ChildLookupField' => $ChildLookupField,
					'SelectedID' => $SelectedID,
					'Page' => $Page,
					'SortBy' => $SortBy,
					'SortDirection' => $SortDirection,
					'Operation' => $Operation
				),
				'records' => array(),
				'totalMatches' => $totalMatches
			);

			// build the data query
			if($totalMatches) { // if we have at least one record, proceed with fetching data
				$startRecord = $userPCConfig[$ChildTable][$ChildLookupField]['records-per-page'] * ($Page - 1);
				$data['query'] = 
					$userPCConfig[$ChildTable][$ChildLookupField]['query'] .
					$permissionsJoin . " WHERE " .
					($permissionsWhere ? "( $permissionsWhere )" : "( 1=1 )") . " AND " .
					($forcedWhere ? "( $forcedWhere )" : "( 2=2 )") . " AND " .
					"`$ChildTable`.`$ChildLookupField`='" . makeSafe($SelectedID) . "'" . 
					($SortBy !== false && $userPCConfig[$ChildTable][$ChildLookupField]['sortable-fields'][$SortBy] ? " ORDER BY {$userPCConfig[$ChildTable][$ChildLookupField]['sortable-fields'][$SortBy]} $SortDirection" : '') .
					" LIMIT $startRecord, {$userPCConfig[$ChildTable][$ChildLookupField]['records-per-page']}";
				$res = sql($data['query'], $eo);
				while($row = db_fetch_row($res)) {
					$data['records'][$row[$userPCConfig[$ChildTable][$ChildLookupField]['child-primary-key-index']]] = $row;
				}
			}else{ // if no matching records
				$startRecord = 0;
			}

			if($Operation == 'get-records-printable') {
				$response = loadView($userPCConfig[$ChildTable][$ChildLookupField]['template-printable'], $data);
			}else{
				$response = loadView($userPCConfig[$ChildTable][$ChildLookupField]['template'], $data);
			}

			// change name space to ensure uniqueness
			$uniqueNameSpace = $ChildTable.ucfirst($ChildLookupField).'GetRecords';
			echo str_replace("{$ChildTable}GetChildrenRecordsList", $uniqueNameSpace, $response);
		/************************************************/
	}
