<?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(   
			),
			'clients' => array(   
			),
			'gestionnaires' => array(   
			),
			'partenaires' => array(   
			),
			'paiements' => array(   
			),
			'emetteurs' => array(   
			),
			'bieleteries' => array(   
			),
			'tarifs' => array(   
				'link_bieleterie' => array(   
					'parent-table' => 'bieleteries',
					'parent-primary-key' => 'idbieleterie',
					'child-primary-key' => 'idtarif',
					'child-primary-key-index' => 0,
					'tab-label' => 'BILLETERIES <span class="hidden child-label-tarifs child-field-caption">(Link tarif)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/gei (81).png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(2 => 'Parcours', 3 => 'Compagnie', 4 => 'PNR', 5 => 'Emetteur', 6 => 'Prix achat', 7 => 'Commission', 8 => 'Net &#224; payer'),
					'display-field-names' => array(2 => 'parcours', 3 => 'compagnie', 4 => 'pnr', 5 => 'emetteur', 6 => 'prix_achat', 7 => 'commission', 8 => 'total'),
					'sortable-fields' => array(0 => '`tarifs`.`idtarif`', 1 => '`bieleteries1`.`link_tarif`', 2 => 3, 3 => '`compagnies1`.`compagnie`', 4 => 5, 5 => '`emetteurs1`.`emetteur`', 6 => '`tarifs`.`prix_achat`', 7 => '`tarifs`.`commission`', 8 => '`tarifs`.`total`'),
					'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-tarifs',
					'template-printable' => 'children-tarifs-printable',
					'query' => "SELECT `tarifs`.`idtarif` as 'idtarif', IF(    CHAR_LENGTH(`bieleteries1`.`link_tarif`), CONCAT_WS('',   `bieleteries1`.`link_tarif`), '') as 'link_bieleterie', `tarifs`.`parcours` as 'parcours', IF(    CHAR_LENGTH(`compagnies1`.`compagnie`), CONCAT_WS('',   `compagnies1`.`compagnie`), '') as 'compagnie', `tarifs`.`pnr` as 'pnr', IF(    CHAR_LENGTH(`emetteurs1`.`emetteur`), CONCAT_WS('',   `emetteurs1`.`emetteur`), '') as 'emetteur', `tarifs`.`prix_achat` as 'prix_achat', `tarifs`.`commission` as 'commission', `tarifs`.`total` as 'total' FROM `tarifs` LEFT JOIN `bieleteries` as bieleteries1 ON `bieleteries1`.`idbieleterie`=`tarifs`.`link_bieleterie` LEFT JOIN `compagnies` as compagnies1 ON `compagnies1`.`idcompagnie`=`tarifs`.`compagnie` LEFT JOIN `emetteurs` as emetteurs1 ON `emetteurs1`.`idemetteur`=`tarifs`.`emetteur` "
				)
			),
			'transferts_argent' => array(   
			),
			'colis' => array(   
			),
			'envois_colis' => array(   
				'link_colis' => array(   
					'parent-table' => 'colis',
					'parent-primary-key' => 'idcoli',
					'child-primary-key' => 'idenvois_coli',
					'child-primary-key-index' => 0,
					'tab-label' => 'DESCRIPTIONS <span class="hidden child-label-envois_colis child-field-caption">(Link colis)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/lorry.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(2 => 'Description', 3 => 'Destination', 4 => 'Agence', 5 => 'Prix unitaire', 6 => 'Quantit&#233;', 7 => 'Net &#224; payer', 8 => 'B&#233;n&#233;ficiaire', 9 => 'T&#233;l&#233;phone b&#233;n&#233;ficiaire', 10 => 'NNI b&#233;n&#233;ficiaire', 11 => 'Date de reception', 12 => 'Statut'),
					'display-field-names' => array(2 => 'description', 3 => 'destination', 4 => 'ville', 5 => 'prix_unitaire', 6 => 'qts', 7 => 'prix_total', 8 => 'beneficiaire', 9 => 'telephone_beneficiaire', 10 => 'nni_beneficiaire', 11 => 'date_reception', 12 => 'statut'),
					'sortable-fields' => array(0 => '`envois_colis`.`idenvois_coli`', 1 => '`colis1`.`link_envoi_colis`', 2 => 3, 3 => '`agences1`.`agence`', 4 => 5, 5 => '`envois_colis`.`prix_unitaire`', 6 => '`envois_colis`.`qts`', 7 => '`envois_colis`.`prix_total`', 8 => 9, 9 => 10, 10 => 11, 11 => '`envois_colis`.`date_reception`', 12 => 13, 13 => 14),
					'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-envois_colis',
					'template-printable' => 'children-envois_colis-printable',
					'query' => "SELECT `envois_colis`.`idenvois_coli` as 'idenvois_coli', IF(    CHAR_LENGTH(`colis1`.`link_envoi_colis`), CONCAT_WS('',   `colis1`.`link_envoi_colis`), '') as 'link_colis', `envois_colis`.`description` as 'description', IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') as 'destination', IF(    CHAR_LENGTH(`villes1`.`ville`), CONCAT_WS('',   `villes1`.`ville`), '') as 'ville', `envois_colis`.`prix_unitaire` as 'prix_unitaire', `envois_colis`.`qts` as 'qts', `envois_colis`.`prix_total` as 'prix_total', `envois_colis`.`beneficiaire` as 'beneficiaire', `envois_colis`.`telephone_beneficiaire` as 'telephone_beneficiaire', `envois_colis`.`nni_beneficiaire` as 'nni_beneficiaire', if(`envois_colis`.`date_reception`,date_format(`envois_colis`.`date_reception`,'%d/%m/%Y'),'') as 'date_reception', `envois_colis`.`statut` as 'statut', `envois_colis`.`observation` as 'observation' FROM `envois_colis` LEFT JOIN `colis` as colis1 ON `colis1`.`idcoli`=`envois_colis`.`link_colis` LEFT JOIN `agences` as agences1 ON `agences1`.`idagence`=`envois_colis`.`destination` LEFT JOIN `villes` as villes1 ON `villes1`.`idville`=`agences1`.`ville` "
				)
			),
			'parc_auto' => array(   
			),
			'location_vh' => array(   
			),
			'liste_location' => array(   
				'link_location' => array(   
					'parent-table' => 'location_vh',
					'parent-primary-key' => 'idlocation',
					'child-primary-key' => 'idlocations',
					'child-primary-key-index' => 0,
					'tab-label' => 'LISTE LOCATION VH <span class="hidden child-label-liste_location child-field-caption">(Link location)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/small_car.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(2 => 'Date location', 3 => 'Date fin location', 4 => 'Vehicule', 5 => 'Model', 6 => 'Type', 7 => 'Prix unitaire', 8 => 'Qts', 9 => 'Prix total'),
					'display-field-names' => array(2 => 'date_location', 3 => 'date_fin_location', 4 => 'vehicule', 5 => 'model', 6 => 'type', 7 => 'prix_unitaire', 8 => 'qts', 9 => 'prix_total'),
					'sortable-fields' => array(0 => '`liste_location`.`idlocations`', 1 => '`location_vh1`.`link_liste`', 2 => '`liste_location`.`date_location`', 3 => '`liste_location`.`date_fin_location`', 4 => 5, 5 => '`models2`.`model`', 6 => 7, 7 => '`liste_location`.`prix_unitaire`', 8 => '`liste_location`.`qts`', 9 => '`liste_location`.`prix_total`', 10 => 11),
					'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-liste_location',
					'template-printable' => 'children-liste_location-printable',
					'query' => "SELECT `liste_location`.`idlocations` as 'idlocations', IF(    CHAR_LENGTH(`location_vh1`.`link_liste`), CONCAT_WS('',   `location_vh1`.`link_liste`), '') as 'link_location', if(`liste_location`.`date_location`,date_format(`liste_location`.`date_location`,'%d/%m/%Y'),'') as 'date_location', if(`liste_location`.`date_fin_location`,date_format(`liste_location`.`date_fin_location`,'%d/%m/%Y'),'') as 'date_fin_location', IF(    CHAR_LENGTH(`models1`.`marque`), CONCAT_WS('',   `models1`.`marque`), '') as 'vehicule', IF(    CHAR_LENGTH(`models2`.`model`), CONCAT_WS('',   `models2`.`model`), '') as 'model', `liste_location`.`type` as 'type', `liste_location`.`prix_unitaire` as 'prix_unitaire', `liste_location`.`qts` as 'qts', `liste_location`.`prix_total` as 'prix_total', `liste_location`.`observation` as 'observation' FROM `liste_location` LEFT JOIN `location_vh` as location_vh1 ON `location_vh1`.`idlocation`=`liste_location`.`link_location` LEFT JOIN `parc_auto` as parc_auto1 ON `parc_auto1`.`idparc`=`liste_location`.`vehicule` LEFT JOIN `models` as models1 ON `models1`.`idmodel`=`parc_auto1`.`marque` LEFT JOIN `models` as models2 ON `models2`.`idmodel`=`liste_location`.`model` "
				)
			),
			'fonctions' => array(   
			),
			'agences' => array(   
			),
			'villes' => array(   
			),
			'banques' => array(   
			),
			'type_paiements' => array(   
			),
			'compagnies' => array(   
			),
			'statuts' => array(   
			),
			'visas' => array(   
			),
			'type_visas' => array(   
			),
			'models' => array(   
			),
			'facture' => array(   
			),
			'detail_facture' => array(   
				'factureid' => array(   
					'parent-table' => 'facture',
					'parent-primary-key' => 'idfacture',
					'child-primary-key' => 'dfid',
					'child-primary-key-index' => 0,
					'tab-label' => 'LISTE DES FACTURES <span class="hidden child-label-detail_facture child-field-caption">(Factureid)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/blogs.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(2 => 'Designation', 3 => 'Quantite', 4 => 'Prix unitaire', 5 => 'Total'),
					'display-field-names' => array(2 => 'designation', 3 => 'quantite', 4 => 'prixunitaire', 5 => 'total'),
					'sortable-fields' => array(0 => '`detail_facture`.`dfid`', 1 => '`facture1`.`idfacture`', 2 => 3, 3 => '`detail_facture`.`quantite`', 4 => '`detail_facture`.`prixunitaire`', 5 => '`detail_facture`.`total`'),
					'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-detail_facture',
					'template-printable' => 'children-detail_facture-printable',
					'query' => "SELECT `detail_facture`.`dfid` as 'dfid', IF(    CHAR_LENGTH(`facture1`.`idfacture`), CONCAT_WS('',   `facture1`.`idfacture`), '') as 'factureid', `detail_facture`.`designation` as 'designation', `detail_facture`.`quantite` as 'quantite', `detail_facture`.`prixunitaire` as 'prixunitaire', `detail_facture`.`total` as 'total' FROM `detail_facture` LEFT JOIN `facture` as facture1 ON `facture1`.`idfacture`=`detail_facture`.`factureid` "
				)
			),
			'compta' => array(   
			),
			'tresorerie_caisse' => array(   
			),
			'tresorerie_items' => array(   
				'link_out' => array(   
					'parent-table' => 'tresorerie_caisse',
					'parent-primary-key' => 'idtresorerie_caisse',
					'child-primary-key' => 'idtresorerie_item',
					'child-primary-key-index' => 0,
					'tab-label' => 'Tresorerie items <span class="hidden child-label-tresorerie_items child-field-caption">(Link out)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/coffre-fort.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(0 => 'N&#176; d\'ordre', 2 => 'P. Justificative', 3 => 'Libelles', 4 => 'Montant', 5 => 'Commission agence', 6 => 'Net compagnie', 7 => 'Observations'),
					'display-field-names' => array(0 => 'idtresorerie_item', 2 => 'justificatif', 3 => 'libelles', 4 => 'montant', 5 => 'commission_agence', 6 => 'net_compagnie', 7 => 'observations'),
					'sortable-fields' => array(0 => '`tresorerie_items`.`idtresorerie_item`', 1 => '`tresorerie_caisse1`.`link_in`', 2 => 3, 3 => 4, 4 => '`tresorerie_items`.`montant`', 5 => '`tresorerie_items`.`commission_agence`', 6 => '`tresorerie_items`.`net_compagnie`', 7 => 8),
					'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-tresorerie_items',
					'template-printable' => 'children-tresorerie_items-printable',
					'query' => "SELECT `tresorerie_items`.`idtresorerie_item` as 'idtresorerie_item', IF(    CHAR_LENGTH(`tresorerie_caisse1`.`link_in`), CONCAT_WS('',   `tresorerie_caisse1`.`link_in`), '') as 'link_out', `tresorerie_items`.`justificatif` as 'justificatif', `tresorerie_items`.`libelles` as 'libelles', `tresorerie_items`.`montant` as 'montant', `tresorerie_items`.`commission_agence` as 'commission_agence', `tresorerie_items`.`net_compagnie` as 'net_compagnie', `tresorerie_items`.`observations` as 'observations' FROM `tresorerie_items` LEFT JOIN `tresorerie_caisse` as tresorerie_caisse1 ON `tresorerie_caisse1`.`idtresorerie_caisse`=`tresorerie_items`.`link_out` "
				)
			),
			'piece_reglements' => array(   
			),
			'autorisation_change' => array(   
			),
			'fiches_changes' => array(   
			),
			'fiches_changes_items' => array(   
				'link_out' => array(   
					'parent-table' => 'fiches_changes',
					'parent-primary-key' => 'idfichechange',
					'child-primary-key' => 'idfiche_change_item',
					'child-primary-key-index' => 0,
					'tab-label' => 'LISTE FICHES DE CHANGES <span class="hidden child-label-fiches_changes_items child-field-caption">(Link out)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/conversion_of_currency.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(0 => 'N&#176; d\'ordre', 2 => 'Nom et Pr&#233;nom', 3 => 'N&#176; Identit&#233;', 4 => 'Montant', 5 => 'Devise', 6 => 'Achats', 7 => 'Ventes', 8 => 'Observations'),
					'display-field-names' => array(0 => 'idfiche_change_item', 2 => 'nom_prenom', 3 => 'num_identite', 4 => 'montant', 5 => 'devises', 6 => 'achats', 7 => 'ventes', 8 => 'observations'),
					'sortable-fields' => array(0 => '`fiches_changes_items`.`idfiche_change_item`', 1 => '`fiches_changes1`.`link_in`', 2 => 3, 3 => 4, 4 => '`fiches_changes_items`.`montant`', 5 => 6, 6 => '`fiches_changes_items`.`achats`', 7 => '`fiches_changes_items`.`ventes`', 8 => 9),
					'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-fiches_changes_items',
					'template-printable' => 'children-fiches_changes_items-printable',
					'query' => "SELECT `fiches_changes_items`.`idfiche_change_item` as 'idfiche_change_item', IF(    CHAR_LENGTH(`fiches_changes1`.`link_in`), CONCAT_WS('',   `fiches_changes1`.`link_in`), '') as 'link_out', `fiches_changes_items`.`nom_prenom` as 'nom_prenom', `fiches_changes_items`.`num_identite` as 'num_identite', `fiches_changes_items`.`montant` as 'montant', `fiches_changes_items`.`devises` as 'devises', `fiches_changes_items`.`achats` as 'achats', `fiches_changes_items`.`ventes` as 'ventes', `fiches_changes_items`.`observations` as 'observations' FROM `fiches_changes_items` LEFT JOIN `fiches_changes` as fiches_changes1 ON `fiches_changes1`.`idfichechange`=`fiches_changes_items`.`link_out` "
				)
			),
			'bon_passage' => array(   
			),
			'caisse_journaliere' => array(   
			),
			'item_caisse_journaliere' => array(   
				'link_out' => array(   
					'parent-table' => 'caisse_journaliere',
					'parent-primary-key' => 'idcaisse_journaliere',
					'child-primary-key' => 'iditem_caisse_journaliere',
					'child-primary-key-index' => 1,
					'tab-label' => 'Item caisse journaliere <span class="hidden child-label-item_caisse_journaliere child-field-caption">(Link out)</span>',
					'auto-close' => false,
					'table-icon' => 'resources/table_icons/caisse-2.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'N&#176; Ordre', 2 => 'P. Justificative', 3 => 'Nom et Pr&#233;nom', 4 => 'Destination', 5 => 'Compagnie/Agence', 6 => 'Prix HT', 7 => 'Commission', 8 => 'Prix TTC', 9 => 'Observations'),
					'display-field-names' => array(1 => 'iditem_caisse_journaliere', 2 => 'justificatif', 3 => 'nom_prenom', 4 => 'destination', 5 => 'compagnie', 6 => 'prix_ht', 7 => 'commission', 8 => 'prix_ttc', 9 => 'observations'),
					'sortable-fields' => array(0 => '`caisse_journaliere1`.`link_in`', 1 => '`item_caisse_journaliere`.`iditem_caisse_journaliere`', 2 => 3, 3 => 4, 4 => 5, 5 => 6, 6 => '`item_caisse_journaliere`.`prix_ht`', 7 => '`item_caisse_journaliere`.`commission`', 8 => '`item_caisse_journaliere`.`prix_ttc`', 9 => 10),
					'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-item_caisse_journaliere',
					'template-printable' => 'children-item_caisse_journaliere-printable',
					'query' => "SELECT IF(    CHAR_LENGTH(`caisse_journaliere1`.`link_in`), CONCAT_WS('',   `caisse_journaliere1`.`link_in`), '') as 'link_out', `item_caisse_journaliere`.`iditem_caisse_journaliere` as 'iditem_caisse_journaliere', `item_caisse_journaliere`.`justificatif` as 'justificatif', `item_caisse_journaliere`.`nom_prenom` as 'nom_prenom', `item_caisse_journaliere`.`destination` as 'destination', `item_caisse_journaliere`.`compagnie` as 'compagnie', REPLACE(FORMAT(`item_caisse_journaliere`.`prix_ht`, 0), ',', ' ') as 'prix_ht', `item_caisse_journaliere`.`commission` as 'commission', REPLACE(FORMAT(`item_caisse_journaliere`.`prix_ttc`, 0), ',', ' ') as 'prix_ttc', `item_caisse_journaliere`.`observations` as 'observations' FROM `item_caisse_journaliere` LEFT JOIN `caisse_journaliere` as caisse_journaliere1 ON `caisse_journaliere1`.`idcaisse_journaliere`=`item_caisse_journaliere`.`link_out` "
				)
			)
		);

	/*************************************/
	/* 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);
		/************************************************/
	}
