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

	$currDir=dirname(__FILE__);
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");
	@include("$currDir/hooks/gestion_comptes.php");
	include("$currDir/gestion_comptes_dml.php");

	// mm: can the current member access this page?
	$perm=getTablePermissions('gestion_comptes');
	if(!$perm[0]) {
		echo error_message($Translation['tableAccessDenied'], false);
		echo '<script>setTimeout("window.location=\'index.php?signOut=1\'", 2000);</script>';
		exit;
	}

	$x = new DataList;
	$x->TableName = "gestion_comptes";

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(
		"`gestion_comptes`.`idgestioncompte`" => "idgestioncompte",
		"`gestion_comptes`.`nom_compte`" => "nom_compte",
		"`gestion_comptes`.`num_compte`" => "num_compte",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "banque",
		"IF(    CHAR_LENGTH(`type_comptes1`.`type_compte`), CONCAT_WS('',   `type_comptes1`.`type_compte`), '') /* Type de compte */" => "type_compte",
		"`gestion_comptes`.`observations`" => "observations",
		"`gestion_comptes`.`groupe`" => "groupe",
		"`gestion_comptes`.`agent`" => "agent",
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(
		1 => '`gestion_comptes`.`idgestioncompte`',
		2 => 2,
		3 => '`gestion_comptes`.`num_compte`',
		4 => '`banques1`.`banque`',
		5 => '`type_comptes1`.`type_compte`',
		6 => 6,
		7 => 7,
		8 => 8,
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(
		"`gestion_comptes`.`idgestioncompte`" => "idgestioncompte",
		"`gestion_comptes`.`nom_compte`" => "nom_compte",
		"`gestion_comptes`.`num_compte`" => "num_compte",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "banque",
		"IF(    CHAR_LENGTH(`type_comptes1`.`type_compte`), CONCAT_WS('',   `type_comptes1`.`type_compte`), '') /* Type de compte */" => "type_compte",
		"`gestion_comptes`.`observations`" => "observations",
		"`gestion_comptes`.`groupe`" => "groupe",
		"`gestion_comptes`.`agent`" => "agent",
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(
		"`gestion_comptes`.`idgestioncompte`" => "ID",
		"`gestion_comptes`.`nom_compte`" => "Nom du compte",
		"`gestion_comptes`.`num_compte`" => "N&#176; du compte",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "Banque",
		"IF(    CHAR_LENGTH(`type_comptes1`.`type_compte`), CONCAT_WS('',   `type_comptes1`.`type_compte`), '') /* Type de compte */" => "Type de compte",
		"`gestion_comptes`.`observations`" => "Observations",
		"`gestion_comptes`.`groupe`" => "Groupe",
		"`gestion_comptes`.`agent`" => "Agent",
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(
		"`gestion_comptes`.`idgestioncompte`" => "idgestioncompte",
		"`gestion_comptes`.`nom_compte`" => "nom_compte",
		"`gestion_comptes`.`num_compte`" => "num_compte",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "banque",
		"IF(    CHAR_LENGTH(`type_comptes1`.`type_compte`), CONCAT_WS('',   `type_comptes1`.`type_compte`), '') /* Type de compte */" => "type_compte",
		"`gestion_comptes`.`observations`" => "observations",
		"`gestion_comptes`.`groupe`" => "groupe",
		"`gestion_comptes`.`agent`" => "agent",
	);

	// Lookup fields that can be used as filterers
	$x->filterers = array('banque' => 'Banque', 'type_compte' => 'Type de compte', );

	$x->QueryFrom = "`gestion_comptes` LEFT JOIN `banques` as banques1 ON `banques1`.`idbanque`=`gestion_comptes`.`banque` LEFT JOIN `type_comptes` as type_comptes1 ON `type_comptes1`.`idtypecompte`=`gestion_comptes`.`type_compte` ";
	$x->QueryWhere = '';
	$x->QueryOrder = '';

	$x->AllowSelection = 1;
	$x->HideTableView = ($perm[2]==0 ? 1 : 0);
	$x->AllowDelete = $perm[4];
	$x->AllowMassDelete = true;
	$x->AllowInsert = $perm[1];
	$x->AllowUpdate = $perm[3];
	$x->SeparateDV = 1;
	$x->AllowDeleteOfParents = 0;
	$x->AllowFilters = 1;
	$x->AllowSavingFilters = 1;
	$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 = "gestion_comptes_view.php";
	$x->RedirectAfterInsert = "gestion_comptes_view.php?SelectedID=#ID#";
	$x->TableTitle = "COMPTES MV";
	$x->TableIcon = "resources/table_icons/application_from_storage.png";
	$x->PrimaryKey = "`gestion_comptes`.`idgestioncompte`";
	$x->DefaultSortField = '3';
	$x->DefaultSortDirection = 'asc';

	$x->ColWidth   = array(  150, 150, 150, 150, 150);
	$x->ColCaption = array("Nom du compte", "N&#176; du compte", "Banque", "Groupe", "Agent");
	$x->ColFieldName = array('nom_compte', 'num_compte', 'banque', 'groupe', 'agent');
	$x->ColNumber  = array(2, 3, 4, 7, 8);

	// template paths below are based on the app main directory
	$x->Template = 'templates/gestion_comptes_templateTV.html';
	$x->SelectedTemplate = 'templates/gestion_comptes_templateTVS.html';
	$x->TemplateDV = 'templates/gestion_comptes_templateDV.html';
	$x->TemplateDVP = 'templates/gestion_comptes_templateDVP.html';

	$x->ShowTableHeader = 1;
	$x->TVClasses = "";
	$x->DVClasses = "";
	$x->HighlightColor = '#FFF0C2';
	$x->HasCalculatedFields = false;

	// mm: build the query based on current member's permissions
	$DisplayRecords = $_REQUEST['DisplayRecords'];
	if(!in_array($DisplayRecords, array('user', 'group'))) { $DisplayRecords = 'all'; }
	if($perm[2]==1 || ($perm[2]>1 && $DisplayRecords=='user' && !$_REQUEST['NoFilter_x'])) { // view owner only
		$x->QueryFrom.=', membership_userrecords';
		$x->QueryWhere="where `gestion_comptes`.`idgestioncompte`=membership_userrecords.pkValue and membership_userrecords.tableName='gestion_comptes' and lcase(membership_userrecords.memberID)='".getLoggedMemberID()."'";
	}elseif($perm[2]==2 || ($perm[2]>2 && $DisplayRecords=='group' && !$_REQUEST['NoFilter_x'])) { // view group only
		$x->QueryFrom.=', membership_userrecords';
		$x->QueryWhere="where `gestion_comptes`.`idgestioncompte`=membership_userrecords.pkValue and membership_userrecords.tableName='gestion_comptes' and membership_userrecords.groupID='".getLoggedGroupID()."'";
	}elseif($perm[2]==3) { // view all
		// no further action
	}elseif($perm[2]==0) { // view none
		$x->QueryFields = array("Not enough permissions" => "NEP");
		$x->QueryFrom = '`gestion_comptes`';
		$x->QueryWhere = '';
		$x->DefaultSortField = '';
	}
	// hook: gestion_comptes_init
	$render=TRUE;
	if(function_exists('gestion_comptes_init')) {
		$args=array();
		$render=gestion_comptes_init($x, getMemberInfo(), $args);
	}

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

	// hook: gestion_comptes_header
	$headerCode='';
	if(function_exists('gestion_comptes_header')) {
		$args=array();
		$headerCode=gestion_comptes_header($x->ContentType, getMemberInfo(), $args);
	}  
	if(!$headerCode) {
		include_once("$currDir/header.php"); 
	}else{
		ob_start(); include_once("$currDir/header.php"); $dHeader=ob_get_contents(); ob_end_clean();
		echo str_replace('<%%HEADER%%>', $dHeader, $headerCode);
	}

	echo $x->HTML;
	// hook: gestion_comptes_footer
	$footerCode='';
	if(function_exists('gestion_comptes_footer')) {
		$args=array();
		$footerCode=gestion_comptes_footer($x->ContentType, getMemberInfo(), $args);
	}  
	if(!$footerCode) {
		include_once("$currDir/footer.php"); 
	}else{
		ob_start(); include_once("$currDir/footer.php"); $dFooter=ob_get_contents(); ob_end_clean();
		echo str_replace('<%%FOOTER%%>', $dFooter, $footerCode);
	}
?>