/**
 * Parses options provided for script
 * @param array $options
 * @return array
 * @throws Exception If any mandatory field is missing, an exception will be thrown
 */
function parseOptions(array $options) {
    $clusterIdentifier = $options['cluster_identifier'];
    if (empty($clusterIdentifier)) {
        throw new Exception('You have to provide cluster identifier');
    }

    $outputFile = $options['output_file'];
    if (empty($outputFile)) {
        throw new Exception('You have to provide output file');
    }

    $fields = !empty($options['fields']) ? $options['fields'] : getDefaultFields();
    $fields = parseFields($fields);

    $header = isset($options['header']) ? filter_var($options['header'], FILTER_VALIDATE_BOOLEAN) : true;

    $delimiter = isset($options['delimiter']) ? $options['delimiter'] : DEFAULT_CSV_DELIMITER;
    $enclosure = isset($options['enclosure']) ? $options['enclosure'] : DEFAULT_CSV_ENCLOSURE;

    return array(
        'cluster_identifier' => $clusterIdentifier,
        'output_file' => $outputFile,
        'fields' => $fields['parsed'],
        'fields_array' => $fields['array'],
        'application_identifier' => $options['application_identifier'],
        'header' => $header,
        'delimiter' => $delimiter,
        'enclosure' => $enclosure,
    );
}
function fieldSave()
{
    G::LoadSystem('inputfilter');
    $filter = new InputFilter();
    global $conf;
    global $action;
    $arrayField = getDefaultFields($action, 0);
    $arrayConfig = getDefaultConfig($action, 0);
    //Get values from JSON request
    $first = G::json_decode(isset($_POST["first"]) ? $_POST["first"] : G::json_encode(array()));
    $first = $filter->xssFilterHard($first);
    $second = G::json_decode(isset($_POST["second"]) ? $_POST["second"] : G::json_encode(array()));
    $pmtable = isset($_POST["pmtable"]) ? $_POST["pmtable"] : "";
    $pmtable = $filter->xssFilterHard($pmtable);
    $rowsperpage = isset($_POST["rowsperpage"]) ? $_POST["rowsperpage"] : $arrayConfig["rowsperpage"];
    $rowsperpage = $filter->xssFilterHard($rowsperpage);
    $dateformat = isset($_POST["dateformat"]) && !empty($_POST["dateformat"]) ? $_POST["dateformat"] : $arrayConfig["dateformat"];
    $dateformat = $filter->xssFilterHard($dateformat);
    //Adding the key fields to second array
    //Required fields for AppCacheView.php - addPMFieldsToCriteria()
    $appUid = new stdClass();
    $appUid->name = "APP_UID";
    $delIndex = new stdClass();
    $delIndex->name = "DEL_INDEX";
    $usrUid = new stdClass();
    $usrUid->name = "USR_UID";
    $previousUsrUid = new stdClass();
    $previousUsrUid->name = "PREVIOUS_USR_UID";
    array_unshift($second, $previousUsrUid);
    array_unshift($second, $usrUid);
    array_unshift($second, $delIndex);
    array_unshift($second, $appUid);
    $arrayNewFirst = setAvailableFields($first, $arrayField);
    $arrayNewSecond = setCasesListFields($second, $arrayField);
    $result = genericJsonResponse($pmtable, $arrayNewFirst, $arrayNewSecond, $rowsperpage, $dateformat);
    $conf->saveObject($result, "casesList", $action, "", "", "");
    $msgLog = '';
    if ($action == 'todo') {
        $list = 'Inbox';
    } elseif ($action == 'sent') {
        $list = 'Participated';
    } else {
        $list = ucwords($action);
    }
    for ($i = 4; $i < count($arrayNewSecond); $i++) {
        if ($i == count($arrayNewSecond) - 1) {
            $msgLog .= $arrayNewSecond[$i]['label'];
        } else {
            $msgLog .= $arrayNewSecond[$i]['label'] . '-';
        }
    }
    G::auditLog("SetColumns", "Set " . $list . " List Columns" . $msgLog);
    echo G::json_encode($result);
}