<?php

/**
 * This example requests access to the datacenter
 *
 * @copyright Copyright 2011 TransIP BV
 * @author TransIP BV <*****@*****.**>
 */
require_once 'Transip/ColocationService.php';
if (isset($_POST['request']) && count($_POST['request']) > 0) {
    extract($_POST['request']);
    try {
        // filter out empty input fields
        $visitor = array_filter($visitor, 'strlen');
        $dataCenterVisitors = Transip_ColocationService::requestAccess($when, $duration, $visitor, $phoneNumber);
        $result = '';
        foreach ($dataCenterVisitors as $dataCenterVisitor) {
            if ($dataCenterVisitor->hasBeenRegisteredBefore) {
                $result .= "{$dataCenterVisitor->name} (no code needed)\n";
            } else {
                $result .= "{$dataCenterVisitor->name}";
                $result .= " (reservation number: {$dataCenterVisitor->reservationNumber};";
                $result .= " access code: {$dataCenterVisitor->accessCode})\n";
            }
        }
    } catch (SoapFault $e) {
        // It is possible that an error occurs when connecting to the TransIP Soap API,
        // those errors will be thrown as a SoapFault exception.
        $result = 'An error occurred: ' . htmlspecialchars($e->getMessage());
    }
}