<?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.php");
	include("$currDir/demandes_dml.php");

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

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(   
		"`demandes`.`iddemande`" => "iddemande",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* MATRICULE */" => "matricule",
		"`demandes`.`numero`" => "numero",
		"if(`demandes`.`date_demande`,date_format(`demandes`.`date_demande`,'%d/%m/%Y'),'')" => "date_demande",
		"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 RECRUTEMENT */" => "recrutement",
		"if(`demandes`.`date_depart_souhaite`,date_format(`demandes`.`date_depart_souhaite`,'%d/%m/%Y'),'')" => "date_depart_souhaite",
		"if(`demandes`.`date_depart_dernier`,date_format(`demandes`.`date_depart_dernier`,'%d/%m/%Y'),'')" => "date_depart_dernier",
		"if(`demandes`.`date_reprise`,date_format(`demandes`.`date_reprise`,'%d/%m/%Y'),'')" => "date_reprise",
		"`demandes`.`raison_permission`" => "raison_permission",
		"`demandes`.`adresse_abscence`" => "adresse_abscence",
		"`demandes`.`nbre_jour`" => "nbre_jour",
		"`demandes`.`pj_conge`" => "pj_conge"
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(   
		1 => '`demandes`.`iddemande`',
		2 => '`dossier_individuel1`.`matricule`',
		3 => 3,
		4 => '`demandes`.`date_demande`',
		5 => '`dossier_individuel1`.`nom_prenom`',
		6 => 'date_format(`dossier_individuel1`.`date_recrutement`,\'%d/%m/%Y\')',
		7 => '`demandes`.`date_depart_souhaite`',
		8 => '`demandes`.`date_depart_dernier`',
		9 => '`demandes`.`date_reprise`',
		10 => 10,
		11 => 11,
		12 => '`demandes`.`nbre_jour`',
		13 => 13
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(   
		"`demandes`.`iddemande`" => "iddemande",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* MATRICULE */" => "matricule",
		"`demandes`.`numero`" => "numero",
		"if(`demandes`.`date_demande`,date_format(`demandes`.`date_demande`,'%d/%m/%Y'),'')" => "date_demande",
		"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 RECRUTEMENT */" => "recrutement",
		"if(`demandes`.`date_depart_souhaite`,date_format(`demandes`.`date_depart_souhaite`,'%d/%m/%Y'),'')" => "date_depart_souhaite",
		"if(`demandes`.`date_depart_dernier`,date_format(`demandes`.`date_depart_dernier`,'%d/%m/%Y'),'')" => "date_depart_dernier",
		"if(`demandes`.`date_reprise`,date_format(`demandes`.`date_reprise`,'%d/%m/%Y'),'')" => "date_reprise",
		"`demandes`.`raison_permission`" => "raison_permission",
		"`demandes`.`adresse_abscence`" => "adresse_abscence",
		"`demandes`.`nbre_jour`" => "nbre_jour",
		"`demandes`.`pj_conge`" => "pj_conge"
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(   
		"`demandes`.`iddemande`" => "ID",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* MATRICULE */" => "MATRICULE",
		"`demandes`.`numero`" => "N&#176;",
		"`demandes`.`date_demande`" => "DATE",
		"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 RECRUTEMENT */" => "DATE RECRUTEMENT",
		"`demandes`.`date_depart_souhaite`" => "DATE DE DEPART SOUHAITE",
		"`demandes`.`date_depart_dernier`" => "DATE DEPART DERNIERRE ABSENCE",
		"`demandes`.`date_reprise`" => "DATE REPRISE",
		"`demandes`.`raison_permission`" => "Raison permission",
		"`demandes`.`adresse_abscence`" => "ADRESSE DURANT VOTRE ABSENCE",
		"`demandes`.`nbre_jour`" => "NOMBRE DE JOURS",
		"`demandes`.`pj_conge`" => "PJ"
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(   
		"`demandes`.`iddemande`" => "iddemande",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* MATRICULE */" => "matricule",
		"`demandes`.`numero`" => "numero",
		"if(`demandes`.`date_demande`,date_format(`demandes`.`date_demande`,'%d/%m/%Y'),'')" => "date_demande",
		"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 RECRUTEMENT */" => "recrutement",
		"if(`demandes`.`date_depart_souhaite`,date_format(`demandes`.`date_depart_souhaite`,'%d/%m/%Y'),'')" => "date_depart_souhaite",
		"if(`demandes`.`date_depart_dernier`,date_format(`demandes`.`date_depart_dernier`,'%d/%m/%Y'),'')" => "date_depart_dernier",
		"if(`demandes`.`date_reprise`,date_format(`demandes`.`date_reprise`,'%d/%m/%Y'),'')" => "date_reprise",
		"`demandes`.`raison_permission`" => "raison_permission",
		"`demandes`.`adresse_abscence`" => "adresse_abscence",
		"`demandes`.`nbre_jour`" => "nbre_jour",
		"`demandes`.`pj_conge`" => "pj_conge"
	);

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

	$x->QueryFrom = "`demandes` LEFT JOIN `dossier_individuel` as dossier_individuel1 ON `dossier_individuel1`.`iddindividuel`=`demandes`.`matricule` ";
	$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 = 1000;
	$x->QuickSearch = 1;
	$x->QuickSearchText = $Translation["quick search"];
	$x->ScriptFileName = "demandes_view.php";
	$x->RedirectAfterInsert = "demandes_view.php?SelectedID=#ID#";
	$x->TableTitle = "DEMANDES";
	$x->TableIcon = "resources/table_icons/folder_page.png";
	$x->PrimaryKey = "`demandes`.`iddemande`";
	$x->DefaultSortField = '1';
	$x->DefaultSortDirection = 'asc';

	$x->ColWidth   = array(  150, 150, 150, 150, 150, 150, 150, 150, 150, 150);
	$x->ColCaption = array("DATE", "NOM ET PRENOM", "DATE RECRUTEMENT", "DATE DE DEPART SOUHAITE", "DATE DEPART DERNIERRE ABSENCE", "DATE REPRISE", "Raison permission", "ADRESSE DURANT VOTRE ABSENCE", "NOMBRE DE JOURS", "PJ");
	$x->ColFieldName = array('date_demande', 'nom_prenom', 'recrutement', 'date_depart_souhaite', 'date_depart_dernier', 'date_reprise', 'raison_permission', 'adresse_abscence', 'nbre_jour', 'pj_conge');
	$x->ColNumber  = array(4, 5, 6, 7, 8, 9, 10, 11, 12, 13);

	// template paths below are based on the app main directory
	$x->Template = 'templates/demandes_templateTV.html';
	$x->SelectedTemplate = 'templates/demandes_templateTVS.html';
	$x->TemplateDV = 'templates/demandes_templateDV.html';
	$x->TemplateDVP = 'templates/demandes_templateDVP.html';

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

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

	// column sums
	if(strpos($x->HTML, '<!-- tv data below -->')){
		// if printing multi-selection TV, calculate the sum only for the selected records
		if(isset($_REQUEST['Print_x']) && is_array($_REQUEST['record_selector'])){
			$QueryWhere = '';
			foreach($_REQUEST['record_selector'] as $id){   // get selected records
				if($id != '') $QueryWhere .= "'" . makeSafe($id) . "',";
			}
			if($QueryWhere != ''){
				$QueryWhere = 'where `demandes`.`iddemande` in ('.substr($QueryWhere, 0, -1).')';
			}else{ // if no selected records, write the where clause to return an empty result
				$QueryWhere = 'where 1=0';
			}
		}else{
			$QueryWhere = $x->QueryWhere;
		}

		$sumQuery = "select sum(`demandes`.`nbre_jour`) from {$x->QueryFrom} {$QueryWhere}";
		$res = sql($sumQuery, $eo);
		if($row = db_fetch_row($res)){
			$sumRow = '<tr class="success">';
			if(!isset($_REQUEST['Print_x'])) $sumRow .= '<td class="text-center"><strong>&sum;</strong></td>';
			$sumRow .= '<td class="demandes-date_demande"></td>';
			$sumRow .= '<td class="demandes-nom_prenom"></td>';
			$sumRow .= '<td class="demandes-recrutement"></td>';
			$sumRow .= '<td class="demandes-date_depart_souhaite"></td>';
			$sumRow .= '<td class="demandes-date_depart_dernier"></td>';
			$sumRow .= '<td class="demandes-date_reprise"></td>';
			$sumRow .= '<td class="demandes-raison_permission"></td>';
			$sumRow .= '<td class="demandes-adresse_abscence"></td>';
			$sumRow .= "<td class=\"demandes-nbre_jour text-right\">{$row[0]}</td>";
			$sumRow .= '<td class="demandes-pj_conge"></td>';
			$sumRow .= '</tr>';

			$x->HTML = str_replace('<!-- tv data below -->', '', $x->HTML);
			$x->HTML = str_replace('<!-- tv data above -->', $sumRow, $x->HTML);
		}
	}

	// hook: demandes_header
	$headerCode='';
	if(function_exists('demandes_header')){
		$args=array();
		$headerCode=demandes_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_footer
	$footerCode='';
	if(function_exists('demandes_footer')){
		$args=array();
		$footerCode=demandes_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);
	}
?>