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

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

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(   
		"`courses`.`course_id`" => "course_id",
		"IF(    CHAR_LENGTH(`courses_catalog1`.`course_name`), CONCAT_WS('',   `courses_catalog1`.`course_name`, '  '), '') /* Course */" => "course_name",
		"IF(    CHAR_LENGTH(`courses_catalog1`.`course_summary`), CONCAT_WS('',   `courses_catalog1`.`course_summary`), '') /* Course Description */" => "description",
		"IF(    CHAR_LENGTH(`instructors1`.`inst_name`), CONCAT_WS('',   `instructors1`.`inst_name`), '') /* Instructor */" => "instructor_id",
		"IF(    CHAR_LENGTH(`labs1`.`lab_code`), CONCAT_WS('',   `labs1`.`lab_code`), '') /* Class */" => "lab_id",
		"if(`courses`.`start_date`,date_format(`courses`.`start_date`,'%d.%m.%Y'),'')" => "start_date",
		"if(`courses`.`end_date`,date_format(`courses`.`end_date`,'%d.%m.%Y'),'')" => "end_date",
		"TIME_FORMAT(`courses`.`start_time`, '%r')" => "start_time",
		"TIME_FORMAT(`courses`.`end_time`, '%r')" => "end_time",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`mon`, 'check', 'unchecked'), '\"></i>')" => "mon",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`tue`, 'check', 'unchecked'), '\"></i>')" => "tue",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`wed`, 'check', 'unchecked'), '\"></i>')" => "wed",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`thu`, 'check', 'unchecked'), '\"></i>')" => "thu",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`fri`, 'check', 'unchecked'), '\"></i>')" => "fri",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`sat`, 'check', 'unchecked'), '\"></i>')" => "sat",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`sun`, 'check', 'unchecked'), '\"></i>')" => "sun",
		"`courses`.`fees`" => "fees"
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(   
		1 => '`courses`.`course_id`',
		2 => 2,
		3 => '`courses_catalog1`.`course_summary`',
		4 => '`instructors1`.`inst_name`',
		5 => '`labs1`.`lab_code`',
		6 => '`courses`.`start_date`',
		7 => '`courses`.`end_date`',
		8 => '`courses`.`start_time`',
		9 => '`courses`.`end_time`',
		10 => 10,
		11 => 11,
		12 => 12,
		13 => 13,
		14 => 14,
		15 => 15,
		16 => 16,
		17 => '`courses`.`fees`'
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(   
		"`courses`.`course_id`" => "course_id",
		"IF(    CHAR_LENGTH(`courses_catalog1`.`course_name`), CONCAT_WS('',   `courses_catalog1`.`course_name`, '  '), '') /* Course */" => "course_name",
		"IF(    CHAR_LENGTH(`courses_catalog1`.`course_summary`), CONCAT_WS('',   `courses_catalog1`.`course_summary`), '') /* Course Description */" => "description",
		"IF(    CHAR_LENGTH(`instructors1`.`inst_name`), CONCAT_WS('',   `instructors1`.`inst_name`), '') /* Instructor */" => "instructor_id",
		"IF(    CHAR_LENGTH(`labs1`.`lab_code`), CONCAT_WS('',   `labs1`.`lab_code`), '') /* Class */" => "lab_id",
		"if(`courses`.`start_date`,date_format(`courses`.`start_date`,'%d.%m.%Y'),'')" => "start_date",
		"if(`courses`.`end_date`,date_format(`courses`.`end_date`,'%d.%m.%Y'),'')" => "end_date",
		"TIME_FORMAT(`courses`.`start_time`, '%r')" => "start_time",
		"TIME_FORMAT(`courses`.`end_time`, '%r')" => "end_time",
		"`courses`.`mon`" => "mon",
		"`courses`.`tue`" => "tue",
		"`courses`.`wed`" => "wed",
		"`courses`.`thu`" => "thu",
		"`courses`.`fri`" => "fri",
		"`courses`.`sat`" => "sat",
		"`courses`.`sun`" => "sun",
		"`courses`.`fees`" => "fees"
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(   
		"`courses`.`course_id`" => "Course ID",
		"IF(    CHAR_LENGTH(`courses_catalog1`.`course_name`), CONCAT_WS('',   `courses_catalog1`.`course_name`, '  '), '') /* Course */" => "Course",
		"IF(    CHAR_LENGTH(`courses_catalog1`.`course_summary`), CONCAT_WS('',   `courses_catalog1`.`course_summary`), '') /* Course Description */" => "Course Description",
		"IF(    CHAR_LENGTH(`instructors1`.`inst_name`), CONCAT_WS('',   `instructors1`.`inst_name`), '') /* Instructor */" => "Instructor",
		"IF(    CHAR_LENGTH(`labs1`.`lab_code`), CONCAT_WS('',   `labs1`.`lab_code`), '') /* Class */" => "Class",
		"`courses`.`start_date`" => "Start Date",
		"`courses`.`end_date`" => "End Date",
		"`courses`.`start_time`" => "Start Time",
		"`courses`.`end_time`" => "End Time",
		"`courses`.`mon`" => "Mon",
		"`courses`.`tue`" => "Tue",
		"`courses`.`wed`" => "Wed",
		"`courses`.`thu`" => "Thu",
		"`courses`.`fri`" => "Fri",
		"`courses`.`sat`" => "Sat",
		"`courses`.`sun`" => "Sun",
		"`courses`.`fees`" => "Fees"
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(   
		"`courses`.`course_id`" => "course_id",
		"IF(    CHAR_LENGTH(`courses_catalog1`.`course_name`), CONCAT_WS('',   `courses_catalog1`.`course_name`, '  '), '') /* Course */" => "course_name",
		"IF(    CHAR_LENGTH(`courses_catalog1`.`course_summary`), CONCAT_WS('',   `courses_catalog1`.`course_summary`), '') /* Course Description */" => "description",
		"IF(    CHAR_LENGTH(`instructors1`.`inst_name`), CONCAT_WS('',   `instructors1`.`inst_name`), '') /* Instructor */" => "instructor_id",
		"IF(    CHAR_LENGTH(`labs1`.`lab_code`), CONCAT_WS('',   `labs1`.`lab_code`), '') /* Class */" => "lab_id",
		"if(`courses`.`start_date`,date_format(`courses`.`start_date`,'%d.%m.%Y'),'')" => "start_date",
		"if(`courses`.`end_date`,date_format(`courses`.`end_date`,'%d.%m.%Y'),'')" => "end_date",
		"TIME_FORMAT(`courses`.`start_time`, '%r')" => "start_time",
		"TIME_FORMAT(`courses`.`end_time`, '%r')" => "end_time",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`mon`, 'check', 'unchecked'), '\"></i>')" => "mon",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`tue`, 'check', 'unchecked'), '\"></i>')" => "tue",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`wed`, 'check', 'unchecked'), '\"></i>')" => "wed",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`thu`, 'check', 'unchecked'), '\"></i>')" => "thu",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`fri`, 'check', 'unchecked'), '\"></i>')" => "fri",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`sat`, 'check', 'unchecked'), '\"></i>')" => "sat",
		"concat('<i class=\"glyphicon glyphicon-', if(`courses`.`sun`, 'check', 'unchecked'), '\"></i>')" => "sun",
		"`courses`.`fees`" => "fees"
	);

	// Lookup fields that can be used as filterers
	$x->filterers = array(  'course_name' => 'Course', 'instructor_id' => 'Instructor', 'lab_id' => 'Class');

	$x->QueryFrom = "`courses` LEFT JOIN `courses_catalog` as courses_catalog1 ON `courses_catalog1`.`cat_id`=`courses`.`course_name` LEFT JOIN `instructors` as instructors1 ON `instructors1`.`inst_id`=`courses`.`instructor_id` LEFT JOIN `labs` as labs1 ON `labs1`.`lab_id`=`courses`.`lab_id` ";
	$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 = 0;
	$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 = "courses_view.php";
	$x->RedirectAfterInsert = "courses_view.php";
	$x->TableTitle = "Courses";
	$x->TableIcon = "resources/table_icons/books.png";
	$x->PrimaryKey = "`courses`.`course_id`";
	$x->DefaultSortField = '1';
	$x->DefaultSortDirection = 'asc';

	$x->ColWidth   = array(  50, 200, 130, 50, 100, 100, 80, 80, 35, 35, 35, 35, 35, 35, 35, 45);
	$x->ColCaption = array("Course ID", "Course", "Instructor", "Class", "Start Date", "End Date", "Start Time", "End Time", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Fees");
	$x->ColFieldName = array('course_id', 'course_name', 'instructor_id', 'lab_id', 'start_date', 'end_date', 'start_time', 'end_time', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun', 'fees');
	$x->ColNumber  = array(1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17);

	// template paths below are based on the app main directory
	$x->Template = 'templates/courses_templateTV.html';
	$x->SelectedTemplate = 'templates/courses_templateTVS.html';
	$x->TemplateDV = 'templates/courses_templateDV.html';
	$x->TemplateDVP = 'templates/courses_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 `courses`.`course_id`=membership_userrecords.pkValue and membership_userrecords.tableName='courses' 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 `courses`.`course_id`=membership_userrecords.pkValue and membership_userrecords.tableName='courses' 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 = '`courses`';
		$x->QueryWhere = '';
		$x->DefaultSortField = '';
	}
	// hook: courses_init
	$render=TRUE;
	if(function_exists('courses_init')){
		$args=array();
		$render=courses_init($x, getMemberInfo(), $args);
	}

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

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