Template::InUse PHP Method

InUse() public static method

Returns TRUE if the template is being used in an Issue or Section.
public static InUse ( $p_templateName ) : boolean
return boolean
    public static function InUse($p_templateName)
    {
        global $Campsite;
        global $g_ado_db;
        $p_templateName = ltrim($p_templateName, '/');
        $queryStr = "SELECT * FROM Templates WHERE Name = '{$p_templateName}'";
        $row = $g_ado_db->GetRow($queryStr);
        if (!$row) {
            return false;
        }
        $id = $row['Id'];
        $queryStr = "SELECT COUNT(*) FROM Issues " . " WHERE IssueTplId = " . $id . " OR SectionTplId = " . $id . " OR ArticleTplId = " . $id;
        $numMatches = $g_ado_db->GetOne($queryStr);
        if ($numMatches > 0) {
            return true;
        }
        $queryStr = "SELECT COUNT(*) FROM Sections " . " WHERE SectionTplId = " . $id . " OR ArticleTplId = " . $id;
        $numMatches = $g_ado_db->GetOne($queryStr);
        if ($numMatches > 0) {
            return true;
        }
        $tplFindObj = new FileTextSearch();
        $tplFindObj->setExtensions(array('tpl', 'css'));
        $tplFindObj->setSearchKey($p_templateName);
        $result = $tplFindObj->findReplace($Campsite['TEMPLATE_DIRECTORY']);
        if (is_array($result) && sizeof($result) > 0) {
            return $result[0];
        }
        if (pathinfo($p_templateName, PATHINFO_EXTENSION) == 'tpl') {
            $p_templateName = ' ' . $p_templateName;
        }
        $tplFindObj->setSearchKey($p_templateName);
        $result = $tplFindObj->findReplace($Campsite['TEMPLATE_DIRECTORY']);
        if (is_array($result) && sizeof($result) > 0) {
            return $result[0];
        }
        if ($tplFindObj->m_totalFound > 0) {
            return true;
        }
        return false;
    }

Usage Example

Example #1
0
$fileFullName = preg_replace('#^/+#', '', (!empty($Path)) ? $Path.DIR_SEP.$Name : $Name);
$fileFullPath = Template::GetFullPath($Path, '');
$errorMsgs = array();

$deleted = false;
if (!$isFile) {
        $deleted = rmdir($fileFullPath.$Name);
	if ($deleted) {
		$logtext = getGS('Directory $1 was deleted', mysql_real_escape_string($fileFullName));
		Log::Message($logtext, $g_user->getUserId(), 112);
		camp_html_add_msg($logtext, "ok");
	} else {
		camp_html_add_msg(camp_get_error_message(CAMP_ERROR_RMDIR, $fileFullPath));
	}
} else {
	$inUse = Template::InUse($fileFullName);
	if ($inUse === CAMP_ERROR_READ_FILE || $inUse === CAMP_ERROR_READ_DIR) {
                camp_html_add_msg(getGS("There are some files which can not be readed so Newscoop was not able to determine whether '$1' is in use or not. Please fix this, then try to delete the template again.", basename($fileFullName)));
	} elseif ($inUse == false) {
	        $template = new Template($fileFullName);
		if ($template->exists() && $template->delete()) {
			// Clear compiled template
			require_once($GLOBALS['g_campsiteDir']."/template_engine/classes/CampTemplate.php");
			CampTemplate::singleton()->clear_compiled_tpl($fileFullName);

			$logtext = getGS('Template object $1 was deleted', mysql_real_escape_string($fileFullName));
			Log::Message($logtext, $g_user->getUserId(), 112);
			camp_html_add_msg($logtext, "ok");
		} else {
			camp_html_add_msg(camp_get_error_message(CAMP_ERROR_DELETE_FILE, $fileFullName));
		}