<?php
 
/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simply to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */
 
include_once ("includes/setup.inc.php");
//*****************************************************
//Get all POST and GET value
//*****************************************************
$sqlOnly = fetch_postget_val('sqlOnly',$_POST, $_GET);
$peranan = fetch_postget_val('peranan',$_POST, $_GET);
$aktif = fetch_postget_val('aktif',$_POST, $_GET);
$jabatan = fetch_postget_val('jabatan',$_POST, $_GET);

//*****************************************************
// MAIN TABLE AND JOIN TABLE
// main = main table name in the database,
// sub['name'] = join table name
// sub['sub_id'] = join table identifier
// sub['main_id'] = main table identifier
// sub['join_type'] = type of join. Default = 'LEFT'
// sub['aka'] = put alternative name for table
//*****************************************************
$tableArray = array(
	'main' => 't_pengguna',
	'primary_key' => 'id_pengguna',
	'sub' => array(
		array('name' => 't_peranan', 'sub_id' => 'id_peranan', 'main_id' => 'id_peranan'),
		array('name' => 'i_aktif', 'sub_id' => 'id_aktif', 'main_id' => 'aktif'),
	),
);

//*****************************************************
// DATATABLE COLUMN
// db = column name in the database,
// dt = DataTables column identifier
// table = database table name
// akaFrom = use if and only if there is aka name for table
//*****************************************************
$columns = array(
    array( 'user_defined' => '', 'dt' => 0),
    array( 'db' => 'nama', 'dt' => 1, 'table' => 't_pengguna' ),
    array( 'db' => 'kpbaru', 'dt' => 2, 'table' => 't_pengguna' ),
    array( 'db' => 'emel', 'dt' => 3, 'table' => 't_pengguna' ),
	array( 'db' => 'desk_peranan', 'dt' => 4, 'table' => 't_peranan' ),
	array( 'db' => 'desk_aktif', 'dt' => 5, 'table' => 'i_aktif' ),
	array( 'db' => 'masaakhirlogin', 'dt' => 6, 'table' => 't_pengguna' ),
	array( 'dt' => 7, 
	'user_defined' => 
	'<a class="btn btn-sm btn-clean btn-icon mr-2" onclick="loadMyModal(\'in_pengguna-tambah.php?id=%t_pengguna.id_pengguna%&readonly=1\', \'#ajax-modal\')" title="Papar">
		<span class="svg-icon svg-icon-primary svg-icon-2x">'.$File_svg.'</span>
	</a>
	<a class="btn btn-sm btn-clean btn-icon mr-2" onclick="loadMyModal(\'in_pengguna-tambah.php?id=%t_pengguna.id_pengguna%\', \'#ajax-modal\')" title="Edit">
		<span class="svg-icon svg-icon-primary svg-icon-2x">'.$Edit_svg.'</span>
	</a>
	<a class="btn btn-sm btn-clean btn-icon mr-2" id="resetBtn" title="Reset" onclick="resetFunction(%t_pengguna.id_pengguna%, \'%t_pengguna.kpbaru%\')">
		<span class="svg-icon svg-icon-primary svg-icon-2x">'.$Update_svg.'</span>
	</a>
	<a class="btn btn-sm btn-clean btn-icon mr-2" onclick="loadMyModal(\'in_pengguna-upload.php?id=%t_pengguna.id_pengguna%\', \'#ajax-modal\')" title="Muat Naik Dokumen">
		<span class="svg-icon svg-icon-primary svg-icon-2x">'.$upload_svg.'</span>
	</a>
	<!--<a class="btn btn-sm btn-clean btn-icon mr-2" id="deleteBtn" title="Aktif/Tidak Aktif" onclick="deleteFunction(%t_pengguna.id_pengguna%, \'%t_pengguna.nama%\')">
		<span class="svg-icon svg-icon-danger svg-icon-2x">'.$Activate_svg.'</span>
	</a>-->'
	),
    array( 'db' => 'id_pengguna', 'dt' => 8, 'table' => 't_pengguna' ),
);

//*****************************************************
// WHERE SQL
// Array ($table_name, $column_name, $value, $symbol(default : '='))
//*****************************************************
if($my_details->user_level == 1 || $my_details->user_level == 3)
{
	$whereArray = array(
		//array('t_pengguna', 'aktif', 1),
	);
}
else if($my_details->user_level == 2)
{
	$whereArray[] = array('t_pengguna', 'id_peranan', 1, '!=');
	$whereArray[] = array('t_pengguna', 'id_peranan', 3, '!=');
}
else
{
	$whereArray[] = array('t_pengguna', 'id_peranan', 1, '!=');
	$whereArray[] = array('t_pengguna', 'id_peranan', 2, '!=');
	$whereArray[] = array('t_pengguna', 'id_peranan', 3, '!=');
}

if($peranan > 0)
{
	$whereArray[] = array('t_pengguna', 'id_peranan', $peranan);
}

if($aktif > 0)
{
	$whereArray[] = array('t_pengguna', 'aktif', $aktif);
}

if($jabatan > 0)
{
	$whereArray[] = array('t_pengguna', 'id_jabatan', $jabatan);
}
//echo SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), buildFromQuery($tableArray), $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 1, null);
//echo "whereArray = ".count($whereArray);
//exit(0);
//*****************************************************
// SSP GET JSON
// No need to modify below
//*****************************************************
if($sqlOnly == 1)
{
	//LEAVE EMPTY FOR FUTURE USAGE
}
else
{
	$temp_result = SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), buildFromQuery($tableArray), $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 0, buildWhereQuery($whereArray));
	for($num = 0; $num < count($temp_result['data']); $num++)
	{
		$start_index = 8;
		$id_pengguna = $temp_result['data'][$num][$start_index];			
		
		$pathpengguna = 'upload_pengguna/'.$id_pengguna.'/*';
		$imagespengguna = glob($pathpengguna);
		if(count($imagespengguna) > 0)
		{
			for($numk = 0; $numk < count($imagespengguna); $numk++)
			{
				$temp_result['data'][$num][3] = $temp_result['data'][$num][3].'<br><a href="'.$imagespengguna[$numk].'" target="_blank">'.end(explode('/', $imagespengguna[$numk])).'</a>';
			}
		}
	}
	echo json_encode(
		$temp_result
	);
	
	/*echo json_encode(
		SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), buildFromQuery($tableArray), $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 0, buildWhereQuery($whereArray))
	);*/
}