<?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/patients.php');
	include_once(__DIR__ . '/patients_dml.php');

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

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

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = [
		"`patients`.`id`" => "id",
		"`patients`.`last_name`" => "last_name",
		"`patients`.`first_name`" => "first_name",
		"`patients`.`gender`" => "gender",
		"`patients`.`sexual_orientation`" => "sexual_orientation",
		"if(`patients`.`birth_date`,date_format(`patients`.`birth_date`,'%m/%d/%Y'),'')" => "birth_date",
		"`patients`.`age`" => "age",
		"`patients`.`image`" => "image",
		"`patients`.`address`" => "address",
		"`patients`.`city`" => "city",
		"`patients`.`state`" => "state",
		"`patients`.`zip`" => "zip",
		"CONCAT_WS('-', LEFT(`patients`.`home_phone`,3), MID(`patients`.`home_phone`,4,3), RIGHT(`patients`.`home_phone`,4))" => "home_phone",
		"CONCAT_WS('-', LEFT(`patients`.`work_phone`,3), MID(`patients`.`work_phone`,4,3), RIGHT(`patients`.`work_phone`,4))" => "work_phone",
		"CONCAT_WS('-', LEFT(`patients`.`mobile`,3), MID(`patients`.`mobile`,4,3))" => "mobile",
		"`patients`.`tobacco_usage`" => "tobacco_usage",
		"`patients`.`alcohol_intake`" => "alcohol_intake",
		"`patients`.`history`" => "history",
		"`patients`.`surgical_history`" => "surgical_history",
		"`patients`.`obstetric_history`" => "obstetric_history",
		"`patients`.`genetic_diseases`" => "genetic_diseases",
		"`patients`.`contact_person`" => "contact_person",
		"`patients`.`other_details`" => "other_details",
		"`patients`.`comments`" => "comments",
		"DATE_FORMAT(`patients`.`filed`, '%c/%e/%Y %l:%i%p')" => "filed",
		"DATE_FORMAT(`patients`.`last_modified`, '%c/%e/%Y %l:%i%p')" => "last_modified",
	];
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = [
		1 => '`patients`.`id`',
		2 => 2,
		3 => 3,
		4 => 4,
		5 => 5,
		6 => '`patients`.`birth_date`',
		7 => '`patients`.`age`',
		8 => 8,
		9 => 9,
		10 => 10,
		11 => 11,
		12 => 12,
		13 => 13,
		14 => 14,
		15 => 15,
		16 => 16,
		17 => 17,
		18 => 18,
		19 => 19,
		20 => 20,
		21 => 21,
		22 => 22,
		23 => 23,
		24 => 24,
		25 => '`patients`.`filed`',
		26 => '`patients`.`last_modified`',
	];

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = [
		"`patients`.`id`" => "id",
		"`patients`.`last_name`" => "last_name",
		"`patients`.`first_name`" => "first_name",
		"`patients`.`gender`" => "gender",
		"`patients`.`sexual_orientation`" => "sexual_orientation",
		"if(`patients`.`birth_date`,date_format(`patients`.`birth_date`,'%m/%d/%Y'),'')" => "birth_date",
		"`patients`.`age`" => "age",
		"`patients`.`image`" => "image",
		"`patients`.`address`" => "address",
		"`patients`.`city`" => "city",
		"`patients`.`state`" => "state",
		"`patients`.`zip`" => "zip",
		"CONCAT_WS('-', LEFT(`patients`.`home_phone`,3), MID(`patients`.`home_phone`,4,3), RIGHT(`patients`.`home_phone`,4))" => "home_phone",
		"CONCAT_WS('-', LEFT(`patients`.`work_phone`,3), MID(`patients`.`work_phone`,4,3), RIGHT(`patients`.`work_phone`,4))" => "work_phone",
		"CONCAT_WS('-', LEFT(`patients`.`mobile`,3), MID(`patients`.`mobile`,4,3))" => "mobile",
		"`patients`.`tobacco_usage`" => "tobacco_usage",
		"`patients`.`alcohol_intake`" => "alcohol_intake",
		"`patients`.`history`" => "history",
		"`patients`.`surgical_history`" => "surgical_history",
		"`patients`.`obstetric_history`" => "obstetric_history",
		"`patients`.`genetic_diseases`" => "genetic_diseases",
		"`patients`.`contact_person`" => "contact_person",
		"`patients`.`other_details`" => "other_details",
		"`patients`.`comments`" => "comments",
		"DATE_FORMAT(`patients`.`filed`, '%c/%e/%Y %l:%i%p')" => "filed",
		"DATE_FORMAT(`patients`.`last_modified`, '%c/%e/%Y %l:%i%p')" => "last_modified",
	];
	// Fields that can be filtered
	$x->QueryFieldsFilters = [
		"`patients`.`id`" => "ID",
		"`patients`.`last_name`" => "Last name",
		"`patients`.`first_name`" => "First name",
		"`patients`.`gender`" => "Gender",
		"`patients`.`sexual_orientation`" => "Sexual orientation",
		"`patients`.`birth_date`" => "Birth date",
		"`patients`.`age`" => "Age",
		"`patients`.`address`" => "Address",
		"`patients`.`city`" => "City",
		"`patients`.`state`" => "State",
		"`patients`.`zip`" => "Zip",
		"`patients`.`home_phone`" => "Home phone",
		"`patients`.`work_phone`" => "Work phone",
		"`patients`.`mobile`" => "Mobile",
		"`patients`.`tobacco_usage`" => "Tobacco usage",
		"`patients`.`alcohol_intake`" => "Alcohol Intake",
		"`patients`.`history`" => "History",
		"`patients`.`surgical_history`" => "Surgical history",
		"`patients`.`obstetric_history`" => "Obstetric history",
		"`patients`.`genetic_diseases`" => "Genetic diseases",
		"`patients`.`contact_person`" => "Contact person in case of Emergency",
		"`patients`.`other_details`" => "Other details",
		"`patients`.`comments`" => "Comments",
		"`patients`.`filed`" => "Filed",
		"`patients`.`last_modified`" => "Last modified",
	];

	// Fields that can be quick searched
	$x->QueryFieldsQS = [
		"`patients`.`id`" => "id",
		"`patients`.`last_name`" => "last_name",
		"`patients`.`first_name`" => "first_name",
		"`patients`.`gender`" => "gender",
		"`patients`.`sexual_orientation`" => "sexual_orientation",
		"if(`patients`.`birth_date`,date_format(`patients`.`birth_date`,'%m/%d/%Y'),'')" => "birth_date",
		"`patients`.`age`" => "age",
		"`patients`.`address`" => "address",
		"`patients`.`city`" => "city",
		"`patients`.`state`" => "state",
		"`patients`.`zip`" => "zip",
		"CONCAT_WS('-', LEFT(`patients`.`home_phone`,3), MID(`patients`.`home_phone`,4,3), RIGHT(`patients`.`home_phone`,4))" => "home_phone",
		"CONCAT_WS('-', LEFT(`patients`.`work_phone`,3), MID(`patients`.`work_phone`,4,3), RIGHT(`patients`.`work_phone`,4))" => "work_phone",
		"CONCAT_WS('-', LEFT(`patients`.`mobile`,3), MID(`patients`.`mobile`,4,3))" => "mobile",
		"`patients`.`tobacco_usage`" => "tobacco_usage",
		"`patients`.`alcohol_intake`" => "alcohol_intake",
		"`patients`.`history`" => "history",
		"`patients`.`surgical_history`" => "surgical_history",
		"`patients`.`obstetric_history`" => "obstetric_history",
		"`patients`.`genetic_diseases`" => "genetic_diseases",
		"`patients`.`contact_person`" => "contact_person",
		"`patients`.`other_details`" => "other_details",
		"`patients`.`comments`" => "comments",
		"DATE_FORMAT(`patients`.`filed`, '%c/%e/%Y %l:%i%p')" => "filed",
		"DATE_FORMAT(`patients`.`last_modified`, '%c/%e/%Y %l:%i%p')" => "last_modified",
	];

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

	$x->QueryFrom = "`patients` ";
	$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 = 1;
	$x->AllowFilters = 1;
	$x->AllowSavingFilters = 1;
	$x->AllowSorting = 1;
	$x->AllowNavigation = 1;
	$x->AllowPrinting = 1;
	$x->AllowPrintingDV = 1;
	$x->AllowCSV = 1;
	$x->RecordsPerPage = 20;
	$x->QuickSearch = 1;
	$x->QuickSearchText = $Translation['quick search'];
	$x->ScriptFileName = 'patients_view.php';
	$x->RedirectAfterInsert = 'patients_view.php?SelectedID=#ID#';
	$x->TableTitle = 'Patients';
	$x->TableIcon = 'resources/table_icons/administrator.png';
	$x->PrimaryKey = '`patients`.`id`';
	$x->DefaultSortField = '1';
	$x->DefaultSortDirection = 'desc';

	$x->ColWidth = [120, 120, 70, 150, 50, 150, 50, 100, 150, 150, 150, 150, 150, 150, ];
	$x->ColCaption = ['Last name', 'First name', 'Gender', 'Sexual orientation', 'Age', 'Image', 'State', 'Mobile', 'Tobacco usage', 'Alcohol Intake', 'History', 'Surgical history', 'Obstetric history', 'Genetic diseases', ];
	$x->ColFieldName = ['last_name', 'first_name', 'gender', 'sexual_orientation', 'age', 'image', 'state', 'mobile', 'tobacco_usage', 'alcohol_intake', 'history', 'surgical_history', 'obstetric_history', 'genetic_diseases', ];
	$x->ColNumber  = [2, 3, 4, 5, 7, 8, 11, 15, 16, 17, 18, 19, 20, 21, ];

	// template paths below are based on the app main directory
	$x->Template = 'templates/patients_templateTV.html';
	$x->SelectedTemplate = 'templates/patients_templateTVS.html';
	$x->TemplateDV = 'templates/patients_templateDV.html';
	$x->TemplateDVP = 'templates/patients_templateDVP.html';

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

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

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

	// hook: patients_header
	$headerCode = '';
	if(function_exists('patients_header')) {
		$args = [];
		$headerCode = patients_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: patients_footer
	$footerCode = '';
	if(function_exists('patients_footer')) {
		$args = [];
		$footerCode = patients_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);
	}
