функция php не вставляется в документ Microsoft Word - PullRequest
0 голосов
/ 19 декабря 2018

Я создал несколько простых php-страниц, которые получают некоторый текстовый ввод, и я вставляю эти значения в документ rtf и отображаю его в Microsoft Word.Большинство моих ценностей заменяются местозаполнителями, обозначенными внутри [], такими как [Подарки] или [Представитель].Но когда я попытался добавить новую функцию для Guardian, я вижу свои значения в консоли, когда я их выводю, но они не вставляются в мой шаблон rtf.Может ли кто-нибудь еще увидеть ошибку, которую я здесь делаю?

Вот мой файл functions.php

<?php
echo "<script>console.log( 'inside functions.php' );</script>";
$functions_test = "functions_on";
function redirect_to($new_location){ //link using php
    //header("Location: " . $new_location);
    echo "<script type='text/javascript'>";
    echo "window.location.href = '{$new_location}';";
    echo "</script>";
    exit;
}

function mysql_prep($string){
    global $db;
    $escaped_string = mysqli_real_escape_string($db, $string);
    return $escaped_string;
}



function find_beneficiaries_by_id($id){
    global $db;

    $id = mysqli_real_escape_string($db, $id);

    $query = "SELECT * FROM beneficiaries WHERE id = {$id} LIMIT 1";
    $set = mysqli_query($db, $query);

    if($set && $row = mysqli_fetch_assoc($set)){
        return $row;
    }else{
        return null;
    }
}



function fix_name($first = "", $middle = "", $last = ""){
    $full_name = "[Insert Your Name Here]"; //the default

    $first = ucfirst(trim($first)); //remove spaces before and after then capitalize first letter
    $middle = ucfirst(trim($middle));
    $last = ucfirst(trim($last));

    if($first != "" || $middle != "" || $last != ""){
        $full_name = "";

        if($first != ""){
            $full_name .= " ".$first;
        }

        if($middle != ""){
            $full_name .= " ".$middle;
        }

        if($last != ""){
            $full_name .= " ".$last;
        }
    }

    return trim($full_name);
}

/*function fix_state($state = ""){
    $default_state = "[Insert State]";
    if($state == ""){$state = $default_state;}
    return $state;
}*/

function fix_address($city = "", $county = "", $state = ""){
    $string = "[Insert City], [Insert County], [Insert State]"; //the default
    $default_city = "[Insert City]";
    $default_county = "[Insert County]";
    $default_state = "[Insert State]";

    $city = ucfirst(trim(mysql_prep($city))); //remove spaces before and after then capitalize first letter
    $county = ucfirst(trim(mysql_prep($county)));
    $state = ucfirst(trim($state));

    if($state == ""){$state = $default_state;}
    if($city == "" && $county == ""){$city = $default_city; $county = $default_county;}

    echo "<script>console.log( 'City, County, State: {$city}, {$county}, {$state}' );</script>";

    if($city != "" || $county != "" || $state != ""){
        $string = "";

        if($city != ""){
            $string .= $city;
        }
        echo "<script>console.log( 'String: {$string}' );</script>";

        if($county != ""){
            if($city != ""){
                $string .= ", ";
            }
            $string .= $county;
        }

        echo "<script>console.log( 'String 2: {$string}' );</script>";

        if($state != "" && $state != "null"){
            if($city != "" || $county != ""){
                $string .= ", ";
            }
            $string .= $state;
        }

        echo "<script>console.log( 'String 3: {$string}' );</script>";
    }

    return $string;
}

function fix_children_list($array){
    $string = "";
    if (!empty($array)) {
        for($i = 0; $i < count($array); $i++) {
            $string .= ", ".$array[$i];
        }
    }

    return $string;
}

function fix_numbers($num){
    if($num == 1){$num = "one";}
    else if($num == 2){$num = "two";}
    else if($num == 3){$num = "three";}
    else if($num == 4){$num = "four";}
    else if($num == 5){$num = "five";}
    else if($num == 6){$num = "six";}
    else if($num == 7){$num = "seven";}
    else if($num == 8){$num = "eight";}
    else if($num == 9){$num = "nine";}
    return $num;
}

