<?php
// This script and data application were generated by AppGini 5.76
// 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/demandes_permission.php");
	include("$currDir/demandes_permission_dml.php");

	// mm: can the current member access this page?
	$perm=getTablePermissions('demandes_permission');
	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 = "demandes_permission";

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(   
		"`demandes_permission`.`iddemande_permission`" => "iddemande_permission",
		"if(`demandes_permission`.`date_demande_p`,date_format(`demandes_permission`.`date_demande_p`,'%d/%m/%Y'),'')" => "date_demande_p",
		"`demandes_permission`.`numero`" => "numero",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* MATRICULE */" => "matricule",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nom_prenom`), CONCAT_WS('',   `dossier_individuel1`.`nom_prenom`), '') /* NOM ET PRENOM */" => "nom_prenom",
		"IF(    CHAR_LENGTH(if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), CONCAT_WS('',   if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), '') /* DATE DE RECRUTEMENT */" => "date_recrutement",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* FONCTION */" => "fonction",
		"IF(    CHAR_LENGTH(`divisions1`.`division`), CONCAT_WS('',   `divisions1`.`division`), '') /* Division */" => "division",
		"IF(    CHAR_LENGTH(`directions1`.`direction`), CONCAT_WS('',   `directions1`.`direction`), '') /* Direction */" => "direction",
		"IF(    CHAR_LENGTH(`services1`.`service`), CONCAT_WS('',   `services1`.`service`), '') /* Service */" => "service",
		"if(`demandes_permission`.`depart`,date_format(`demandes_permission`.`depart`,'%d/%m/%Y'),'')" => "depart",
		"if(`demandes_permission`.`retour`,date_format(`demandes_permission`.`retour`,'%d/%m/%Y'),'')" => "retour",
		"`demandes_permission`.`raison`" => "raison",
		"if(`demandes_permission`.`date_dernier_permission`,date_format(`demandes_permission`.`date_dernier_permission`,'%d/%m/%Y'),'')" => "date_dernier_permission",
		"`demandes_permission`.`jours`" => "jours",
		"`demandes_permission`.`adresse_permission`" => "adresse_permission",
		"`demandes_permission`.`justificatif_permission`" => "justificatif_permission",
		"`demandes_permission`.`observation`" => "observation"
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(   
		1 => '`demandes_permission`.`iddemande_permission`',
		2 => '`demandes_permission`.`date_demande_p`',
		3 => 3,
		4 => '`dossier_individuel1`.`matricule`',
		5 => '`dossier_individuel1`.`nom_prenom`',
		6 => 'date_format(`dossier_individuel1`.`date_recrutement`,\'%d/%m/%Y\')',
		7 => 7,
		8 => 8,
		9 => 9,
		10 => 10,
		11 => '`demandes_permission`.`depart`',
		12 => '`demandes_permission`.`retour`',
		13 => 13,
		14 => '`demandes_permission`.`date_dernier_permission`',
		15 => '`demandes_permission`.`jours`',
		16 => 16,
		17 => 17,
		18 => 18
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(   
		"`demandes_permission`.`iddemande_permission`" => "iddemande_permission",
		"if(`demandes_permission`.`date_demande_p`,date_format(`demandes_permission`.`date_demande_p`,'%d/%m/%Y'),'')" => "date_demande_p",
		"`demandes_permission`.`numero`" => "numero",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* MATRICULE */" => "matricule",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nom_prenom`), CONCAT_WS('',   `dossier_individuel1`.`nom_prenom`), '') /* NOM ET PRENOM */" => "nom_prenom",
		"IF(    CHAR_LENGTH(if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), CONCAT_WS('',   if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), '') /* DATE DE RECRUTEMENT */" => "date_recrutement",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* FONCTION */" => "fonction",
		"IF(    CHAR_LENGTH(`divisions1`.`division`), CONCAT_WS('',   `divisions1`.`division`), '') /* Division */" => "division",
		"IF(    CHAR_LENGTH(`directions1`.`direction`), CONCAT_WS('',   `directions1`.`direction`), '') /* Direction */" => "direction",
		"IF(    CHAR_LENGTH(`services1`.`service`), CONCAT_WS('',   `services1`.`service`), '') /* Service */" => "service",
		"if(`demandes_permission`.`depart`,date_format(`demandes_permission`.`depart`,'%d/%m/%Y'),'')" => "depart",
		"if(`demandes_permission`.`retour`,date_format(`demandes_permission`.`retour`,'%d/%m/%Y'),'')" => "retour",
		"`demandes_permission`.`raison`" => "raison",
		"if(`demandes_permission`.`date_dernier_permission`,date_format(`demandes_permission`.`date_dernier_permission`,'%d/%m/%Y'),'')" => "date_dernier_permission",
		"`demandes_permission`.`jours`" => "jours",
		"`demandes_permission`.`adresse_permission`" => "adresse_permission",
		"`demandes_permission`.`justificatif_permission`" => "justificatif_permission",
		"`demandes_permission`.`observation`" => "observation"
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(   
		"`demandes_permission`.`iddemande_permission`" => "N&#176;",
		"`demandes_permission`.`date_demande_p`" => "DATE DE DEMANDE",
		"`demandes_permission`.`numero`" => "NUMERO",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* MATRICULE */" => "MATRICULE",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nom_prenom`), CONCAT_WS('',   `dossier_individuel1`.`nom_prenom`), '') /* NOM ET PRENOM */" => "NOM ET PRENOM",
		"IF(    CHAR_LENGTH(if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), CONCAT_WS('',   if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), '') /* DATE DE RECRUTEMENT */" => "DATE DE RECRUTEMENT",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* FONCTION */" => "FONCTION",
		"IF(    CHAR_LENGTH(`divisions1`.`division`), CONCAT_WS('',   `divisions1`.`division`), '') /* Division */" => "Division",
		"IF(    CHAR_LENGTH(`directions1`.`direction`), CONCAT_WS('',   `directions1`.`direction`), '') /* Direction */" => "Direction",
		"IF(    CHAR_LENGTH(`services1`.`service`), CONCAT_WS('',   `services1`.`service`), '') /* Service */" => "Service",
		"`demandes_permission`.`depart`" => "DATE DEPART",
		"`demandes_permission`.`retour`" => "DATE RETOUR",
		"`demandes_permission`.`raison`" => "RAISON DE PERMISSION",
		"`demandes_permission`.`date_dernier_permission`" => "DATE DERNIERE PERMISSION",
		"`demandes_permission`.`jours`" => "NBR JOURS",
		"`demandes_permission`.`adresse_permission`" => "ADRESSE PERMISSION",
		"`demandes_permission`.`justificatif_permission`" => "JUSTIFICATIF",
		"`demandes_permission`.`observation`" => "OBSERVATION"
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(   
		"`demandes_permission`.`iddemande_permission`" => "iddemande_permission",
		"if(`demandes_permission`.`date_demande_p`,date_format(`demandes_permission`.`date_demande_p`,'%d/%m/%Y'),'')" => "date_demande_p",
		"`demandes_permission`.`numero`" => "numero",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* MATRICULE */" => "matricule",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nom_prenom`), CONCAT_WS('',   `dossier_individuel1`.`nom_prenom`), '') /* NOM ET PRENOM */" => "nom_prenom",
		"IF(    CHAR_LENGTH(if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), CONCAT_WS('',   if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), '') /* DATE DE RECRUTEMENT */" => "date_recrutement",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* FONCTION */" => "fonction",
		"IF(    CHAR_LENGTH(`divisions1`.`division`), CONCAT_WS('',   `divisions1`.`division`), '') /* Division */" => "division",
		"IF(    CHAR_LENGTH(`directions1`.`direction`), CONCAT_WS('',   `directions1`.`direction`), '') /* Direction */" => "direction",
		"IF(    CHAR_LENGTH(`services1`.`service`), CONCAT_WS('',   `services1`.`service`), '') /* Service */" => "service",
		"if(`demandes_permission`.`depart`,date_format(`demandes_permission`.`depart`,'%d/%m/%Y'),'')" => "depart",
		"if(`demandes_permission`.`retour`,date_format(`demandes_permission`.`retour`,'%d/%m/%Y'),'')" => "retour",
		"`demandes_permission`.`raison`" => "raison",
		"if(`demandes_permission`.`date_dernier_permission`,date_format(`demandes_permission`.`date_dernier_permission`,'%d/%m/%Y'),'')" => "date_dernier_permission",
		"`demandes_permission`.`jours`" => "jours",
		"`demandes_permission`.`adresse_permission`" => "adresse_permission",
		"`demandes_permission`.`justificatif_permission`" => "justificatif_permission",
		"`demandes_permission`.`observation`" => "observation"
	);

	// Lookup fields that can be used as filterers
	$x->filterers = array(  'matricule' => 'MATRICULE');

	$x->QueryFrom = "`demandes_permission` LEFT JOIN `dossier_individuel` as dossier_individuel1 ON `dossier_individuel1`.`iddindividuel`=`demandes_permission`.`matricule` LEFT JOIN `fonctions` as fonctions1 ON `fonctions1`.`idfonction`=`dossier_individuel1`.`fonction` LEFT JOIN `divisions` as divisions1 ON `divisions1`.`iddivision`=`dossier_individuel1`.`division` LEFT JOIN `directions` as directions1 ON `directions1`.`iddirection`=`dossier_individuel1`.`direction` LEFT JOIN `services` as services1 ON `services1`.`idservice`=`dossier_individuel1`.`service` ";
	$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 = 100;
	$x->QuickSearch = 1;
	$x->QuickSearchText = $Translation["quick search"];
	$x->ScriptFileName = "demandes_permission_view.php";
	$x->RedirectAfterInsert = "demandes_permission_view.php?SelectedID=#ID#";
	$x->TableTitle = "DEMANDES PERMISSION";
	$x->TableIcon = "resources/table_icons/folder_table.png";
	$x->PrimaryKey = "`demandes_permission`.`iddemande_permission`";
	$x->DefaultSortField = '1';
	$x->DefaultSortDirection = 'desc';

	$x->ColWidth   = array(  150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150);
	$x->ColCaption = array("N&#176;", "DATE DE DEMANDE", "DATE DE RECRUTEMENT", "FONCTION", "Division", "Direction", "Service", "DATE DEPART", "DATE RETOUR", "RAISON DE PERMISSION", "DATE DERNIERE PERMISSION");
	$x->ColFieldName = array('iddemande_permission', 'date_demande_p', 'date_recrutement', 'fonction', 'division', 'direction', 'service', 'depart', 'retour', 'raison', 'date_dernier_permission');
	$x->ColNumber  = array(1, 2, 6, 7, 8, 9, 10, 11, 12, 13, 14);

	// template paths below are based on the app main directory
	$x->Template = 'templates/demandes_permission_templateTV.html';
	$x->SelectedTemplate = 'templates/demandes_permission_templateTVS.html';
	$x->TemplateDV = 'templates/demandes_permission_templateDV.html';
	$x->TemplateDVP = 'templates/demandes_permission_templateDVP.html';

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

	// 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 `demandes_permission`.`iddemande_permission`=membership_userrecords.pkValue and membership_userrecords.tableName='demandes_permission' 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 `demandes_permission`.`iddemande_permission`=membership_userrecords.pkValue and membership_userrecords.tableName='demandes_permission' 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 = '`demandes_permission`';
		$x->QueryWhere = '';
		$x->DefaultSortField = '';
	}
	// hook: demandes_permission_init
	$render=TRUE;
	if(function_exists('demandes_permission_init')){
		$args=array();
		$render=demandes_permission_init($x, getMemberInfo(), $args);
	}

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

	// hook: demandes_permission_header
	$headerCode='';
	if(function_exists('demandes_permission_header')){
		$args=array();
		$headerCode=demandes_permission_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: demandes_permission_footer
	$footerCode='';
	if(function_exists('demandes_permission_footer')){
		$args=array();
		$footerCode=demandes_permission_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);
	}
?>