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

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

		$pcConfig = array(
			'main' => array(   
			),
			'courses' => array(   
				'course_name' => array(   
					'parent-table' => 'courses_catalog',
					'parent-primary-key' => 'cat_id',
					'child-primary-key' => 'course_id',
					'child-primary-key-index' => 0,
					'tab-label' => 'Held/scheduled Courses <span class="hidden child-label-courses child-field-caption">(Course)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/books.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(0 => 'Course ID', 1 => 'Course', 3 => 'Instructor', 4 => 'Class', 5 => 'Start Date', 6 => 'End Date', 7 => 'Start Time', 8 => 'End Time', 9 => 'Mon', 10 => 'Tue', 11 => 'Wed', 12 => 'Thu', 13 => 'Fri', 14 => 'Sat', 15 => 'Sun', 16 => 'Fees'),
					'display-field-names' => array(0 => 'course_id', 1 => 'course_name', 3 => 'instructor_id', 4 => 'lab_id', 5 => 'start_date', 6 => 'end_date', 7 => 'start_time', 8 => 'end_time', 9 => 'mon', 10 => 'tue', 11 => 'wed', 12 => 'thu', 13 => 'fri', 14 => 'sat', 15 => 'sun', 16 => 'fees'),
					'sortable-fields' => array(0 => '`courses`.`course_id`', 1 => 2, 2 => '`courses_catalog1`.`course_summary`', 3 => '`instructors1`.`inst_name`', 4 => '`labs1`.`lab_code`', 5 => '`courses`.`start_date`', 6 => '`courses`.`end_date`', 7 => '`courses`.`start_time`', 8 => '`courses`.`end_time`', 9 => 10, 10 => 11, 11 => 12, 12 => 13, 13 => 14, 14 => 15, 15 => 16, 16 => '`courses`.`fees`'),
					'records-per-page' => 10,
					'default-sort-by' => 0,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-courses',
					'template-printable' => 'children-courses-printable',
					'query' => "SELECT `courses`.`course_id` as 'course_id', IF(    CHAR_LENGTH(`courses_catalog1`.`course_name`), CONCAT_WS('',   `courses_catalog1`.`course_name`, '  '), '') as 'course_name', IF(    CHAR_LENGTH(`courses_catalog1`.`course_summary`), CONCAT_WS('',   `courses_catalog1`.`course_summary`), '') as 'description', IF(    CHAR_LENGTH(`instructors1`.`inst_name`), CONCAT_WS('',   `instructors1`.`inst_name`), '') as 'instructor_id', IF(    CHAR_LENGTH(`labs1`.`lab_code`), CONCAT_WS('',   `labs1`.`lab_code`), '') as 'lab_id', if(`courses`.`start_date`,date_format(`courses`.`start_date`,'%d.%m.%Y'),'') as 'start_date', if(`courses`.`end_date`,date_format(`courses`.`end_date`,'%d.%m.%Y'),'') as 'end_date', TIME_FORMAT(`courses`.`start_time`, '%r') as 'start_time', TIME_FORMAT(`courses`.`end_time`, '%r') as 'end_time', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`mon`, 'check', 'unchecked'), '\"></i>') as 'mon', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`tue`, 'check', 'unchecked'), '\"></i>') as 'tue', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`wed`, 'check', 'unchecked'), '\"></i>') as 'wed', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`thu`, 'check', 'unchecked'), '\"></i>') as 'thu', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`fri`, 'check', 'unchecked'), '\"></i>') as 'fri', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`sat`, 'check', 'unchecked'), '\"></i>') as 'sat', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`sun`, 'check', 'unchecked'), '\"></i>') as 'sun', `courses`.`fees` as 'fees' FROM `courses` LEFT JOIN `courses_catalog` as courses_catalog1 ON `courses_catalog1`.`cat_id`=`courses`.`course_name` LEFT JOIN `instructors` as instructors1 ON `instructors1`.`inst_id`=`courses`.`instructor_id` LEFT JOIN `labs` as labs1 ON `labs1`.`lab_id`=`courses`.`lab_id` "
				),
				'instructor_id' => array(   
					'parent-table' => 'instructors',
					'parent-primary-key' => 'inst_id',
					'child-primary-key' => 'course_id',
					'child-primary-key-index' => 0,
					'tab-label' => 'Courses <span class="hidden child-label-courses child-field-caption">(Instructor)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/books.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(0 => 'Course ID', 1 => 'Course', 3 => 'Instructor', 4 => 'Class', 5 => 'Start Date', 6 => 'End Date', 7 => 'Start Time', 8 => 'End Time', 9 => 'Mon', 10 => 'Tue', 11 => 'Wed', 12 => 'Thu', 13 => 'Fri', 14 => 'Sat', 15 => 'Sun', 16 => 'Fees'),
					'display-field-names' => array(0 => 'course_id', 1 => 'course_name', 3 => 'instructor_id', 4 => 'lab_id', 5 => 'start_date', 6 => 'end_date', 7 => 'start_time', 8 => 'end_time', 9 => 'mon', 10 => 'tue', 11 => 'wed', 12 => 'thu', 13 => 'fri', 14 => 'sat', 15 => 'sun', 16 => 'fees'),
					'sortable-fields' => array(0 => '`courses`.`course_id`', 1 => 2, 2 => '`courses_catalog1`.`course_summary`', 3 => '`instructors1`.`inst_name`', 4 => '`labs1`.`lab_code`', 5 => '`courses`.`start_date`', 6 => '`courses`.`end_date`', 7 => '`courses`.`start_time`', 8 => '`courses`.`end_time`', 9 => 10, 10 => 11, 11 => 12, 12 => 13, 13 => 14, 14 => 15, 15 => 16, 16 => '`courses`.`fees`'),
					'records-per-page' => 10,
					'default-sort-by' => 0,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-courses',
					'template-printable' => 'children-courses-printable',
					'query' => "SELECT `courses`.`course_id` as 'course_id', IF(    CHAR_LENGTH(`courses_catalog1`.`course_name`), CONCAT_WS('',   `courses_catalog1`.`course_name`, '  '), '') as 'course_name', IF(    CHAR_LENGTH(`courses_catalog1`.`course_summary`), CONCAT_WS('',   `courses_catalog1`.`course_summary`), '') as 'description', IF(    CHAR_LENGTH(`instructors1`.`inst_name`), CONCAT_WS('',   `instructors1`.`inst_name`), '') as 'instructor_id', IF(    CHAR_LENGTH(`labs1`.`lab_code`), CONCAT_WS('',   `labs1`.`lab_code`), '') as 'lab_id', if(`courses`.`start_date`,date_format(`courses`.`start_date`,'%d.%m.%Y'),'') as 'start_date', if(`courses`.`end_date`,date_format(`courses`.`end_date`,'%d.%m.%Y'),'') as 'end_date', TIME_FORMAT(`courses`.`start_time`, '%r') as 'start_time', TIME_FORMAT(`courses`.`end_time`, '%r') as 'end_time', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`mon`, 'check', 'unchecked'), '\"></i>') as 'mon', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`tue`, 'check', 'unchecked'), '\"></i>') as 'tue', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`wed`, 'check', 'unchecked'), '\"></i>') as 'wed', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`thu`, 'check', 'unchecked'), '\"></i>') as 'thu', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`fri`, 'check', 'unchecked'), '\"></i>') as 'fri', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`sat`, 'check', 'unchecked'), '\"></i>') as 'sat', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`sun`, 'check', 'unchecked'), '\"></i>') as 'sun', `courses`.`fees` as 'fees' FROM `courses` LEFT JOIN `courses_catalog` as courses_catalog1 ON `courses_catalog1`.`cat_id`=`courses`.`course_name` LEFT JOIN `instructors` as instructors1 ON `instructors1`.`inst_id`=`courses`.`instructor_id` LEFT JOIN `labs` as labs1 ON `labs1`.`lab_id`=`courses`.`lab_id` "
				),
				'lab_id' => array(   
					'parent-table' => 'labs',
					'parent-primary-key' => 'lab_id',
					'child-primary-key' => 'course_id',
					'child-primary-key-index' => 0,
					'tab-label' => 'Courses taken in this lab <span class="hidden child-label-courses child-field-caption">(Class)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/books.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(0 => 'Course ID', 1 => 'Course', 3 => 'Instructor', 4 => 'Class', 5 => 'Start Date', 6 => 'End Date', 7 => 'Start Time', 8 => 'End Time', 9 => 'Mon', 10 => 'Tue', 11 => 'Wed', 12 => 'Thu', 13 => 'Fri', 14 => 'Sat', 15 => 'Sun', 16 => 'Fees'),
					'display-field-names' => array(0 => 'course_id', 1 => 'course_name', 3 => 'instructor_id', 4 => 'lab_id', 5 => 'start_date', 6 => 'end_date', 7 => 'start_time', 8 => 'end_time', 9 => 'mon', 10 => 'tue', 11 => 'wed', 12 => 'thu', 13 => 'fri', 14 => 'sat', 15 => 'sun', 16 => 'fees'),
					'sortable-fields' => array(0 => '`courses`.`course_id`', 1 => 2, 2 => '`courses_catalog1`.`course_summary`', 3 => '`instructors1`.`inst_name`', 4 => '`labs1`.`lab_code`', 5 => '`courses`.`start_date`', 6 => '`courses`.`end_date`', 7 => '`courses`.`start_time`', 8 => '`courses`.`end_time`', 9 => 10, 10 => 11, 11 => 12, 12 => 13, 13 => 14, 14 => 15, 15 => 16, 16 => '`courses`.`fees`'),
					'records-per-page' => 10,
					'default-sort-by' => 0,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-courses',
					'template-printable' => 'children-courses-printable',
					'query' => "SELECT `courses`.`course_id` as 'course_id', IF(    CHAR_LENGTH(`courses_catalog1`.`course_name`), CONCAT_WS('',   `courses_catalog1`.`course_name`, '  '), '') as 'course_name', IF(    CHAR_LENGTH(`courses_catalog1`.`course_summary`), CONCAT_WS('',   `courses_catalog1`.`course_summary`), '') as 'description', IF(    CHAR_LENGTH(`instructors1`.`inst_name`), CONCAT_WS('',   `instructors1`.`inst_name`), '') as 'instructor_id', IF(    CHAR_LENGTH(`labs1`.`lab_code`), CONCAT_WS('',   `labs1`.`lab_code`), '') as 'lab_id', if(`courses`.`start_date`,date_format(`courses`.`start_date`,'%d.%m.%Y'),'') as 'start_date', if(`courses`.`end_date`,date_format(`courses`.`end_date`,'%d.%m.%Y'),'') as 'end_date', TIME_FORMAT(`courses`.`start_time`, '%r') as 'start_time', TIME_FORMAT(`courses`.`end_time`, '%r') as 'end_time', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`mon`, 'check', 'unchecked'), '\"></i>') as 'mon', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`tue`, 'check', 'unchecked'), '\"></i>') as 'tue', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`wed`, 'check', 'unchecked'), '\"></i>') as 'wed', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`thu`, 'check', 'unchecked'), '\"></i>') as 'thu', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`fri`, 'check', 'unchecked'), '\"></i>') as 'fri', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`sat`, 'check', 'unchecked'), '\"></i>') as 'sat', concat('<i class=\"glyphicon glyphicon-', if(`courses`.`sun`, 'check', 'unchecked'), '\"></i>') as 'sun', `courses`.`fees` as 'fees' FROM `courses` LEFT JOIN `courses_catalog` as courses_catalog1 ON `courses_catalog1`.`cat_id`=`courses`.`course_name` LEFT JOIN `instructors` as instructors1 ON `instructors1`.`inst_id`=`courses`.`instructor_id` LEFT JOIN `labs` as labs1 ON `labs1`.`lab_id`=`courses`.`lab_id` "
				)
			),
			'enrollment' => array(   
				'stud_id' => array(   
					'parent-table' => 'students',
					'parent-primary-key' => 'student_id',
					'child-primary-key' => 'rec_id',
					'child-primary-key-index' => 0,
					'tab-label' => 'Courses enrolled to <span class="hidden child-label-enrollment child-field-caption">(Student)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/data_chooser.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Student', 2 => 'Course', 3 => 'Score', 4 => 'Comments'),
					'display-field-names' => array(1 => 'stud_id', 2 => 'course_id', 3 => 'score', 4 => 'notes'),
					'sortable-fields' => array(0 => '`enrollment`.`rec_id`', 1 => '`students1`.`student_name`', 2 => 3, 3 => 4, 4 => 5, 5 => 6),
					'records-per-page' => 10,
					'default-sort-by' => 0,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-enrollment',
					'template-printable' => 'children-enrollment-printable',
					'query' => "SELECT `enrollment`.`rec_id` as 'rec_id', IF(    CHAR_LENGTH(`students1`.`student_name`), CONCAT_WS('',   `students1`.`student_name`), '') as 'stud_id', IF(    CHAR_LENGTH(`courses_catalog1`.`course_name`), CONCAT_WS('',   `courses_catalog1`.`course_name`, '  '), '') as 'course_id', `enrollment`.`score` as 'score', `enrollment`.`notes` as 'notes', `enrollment`.`certificate_notes` as 'certificate_notes' FROM `enrollment` LEFT JOIN `students` as students1 ON `students1`.`student_id`=`enrollment`.`stud_id` LEFT JOIN `courses` as courses1 ON `courses1`.`course_id`=`enrollment`.`course_id` LEFT JOIN `courses_catalog` as courses_catalog1 ON `courses_catalog1`.`cat_id`=`courses1`.`course_name` "
				),
				'course_id' => array(   
					'parent-table' => 'courses',
					'parent-primary-key' => 'course_id',
					'child-primary-key' => 'rec_id',
					'child-primary-key-index' => 0,
					'tab-label' => 'Students Enrolled <span class="hidden child-label-enrollment child-field-caption">(Course)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/data_chooser.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Student', 2 => 'Course', 3 => 'Score', 4 => 'Comments'),
					'display-field-names' => array(1 => 'stud_id', 2 => 'course_id', 3 => 'score', 4 => 'notes'),
					'sortable-fields' => array(0 => '`enrollment`.`rec_id`', 1 => '`students1`.`student_name`', 2 => 3, 3 => 4, 4 => 5, 5 => 6),
					'records-per-page' => 10,
					'default-sort-by' => 0,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-enrollment',
					'template-printable' => 'children-enrollment-printable',
					'query' => "SELECT `enrollment`.`rec_id` as 'rec_id', IF(    CHAR_LENGTH(`students1`.`student_name`), CONCAT_WS('',   `students1`.`student_name`), '') as 'stud_id', IF(    CHAR_LENGTH(`courses_catalog1`.`course_name`), CONCAT_WS('',   `courses_catalog1`.`course_name`, '  '), '') as 'course_id', `enrollment`.`score` as 'score', `enrollment`.`notes` as 'notes', `enrollment`.`certificate_notes` as 'certificate_notes' FROM `enrollment` LEFT JOIN `students` as students1 ON `students1`.`student_id`=`enrollment`.`stud_id` LEFT JOIN `courses` as courses1 ON `courses1`.`course_id`=`enrollment`.`course_id` LEFT JOIN `courses_catalog` as courses_catalog1 ON `courses_catalog1`.`cat_id`=`courses1`.`course_name` "
				)
			),
			'attendance' => array(   
				'student_course' => array(   
					'parent-table' => 'enrollment',
					'parent-primary-key' => 'rec_id',
					'child-primary-key' => 'attendance_id',
					'child-primary-key-index' => 0,
					'tab-label' => 'Course attendance <span class="hidden child-label-attendance child-field-caption">(Student / Course)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/attributes_display.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Student / Course', 2 => 'Date', 3 => 'Attended'),
					'display-field-names' => array(1 => 'student_course', 2 => 'date', 3 => 'attended'),
					'sortable-fields' => array(0 => '`attendance`.`attendance_id`', 1 => 2, 2 => '`attendance`.`date`', 3 => 4, 4 => 5),
					'records-per-page' => 10,
					'default-sort-by' => 2,
					'default-sort-direction' => 'desc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-attendance',
					'template-printable' => 'children-attendance-printable',
					'query' => "SELECT `attendance`.`attendance_id` as 'attendance_id', IF(    CHAR_LENGTH(`students1`.`student_name`) || CHAR_LENGTH(`courses_catalog1`.`course_name`), CONCAT_WS('',   `students1`.`student_name`, ' / ', `courses_catalog1`.`course_name`, '  '), '') as 'student_course', if(`attendance`.`date`,date_format(`attendance`.`date`,'%d.%m.%Y'),'') as 'date', concat('<i class=\"glyphicon glyphicon-', if(`attendance`.`attended`, 'check', 'unchecked'), '\"></i>') as 'attended', `attendance`.`notes` as 'notes' FROM `attendance` LEFT JOIN `enrollment` as enrollment1 ON `enrollment1`.`rec_id`=`attendance`.`student_course` LEFT JOIN `students` as students1 ON `students1`.`student_id`=`enrollment1`.`stud_id` LEFT JOIN `courses` as courses1 ON `courses1`.`course_id`=`enrollment1`.`course_id` LEFT JOIN `courses_catalog` as courses_catalog1 ON `courses_catalog1`.`cat_id`=`courses1`.`course_name` "
				)
			),
			'students' => array(   
			),
			'instructors' => array(   
			),
			'labs' => array(   
			),
			'courses_catalog' => array(   
			),
			'companies' => 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);
		/************************************************/
	}
