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

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

		$pcConfig = array(
			'books' => array(   
				'Book_Type' => array(   
					'parent-table' => 'Types',
					'parent-primary-key' => 'id',
					'child-primary-key' => 'id',
					'child-primary-key-index' => 0,
					'tab-label' => 'Books',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/books.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'ISBN NO', 2 => 'Book Title', 3 => 'Book Type', 4 => 'Author Name', 5 => 'Quantity', 6 => 'Purchase Date', 7 => 'Edition', 8 => 'Price', 9 => 'Pages', 10 => 'Publisher'),
					'display-field-names' => array(1 => 'ISBN_NO', 2 => 'Book_Title', 3 => 'Book_Type', 4 => 'Author_Name', 5 => 'Quantity', 6 => 'Purchase_Date', 7 => 'Edition', 8 => 'Price', 9 => 'Pages', 10 => 'Publisher'),
					'sortable-fields' => array(0 => '`books`.`id`', 1 => '`books`.`ISBN_NO`', 2 => '`books`.`Book_Title`', 3 => '`books`.`Book_Type`', 4 => '`books`.`Author_Name`', 5 => '`books`.`Quantity`', 6 => '`books`.`Purchase_Date`', 7 => '`books`.`Edition`', 8 => '`books`.`Price`', 9 => '`books`.`Pages`', 10 => '`books`.`Publisher`'),
					'records-per-page' => 10,
					'default-sort-by' => false,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-books',
					'template-printable' => 'children-books-printable',
					'query' => "SELECT `books`.`id` as 'id', `books`.`ISBN_NO` as 'ISBN_NO', `books`.`Book_Title` as 'Book_Title', IF(    CHAR_LENGTH(`Types1`.`Name`), CONCAT_WS('',   `Types1`.`Name`), '') as 'Book_Type', `books`.`Author_Name` as 'Author_Name', `books`.`Quantity` as 'Quantity', if(`books`.`Purchase_Date`,date_format(`books`.`Purchase_Date`,'%m/%d/%Y'),'') as 'Purchase_Date', `books`.`Edition` as 'Edition', `books`.`Price` as 'Price', `books`.`Pages` as 'Pages', `books`.`Publisher` as 'Publisher' FROM `books` LEFT JOIN `Types` as Types1 ON `Types1`.`id`=`books`.`Book_Type` "
				)
			),
			'NewsPapers' => array(   
			),
			'Magazines' => array(   
			),
			'Users' => array(   
			),
			'Book_Issue' => array(   
				'Member' => array(   
					'parent-table' => 'Users',
					'parent-primary-key' => 'id',
					'child-primary-key' => 'id',
					'child-primary-key-index' => 0,
					'tab-label' => 'Issue History',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/application_from_storage.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Issue id', 2 => 'Member', 3 => 'Number', 4 => 'Book Number', 5 => 'Book Title', 6 => 'Issue Date', 7 => 'Return Date', 8 => 'Status'),
					'display-field-names' => array(1 => 'issue_id', 2 => 'Member', 3 => 'Number', 4 => 'Book_Number', 5 => 'Book_Title', 6 => 'Issue_Date', 7 => 'Return_Date', 8 => 'Status'),
					'sortable-fields' => array(0 => '`Book_Issue`.`id`', 1 => '`Book_Issue`.`issue_id`', 2 => '`Book_Issue`.`Member`', 3 => '`Book_Issue`.`Number`', 4 => '`Book_Issue`.`Book_Number`', 5 => '`Book_Issue`.`Book_Title`', 6 => '`Book_Issue`.`Issue_Date`', 7 => '`Book_Issue`.`Return_Date`', 8 => '`Book_Issue`.`Status`'),
					'records-per-page' => 10,
					'default-sort-by' => false,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-Book_Issue',
					'template-printable' => 'children-Book_Issue-printable',
					'query' => "SELECT `Book_Issue`.`id` as 'id', `Book_Issue`.`issue_id` as 'issue_id', IF(    CHAR_LENGTH(`Users1`.`Name`), CONCAT_WS('',   `Users1`.`Name`), '') as 'Member', IF(    CHAR_LENGTH(`Users1`.`Membership_Number`), CONCAT_WS('',   `Users1`.`Membership_Number`), '') as 'Number', IF(    CHAR_LENGTH(`books1`.`ISBN_NO`), CONCAT_WS('',   `books1`.`ISBN_NO`), '') as 'Book_Number', IF(    CHAR_LENGTH(`books1`.`Book_Title`), CONCAT_WS('',   `books1`.`Book_Title`), '') as 'Book_Title', if(`Book_Issue`.`Issue_Date`,date_format(`Book_Issue`.`Issue_Date`,'%m/%d/%Y'),'') as 'Issue_Date', if(`Book_Issue`.`Return_Date`,date_format(`Book_Issue`.`Return_Date`,'%m/%d/%Y'),'') as 'Return_Date', `Book_Issue`.`Status` as 'Status' FROM `Book_Issue` LEFT JOIN `Users` as Users1 ON `Users1`.`id`=`Book_Issue`.`Member` LEFT JOIN `books` as books1 ON `books1`.`id`=`Book_Issue`.`Book_Number` "
				),
				'Book_Number' => array(   
					'parent-table' => 'books',
					'parent-primary-key' => 'id',
					'child-primary-key' => 'id',
					'child-primary-key-index' => 0,
					'tab-label' => 'Issue Record',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/application_from_storage.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Issue id', 2 => 'Member', 3 => 'Number', 4 => 'Book Number', 5 => 'Book Title', 6 => 'Issue Date', 7 => 'Return Date', 8 => 'Status'),
					'display-field-names' => array(1 => 'issue_id', 2 => 'Member', 3 => 'Number', 4 => 'Book_Number', 5 => 'Book_Title', 6 => 'Issue_Date', 7 => 'Return_Date', 8 => 'Status'),
					'sortable-fields' => array(0 => '`Book_Issue`.`id`', 1 => '`Book_Issue`.`issue_id`', 2 => '`Book_Issue`.`Member`', 3 => '`Book_Issue`.`Number`', 4 => '`Book_Issue`.`Book_Number`', 5 => '`Book_Issue`.`Book_Title`', 6 => '`Book_Issue`.`Issue_Date`', 7 => '`Book_Issue`.`Return_Date`', 8 => '`Book_Issue`.`Status`'),
					'records-per-page' => 10,
					'default-sort-by' => false,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-Book_Issue',
					'template-printable' => 'children-Book_Issue-printable',
					'query' => "SELECT `Book_Issue`.`id` as 'id', `Book_Issue`.`issue_id` as 'issue_id', IF(    CHAR_LENGTH(`Users1`.`Name`), CONCAT_WS('',   `Users1`.`Name`), '') as 'Member', IF(    CHAR_LENGTH(`Users1`.`Membership_Number`), CONCAT_WS('',   `Users1`.`Membership_Number`), '') as 'Number', IF(    CHAR_LENGTH(`books1`.`ISBN_NO`), CONCAT_WS('',   `books1`.`ISBN_NO`), '') as 'Book_Number', IF(    CHAR_LENGTH(`books1`.`Book_Title`), CONCAT_WS('',   `books1`.`Book_Title`), '') as 'Book_Title', if(`Book_Issue`.`Issue_Date`,date_format(`Book_Issue`.`Issue_Date`,'%m/%d/%Y'),'') as 'Issue_Date', if(`Book_Issue`.`Return_Date`,date_format(`Book_Issue`.`Return_Date`,'%m/%d/%Y'),'') as 'Return_Date', `Book_Issue`.`Status` as 'Status' FROM `Book_Issue` LEFT JOIN `Users` as Users1 ON `Users1`.`id`=`Book_Issue`.`Member` LEFT JOIN `books` as books1 ON `books1`.`id`=`Book_Issue`.`Book_Number` "
				)
			),
			'Return_Book' => array(   
				'Book_Number' => array(   
					'parent-table' => 'Book_Issue',
					'parent-primary-key' => 'id',
					'child-primary-key' => 'id',
					'child-primary-key-index' => 0,
					'tab-label' => 'Return Records',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/application_side_expand.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Book Number', 2 => 'Book Title', 3 => 'Issue Date', 4 => 'Due Date', 5 => 'Return Date', 6 => 'Member', 7 => 'Number', 8 => 'Fine', 9 => 'Status'),
					'display-field-names' => array(1 => 'Book_Number', 2 => 'Book_Title', 3 => 'Issue_Date', 4 => 'Due_Date', 5 => 'Return_Date', 6 => 'Member', 7 => 'Number', 8 => 'Fine', 9 => 'Status'),
					'sortable-fields' => array(0 => '`Return_Book`.`id`', 1 => '`Return_Book`.`Book_Number`', 2 => '`Return_Book`.`Book_Title`', 3 => '`Return_Book`.`Issue_Date`', 4 => '`Return_Book`.`Due_Date`', 5 => '`Return_Book`.`Return_Date`', 6 => '`Return_Book`.`Member`', 7 => '`Return_Book`.`Number`', 8 => '`Return_Book`.`Fine`', 9 => '`Return_Book`.`Status`'),
					'records-per-page' => 10,
					'default-sort-by' => false,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-Return_Book',
					'template-printable' => 'children-Return_Book-printable',
					'query' => "SELECT `Return_Book`.`id` as 'id', IF(    CHAR_LENGTH(`books1`.`ISBN_NO`), CONCAT_WS('',   `books1`.`ISBN_NO`), '') as 'Book_Number', IF(    CHAR_LENGTH(`books1`.`Book_Title`), CONCAT_WS('',   `books1`.`Book_Title`), '') as 'Book_Title', IF(    CHAR_LENGTH(if(`Book_Issue1`.`Issue_Date`,date_format(`Book_Issue1`.`Issue_Date`,'%m/%d/%Y'),'')), CONCAT_WS('',   if(`Book_Issue1`.`Issue_Date`,date_format(`Book_Issue1`.`Issue_Date`,'%m/%d/%Y'),'')), '') as 'Issue_Date', IF(    CHAR_LENGTH(if(`Book_Issue1`.`Return_Date`,date_format(`Book_Issue1`.`Return_Date`,'%m/%d/%Y'),'')), CONCAT_WS('',   if(`Book_Issue1`.`Return_Date`,date_format(`Book_Issue1`.`Return_Date`,'%m/%d/%Y'),'')), '') as 'Due_Date', if(`Return_Book`.`Return_Date`,date_format(`Return_Book`.`Return_Date`,'%m/%d/%Y'),'') as 'Return_Date', IF(    CHAR_LENGTH(`Users1`.`Name`), CONCAT_WS('',   `Users1`.`Name`), '') as 'Member', IF(    CHAR_LENGTH(`Users1`.`Membership_Number`), CONCAT_WS('',   `Users1`.`Membership_Number`), '') as 'Number', `Return_Book`.`Fine` as 'Fine', `Return_Book`.`Status` as 'Status' FROM `Return_Book` LEFT JOIN `Book_Issue` as Book_Issue1 ON `Book_Issue1`.`id`=`Return_Book`.`Book_Number` LEFT JOIN `books` as books1 ON `books1`.`id`=`Book_Issue1`.`Book_Number` LEFT JOIN `Users` as Users1 ON `Users1`.`id`=`Return_Book`.`Member` "
				),
				'Member' => array(   
					'parent-table' => 'Users',
					'parent-primary-key' => 'id',
					'child-primary-key' => 'id',
					'child-primary-key-index' => 0,
					'tab-label' => 'Return History',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/application_side_expand.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Book Number', 2 => 'Book Title', 3 => 'Issue Date', 4 => 'Due Date', 5 => 'Return Date', 6 => 'Member', 7 => 'Number', 8 => 'Fine', 9 => 'Status'),
					'display-field-names' => array(1 => 'Book_Number', 2 => 'Book_Title', 3 => 'Issue_Date', 4 => 'Due_Date', 5 => 'Return_Date', 6 => 'Member', 7 => 'Number', 8 => 'Fine', 9 => 'Status'),
					'sortable-fields' => array(0 => '`Return_Book`.`id`', 1 => '`Return_Book`.`Book_Number`', 2 => '`Return_Book`.`Book_Title`', 3 => '`Return_Book`.`Issue_Date`', 4 => '`Return_Book`.`Due_Date`', 5 => '`Return_Book`.`Return_Date`', 6 => '`Return_Book`.`Member`', 7 => '`Return_Book`.`Number`', 8 => '`Return_Book`.`Fine`', 9 => '`Return_Book`.`Status`'),
					'records-per-page' => 10,
					'default-sort-by' => false,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-Return_Book',
					'template-printable' => 'children-Return_Book-printable',
					'query' => "SELECT `Return_Book`.`id` as 'id', IF(    CHAR_LENGTH(`books1`.`ISBN_NO`), CONCAT_WS('',   `books1`.`ISBN_NO`), '') as 'Book_Number', IF(    CHAR_LENGTH(`books1`.`Book_Title`), CONCAT_WS('',   `books1`.`Book_Title`), '') as 'Book_Title', IF(    CHAR_LENGTH(if(`Book_Issue1`.`Issue_Date`,date_format(`Book_Issue1`.`Issue_Date`,'%m/%d/%Y'),'')), CONCAT_WS('',   if(`Book_Issue1`.`Issue_Date`,date_format(`Book_Issue1`.`Issue_Date`,'%m/%d/%Y'),'')), '') as 'Issue_Date', IF(    CHAR_LENGTH(if(`Book_Issue1`.`Return_Date`,date_format(`Book_Issue1`.`Return_Date`,'%m/%d/%Y'),'')), CONCAT_WS('',   if(`Book_Issue1`.`Return_Date`,date_format(`Book_Issue1`.`Return_Date`,'%m/%d/%Y'),'')), '') as 'Due_Date', if(`Return_Book`.`Return_Date`,date_format(`Return_Book`.`Return_Date`,'%m/%d/%Y'),'') as 'Return_Date', IF(    CHAR_LENGTH(`Users1`.`Name`), CONCAT_WS('',   `Users1`.`Name`), '') as 'Member', IF(    CHAR_LENGTH(`Users1`.`Membership_Number`), CONCAT_WS('',   `Users1`.`Membership_Number`), '') as 'Number', `Return_Book`.`Fine` as 'Fine', `Return_Book`.`Status` as 'Status' FROM `Return_Book` LEFT JOIN `Book_Issue` as Book_Issue1 ON `Book_Issue1`.`id`=`Return_Book`.`Book_Number` LEFT JOIN `books` as books1 ON `books1`.`id`=`Book_Issue1`.`Book_Number` LEFT JOIN `Users` as Users1 ON `Users1`.`id`=`Return_Book`.`Member` "
				)
			),
			'Types' => 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; ?>
				})
			</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 = '';
			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);
		/************************************************/
	}
