public static function UpdateStatus()
{
global $Campsite;
global $g_ado_db;
// check if each template record in the database has the corresponding file
$templates = self::GetAllTemplates(null, false);
foreach ($templates as $index => $template) {
if (!$template->fileExists()) {
$g_ado_db->Execute("DELETE FROM Templates WHERE Id = " . $template->m_data['Id']);
}
}
// insert new templates
// TODO there's some bug here maybe, you get a notice on this line cause that key is undefined
$rootDir = @$Campsite['TEMPLATE_DIRECTORY'];
$dirs[] = $rootDir;
while (($currDir = array_pop($dirs)) != "") {
if (!is_readable($currDir)) {
continue;
}
if (!($dirHandle = opendir($currDir))) {
continue;
}
while ($file = readdir($dirHandle)) {
$fullPath = $currDir . '/' . $file;
if (!is_readable($fullPath)) {
continue;
}
if ($file == "." || $file == "..") {
continue;
}
$fileType = filetype($fullPath);
if ($fileType == "dir") {
$dirs[] = $fullPath;
continue;
}
if ($fileType != "file") {
// ignore special files and links
continue;
}
$relPath = substr($fullPath, strlen($rootDir) + 1);
$level = $relPath == "" ? 0 : substr_count($relPath, "/");
$sql = "SELECT count(*) AS nr FROM Templates WHERE Name = '" . $relPath . "'";
$existingTemplates = $g_ado_db->GetOne($sql);
if ($existingTemplates == 0) {
$ending = substr($file, strlen($file) - 4);
if ($ending != ".tpl") {
// ignore files that are not templates (end in .tpl)
$sql = "SELECT Id FROM TemplateTypes WHERE Name = 'nontpl'";
$nonTplTypeId = $g_ado_db->GetOne($sql);
$sql = "INSERT IGNORE INTO Templates (Name, Type, Level) " . "VALUES('{$relPath}', " . "{$nonTplTypeId}, " . "{$level})";
} else {
$sql = "INSERT IGNORE INTO Templates (Name, Level) " . "VALUES('{$relPath}', " . "{$level})";
}
$g_ado_db->Execute($sql);
}
}
}
}