function fix_married($married, $spouse){
    $spouse = ucfirst(trim($spouse));
    $default_spouse = "[Insert Spouse Name]";
    $string = "married to ".$default_spouse;

    if($married == "yes"){
        if($spouse == ""){$spouse = $default_spouse;}
        $string = "married to ".$spouse;
    }else if($married == "no"){
        $string = "not married";
    }

    return $string;
}

function fix_children($children, $children_array){
    $children_count = fix_numbers(count($children_array));
    $default_child = "[Insert Child Name]";
    $string = "one child whose name is the following: \\line \\line ".$default_child;

    if($children == "yes"){
        $grammar = "children";
        $grammer2 = "names are";
        if($children_count == "one"){$grammar = "child"; $grammer2 = "name is";}
        $string = $children_count." ".$grammar." whose ".$grammer2." the following:";

        $string .= "\\line";
        for($i = 0; $i < count($children_array); $i++) {
            $child = ucfirst(trim($children_array[$i]));
            if($child == ""){$child = $default_child;}
            $string .= "\\line ".$child;
        }
    }else if($children == "no"){
        $string = "no children.";
    }

    return $string;
}

function fix_family($married, $spouse, $children, $children_array){
    $string = "";

    //if($married == "yes" || $children == "yes"){
        $string .= "I am ".fix_married($married, $spouse).". ";
        $string .= "I have ".fix_children($children, $children_array);
    //}

    return $string;
}

/*function fix_family_heading($married, $children){
    $string = "\\line Article One \\line Family Information ";

    if($married == "no" && $children == "no"){
        $string = "";
    }

    return $string;
}*/

function fix_beneficiaries($ben_array = array(""), $percent_array = array(""), $con_ben = ""){
    $default_ben = "[Insert Beneficiary Name]";
    $default_percent = "[Insert Beneficiary Percent]";
    $default_con_ben = "[Insert Contingent Beneficiary Name]";
    $string = "I give my tangible personal property to ".$default_ben.". If ".$default_ben." does not survive me, then ".$default_con_ben." will take my tangible personal property instead.";
    $count = count($ben_array);

    $con_ben = ucfirst(trim(mysql_prep($con_ben)));
    if($con_ben == ""){$con_ben = $default_con_ben;}

    for($i = 0; $i < $count; $i++) {
        $ben_array[$i] = ucfirst(trim(mysql_prep($ben_array[$i])));
        $ben = $ben_array[$i];
        if($ben == ""){
            $ben_array[$i] = $default_ben;
        }
        if($percent_array[$i] == ""){
            $percent_array[$i] = $default_percent;
        }
    }

    if($count == 1){
        $string = "I give my tangible personal property to ".$ben_array[0].". If ".$ben_array[0]." does not survive me, then ".$con_ben." will take my tangible personal property instead.";
        //I give my tangible personal property to [ben_name1]. If [ben_name1] does not survive me, then [con_ben_name1] will take my tangible personal property instead.
    }else if($count == 2){
        $string = "I give ".$percent_array[0]."% of my tangible personal property to ".$ben_array[0]." and ".$percent_array[1]."% of my tangible personal property to ".$ben_array[1].", if they survive me. If they do not survive me, then ".$con_ben." will take my tangible personal property instead.";
        //I give 40% of my tangible personal property to [ben_name1] and 60% of my tangible personal property to [ben_name2], if they survive me. If they do not survive me, then [con_ben_name1] will take my tangible personal property instead.
    }else if($count == 3){
        $string = "I give ".$percent_array[0]."% of my tangible personal property to ".$ben_array[0].", ".$percent_array[1]."% of my tangible personal property to ".$ben_array[1].", and give ".$percent_array[2]."% of my tangible personal property to ".$ben_array[2].", if they survive me. If they do not survive me, then ".$con_ben." will take my tangible personal property instead.";
        //I give 40% of my tangible personal property to [ben_name1], 25% of my tangible personal property to [ben_name2], and give 35% of my tangible personal property to [ben_name3], if they survive me. If they do not survive me, then [con_ben_name1] will take my tangible personal property instead.
    }

    return $string;
}

