<?php

//Assigns the form variables to upload.
$results = allData($_SESSION['id']);
if (isPostRequest()) {
    if (filter_input(INPUT_POST, 'address_group_id') != null) {
        $selectedGroup = filter_input(INPUT_POST, 'address_group_id');
        $results = groupData($_SESSION['id'], $selectedGroup);
    } elseif (filter_input(INPUT_POST, 'searchq') != null) {
        $searchq = filter_input(INPUT_POST, 'searchq');
        $selectedCol = filter_input(INPUT_POST, 'col');
        $results = searchData($_SESSION['id'], $searchq, $selectedCol);
    }
} else {
}
use Symfony\Component\Console\Input\ArgvInput;
$defaultDataSrc = __DIR__ . '/../apidoc/api_data.json';
try {
    $inputDefinition = new InputDefinition(array(new InputOption('data-src', 's', InputOption::VALUE_OPTIONAL, 'Location of apidoc data.json file', $defaultDataSrc), new InputOption('data-target', 't', InputOption::VALUE_OPTIONAL, 'Where to write json output', null)));
    $input = new ArgvInput($argv, $inputDefinition);
    $dataSrc = $input->getOption('data-src');
    $dataTarget = $input->getOption('data-target');
    $targetStream = $dataTarget === null ? 'php://stdout' : 'file://' . $dataTarget;
    $targetHandle = fopen($targetStream, 'r+');
    if ($targetHandle === false) {
        throw new RuntimeException("Could not write to '{$targetStream}'");
    }
    $data = loadApiDocData($dataSrc);
    $version = findLatestVersion($data);
    $latestData = extractVersionData($data, $version);
    $groupedData = groupData($latestData);
    $outData = $groupedData;
    $outJson = json_encode($outData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
    fwrite($targetHandle, $outJson);
    fclose($targetHandle);
    echo PHP_EOL;
} catch (Exception $e) {
    echo $e;
    exit(1);
}
function loadApiDocData($file)
{
    if (file_exists($file) === false) {
        throw new InvalidArgumentException('Could not find data src file: ' . $file);
    }
    $json = file_get_contents($file);