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

	include_once(__DIR__ . '/lib.php');
	@include_once(__DIR__ . '/hooks/medical_records.php');
	include_once(__DIR__ . '/medical_records_dml.php');

	// mm: can the current member access this page?
	$perm = getTablePermissions('medical_records');
	if(!$perm['access']) {
		echo error_message($Translation['tableAccessDenied']);
		exit;
	}

	$x = new DataList;
	$x->TableName = 'medical_records';

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = [
		"`medical_records`.`id`" => "id",
		"IF(    CHAR_LENGTH(`patients1`.`last_name`) || CHAR_LENGTH(`patients1`.`first_name`), CONCAT_WS('',   `patients1`.`last_name`, ',', `patients1`.`first_name`), '') /* Patient */" => "patient",
		"`medical_records`.`image_1`" => "image_1",
		"`medical_records`.`image_2`" => "image_2",
		"`medical_records`.`image_3`" => "image_3",
		"`medical_records`.`image_4`" => "image_4",
		"`medical_records`.`image_5`" => "image_5",
		"`medical_records`.`document_1`" => "document_1",
		"`medical_records`.`document_2`" => "document_2",
		"`medical_records`.`document_3`" => "document_3",
		"`medical_records`.`document_4`" => "document_4",
		"`medical_records`.`document_5`" => "document_5",
		"`medical_records`.`description`" => "description",
	];
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = [
		1 => '`medical_records`.`id`',
		2 => 2,
		3 => 3,
		4 => 4,
		5 => 5,
		6 => 6,
		7 => 7,
		8 => 8,
		9 => 9,
		10 => 10,
		11 => 11,
		12 => 12,
		13 => 13,
	];

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = [
		"`medical_records`.`id`" => "id",
		"IF(    CHAR_LENGTH(`patients1`.`last_name`) || CHAR_LENGTH(`patients1`.`first_name`), CONCAT_WS('',   `patients1`.`last_name`, ',', `patients1`.`first_name`), '') /* Patient */" => "patient",
		"`medical_records`.`image_1`" => "image_1",
		"`medical_records`.`image_2`" => "image_2",
		"`medical_records`.`image_3`" => "image_3",
		"`medical_records`.`image_4`" => "image_4",
		"`medical_records`.`image_5`" => "image_5",
		"`medical_records`.`document_1`" => "document_1",
		"`medical_records`.`document_2`" => "document_2",
		"`medical_records`.`document_3`" => "document_3",
		"`medical_records`.`document_4`" => "document_4",
		"`medical_records`.`document_5`" => "document_5",
		"`medical_records`.`description`" => "description",
	];
	// Fields that can be filtered
	$x->QueryFieldsFilters = [
		"`medical_records`.`id`" => "ID",
		"IF(    CHAR_LENGTH(`patients1`.`last_name`) || CHAR_LENGTH(`patients1`.`first_name`), CONCAT_WS('',   `patients1`.`last_name`, ',', `patients1`.`first_name`), '') /* Patient */" => "Patient",
		"`medical_records`.`document_1`" => "Document 1",
		"`medical_records`.`document_2`" => "Document 2",
		"`medical_records`.`document_3`" => "Document 3",
		"`medical_records`.`document_4`" => "Document 4",
		"`medical_records`.`document_5`" => "Document 5",
		"`medical_records`.`description`" => "Description",
	];

	// Fields that can be quick searched
	$x->QueryFieldsQS = [
		"`medical_records`.`id`" => "id",
		"IF(    CHAR_LENGTH(`patients1`.`last_name`) || CHAR_LENGTH(`patients1`.`first_name`), CONCAT_WS('',   `patients1`.`last_name`, ',', `patients1`.`first_name`), '') /* Patient */" => "patient",
		"`medical_records`.`document_1`" => "document_1",
		"`medical_records`.`document_2`" => "document_2",
		"`medical_records`.`document_3`" => "document_3",
		"`medical_records`.`document_4`" => "document_4",
		"`medical_records`.`document_5`" => "document_5",
		"`medical_records`.`description`" => "description",
	];

	// Lookup fields that can be used as filterers
	$x->filterers = ['patient' => 'Patient', ];

	$x->QueryFrom = "`medical_records` LEFT JOIN `patients` as patients1 ON `patients1`.`id`=`medical_records`.`patient` ";
	$x->QueryWhere = '';
	$x->QueryOrder = '';

	$x->AllowSelection = 1;
	$x->HideTableView = ($perm['view'] == 0 ? 1 : 0);
	$x->AllowDelete = $perm['delete'];
	$x->AllowMassDelete = (getLoggedAdmin() !== false);
	$x->AllowInsert = $perm['insert'];
	$x->AllowUpdate = $perm['edit'];
	$x->SeparateDV = 1;
	$x->AllowDeleteOfParents = 0;
	$x->AllowFilters = 1;
	$x->AllowSavingFilters = (getLoggedAdmin() !== false);
	$x->AllowSorting = 1;
	$x->AllowNavigation = 1;
	$x->AllowPrinting = 1;
	$x->AllowPrintingDV = 1;
	$x->AllowCSV = 1;
	$x->RecordsPerPage = 10;
	$x->QuickSearch = 1;
	$x->QuickSearchText = $Translation['quick search'];
	$x->ScriptFileName = 'medical_records_view.php';
	$x->RedirectAfterInsert = 'medical_records_view.php?SelectedID=#ID#';
	$x->TableTitle = 'Medical Records';
	$x->TableIcon = 'resources/table_icons/cash_terminal.png';
	$x->PrimaryKey = '`medical_records`.`id`';

	$x->ColWidth = [150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, ];
	$x->ColCaption = ['Patient', 'Image 1', 'Image 2', 'Image 3', 'Image 4', 'Image 5', 'Document 1', 'Document 2', 'Document 3', 'Document 4', 'Document 5', 'Description', ];
	$x->ColFieldName = ['patient', 'image_1', 'image_2', 'image_3', 'image_4', 'image_5', 'document_1', 'document_2', 'document_3', 'document_4', 'document_5', 'description', ];
	$x->ColNumber  = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, ];

	// template paths below are based on the app main directory
	$x->Template = 'templates/medical_records_templateTV.html';
	$x->SelectedTemplate = 'templates/medical_records_templateTVS.html';
	$x->TemplateDV = 'templates/medical_records_templateDV.html';
	$x->TemplateDVP = 'templates/medical_records_templateDVP.html';

	$x->ShowTableHeader = 1;
	$x->TVClasses = "";
	$x->DVClasses = "";
	$x->HasCalculatedFields = false;
	$x->AllowConsoleLog = false;
	$x->AllowDVNavigation = true;

	// hook: medical_records_init
	$render = true;
	if(function_exists('medical_records_init')) {
		$args = [];
		$render = medical_records_init($x, getMemberInfo(), $args);
	}

	if($render) $x->Render();

	// hook: medical_records_header
	$headerCode = '';
	if(function_exists('medical_records_header')) {
		$args = [];
		$headerCode = medical_records_header($x->ContentType, getMemberInfo(), $args);
	}

	if(!$headerCode) {
		include_once(__DIR__ . '/header.php'); 
	} else {
		ob_start();
		include_once(__DIR__ . '/header.php');
		echo str_replace('<%%HEADER%%>', ob_get_clean(), $headerCode);
	}

	echo $x->HTML;

	// hook: medical_records_footer
	$footerCode = '';
	if(function_exists('medical_records_footer')) {
		$args = [];
		$footerCode = medical_records_footer($x->ContentType, getMemberInfo(), $args);
	}

	if(!$footerCode) {
		include_once(__DIR__ . '/footer.php'); 
	} else {
		ob_start();
		include_once(__DIR__ . '/footer.php');
		echo str_replace('<%%FOOTER%%>', ob_get_clean(), $footerCode);
	}
