<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

  include_once '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'constants.php';

  if (session_id() == '') {
    session_start();
  }
  if (!SessionOperations::getVarValueIntern('wizard_authorized', false)) {
    header('Location: ../index.php');
    die();
  }
    

    

  $dboperation = DBOperation::getInstance(1);

  
    $sTable = 'web_privileges_t_u wtu join web_privileges wp on wp.id = wtu.privileg_id ';

    $aColumns = array('wp.id', 'wp.keynumber', 'wp.description', 'wtu.accessnumber');
  $columns = array('id', 'keynumber', 'description', 'accessnumber');
  $columnsForWhere = array('wp.id', 'wp.keynumber', 'wp.description', 'wtu.accessnumber');

    
    $sIndexColumn = 'wp.id';


  $tool_user_id = -1;
  if (isset($_REQUEST['tool_user_id'])) {
    $tool_user_id = $_REQUEST['tool_user_id'];
  }

    

    
    $sLimit = -1;
    if (isset($_GET['iDisplayStart']) && $_GET['iDisplayLength'] != '-1') {
      $sLimit = 'LIMIT ' . $dboperation->dbparser->escape($_GET['iDisplayStart']) . ', '
            . $dboperation->dbparser->escape($_GET['iDisplayLength']);
    }


    
  $sOrder = '';
    if (isset($_GET['iSortCol_0'])) {
      $sOrder = 'ORDER BY  ';
      for ($i = 0; $i < intval($_GET['iSortingCols']); $i++) {
        if ($_GET['bSortable_' . intval($_GET['iSortCol_' . $i])] == 'true') {
          $sOrder .= $columnsForWhere[intval($_GET['iSortCol_' . $i])] . '
				 	' . $dboperation->dbparser->escape($_GET['sSortDir_' . $i]) . ', ';
        }
      }

      $sOrder = substr_replace($sOrder, '', -2);
      if ($sOrder == 'ORDER BY') {
        $sOrder = '';
      }
    }


    
    $sWhere = '';
    if (isset($_GET['sSearch']) && $_GET['sSearch'] != '') {
      $sWhere = 'WHERE (';
      for ($i = 0; $i < count($columnsForWhere); $i++) {
        $sWhere .= $columnsForWhere[$i] . " LIKE '%" . $dboperation->dbparser->escape($_GET['sSearch']) . "%' OR ";
      }
      $sWhere = substr_replace($sWhere, '', -3);
      $sWhere .= ')';
    }

    
    for ($i = 0; $i < count($columnsForWhere); $i++) {
      if (isset($_GET['bSearchable_' . $i]) && ($_GET['bSearchable_' . $i] == 'true' && $_GET['sSearch_' . $i] != '')) {
        if ($sWhere == '') {
          $sWhere = 'WHERE ';
        } else {
          $sWhere .= ' AND ';
        }
        $sWhere .= $columnsForWhere[$i] . " LIKE '%" . $dboperation->dbparser->escape($_GET['sSearch_' . $i]) . "%' ";
      }
    }

  
  if ($sWhere == '') {
    $sWhere = 'WHERE wtu.tool_user_id = ' . $tool_user_id;
  } else {
    $sWhere .= ' AND wtu.tool_user_id = ' . $tool_user_id;
  }

    
    $sQuery1 = '
		SELECT ' . str_replace(' , ', ' ', implode(', ', $aColumns)) . "
		FROM   $sTable
		$sWhere
		$sOrder
		$sLimit
	";

    

    
    $sQuery2 = "
		SELECT count(*) as count
		FROM   $sTable
		$sWhere
		$sOrder
	";

  $dboperation->clear();
  $dboperation->query($sQuery2);
  $dboperation->selectrow(0);

    $aResultFilterTotal = $dboperation->fieldbyname('count');
  if ($aResultFilterTotal > 0) {
    $iFilteredTotal = $aResultFilterTotal;
  } else {
    $iFilteredTotal = 0;
  }

    
    $sQuery3 = '
		SELECT COUNT(' . $sIndexColumn . ") as count
		FROM   $sTable
	";

  $dboperation->clear();
  $dboperation->query($sQuery3);
  $dboperation->selectrow(0);

    $aResultTotal = $dboperation->fieldbyname('count');
  if ($aResultTotal > 0) {
    $iTotal = $aResultTotal;
  } else {
    $iTotal = 0;
  }



    
  if (isset($_GET['sEcho'])) {
    $echo = $_GET['sEcho'];
  } else {
    $echo = '';
  }

    $output = array(
        'sEcho' => intval($echo),
        'iTotalRecords' => $iTotal,
        'iTotalDisplayRecords' => $iFilteredTotal,
        'aaData' => array()
    );

  $dboperation->clear();
  $dboperation->query($sQuery1);
  $rowCount = $dboperation->rowcount();

    for ($index = 0; $index < $rowCount; ++$index) {
      $dboperation->selectrow($index);
      $aRow = $dboperation->fetchArray($columns, true);
      $row = array();

      for ($i = 0; $i < count($columns); $i++) {
        if ($columns[$i] != ' ') {
          
          $row[] = StringOperations::utf8_encode($aRow[$columns[$i]]);
        }
      }
      $row['DT_RowId'] = 'row_' . $aRow['id'];
      $row['DT_RowClass'] = 'gradeA';
      $output['aaData'][] = $row;
    }



    echo json_encode($output);

  if (json_last_error() !== 0) {
    Logger::logg('json_last_error() = ' . json_last_error(), 80);
  }