function cron_tpl_combine($entry, $templates, $mapfield, $fileid)
{
    $files = positionsort($templates['meta']['files']);
    $mfileurl = "";
    $proceed = 0;
    $tpls = array();
    $js = 0;
    foreach ($files as $file) {
        $tpl_values = array();
        if (!empty($file['filepath'])) {
            $proceed = 1;
            $tpl_values = tpl_values_merge($entry, $mapfield);
            if ($proceed == 1 && !empty($tpl_values)) {
                $tpls[] = array('file' => $file, 'values' => $tpl_values);
            }
            echo '<br/>Tpls Array: ' . print_r($tpls, true);
            $ofieldvalue = "";
            $ofieldvalue_e = "";
            $ofieldvalue_f = "";
            if (isset($entry[$file['form_field']])) {
                $ofieldvalue = explode("|", $entry[$file['form_field']]);
            }
            if (isset($entry[$file['form_field_e']])) {
                $ofieldvalue_e = explode("|", $entry[$file['form_field_e']]);
            }
            if (isset($entry[$file['form_field_f']])) {
                $ofieldvalue_f = explode("|", $entry[$file['form_field_f']]);
            }
            if (isset($entry[$file['form_field_g']])) {
                $ofieldvalue_g = explode("|", $entry[$file['form_field_g']]);
            }
            $remove = 0;
            if ((!empty($file['form_field_e']) || !empty($file['form_field_f'])) && !empty($file['form_field'])) {
                if (!empty($file['operator_value']) && !empty($file['form_field']) && $file['operator_value'] != $ofieldvalue[0]) {
                    $remove = 1;
                } else {
                    if (empty($file['operator_value']) && !empty($file['form_field']) && empty($entry[$file['form_field']])) {
                        $remove = 1;
                    }
                }
                if (!empty($file['operator_value_e']) && !empty($file['form_field_e']) && $file['operator_value_e'] != $ofieldvalue_e[0]) {
                    $remove = 1;
                } else {
                    if (empty($file['operator_value_e']) && !empty($file['form_field_e']) && empty($entry[$file['form_field_e']])) {
                        $remove = 1;
                    }
                }
                if (!empty($file['operator_value_f']) && !empty($file['form_field_f']) && $file['operator_value_f'] != $ofieldvalue_f[0]) {
                    $remove = 1;
                } else {
                    if (empty($file['operator_value_f']) && !empty($file['form_field_f']) && empty($entry[$file['form_field_f']])) {
                        $remove = 1;
                    }
                }
                if (!empty($file['operator_value_g']) && !empty($file['form_field_g']) && $file['operator_value_g'] != $ofieldvalue_g[0]) {
                    $remove = 1;
                } else {
                    if (empty($file['operator_value_g']) && !empty($file['form_field_g']) && empty($entry[$file['form_field_g']])) {
                        $remove = 1;
                    }
                }
                if ($remove == 1) {
                    array_pop($tpls);
                }
            } else {
                if (!empty($file['operator_value']) && !empty($file['form_field']) && $file['operator_value'] != $ofieldvalue[0]) {
                    array_pop($tpls);
                } else {
                    if (empty($file['operator_value']) && !empty($file['form_field']) && empty($entry[$file['form_field']])) {
                        array_pop($tpls);
                    }
                }
            }
        }
        $js++;
        echo '<br/>Empty Template Values: ' . empty($tpl_values);
        echo '<br/>Proceed: ' . $proceed;
    }
    if (!empty($tpls)) {
        $tpls = templatemergealgorithm($tpls, $entry['form_id']);
        $mfileurl = cron_tpl_merge_process($tpls, $entry, "", $fileid);
        echo '<br/>TPLS: ' . $tpls . '<br/>MFILE: ' . $mfileurl;
    }
    return $mfileurl;
}
function tpl_combine($entry, $templates, $mapfield)
{
    $files = positionsort($templates['meta']['files']);
    $mfileurl = "";
    $proceed = 0;
    $tpls = array();
    $js = 0;
    foreach ($files as $file) {
        $tpl_values = array();
        if (!empty($file['filepath'])) {
            $proceed = 1;
            $tpl_values = tpl_values_merge($entry, $mapfield);
            if ($proceed == 1 && !empty($tpl_values)) {
                $tpls[] = array('file' => $file, 'values' => $tpl_values);
            }
            $ofieldvalue = explode("|", $entry[$file['form_field']]);
            $ofieldvalue_e = explode("|", $entry[$file['form_field_e']]);
            $ofieldvalue_f = explode("|", $entry[$file['form_field_f']]);
            $ofieldvalue_g = explode("|", $entry[$file['form_field_g']]);
            $remove = 0;
            /*printp($entry);
              printp($file);*/
            if ((!empty($file['form_field_e']) || !empty($file['form_field_f']) || !empty($file['form_field_g'])) && !empty($file['form_field'])) {
                if (!empty($file['operator_value']) && !empty($file['form_field']) && $file['operator_value'] != $ofieldvalue[0]) {
                    //echo "test1"."<br>";
                    $remove = 1;
                } else {
                    if (empty($file['operator_value']) && !empty($file['form_field']) && empty($entry[$file['form_field']])) {
                        //echo "test2"."<br>";
                        $remove = 1;
                    }
                }
                if (!empty($file['operator_value_e']) && !empty($file['form_field_e']) && $file['operator_value_e'] != $ofieldvalue_e[0]) {
                    //echo "test3"."<br>";
                    $remove = 1;
                } else {
                    if (empty($file['operator_value_e']) && !empty($file['form_field_e']) && empty($entry[$file['form_field_e']])) {
                        //echo "test4"."<br>";
                        $remove = 1;
                    }
                }
                if (!empty($file['operator_value_f']) && !empty($file['form_field_f']) && $file['operator_value_f'] != $ofieldvalue_f[0]) {
                    // echo "test5"."<br>";
                    $remove = 1;
                } else {
                    if (empty($file['operator_value_f']) && !empty($file['form_field_f']) && empty($entry[$file['form_field_f']])) {
                        //echo "test6"."<br>";
                        $remove = 1;
                    }
                }
                if (!empty($file['operator_value_g']) && !empty($file['form_field_g']) && $file['operator_value_g'] != $ofieldvalue_g[0]) {
                    // echo "test5"."<br>";
                    $remove = 1;
                } else {
                    if (empty($file['operator_value_g']) && !empty($file['form_field_g']) && empty($entry[$file['form_field_g']])) {
                        //echo "test6"."<br>";
                        $remove = 1;
                    }
                }
                if ($remove == 1) {
                    //echo "remove";
                    array_pop($tpls);
                }
            } else {
                if (!empty($file['operator_value']) && !empty($file['form_field']) && $file['operator_value'] != $ofieldvalue[0]) {
                    //echo "test5";
                    array_pop($tpls);
                } else {
                    if (empty($file['operator_value']) && !empty($file['form_field']) && empty($entry[$file['form_field']])) {
                        //echo "test6";
                        array_pop($tpls);
                    }
                }
            }
        }
        $js++;
    }
    if (!empty($tpls)) {
        $tpls = templatemergealgorithm($tpls, $entry['form_id']);
        //          if($entry['form_id'] == 16){
        //		printp($entry);
        //            printp($tpls);exit;
        //		  }
        $mfileurl = tpl_merge_process($tpls, $entry);
    }
    return $mfileurl;
}