<?php

// Perform Login
$SHORTCODE_STATUS = PHPCUS_SHORTCODE::CUS_QUICK_LOGIN();

// Check Array Input
PHPCUS_PARAM::CUS_DOES_NOT_SUPPORT_ARRAY();

// Check already authenticated
PHPCUS_PARAM::CUS_REQUIRES_AUTHENTICATION();
// Check access rights
PHPCUS_PARAM::CUS_HAS_PERMISSION(PHPCUS_SYSTEM::AUTHORITY_VIEW_ONLY, 'account_linksvr');

if
(
    PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
    (
        [
            'PARAM_ACC_LINKSVR_ID',
            'PARAM_ACC_LINKSVR_ACC_GROUP_ID',
            'PARAM_ACC_LINKSVR_BRANCH_ID',
            'PARAM_ACC_LINKSVR_DESCRIPTION',
            'PARAM_ACC_LINKSVR_USERNAME',
            'PARAM_ACC_LINKSVR_PASSWORD',
            'PARAM_ACC_LINKSVR_HASH_KEY',
            'PARAM_ACC_LINKSVR_PUBLIC_KEY',
            'PARAM_ACC_LINKSVR_URL',
            'PARAM_ACC_LINKSVR_AUTH_LEVEL',
            'PARAM_ACC_LINKSVR_IS_SELF',
            'PARAM_ACC_LINKSVR_STATUS',
            
            'PARAM_ORDER_FIELD',
            'PARAM_ORDER_TYPE',
            'PARAM_SKIP',
            'PARAM_LIMIT'
        ], True
        )
    )
{
    Try
    {
        // Read params
        // ------------------------------------------------------------------
        $PARAM_ACC_LINKSVR_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_ACC_LINKSVR_ID');
        $PARAM_ACC_LINKSVR_ACC_GROUP_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_ACC_LINKSVR_ACC_GROUP_ID');
        $PARAM_ACC_LINKSVR_BRANCH_ID = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_BRANCH_ID');
        $PARAM_ACC_LINKSVR_DESCRIPTION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_DESCRIPTION');
        $PARAM_ACC_LINKSVR_USERNAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_USERNAME');
        $PARAM_ACC_LINKSVR_PASSWORD = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_PASSWORD');
        $PARAM_ACC_LINKSVR_HASH_KEY = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_HASH_KEY');
        $PARAM_ACC_LINKSVR_PUBLIC_KEY = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_PUBLIC_KEY');
        $PARAM_ACC_LINKSVR_URL = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_LINKSVR_URL');
        $PARAM_ACC_LINKSVR_AUTH_LEVEL = PHPCUS_PARAM::CUS_READ_INT('PARAM_ACC_LINKSVR_AUTH_LEVEL');
        $PARAM_ACC_LINKSVR_IS_SELF = PHPCUS_PARAM::CUS_READ_INT('PARAM_ACC_LINKSVR_IS_SELF');
        $PARAM_ACC_LINKSVR_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_ACC_LINKSVR_STATUS');
        
        $PARAM_ORDER_FIELD = PHPCUS_PARAM::CUS_READ_SORT_FIELD
        (
            'PARAM_ORDER_FIELD',
            [
                'ACC_LINKSVR_ID',
                'ACC_LINKSVR_ACC_GROUP_ID',
                'ACC_LINKSVR_BRANCH_ID',
                'ACC_LINKSVR_DESCRIPTION',
                'ACC_LINKSVR_USERNAME',
                'ACC_LINKSVR_PASSWORD',
                'ACC_LINKSVR_HASH_KEY',
                'ACC_LINKSVR_PUBLIC_KEY',
                'ACC_LINKSVR_URL',
                'ACC_LINKSVR_AUTH_LEVEL',
                'ACC_LINKSVR_IS_SELF',
                'ACC_LINKSVR_STATUS'
                ,
                'ACC_GROUP_ID',
                'ACC_GROUP_DESCRIPTION',
                'ACC_GROUP_STATUS',
                'ACC_GROUP_SYSTEM_GENERATED'
            ]
            );
        $PARAM_ORDER_TYPE = PHPCUS_PARAM::CUS_READ_SORT_TYPE('PARAM_ORDER_TYPE');
        $PARAM_SKIP	= PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_SKIP');
        $PARAM_LIMIT = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_LIMIT');
        
        // ------------------------------------------------------------------
        
        
        // Populate Map Array
        // ...........................................
        $GET_MAP_ARRAY = PHPCUS_ACCOUNT_LINKSVR::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
        
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_ID'] = $PARAM_ACC_LINKSVR_ID;
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_ACC_GROUP_ID'] = $PARAM_ACC_LINKSVR_ACC_GROUP_ID;
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_BRANCH_ID'] = $PARAM_ACC_LINKSVR_BRANCH_ID;
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_DESCRIPTION'] = $PARAM_ACC_LINKSVR_DESCRIPTION;
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_USERNAME'] = $PARAM_ACC_LINKSVR_USERNAME;
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_PASSWORD'] = $PARAM_ACC_LINKSVR_PASSWORD;
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_HASH_KEY'] = $PARAM_ACC_LINKSVR_HASH_KEY;
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_PUBLIC_KEY'] = $PARAM_ACC_LINKSVR_PUBLIC_KEY;
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_URL'] = $PARAM_ACC_LINKSVR_URL;
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_AUTH_LEVEL'] = $PARAM_ACC_LINKSVR_AUTH_LEVEL;
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_IS_SELF'] = $PARAM_ACC_LINKSVR_IS_SELF;
        $GET_MAP_ARRAY['PARAM_ACC_LINKSVR_STATUS'] = $PARAM_ACC_LINKSVR_STATUS;
        
        $GET_MAP_ARRAY["PARAM_ORDER_FIELD"] = $PARAM_ORDER_FIELD;
        $GET_MAP_ARRAY["PARAM_ORDER_TYPE"] = $PARAM_ORDER_TYPE;
        $GET_MAP_ARRAY["PARAM_SKIP"] = $PARAM_SKIP;
        $GET_MAP_ARRAY["PARAM_LIMIT"] = $PARAM_LIMIT;
        
        
        // Execute Stored Procedure
        // ...........................................
        $DATA_SET = PHPCUS_ACCOUNT_LINKSVR::CUS_GET_MA($GET_MAP_ARRAY);
        
        
        // Return JSON response
        // ...........................................
        if(empty($DATA_SET) === False)
        {
            PHPCUS_PARAM::CUS_JSON_SUCCESS($DATA_SET);
        }
        else
        {
            PHPCUS_PARAM::CUS_JSON_SUCCESS('');
        }
        
    }
    Catch(Exception $ERR)
    {
        PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
        if(isset($_SERVER['HTTP_ORIGIN']))
        {
            PHPVC_LOG::VAR_ERROR('HTTP_ORIGIN', $_SERVER['HTTP_ORIGIN']);
        }
        if(isset($_SERVER['HTTP_HOST']))
        {
            PHPVC_LOG::VAR_ERROR('HTTP_HOST', $_SERVER['HTTP_HOST']);
        }
        if(isset($_SERVER['HTTP_REFERER']))
        {
            PHPVC_LOG::VAR_ERROR('HTTP_REFERER', $_SERVER['HTTP_REFERER']);
        }
        if(isset($_SERVER['REMOTE_ADDR']))
        {
            PHPVC_LOG::VAR_ERROR('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
        }
        
        PHPCUS_PARAM::CUS_JSON_ERROR($ERR->getMessage());
    }
}
else
{
    PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
}

?>