using System;
using System.Data;
using System.Text;
using CMS.GlobalHelper;
using CMS.LicenseProvider;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.UIControls;
public partial class CMSInstall_Controls_WizardSteps_SiteCreationDialog : CMSUserControl
{
///
/// Type of the site creation.
///
public enum CreationTypeEnum
{
///
/// Use template.
///
Template = 0,
///
/// Continue to the new site wizard.
///
NewSiteWizard = 1,
///
/// Import existing site.
///
ExistingSite = 2
}
private bool mStopProcess = true;
private string mCurrentEdition = null;
private string mCurrentPackages = null;
///
/// Template name to be created.
///
public string TemplateName
{
get
{
return hdnName.Value;
}
}
///
/// Stop processing.
///
public override bool StopProcessing
{
get
{
return mStopProcess;
}
set
{
mStopProcess = value;
}
}
///
/// Current edition.
///
protected string CurrentEdition
{
get
{
if (mCurrentEdition == null)
{
// Get current domain and license
string domain = URLHelper.GetCurrentDomain();
LicenseKeyInfo li = LicenseKeyInfoProvider.GetLicenseKeyInfo(domain);
if (li != null)
{
mCurrentEdition = LicenseKeyInfoProvider.EditionToChar(li.Edition);
}
}
return mCurrentEdition;
}
}
///
/// Current edition.
///
protected string CurrentPackages
{
get
{
if (mCurrentPackages == null)
{
// Get current domain and license
string domain = URLHelper.GetCurrentDomain();
LicenseKeyInfo li = LicenseKeyInfoProvider.GetLicenseKeyInfo(domain);
if (li != null)
{
mCurrentPackages = li.LicensePackages;
}
}
return mCurrentPackages;
}
}
///
/// Template name to be created.
///
public CreationTypeEnum CreationType
{
get
{
if (radExisting.Checked)
{
return CreationTypeEnum.ExistingSite;
}
else if (radWizard.Checked)
{
return CreationTypeEnum.NewSiteWizard;
}
else
{
return CreationTypeEnum.Template;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
ltlScript.Text = ScriptHelper.GetScript("var hdnField=document.getElementById('" + hdnName.ClientID + "');var hdnLastSelected=document.getElementById('" + hdnLastSelected.ClientID + "');");
if (RequestHelper.IsPostBack())
{
ltlScriptAfter.Text = ScriptHelper.GetScript("SelectTemplate(hdnLastSelected.value,hdnField.value);");
}
radWizard.Text = ResHelper.GetFileString("Install.radWizard");
radTemplate.Text = ResHelper.GetFileString("Install.radTemplate");
radExisting.Text = ResHelper.GetFileString("Install.radExisting");
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
// Load web templates
if (!StopProcessing)
{
ReloadData();
}
}
///
/// Gets metafile preview.
///
/// Template ID
protected string GetPreviewImage(object objTemplateId)
{
int templateId = ValidationHelper.GetInteger(objTemplateId, 0);
DataSet dsPreview = MetaFileInfoProvider.GetMetaFiles(templateId, SiteObjectType.WEBTEMPLATE);
if (!DataHelper.DataSourceIsEmpty(dsPreview))
{
string guid = ValidationHelper.GetString(dsPreview.Tables[0].Rows[0]["MetaFileGUID"], "");
return ResolveUrl("~/CMSPages/GetMetaFile.aspx?fileguid=" + guid);
}
else
{
return GetImageUrl("Others/Install/no_image.png");
}
}
///
/// Data bind.
///
public void ReloadData()
{
if (WebTemplateInfoProvider.IsAnyTemplatePresent())
{
DataSet dsTemplates = WebTemplateInfoProvider.GetWebTemplates(null, "WebTemplateOrder", 0, null, true);
if (!DataHelper.DataSourceIsEmpty(dsTemplates))
{
string firstTemplateName = "";
int firstTemplateId = 0;
// Find first allowed template
foreach (DataRow row in dsTemplates.Tables[0].Rows)
{
string templateEditions = ValidationHelper.GetString(row["WebTemplateLicenses"], "");
string templatePackages = ValidationHelper.GetString(row["WebTemplatePackages"], "");
if (IsTemplateAllowed(templateEditions, templatePackages))
{
firstTemplateName = ValidationHelper.GetString(row["WebTemplateName"], "");
firstTemplateId = ValidationHelper.GetInteger(row["WebTemplateID"], 0);
break;
}
}
// Bind the list
rptSites.DataSource = dsTemplates;
rptSites.DataBind();
// Select Corporate site by default
if (string.IsNullOrEmpty(hdnLastSelected.Value) || (hdnLastSelected.Value == "0"))
{
ltlScriptAfter.Text += ScriptHelper.GetScript("SelectTemplate('tpl" + firstTemplateId + "','" + firstTemplateName + "');");
}
}
}
else
{
plcInfo.Visible = true;
lblInfo.Text = ResHelper.GetFileString("Install.TemplatesWarning");
radWizard.Enabled = false;
radTemplate.Enabled = false;
plcTemplates.Visible = false;
radExisting.Checked = true;
}
}
protected string GetItemHTML(object dataItem)
{
DataRowView view = (DataRowView)dataItem;
string templateId = ValidationHelper.GetString(view["WebTemplateID"], "");
string templateName = ValidationHelper.GetString(view["WebTemplateName"], "");
string templateDisplayName = ValidationHelper.GetString(view["WebTemplateDisplayName"], "");
string templateDescription = ValidationHelper.GetString(view["WebTemplateDescription"], "");
string templateEditions = ValidationHelper.GetString(view["WebTemplateLicenses"], "");
string templatePackages = ValidationHelper.GetString(view["WebTemplatePackages"], "");
bool isAllowed = false;
// Check if the current license is suitable for webtemplate
isAllowed = IsTemplateAllowed(templateEditions, templatePackages);
// Generate HTML code
StringBuilder builder = new StringBuilder();
string cssClass = isAllowed ? "InstallItem" : "InstallDisabledItem";
string textColor = isAllowed ? "" : "color:Silver;";
string errorMessage = "";
if (!isAllowed)
{
errorMessage = "
";
errorMessage += string.Format(ResHelper.GetFileString("Install.WebTemplateRequiresLicense"), EditionsToString(templateEditions));
string templatePackageNames = PackagesToString(templatePackages);
if (!String.IsNullOrEmpty(templatePackageNames))
{
errorMessage += " " + string.Format(ResHelper.GetFileString("Install.WebTemplateRequiresLicenseOrPackages"), templatePackageNames);
}
errorMessage += ". " + string.Format(ResHelper.GetFileString("Install.WebTemplateCurrentLicense"), GetEditionName(CurrentEdition));
string currentPackagesNames = PackagesToString(CurrentPackages);
if (!String.IsNullOrEmpty(currentPackagesNames))
{
errorMessage += " " + string.Format(ResHelper.GetFileString("Install.WebTemplateCurrentPackages"), currentPackagesNames);
}
// Finish the sentence
errorMessage += ".
";
}
builder.Append("\n");
builder.Append("
\n");
builder.Append("\n\n");
builder.Append(" \n");
builder.Append(" | \n\n");
builder.Append("\n \n" + templateDisplayName + " \n");
builder.Append(" " + templateDescription + " " + errorMessage + " |
\n
\n
\n");
return builder.ToString();
}
private bool IsTemplateAllowed(string templateEditions, string templatePackages)
{
if (CurrentEdition != null)
{
// Check if the current license is suitable for webtemplate
if (templateEditions.Contains(CurrentEdition))
{
return true;
}
}
// Check license packages
if ((!String.IsNullOrEmpty(CurrentPackages))
&& (!String.IsNullOrEmpty(templatePackages)))
{
string[] tempCurrentPackages = CurrentPackages.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
string[] tempTemplatePackages = templatePackages.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
// Check if the current license (package) is suitable for webtemplate
foreach (string templateP in tempTemplatePackages)
{
foreach (string currentP in tempCurrentPackages)
{
if (templateP.EqualsCSafe(currentP, true))
{
return true;
}
}
}
}
return false;
}
private static string EditionsToString(string templateEditions)
{
string names = "";
string[] editions = templateEditions.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
bool isProfInNames = false;
foreach (string editionChar in editions)
{
try
{
// Add all editions to names
// Prof and Pro3x has same resource string, ensure that this string will be only once in list
ProductEditionEnum edition = LicenseKeyInfoProvider.CharToEdition(editionChar);
if ((edition != ProductEditionEnum.Professional) && (edition != ProductEditionEnum.Professional3x))
{
names += LicenseHelper.GetEditionName(edition) + ", ";
}
else if (!isProfInNames)
{
names += LicenseHelper.GetEditionName(edition) + ", ";
isProfInNames = true;
}
}
catch
{
return "#UNKNOWN#";
}
}
if (names == "")
{
return names;
}
else
{
return names.Substring(0, names.Length - 2);
}
}
private static string GetEditionName(string editionChar)
{
try
{
ProductEditionEnum edition = LicenseKeyInfoProvider.CharToEdition(editionChar);
return LicenseHelper.GetEditionName(edition);
}
catch
{
return "#UNKNOWN#";
}
}
///
/// Convert package codes to their names.
///
/// The packages in the format "ECM;SCN"
private static string PackagesToString(string packages)
{
string names = "";
if (!String.IsNullOrEmpty(packages))
{
string[] packageCodes = packages.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string package in packageCodes)
{
try
{
if (!String.IsNullOrEmpty(names))
{
names += ", ";
}
PackagesEnum packageEnum = PackagesEnumFunctions.ToEnum(package);
names += Enum.GetName(typeof(PackagesEnum), packageEnum);
}
catch
{
}
}
}
return names;
}
}