function tplsadmin_copy_templates_f2db($tplset_to, $whr_append = '1')
{
    global $db;
    // get tplsource
    $result = $db->query("SELECT * FROM " . $db->prefix("tplfile") . "  WHERE tpl_tplset='default' AND ({$whr_append})");
    while ($row = $db->fetchArray($result)) {
        $basefilepath = tplsadmin_get_basefilepath($row['tpl_module'], $row['tpl_type'], $row['tpl_file']);
        $tpl_source = rtrim(implode("", file($basefilepath)));
        $lastmodified = filemtime($basefilepath);
        $drs = $db->query("SELECT tpl_id FROM " . $db->prefix("tplfile") . " WHERE tpl_tplset='" . addslashes($tplset_to) . "' AND ({$whr_append}) AND tpl_file='" . addslashes($row['tpl_file']) . "' AND tpl_refid='" . addslashes($row['tpl_refid']) . "'");
        if (!$db->getRowsNum($drs)) {
            // INSERT mode
            $sql = "INSERT INTO " . $db->prefix("tplfile") . " SET tpl_refid='" . addslashes($row['tpl_refid']) . "',tpl_desc='" . addslashes($row['tpl_desc']) . "',tpl_lastmodified='" . addslashes($lastmodified) . "',tpl_type='" . addslashes($row['tpl_type']) . "',tpl_tplset='" . addslashes($tplset_to) . "',tpl_file='" . addslashes($row['tpl_file']) . "',tpl_module='" . addslashes($row['tpl_module']) . "'";
            $db->query($sql);
            $tpl_id = $db->getInsertId();
            $db->query("INSERT INTO " . $db->prefix("tplsource") . " SET tpl_id='{$tpl_id}', tpl_source='" . addslashes($tpl_source) . "'");
            altsys_template_touch($tpl_id);
        } else {
            while (list($tpl_id) = $db->fetchRow($drs)) {
                // UPDATE mode
                $db->query("UPDATE " . $db->prefix("tplfile") . " SET tpl_lastmodified='" . addslashes($lastmodified) . "' WHERE tpl_id='{$tpl_id}'");
                $db->query("UPDATE " . $db->prefix("tplsource") . " SET tpl_source='" . addslashes($tpl_source) . "' WHERE tpl_id='{$tpl_id}'");
                altsys_template_touch($tpl_id);
            }
        }
    }
}
}
if (!empty($_POST['do_create'])) {
    // Ticket Check
    if (!$xoopsGTicket->check(true, 'altsys_tplsform')) {
        redirect_header(XOOPS_URL . '/', 3, $xoopsGTicket->getErrors());
    }
    $sql = "INSERT INTO " . $db->prefix("tplfile") . " SET tpl_file='" . addslashes($myts->stripSlashesGPC($_POST['tpl_file'])) . "',tpl_refid=0,tpl_module='" . addslashes($tpl['tpl_module']) . "',tpl_tplset='" . addslashes($tpl['tpl_tplset']) . "',tpl_lastmodified=UNIX_TIMESTAMP(),tpl_type='" . addslashes($tpl['tpl_type']) . "'";
    if (!$db->query($sql)) {
        die('SQL Error' . __LINE__);
    }
    $tpl_id = intval($db->getInsertId());
    $sql = "INSERT INTO " . $db->prefix("tplsource") . " SET tpl_id={$tpl_id},tpl_source='" . addslashes($myts->stripSlashesGPC($_POST['tpl_source'])) . "'";
    if (!$db->query($sql)) {
        die('SQL Error' . __LINE__);
    }
    altsys_template_touch($tpl_id);
    // continue or end ?
    redirect_header('index.php?mode=admin&lib=altsys&page=mytplsadmin&dirname=' . $tpl['tpl_module'], 1, _MD_A_MYTPLSFORM_CREATED);
    exit;
}
//****************//
//   FORM stage   //
//****************//
xoops_cp_header();
$mymenu_fake_uri = 'index.php?mode=admin&lib=altsys&page=mytplsadmin&dirname=' . $mydirname;
// mymenu
altsys_include_mymenu();
echo "<h3 style='text-align:" . _GLOBAL_LEFT . ";'>" . _MD_A_MYTPLSFORM_EDIT . " : " . htmlspecialchars($tpl['tpl_type'], ENT_QUOTES) . " : " . htmlspecialchars($tpl['tpl_file'], ENT_QUOTES) . " (" . htmlspecialchars($tpl['tpl_tplset'], ENT_QUOTES) . ")</h3>\n";
// diff from file to selected DB template
$basefilepath = tplsadmin_get_basefilepath($tpl['tpl_module'], $tpl['tpl_type'], $tpl['tpl_file']);
$diff_from_file4disp = '';