function fix_gifts($gift_array = array(""), $gift_ben_array = array("")){
    $default_gift = "[Insert Specific Gift]";
    $default_gift_ben = "[Insert Specific Gift Beneficiary Name]";
    $custom_string = "If said individual does not survive me, it will become part of the residuary estate, below. ";
    $string = "I give my ".$default_gift." to ".$default_gift_ben.", if said individual survives me. ".$custom_string;
    $count = count($gift_array);

    for($i = 0; $i < $count; $i++) {
        $gift_ben_array[$i] = ucfirst(trim(mysql_prep($gift_ben_array[$i])));
        $gift_array[$i] = trim(mysql_prep($gift_array[$i]));
        $gift = $gift_array[$i];
        if($gift == ""){
            $gift_array[$i] = $default_gift;
        }
        if($gift_ben_array[$i] == ""){
            $gift_ben_array[$i] = $default_gift_ben;
        }
    }

    if($count > 0){
        $string = "I give my ".$gift_array[0]." to ".$gift_ben_array[0].", if said individual survives me. ";
        $string .= $custom_string;
    }
    if($count > 1){
        $string .= "\\line \\line ";
        $string .= "I give my ".$gift_array[1]." to ".$gift_ben_array[1].", if said individual survives me. ";
        $string .= $custom_string;
    }
    if($count > 2){
        $string .= "\\line \\line ";
        $string .= "I give my ".$gift_array[2]." to ".$gift_ben_array[2].", if said individual survives me. ";
        $string .= $custom_string;
    }

    return $string;
}

function fix_residuary($res_ben_array = array(""), $res_percent_array = array(""), $res_con_array = array("")){
    $default_res_ben = "[Insert Beneficiary]";
    $default_res_percent = "[Insert Beneficiary Percent]";
    $default_res_con = "[Insert Contingent Beneficiary]";
    $per_stirpes = "{\i per stirpes}";
    $string = "I give ".$default_res_percent."% of my residuary estate to ".$default_res_ben.", if said individual survives me.";
    //$string .= "\\line \\line ";
    $string .= " If ".$default_res_ben." predeceases me, I give my residuary estate to ".$default_res_con.", ".$per_stirpes.".";

    $count = count($res_ben_array);

    for($i = 0; $i < $count; $i++) {
        $res_ben_array[$i] = ucfirst(trim(mysql_prep($res_ben_array[$i])));
        //$res_percent_array[$i] = $res_percent_array[$i];
        $res_con_array[$i] = ucfirst(trim(mysql_prep($res_con_array[$i])));

        if($res_ben_array[$i] == ""){
            $res_ben_array[$i] = $default_res_ben;
        }
        if($res_percent_array[$i] == ""){
            $res_percent_array[$i] = $default_res_percent;
        }
        if($res_con_array[$i] == ""){
            $res_con_array[$i] = $default_res_con;
        }
    }

    if($count > 0){
        $string = "I give ".$res_percent_array[0]."% of my residuary estate to ".$res_ben_array[0].", if said individual survives me.";
        //$string .= "\\line \\line ";
        $string .= " If ".$res_ben_array[0]." predeceases me, I give my residuary estate to ".$res_con_array[0].", ".$per_stirpes.".";
    }
    if($count > 1){
        $string .= "\\line \\line ";
        $string .= "I give ".$res_percent_array[1]."% of my residuary estate to ".$res_ben_array[1].", if said individual survives me.";
        //$string .= "\\line \\line ";
        $string .= " If ".$res_ben_array[1]." predeceases me, I give my residuary estate to ".$res_con_array[1].", ".$per_stirpes.".";
    }
    if($count > 2){
        $string .= "\\line \\line ";
        $string .= "I give ".$res_percent_array[2]."% of my residuary estate to ".$res_ben_array[2].", if said individual survives me.";
        //$string .= "\\line \\line ";
        $string .= " If ".$res_ben_array[2]." predeceases me, I give my residuary estate to ".$res_con_array[2].", ".$per_stirpes.".";
    }

    return $string;
}

