<?php

if (isset($_POST) && !empty($_POST)) {
    check_admin_referer('gforms_save_entry', 'gforms_save_entry');
    //Loading files that have been uploaded to temp folder
    $files = GFCommon::json_decode(stripslashes(RGForms::post("gform_uploaded_files")));
    if (!is_array($files)) {
        $files = array();
    }
    GFFormsModel::$uploaded_files[$form_id] = $files;
    GFFormsModel::save_lead($form, $lead);
    do_action("gform_after_update_entry", $form, $lead["id"]);
    do_action("gform_after_update_entry_{$form["id"]}", $form, $lead["id"]);
    $lead = RGFormsModel::get_lead($lead["id"]);
    $lead = GFFormsModel::set_entry_meta($lead, $form);
    $formid = $lead['form_id'];
    $where = 'form_id =' . $formid;
    $params = array('where' => $where);
    $templates = gettplList($params);
    $fieldmap = gettplFieldMap($params);
    if (!empty($templates['meta']['files'])) {
        $return = tpl_combine($lead, $templates, $fieldmap);
    }
}
function merge_template($confirmation, $form, $entry, $ajax)
{
    if (isset($_POST['gform_save_state_' . $form['id']])) {
        return $confirmation;
    }
    /* this should never happend */
    global $wpdb;
    $return = "";
    $formid = $entry['form_id'];
    $where = 'form_id =' . $formid;
    $params = array('where' => $where);
    $templates = gettplList($params);
    $fieldmap = gettplFieldMap($params);
    $data_title = RGFormsModel::get_form($formid);
    $form_title = $data_title->title;
    if (!empty($templates['meta']['files'])) {
        $return = tpl_combine($entry, $templates, $fieldmap);
    }
    $letters = "";
    $invoices = "";
    if ($return != "") {
        $letters = LettersMerge($entry, $fieldmap);
        $invoices = InvoicesMerge($entry, $fieldmap);
        //TriggerOrderEmail($entry['id']);
        $doc_email_msg = TriggerDocumentEmail($entry['id']);
        if (!empty($doc_email_msg) && $doc_email_msg['status'] == 'success') {
            $confirmation .= $doc_email_msg['msg'];
        }
        $return = str_replace("\\", "/", $return);
        //$confirmation .= " <a href='".$return."' target='_blank'>You can also download the document here</a>";
        $confirmation .= " or You can Click and Download the links below: ";
        $confirmation .= "<ol><li><a href='" . $return . "' target='_blank'>" . $form_title . "</a>.</li>";
        if (!empty($invoices)) {
            $invoices = str_replace("\\", "/", $invoices);
            //$confirmation .= " And also <a href='".$invoices."' target='_blank'>download your Invoice document here.</a>";
            $confirmation .= "<li><a href='" . $invoices . "' target='_blank'>Tax Invoice</a></li>";
        }
        if (!empty($letters)) {
            $letters = str_replace("\\", "/", $letters);
            //$confirmation .= " And also <a href='".$letters."' target='_blank'>download your Letter document here.</a>";
            $confirmation .= "<li><a href='" . $letters . "' target='_blank'>Cover Letter</a></li>";
        }
        $confirmation .= "</ol>";
        if (isset($_GET["q"]) && $_GET["type"] == "edit") {
            $id = decrypt_doc_url(rgget("q"));
            TemplateData::delete_confirmation_saved($id);
        }
    }
    return $confirmation;
}