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

	$currDir = dirname(__FILE__);
	include_once("{$currDir}/lib.php");
	@include_once("{$currDir}/hooks/c002_ref.php");
	include_once("{$currDir}/c002_ref_dml.php");

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

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

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = [
		"`c002_ref`.`id`" => "id",
		"IF(    CHAR_LENGTH(`c0021`.`id`), CONCAT_WS('',   `c0021`.`id`), '') /* Id categoria */" => "id_categoria",
		"DATE_FORMAT(`c002_ref`.`data`, '%e/%c/%Y')" => "data",
		"`c002_ref`.`nome`" => "nome",
		"`c002_ref`.`cpf`" => "cpf",
		"`c002_ref`.`celular`" => "celular",
		"`c002_ref`.`email`" => "email",
		"`c002_ref`.`cidade`" => "cidade",
		"`c002_ref`.`uf`" => "uf",
		"`c002_ref`.`regiao`" => "regiao",
		"`c002_ref`.`quantidade`" => "quantidade",
		"`c002_ref`.`unidade`" => "unidade",
		"`c002_ref`.`uta`" => "uta",
		"`c002_ref`.`valor`" => "valor",
		"`c002_ref`.`obs`" => "obs",
		"`c002_ref`.`situacao`" => "situacao",
	];
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = [
		1 => '`c002_ref`.`id`',
		2 => '`c0021`.`id`',
		3 => '`c002_ref`.`data`',
		4 => 4,
		5 => 5,
		6 => 6,
		7 => 7,
		8 => 8,
		9 => 9,
		10 => 10,
		11 => 11,
		12 => 12,
		13 => 13,
		14 => 14,
		15 => 15,
		16 => 16,
	];

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = [
		"`c002_ref`.`id`" => "id",
		"IF(    CHAR_LENGTH(`c0021`.`id`), CONCAT_WS('',   `c0021`.`id`), '') /* Id categoria */" => "id_categoria",
		"DATE_FORMAT(`c002_ref`.`data`, '%e/%c/%Y')" => "data",
		"`c002_ref`.`nome`" => "nome",
		"`c002_ref`.`cpf`" => "cpf",
		"`c002_ref`.`celular`" => "celular",
		"`c002_ref`.`email`" => "email",
		"`c002_ref`.`cidade`" => "cidade",
		"`c002_ref`.`uf`" => "uf",
		"`c002_ref`.`regiao`" => "regiao",
		"`c002_ref`.`quantidade`" => "quantidade",
		"`c002_ref`.`unidade`" => "unidade",
		"`c002_ref`.`uta`" => "uta",
		"`c002_ref`.`valor`" => "valor",
		"`c002_ref`.`obs`" => "obs",
		"`c002_ref`.`situacao`" => "situacao",
	];
	// Fields that can be filtered
	$x->QueryFieldsFilters = [
		"`c002_ref`.`id`" => "ID",
		"IF(    CHAR_LENGTH(`c0021`.`id`), CONCAT_WS('',   `c0021`.`id`), '') /* Id categoria */" => "Id categoria",
		"`c002_ref`.`data`" => "Data",
		"`c002_ref`.`nome`" => "Nome",
		"`c002_ref`.`cpf`" => "CPF",
		"`c002_ref`.`celular`" => "Celular",
		"`c002_ref`.`email`" => "E-mail",
		"`c002_ref`.`cidade`" => "Cidade",
		"`c002_ref`.`uf`" => "UF",
		"`c002_ref`.`regiao`" => "Regi&#227;o/UF",
		"`c002_ref`.`quantidade`" => "Qdade",
		"`c002_ref`.`unidade`" => "Unidade",
		"`c002_ref`.`uta`" => "UTA",
		"`c002_ref`.`valor`" => "Valor",
		"`c002_ref`.`obs`" => "Observa&#231;&#227;o",
		"`c002_ref`.`situacao`" => "Situa&#231;&#227;o",
	];

	// Fields that can be quick searched
	$x->QueryFieldsQS = [
		"`c002_ref`.`id`" => "id",
		"IF(    CHAR_LENGTH(`c0021`.`id`), CONCAT_WS('',   `c0021`.`id`), '') /* Id categoria */" => "id_categoria",
		"DATE_FORMAT(`c002_ref`.`data`, '%e/%c/%Y')" => "data",
		"`c002_ref`.`nome`" => "nome",
		"`c002_ref`.`cpf`" => "cpf",
		"`c002_ref`.`celular`" => "celular",
		"`c002_ref`.`email`" => "email",
		"`c002_ref`.`cidade`" => "cidade",
		"`c002_ref`.`uf`" => "uf",
		"`c002_ref`.`regiao`" => "regiao",
		"`c002_ref`.`quantidade`" => "quantidade",
		"`c002_ref`.`unidade`" => "unidade",
		"`c002_ref`.`uta`" => "uta",
		"`c002_ref`.`valor`" => "valor",
		"`c002_ref`.`obs`" => "obs",
		"`c002_ref`.`situacao`" => "situacao",
	];

	// Lookup fields that can be used as filterers
	$x->filterers = ['id_categoria' => 'Id categoria', ];

	$x->QueryFrom = "`c002_ref` LEFT JOIN `c002` as c0021 ON `c0021`.`id`=`c002_ref`.`id_categoria` ";
	$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 = 0;
	$x->AllowDeleteOfParents = 0;
	$x->AllowFilters = 1;
	$x->AllowSavingFilters = (getLoggedAdmin() !== false);
	$x->AllowSorting = 1;
	$x->AllowNavigation = 1;
	$x->AllowPrinting = 1;
	$x->AllowPrintingDV = 1;
	$x->AllowCSV = (getLoggedAdmin() !== false);
	$x->RecordsPerPage = 5;
	$x->QuickSearch = 1;
	$x->QuickSearchText = $Translation['quick search'];
	$x->ScriptFileName = 'c002_ref_view.php';
	$x->RedirectAfterInsert = 'c002_ref_view.php?SelectedID=#ID#';
	$x->TableTitle = 'Refer&#234;ncias';
	$x->TableIcon = 'table.gif';
	$x->PrimaryKey = '`c002_ref`.`id`';
	$x->DefaultSortField = '10';
	$x->DefaultSortDirection = 'asc';

	$x->ColWidth = [150, 150, 150, 150, 150, ];
	$x->ColCaption = ['Regi&#227;o/UF', 'Qdade', 'Unidade', 'UTA', 'Valor', ];
	$x->ColFieldName = ['regiao', 'quantidade', 'unidade', 'uta', 'valor', ];
	$x->ColNumber  = [10, 11, 12, 13, 14, ];

	// template paths below are based on the app main directory
	$x->Template = 'templates/c002_ref_templateTV.html';
	$x->SelectedTemplate = 'templates/c002_ref_templateTVS.html';
	$x->TemplateDV = 'templates/c002_ref_templateDV.html';
	$x->TemplateDVP = 'templates/c002_ref_templateDVP.html';

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

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

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

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

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

	echo $x->HTML;

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

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