function fix_representative($rep_array = array("")){
    $default_rep = "[Insert Representative]";
    $string = "I name ".$default_rep." as my Personal Representative.";

    $count = count($rep_array);

    for($i = 0; $i < $count; $i++) {
        $rep_array[$i] = ucfirst(trim(mysql_prep($rep_array[$i])));

        if($rep_array[$i] == ""){
            $rep_array[$i] = $default_rep;
        }
    }

    if($count > 0){
        $string = "I name ".$rep_array[0]." as my Personal Representative.";
    }
    if($count == 2){
        $string .= " If ".$rep_array[0]." fails or ceases to act as my Personal Representative, I name ".$rep_array[1]." as my Personal Representative.";
    }
    if($count == 3){
        $string .= " If ".$rep_array[0]." fails or ceases to act as my Personal Representative, I name the following, in the order named, as my Personal Representative: \\line \\line 
    ".$rep_array[1]."; and then \\line \\line 
    ".$rep_array[2].".";
    }

    return $string;
}
// Begin Brendans new function for Guardianship here -------------------------------------------------------------------

function fix_guardian($guardian_array = array("")){
    $default_guardian = "[Insert Guardian]";
    $string = "I appoint ".$default_guardian." as guardian of my children.";

    $count = count($guardian_array);

    for($i = 0; $i < $count; $i++) {
        $guardian_array[$i] = ucfirst(trim(mysql_prep($guardian_array[$i])));

        if($guardian_array[$i] == ""){
            $guardian_array[$i] = $default_guardian;
        }
    }

    if($count > 0){
        $string = "I appoint ".$guardian_array[0]." as guardian of my children.";
        echo "<script>console.log( 'String: {$string}' );</script>";
    }
    if($count == 2){
        $string .= " If for any reason ".$guardian_array[0]." does not act as guardian, I appoint ".$guardian_array[1]." as guardian of my minor children.";
    }
    if($count == 3){
        $string .= " If ".$guardian_array[0]." fails or ceases to act as my Guardian, I name the following, in the order named, as my Guardian: \\line \\line 
    ".$guardian_array[1]."; and then \\line \\line 
    ".$guardian_array[2].".";
    }

    return $string;
}

// End Brendans new function for Guardianship here-----------------------------------------------------------------------


function session_to_file(){
    echo "<script>console.log( 'inside functions.php session_to_will_file' );</script>";
    //var_dump($_SESSION["children_array"]);
    $children_string = fix_children_list($_SESSION["children_array"]);
    $old_substrings = array( //array of all placeholders found in the template file on the server /docs folder
        "[Insert Your Name Here]", "[first name]", "[middle name]", "[last name]",
        "[Address]", "[City]", "[County]", "[State]",
        "[Family Information]", "[married]", "[spouse]", "[children]", "[children_array]",
        "[Beneficiaries]",
        "[Gifts]",
        "[Residuary]",
        "[Representative]",
        "[Guardian]"
    );

    $full_name = fix_name($_SESSION["first_name"], $_SESSION["middle_name"], $_SESSION["last_name"]);
    $full_address = fix_address($_SESSION["city"], $_SESSION["county"], $_SESSION["state"]);
    $full_family = fix_family($_SESSION["married"],$_SESSION["spouse"],$_SESSION["children"],$_SESSION["children_array"]);
    $full_beneficiaries = fix_beneficiaries($_SESSION["ben_array"], $_SESSION["percent_array"], $_SESSION["con_ben"]);
    $full_gifts = fix_gifts($_SESSION["gift_array"], $_SESSION["gift_ben_array"]);
    $full_residuary = fix_residuary($_SESSION["res_ben_array"], $_SESSION["res_percent_array"], $_SESSION["res_con_array"]);
    $full_representative = fix_representative($_SESSION["rep_array"]);
    $full_guardian = fix_guardian($_SESSION["guardian_array"]);

    $new_substrings = array( //array with all sessions variables that will replace the placeholders. Must match the old_substrings array exactly
        $full_name, $_SESSION["first_name"],$_SESSION["middle_name"],$_SESSION["last_name"],
        $full_address, $_SESSION["city"],$_SESSION["county"],$_SESSION["state"],
        $full_family, $_SESSION["married"],$_SESSION["spouse"],$_SESSION["children"],$children_string,
        /*$_SESSION["ben_name1"],$_SESSION["ben_percent1"],$_SESSION["ben_name2"],$_SESSION["ben_percent2"],$_SESSION["ben_name3"],$_SESSION["ben_percent3"],$_SESSION["con_ben_name1"]*/
        $full_beneficiaries,
        $full_gifts,
        $full_residuary,
        $full_representative,
        $full_guardian
    );
    /*for($i = 0; $i < count($new_substrings); $i++) { //if the session variable was empty, keep the placeholder there instead of replacing it with ""
        if($new_substrings[$i] == ""){$new_substrings[$i] = $old_substrings[$i];}
    }*/

    $new_dir = "docs/generated";
    $new_name = $full_name." Will ";
    $file = tempnam($new_dir, $new_name); //create a new file
    echo "<script>console.log( 'FILE: {$file}' );</script>";
    $template_path = "docs/will_template.rtf";
    if($_SESSION["married"] == "no" && $_SESSION["children"] == "no"){
        $template_path = "docs/will_template_no_family.rtf";
    };
    $file_contents = file_get_contents($template_path); //get the contents of the template file
    file_put_contents($file, $file_contents); //fill the new file

    //echo "<script>console.log( 'CONTENTS: {$file_contents}' )</script>";
    $file_contents = str_replace($old_substrings, $new_substrings, $file_contents); //edit the contents as a php string
    //$file_contents = str_replace("[first_name]", $_SESSION["first_name"], $file_contents);
    file_put_contents($file, $file_contents); //replace placeholders with the sessions variables in the new file

    download($file, $new_name);
}

