/**
 * Creates an embedded signing experience.
 */
function createAndSend()
{
    global $_oneSigner;
    $status = "";
    // Construct basic envelope
    $env = new Envelope();
    $env->Subject = "DocuSign API SDK Sample";
    $env->EmailBlurb = "This envelope demonstrates embedded signing";
    $env->AccountId = $_SESSION["AccountID"];
    $env->Recipients = constructRecipients($_oneSigner);
    $doc = new Document();
    $doc->PDFBytes = file_get_contents("resources/Docusign_Demo_11.pdf");
    $doc->Name = "Demo Document";
    $doc->ID = "1";
    $doc->FileExtension = "pdf";
    $env->Documents = array($doc);
    $env->Tabs = addTabs(count($env->Recipients));
    $api = getAPI();
    try {
        $csParams = new CreateAndSendEnvelope();
        $csParams->Envelope = $env;
        $status = $api->CreateAndSendEnvelope($csParams)->CreateAndSendEnvelopeResult;
        addEnvelopeID($status->EnvelopeID);
        getToken($status, 1);
    } catch (SoapFault $e) {
        $_SESSION["errorMessage"] = $e;
        header("Location: error.php");
    }
}
function createSampleEnvelope()
{
    // Create envelope information
    $envinfo = new EnvelopeInformation();
    $envinfo->Subject = $_POST["subject"];
    $envinfo->EmailBlurb = $_POST["emailblurb"];
    $envinfo->AccountId = $_SESSION["AccountID"];
    if ($_POST["reminders"] != null) {
        $remind = new DateTime($_POST["reminders"]);
        $new = new DateTime($_SERVER['REQUEST_TIME']);
        $days = $now->diff($remind)->d;
        if ($envinfo->Notification == null) {
            $envinfo->Notification = new Notification();
        }
        $envinfo->Notification->Reminders = new Reminders();
        $envinfo->Notification->Reminders->ReminderEnabled = true;
        $envinfo->Notification->Reminders->ReminderDelay = $days;
        $envinfo->Notification->Reminders->ReminderFrequency = "2";
    }
    if ($_POST["expiration"] != null) {
        $expire = new DateTime($_POST["reminders"]);
        $new = new DateTime($_SERVER['REQUEST_TIME']);
        $days = $now->diff($expire)->d;
        if ($envinfo->Notification == null) {
            $envinfo->Notification = new Notification();
        }
        $envinfo->Notification->Expirations = new Expirations();
        $envinfo->Notification->Expirations->ExpireEnabled = true;
        $envinfo->Notification->Expirations->ExpireDelay = $days;
        $envinfo->Notification->Expirations->ExpireFrequency = "2";
    }
    // Get all recipients
    $recipients = constructRecipients();
    // Construct the template reference
    $tref = new TemplateReference();
    $tref->TemplateLocation = TemplateLocationCode::Server;
    $tref->Template = $_POST["TemplateID"];
    $tref->RoleAssignments = createFinalRoleAssignments($recipients);
    $trefs = array($tref);
    if (isset($_POST["SendNow"])) {
        sendNow($trefs, $envinfo, $recipients);
    } else {
        embedSending($trefs, $envinfo, $recipients);
    }
}
function buildEnvelope()
{
    $envelope = new Envelope();
    if (isset($_POST["subject"])) {
        $envelope->Subject = $_POST["subject"];
    } else {
        $_SESSION["errorMessage"] = "You must have a subject";
        header("Location: error.php");
    }
    if ($_POST["emailBlurb"]) {
        $envelope->EmailBlurb = $_POST["emailBlurb"];
    } else {
        $_SESSION["errorMessage"] = "You must have an email blurb";
        header("Location: error.php");
    }
    $envelope->AccountId = $_SESSION["AccountID"];
    $envelope->Recipients = constructRecipients();
    $envelope->Tabs = addTabs(count($envelope->Recipients));
    $envelope = processOptions($envelope);
    $envelope->Documents = getDocuments();
    return $envelope;
}