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

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

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(
		"`personnels`.`idpersonnel`" => "idpersonnel",
		"`personnels`.`matricule`" => "matricule",
		"if(`personnels`.`date_recrutement`,date_format(`personnels`.`date_recrutement`,'%d/%m/%Y'),'')" => "date_recrutement",
		"`personnels`.`nom`" => "nom",
		"`personnels`.`prenom`" => "prenom",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Nationalit&#233; */" => "nationalite",
		"`personnels`.`cin`" => "cin",
		"`personnels`.`adresse`" => "adresse",
		"`personnels`.`telephone`" => "telephone",
		"`personnels`.`whatsapp`" => "whatsapp",
		"`personnels`.`email`" => "email",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "agence",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "fonction",
		"IF(    CHAR_LENGTH(`produits_services1`.`produit_service`), CONCAT_WS('',   `produits_services1`.`produit_service`), '') /* Service */" => "service",
		"IF(    CHAR_LENGTH(`postes1`.`poste`), CONCAT_WS('',   `postes1`.`poste`), '') /* Poste */" => "poste",
		"`personnels`.`photo`" => "photo",
		"`personnels`.`cv`" => "cv",
		"`personnels`.`dossier`" => "dossier",
		"`personnels`.`observations`" => "observations",
		"`personnels`.`groupe`" => "groupe",
		"`personnels`.`agent`" => "agent",
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(
		1 => '`personnels`.`idpersonnel`',
		2 => 2,
		3 => '`personnels`.`date_recrutement`',
		4 => 4,
		5 => 5,
		6 => '`pays1`.`pays`',
		7 => 7,
		8 => 8,
		9 => 9,
		10 => 10,
		11 => 11,
		12 => '`agences1`.`agence`',
		13 => '`fonctions1`.`fonction`',
		14 => '`produits_services1`.`produit_service`',
		15 => '`postes1`.`poste`',
		16 => 16,
		17 => 17,
		18 => 18,
		19 => 19,
		20 => 20,
		21 => 21,
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(
		"`personnels`.`idpersonnel`" => "idpersonnel",
		"`personnels`.`matricule`" => "matricule",
		"if(`personnels`.`date_recrutement`,date_format(`personnels`.`date_recrutement`,'%d/%m/%Y'),'')" => "date_recrutement",
		"`personnels`.`nom`" => "nom",
		"`personnels`.`prenom`" => "prenom",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Nationalit&#233; */" => "nationalite",
		"`personnels`.`cin`" => "cin",
		"`personnels`.`adresse`" => "adresse",
		"`personnels`.`telephone`" => "telephone",
		"`personnels`.`whatsapp`" => "whatsapp",
		"`personnels`.`email`" => "email",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "agence",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "fonction",
		"IF(    CHAR_LENGTH(`produits_services1`.`produit_service`), CONCAT_WS('',   `produits_services1`.`produit_service`), '') /* Service */" => "service",
		"IF(    CHAR_LENGTH(`postes1`.`poste`), CONCAT_WS('',   `postes1`.`poste`), '') /* Poste */" => "poste",
		"`personnels`.`photo`" => "photo",
		"`personnels`.`cv`" => "cv",
		"`personnels`.`dossier`" => "dossier",
		"`personnels`.`observations`" => "observations",
		"`personnels`.`groupe`" => "groupe",
		"`personnels`.`agent`" => "agent",
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(
		"`personnels`.`idpersonnel`" => "ID",
		"`personnels`.`matricule`" => "Matricule",
		"`personnels`.`date_recrutement`" => "Date recrutement",
		"`personnels`.`nom`" => "Nom",
		"`personnels`.`prenom`" => "Prenom",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Nationalit&#233; */" => "Nationalit&#233;",
		"`personnels`.`cin`" => "N&#176; C.I.N",
		"`personnels`.`adresse`" => "Adresse",
		"`personnels`.`telephone`" => "Telephone",
		"`personnels`.`whatsapp`" => "Whatsapp",
		"`personnels`.`email`" => "Email",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "Agence",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "Fonction",
		"IF(    CHAR_LENGTH(`produits_services1`.`produit_service`), CONCAT_WS('',   `produits_services1`.`produit_service`), '') /* Service */" => "Service",
		"IF(    CHAR_LENGTH(`postes1`.`poste`), CONCAT_WS('',   `postes1`.`poste`), '') /* Poste */" => "Poste",
		"`personnels`.`cv`" => "Cv",
		"`personnels`.`dossier`" => "Dossier",
		"`personnels`.`observations`" => "Observations",
		"`personnels`.`groupe`" => "Groupe",
		"`personnels`.`agent`" => "Agent",
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(
		"`personnels`.`idpersonnel`" => "idpersonnel",
		"`personnels`.`matricule`" => "matricule",
		"if(`personnels`.`date_recrutement`,date_format(`personnels`.`date_recrutement`,'%d/%m/%Y'),'')" => "date_recrutement",
		"`personnels`.`nom`" => "nom",
		"`personnels`.`prenom`" => "prenom",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Nationalit&#233; */" => "nationalite",
		"`personnels`.`cin`" => "cin",
		"`personnels`.`adresse`" => "adresse",
		"`personnels`.`telephone`" => "telephone",
		"`personnels`.`whatsapp`" => "whatsapp",
		"`personnels`.`email`" => "email",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "agence",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "fonction",
		"IF(    CHAR_LENGTH(`produits_services1`.`produit_service`), CONCAT_WS('',   `produits_services1`.`produit_service`), '') /* Service */" => "service",
		"IF(    CHAR_LENGTH(`postes1`.`poste`), CONCAT_WS('',   `postes1`.`poste`), '') /* Poste */" => "poste",
		"`personnels`.`cv`" => "cv",
		"`personnels`.`dossier`" => "dossier",
		"`personnels`.`observations`" => "observations",
		"`personnels`.`groupe`" => "groupe",
		"`personnels`.`agent`" => "agent",
	);

	// Lookup fields that can be used as filterers
	$x->filterers = array('nationalite' => 'Nationalit&#233;', 'agence' => 'Agence', 'fonction' => 'Fonction', 'service' => 'Service', 'poste' => 'Poste', );

	$x->QueryFrom = "`personnels` LEFT JOIN `pays` as pays1 ON `pays1`.`idpays`=`personnels`.`nationalite` LEFT JOIN `agences` as agences1 ON `agences1`.`idagence`=`personnels`.`agence` LEFT JOIN `fonctions` as fonctions1 ON `fonctions1`.`idfonction`=`personnels`.`fonction` LEFT JOIN `produits_services` as produits_services1 ON `produits_services1`.`idps`=`personnels`.`service` LEFT JOIN `postes` as postes1 ON `postes1`.`idposte`=`personnels`.`poste` ";
	$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 = "personnels_view.php";
	$x->RedirectAfterInsert = "personnels_view.php?SelectedID=#ID#";
	$x->TableTitle = "GESTION DU PERSONNEL";
	$x->TableIcon = "resources/table_icons/client_account_template.png";
	$x->PrimaryKey = "`personnels`.`idpersonnel`";
	$x->DefaultSortField = '`personnels`.`date_recrutement`';
	$x->DefaultSortDirection = 'desc';

	$x->ColWidth   = array(  150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150);
	$x->ColCaption = array("Matricule", "Date recrutement", "Nom", "Prenom", "Nationalit&#233;", "N&#176; C.I.N", "Adresse", "Telephone", "Whatsapp", "Email", "Agence", "Fonction", "Service", "Poste", "Photo", "Groupe", "Agent");
	$x->ColFieldName = array('matricule', 'date_recrutement', 'nom', 'prenom', 'nationalite', 'cin', 'adresse', 'telephone', 'whatsapp', 'email', 'agence', 'fonction', 'service', 'poste', 'photo', 'groupe', 'agent');
	$x->ColNumber  = array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 21);

	// template paths below are based on the app main directory
	$x->Template = 'templates/personnels_templateTV.html';
	$x->SelectedTemplate = 'templates/personnels_templateTVS.html';
	$x->TemplateDV = 'templates/personnels_templateDV.html';
	$x->TemplateDVP = 'templates/personnels_templateDVP.html';

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

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

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

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