function download($path, $name){
    if(!file_exists($path)){
        echo $path." does not exist.";
    }
    else{
        echo "
<a href='download.php?path={$path}&name={$name}' id='download' target='download_frame' style='display:none;'>Download File</a>
<iframe id='download_frame' style='display:none;'></iframe>
<script>document.getElementById('download').click();</script>
";
    }
}
?>

Вот мой файл guardian.php

<?php
include_once("header.php");
?>

<?php
if (isset($_POST["back"]) || isset($_POST["save"]) || isset($_POST["next"]) || isset($_POST["download"])) //if the form was submitted
{
    //save all inputs into session variable
    $_SESSION["guardian_array"] = $_POST["guardian_array"];

    echo "<script>console.log( 'inside submit form' );</script>";

    if(isset($_POST["next"])){
        redirect_to("guardian.php"); //next step
    }
    if(isset($_POST["save"])){
        redirect_to("guardian.php"); //send them to create a profile and dump the session into the db
    }
    if(isset($_POST["back"])){
        redirect_to("representative.php"); //previous step
    }
    if(isset($_POST["download"])){
        session_to_file();
    }
}
?>
    <style>
        .rep_tb{margin-top: 5px; margin-bottom: 20px;}
    </style>
    <form class="" method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
        <table>
            <tr><td><h2>Guardianship</h2></td></tr>

            <tr id="guardian_tr">
                <td id="guardian_td">
                    <?php
                    $count = count($_SESSION["guardian_array"]);
                    if($count > 0){
                        for($i = 0; $i < $count; $i++) {
                            echo "<div class='guardian'>
                            Guardian <input class='guardian_tb' type='text' name='guardian_array[]' value='".$_SESSION["guardian_array"][$i]."' />
                            </div>";
                        }
                    }else{
                        echo "<div class='guardian'>
                            Guardian <input class='guardian_tb' type='text' name='guardian_array[]' value='' />  
                            </div>";
                    }
                    ?>
                </td>
            </tr>
            <tr id="ben_control">
                <td>
                    <input type="button" id="add_button" value="Add" onclick="add_guardian(document.getElementById('guardian_td'), 3);" />
                    <input type="button" id="remove_button" value="Remove" onclick="remove_last_of_class('guardian');" />
                </td>
            </tr>
            <!--script>disable_buttons("rep", 1, 3);</script-->
        </table>
        <!--input type="submit" class="ajax_save" name="input_beneficiaries" value="Save" style="display: none;"/-->
        <br/><br/>
        <input type="submit" class="" name="back" value="Back" />
        <input type="submit" class="" name="next" value="Next" />
        <input type="submit" class="" name="download" value="Download will" />
        <input type="submit" class="" name="save" value="Save" />
    </form>

    Progress: <progress value="80" max="100"></progress>

<?php
include_once("includes/footer.php");
?>
...