8
Thanks for the advice, everyone.
In the end, I had to pull code from class/template.php, kernel/tplfile.php, and modules/system/admin/modulesadmin/modulesadmin.php.
Below is, I believe, a self-contained template recompiler function. It returns false if the update did not succeed, true otherwise. It can easily be made more generic, by basing it on a module id instead of a specific template, but I didn't want to recompile every template every time, just the one I am using at the moment.
(Note: I used queryF() rather than query() because I need to perform this function in querystring-based pages. This can be considered a minor security issue, I know, but I do not plan to have this code called EVER once the site goes live.)
function recompileTemplate($dirpath,$fileName) {
Global $xoopsDB,$xoopsTpl;
//1. get tpl_id based on filename
$sql = "SELECT tpl_id FROM ".$xoopsDB->prefix("tplfile").
" WHERE tpl_file = '".$fileName."'";
$result = $xoopsDB->queryF($sql);
list($tpl_id) = $xoopsDB->fetchRow($result);
//2. read file into memory
$path = XOOPS_ROOT_PATH.'/modules/'.$dirpath.'/templates/'.$fileName;
$lines = file($path);
$filecontents = '';
$count = count($lines);
for ($i = 0; $i < $count; $i++) {
$filecontents .= str_replace("n", "rn", str_replace("rn", "n", $lines[$i]));
}
//3. update tplsource with new template
$sql = "UPDATE ".$xoopsDB->prefix("tplsource").
" SET tpl_source =".$xoopsDB->quoteString($filecontents).
" WHERE tpl_id = ".$tpl_id;
if (!$result = $xoopsDB->queryF($sql)) {
return false;
}
//4. recompile template
include_once XOOPS_ROOT_PATH."/class/template.php";
xoops_template_touch($tpl_id);
return true;
}