<?php

// Include the Companies House module...
require_once '../../../GovTalk.php';
require_once '../CompaniesHouse.php';
// Include the Companies House configuration
require_once 'config.php';
if (isset($_GET['companyname'])) {
    // Deal with form submission, do a CH search and print out a list...
    $companiesHouse = new CompaniesHouse($chUserId, $chPassword);
    if ($companyList = $companiesHouse->companyNameSearch($_GET['companyname'])) {
        // Exact match...
        if (is_array($companyList['exact'])) {
            echo 'Exact name match: ' . $companyList['exact']['name'] . ' (<a href="view-company-details.php?companynumber=' . $companyList['exact']['number'] . '">' . $companyList['exact']['number'] . '</a>)';
        }
        // Similar (including exact match)...
        echo '<ul>';
        foreach ($companyList['match'] as $company) {
            echo '<li>' . $company['name'] . ' (<a href="view-company-details.php?companynumber=' . $company['number'] . '">' . $company['number'] . '</a>)</li>';
        }
        echo '</ul>';
    } else {
        // No companies found / error occured...
        echo 'No companies found for \'' . $_GET['companyname'] . '\'.';
        var_dump($companiesHouse->getFullXMLResponse());
    }
} else {
    // First page visit, display the search box...
    ?>

<form action="" method="get">
// Include the Companies House configuration
require_once 'config.php';
if (isset($_GET['dockey'])) {
    $companiesHouse = new CompaniesHouse($chUserId, $chPassword);
    if ($documentList = $companiesHouse->documentInfo($_GET['companynumber'], $_GET['companyname'], $_GET['dockey'])) {
        if ($documentRequest = $companiesHouse->documentRequest($documentList['key'], 'Document request test')) {
            echo 'Document is available: <a href="' . $documentRequest['endpoint'] . '">here</a>.';
        } else {
            echo 'Unable to fetch document: document request failed.';
        }
    } else {
        echo 'Unable to fetch document: document info failed.';
    }
} else {
    if (isset($_GET['companyname']) && isset($_GET['companynumber'])) {
        $companiesHouse = new CompaniesHouse($chUserId, $chPassword);
        if ($documentList = $companiesHouse->filingHistory($_GET['companynumber'])) {
            ?>
<form action="" method="get">
	<input type="hidden" name="companyname" value="<?php 
            echo $_GET['companyname'];
            ?>
" />
	<input type="hidden" name="companynumber" value="<?php 
            echo $_GET['companynumber'];
            ?>
" />
	Select document: <select name="dockey">
<?php 
            foreach ($documentList as $document) {
                echo '<option value="' . $document['key'] . '">' . implode(', ', $document['description']) . '</option>';
<?php

// Include the Companies House module...
require_once '../../../GovTalk.php';
require_once '../CompaniesHouse.php';
// Include the Companies House configuration
require_once 'config.php';
if (isset($_GET['officersurname']) && isset($_GET['officerforename'])) {
    // Deal with form submission, do a CH search and print out a list...
    $companiesHouse = new CompaniesHouse($chUserId, $chPassword);
    if ($officerList = $companiesHouse->companyOfficerSearch($_GET['officersurname'], $_GET['officerforename'])) {
        // Nearest match...
        if (is_array($officerList['exact'])) {
            echo 'Nearest name match: ' . $officerList['exact']['title'] . ' ' . $officerList['exact']['forename'] . ' ' . $officerList['exact']['surname'] . ' (<a href="view-officer-details.php?id=' . urlencode($officerList['exact']['id']) . '">View details</a>)';
        }
        // Similar (including nearest match)...
        echo '<ul>';
        foreach ($officerList['match'] as $officer) {
            echo '<li>' . $officer['title'] . ' ' . $officer['forename'] . ' ' . $officer['surname'] . ' (<a href="view-officer-details.php?id=' . $officer['id'] . '">View</a>)</li>';
        }
        echo '</ul>';
    } else {
        // No officers found / error occured...
        echo 'No officers found for \'' . $_GET['officerforename'] . ' ' . $_GET['officersurname'] . '\'.';
    }
} else {
    // First page visit, display the search box...
    ?>

<form action="" method="get">
	Search for officer (forename / surname): <input name="officerforename" type="text" /> <input name="officersurname" type="text" /> <input type="submit" value="Search" />
<?php

// Include the Companies House module...
require_once '../../../GovTalk.php';
require_once '../CompaniesHouse.php';
// Include the Companies House configuration
require_once 'config.php';
if (isset($_GET['companynumber'])) {
    // Deal with form submission, do a CH request and print out information...
    $companiesHouse = new CompaniesHouse($chUserId, $chPassword);
    if ($companyDetails = $companiesHouse->companyDetails($_GET['companynumber'])) {
        echo 'Company name: ' . $companyDetails['name'] . '<br />';
        echo 'Company type: ' . $companyDetails['category'] . '<br />';
        echo 'Company status: ' . $companyDetails['status'] . '<br />';
        echo 'Company incorporation date: ' . date('jS F Y', $companyDetails['incorporation_date']) . '<br />';
        # etc.
    } else {
        // No companies found / error occured...
        echo 'No companies found for \'' . $_GET['companynumber'] . '\'.';
    }
} else {
    // First page visit, display the search box...
    ?>

<form action="" method="get">
	Lookup company: <input name="companynumber" type="text" /> <input type="submit" value="Lookup" />
</form>

<?php 
}
<?php

// Include the Companies House module...
require_once '../../../GovTalk.php';
require_once '../CompaniesHouse.php';
// Include the Companies House configuration
require_once 'config.php';
if (isset($_GET['id'])) {
    // Deal with form submission, do a CH request and print out information...
    $companiesHouse = new CompaniesHouse($chUserId, $chPassword);
    if ($officerDetails = $companiesHouse->officerDetails($_GET['id'], 'Testing')) {
        echo 'Officer name: ' . $officerDetails['forename'] . ' ' . $officerDetails['surname'] . '<br />';
        echo 'Officer DOB: ' . date('d-m-y', $officerDetails['dob']) . '<br />';
        foreach ($officerDetails['company'] as $company) {
            echo '<hr />';
            echo 'Company: ' . $company['name'] . ' (<a href="view-company-details.php?companynumber=' . $company['number'] . '">' . $company['number'] . '</a>)<br />';
            foreach ($company['appointment'] as $appointment) {
                echo 'Officer type: ' . $appointment['type'] . '<br />';
            }
        }
        # etc.
    } else {
        // No officer found / error occured...
        echo 'No officer found for \'' . $_GET['id'] . '\'.';
    }
} else {
    // First page visit, display the search box...
    ?>

<form action="" method="get">
	Lookup officer: <input name="id" type="text" /> <input type="submit" value="Lookup" />