Template::UpdateStatus PHP Method

UpdateStatus() public static method

Sync the database with the filesystem.
public static UpdateStatus ( ) : void
return void
    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);
                }
            }
        }
    }

Usage Example

示例#1
0
<?php
require_once($GLOBALS['g_campsiteDir']. "/$ADMIN_DIR/templates/template_common.php");

if (!$g_user->hasPermission('ManageTempl') && !$g_user->hasPermission('DeleteTempl')) {
	camp_html_goto_page("/$ADMIN/");
}

$path = Input::Get('Path', 'string', '');
if (!Template::IsValidPath($path)) {
	$path = "";
}

Template::UpdateStatus();

$crumbs = array();
$crumbs[] = array(getGS("Configure"), "");
$crumbs[] = array(getGS("Templates"), "/$ADMIN/templates/");
$crumbs = array_merge($crumbs, camp_template_path_crumbs($path));
$crumbs[] = array(getGS('Templates'), '');
echo camp_html_breadcrumbs($crumbs);

include_once($GLOBALS['g_campsiteDir']."/$ADMIN_DIR/javascript_common.php");

?>
<P>
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" class="action_buttons">
<TR>
	<?php
	if (!empty($path)) {
		$new_path = substr($path, 0, strrpos($path, '/'));
		?>