<?php
use MGModule\DNSManager2\mgLibs\custom\dns\utils as main;

/**
 * This function is invoked to get DNS Records to display them in DNS Management section
 * @param Array $params Collection of WHMCS parameters
 * @return Array Array with error key is returned in case of error. With success key otherwise
 */
if(!function_exists('%module_name%_GetDNS'))
{
    function %module_name%_GetDNS($params)
    {
        $domainId = null;
        if(isset($params['domainid'])) 
        {
            $domainId = $params['domainid'];
        }

        if(empty($domainId))
        {
            $id = $_GET["id"];
            if (empty($id)) 
            {
                $id = $_GET['domainid'];
            }
            $domainId = $id;
        }

        try 
        {
            $registrarHelper = new main\CustomDnsRegistrarHelper($domainId);
            $dns = $registrarHelper->getDNSRecords($domainId);
        } 
        catch(Exception $e) 
        {
            return array('error' => $e->getMessage());
        }

        return $dns;
    }
}

/**
 * This function is invoked when user tries to save DNS Records form DNS Management section
 * @param Array $params Collection of WHMCS parameters
 * @return Array Array with error key is returned in case of error. With success key otherwise
 */
if(!function_exists('%module_name%_SaveDNS'))
{
    function %module_name%_SaveDNS($params)
    {
        $domainId = null;
        if(isset($params['domainid']))
        {
            $domainId = $params['domainid'];
        }

        if(empty($domainId))
        {
            $id = $_GET["id"];
            if(empty($id))
            {
                $id = $_GET['domainid'];
            }
            $domainId = $id;
        }
        try
        {
            $registrarHelper = new main\CustomDnsRegistrarHelper($domainId);
            $registrarHelper->saveDNSRecords();
        } 
        catch(Exception $ex)
        {
            return array(
                'error' => $ex->getMessage()
            );
        }

        return array("success" => true);
    }
}
