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

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

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(
		"`rapports_activite`.`idrapport`" => "idrapport",
		"if(`rapports_activite`.`date`,date_format(`rapports_activite`.`date`,'%d/%m/%Y'),'')" => "date",
		"`rapports_activite`.`nom_prenom`" => "nom_prenom",
		"IF(    CHAR_LENGTH(`objets1`.`objet`), CONCAT_WS('',   `objets1`.`objet`), '') /* Objet */" => "objet",
		"IF(    CHAR_LENGTH(`type_rapport1`.`type_rapport`), CONCAT_WS('',   `type_rapport1`.`type_rapport`), '') /* Type rapport */" => "type_rapport",
		"`rapports_activite`.`rapport`" => "rapport",
		"`rapports_activite`.`obs`" => "obs",
		"`rapports_activite`.`groupe`" => "groupe",
		"`rapports_activite`.`agent`" => "agent",
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(
		1 => '`rapports_activite`.`idrapport`',
		2 => '`rapports_activite`.`date`',
		3 => 3,
		4 => '`objets1`.`objet`',
		5 => '`type_rapport1`.`type_rapport`',
		6 => 6,
		7 => 7,
		8 => 8,
		9 => 9,
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(
		"`rapports_activite`.`idrapport`" => "idrapport",
		"if(`rapports_activite`.`date`,date_format(`rapports_activite`.`date`,'%d/%m/%Y'),'')" => "date",
		"`rapports_activite`.`nom_prenom`" => "nom_prenom",
		"IF(    CHAR_LENGTH(`objets1`.`objet`), CONCAT_WS('',   `objets1`.`objet`), '') /* Objet */" => "objet",
		"IF(    CHAR_LENGTH(`type_rapport1`.`type_rapport`), CONCAT_WS('',   `type_rapport1`.`type_rapport`), '') /* Type rapport */" => "type_rapport",
		"`rapports_activite`.`rapport`" => "rapport",
		"`rapports_activite`.`obs`" => "obs",
		"`rapports_activite`.`groupe`" => "groupe",
		"`rapports_activite`.`agent`" => "agent",
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(
		"`rapports_activite`.`idrapport`" => "ID",
		"`rapports_activite`.`date`" => "Date",
		"`rapports_activite`.`nom_prenom`" => "Agent",
		"IF(    CHAR_LENGTH(`objets1`.`objet`), CONCAT_WS('',   `objets1`.`objet`), '') /* Objet */" => "Objet",
		"IF(    CHAR_LENGTH(`type_rapport1`.`type_rapport`), CONCAT_WS('',   `type_rapport1`.`type_rapport`), '') /* Type rapport */" => "Type rapport",
		"`rapports_activite`.`rapport`" => "Rapport",
		"`rapports_activite`.`obs`" => "Obs",
		"`rapports_activite`.`groupe`" => "Groupe",
		"`rapports_activite`.`agent`" => "Agent",
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(
		"`rapports_activite`.`idrapport`" => "idrapport",
		"if(`rapports_activite`.`date`,date_format(`rapports_activite`.`date`,'%d/%m/%Y'),'')" => "date",
		"`rapports_activite`.`nom_prenom`" => "nom_prenom",
		"IF(    CHAR_LENGTH(`objets1`.`objet`), CONCAT_WS('',   `objets1`.`objet`), '') /* Objet */" => "objet",
		"IF(    CHAR_LENGTH(`type_rapport1`.`type_rapport`), CONCAT_WS('',   `type_rapport1`.`type_rapport`), '') /* Type rapport */" => "type_rapport",
		"`rapports_activite`.`rapport`" => "rapport",
		"`rapports_activite`.`obs`" => "obs",
		"`rapports_activite`.`groupe`" => "groupe",
		"`rapports_activite`.`agent`" => "agent",
	);

	// Lookup fields that can be used as filterers
	$x->filterers = array('objet' => 'Objet', 'type_rapport' => 'Type rapport', );

	$x->QueryFrom = "`rapports_activite` LEFT JOIN `objets` as objets1 ON `objets1`.`idobjet`=`rapports_activite`.`objet` LEFT JOIN `type_rapport` as type_rapport1 ON `type_rapport1`.`idtrpt`=`rapports_activite`.`type_rapport` ";
	$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 = "rapports_activite_view.php";
	$x->RedirectAfterInsert = "rapports_activite_view.php?SelectedID=#ID#";
	$x->TableTitle = "RAPPORT D'ACTIVIT&#201;";
	$x->TableIcon = "resources/table_icons/document_prepare.png";
	$x->PrimaryKey = "`rapports_activite`.`idrapport`";
	$x->DefaultSortField = '`rapports_activite`.`date`';
	$x->DefaultSortDirection = 'asc';

	$x->ColWidth   = array(  150, 150, 150, 150, 150, 150, 150);
	$x->ColCaption = array("Date", "Agent", "Objet", "Type rapport", "Rapport", "Groupe", "Agent");
	$x->ColFieldName = array('date', 'nom_prenom', 'objet', 'type_rapport', 'rapport', 'groupe', 'agent');
	$x->ColNumber  = array(2, 3, 4, 5, 6, 8, 9);

	// template paths below are based on the app main directory
	$x->Template = 'templates/rapports_activite_templateTV.html';
	$x->SelectedTemplate = 'templates/rapports_activite_templateTVS.html';
	$x->TemplateDV = 'templates/rapports_activite_templateDV.html';
	$x->TemplateDVP = 'templates/rapports_activite_templateDVP.html';

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

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

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