using System;
using System.Data;
using System.Collections;
using System.Text;
using System.Web.UI.WebControls;
using CMS.CMSHelper;
using CMS.CMSImportExport;
using CMS.Controls;
using CMS.GlobalHelper;
using CMS.LicenseProvider;
using CMS.SettingsProvider;
using CMS.UIControls;
public partial class CMSModules_ImportExport_Controls_ImportGridTasks : CMSUserControl, IUniPageable
{
#region "Variables"
// Indicates if object selection is enabled
protected bool selectionEnabled = true;
protected string codeNameColumnName = "";
protected string displayNameColumnName = "";
protected string preposition = "chck_";
protected string taskPreposition = "etchck_";
private SiteImportSettings mSettings = null;
protected ArrayList mExistingObjects = null;
private StringBuilder sbAvailable = new StringBuilder();
#endregion
#region "Public properties"
///
/// Import settings.
///
public SiteImportSettings Settings
{
get
{
return mSettings;
}
set
{
mSettings = value;
}
}
///
/// Existing objects in the database.
///
public ArrayList ExistingObjects
{
get
{
if (mExistingObjects == null)
{
DataSet ds = ImportProvider.GetExistingObjects(Settings, ObjectType, SiteObject, true);
if (!DataHelper.DataSourceIsEmpty(ds))
{
// Get info object
GeneralizedInfo infoObj = CMSObjectHelper.GetReadOnlyObject(ObjectType);
if (infoObj == null)
{
throw new Exception("[ImportGridView]: Object type '" + ObjectType + "' not found.");
}
int colIndex = ds.Tables[0].Columns.IndexOf(infoObj.CodeNameColumn);
if (colIndex >= 0)
{
// For each object get codename
foreach (DataRow dr in ds.Tables[0].Rows)
{
string codeName = ValidationHelper.GetString(dr[colIndex], null);
if (codeName != null)
{
// Initialize array list
if (mExistingObjects == null)
{
mExistingObjects = new ArrayList();
}
mExistingObjects.Add(codeName.ToLowerCSafe());
}
}
}
}
}
return mExistingObjects;
}
}
///
/// Current object type.
///
public string ObjectType
{
get
{
return ValidationHelper.GetString(ViewState["ObjectType"], null);
}
set
{
ViewState["ObjectType"] = value;
}
}
///
/// True if site object.
///
public bool SiteObject
{
get
{
return ValidationHelper.GetBoolean(ViewState["SiteObject"], false);
}
set
{
ViewState["SiteObject"] = value;
}
}
///
/// Data source.
///
public DataSet DataSource
{
get;
set;
}
///
/// Pager control.
///
public UniGridPager PagerControl
{
get
{
return pagerElem;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
pagerElem.PagedControl = this;
if (RequestHelper.IsPostBack())
{
if (Settings != null)
{
// Process the results of the available tasks
string[] available = hdnAvailableTasks.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
if (available != null)
{
foreach (string item in available)
{
int taskId = ValidationHelper.GetInteger(item, 0);
string name = GetCheckBoxName(taskId);
if (Request.Form[name] == null)
{
// Unchecked
Settings.DeselectTask(ObjectType, taskId, SiteObject);
}
else
{
// Checked
Settings.SelectTask(ObjectType, taskId, SiteObject);
}
}
}
}
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
// Render the available task IDs
if (sbAvailable != null)
{
hdnAvailableTasks.Value = sbAvailable.ToString();
}
else
{
hdnAvailableTasks.Value = "";
}
}
///
/// Returns the ID for the checkbox.
///
/// Task ID
protected string GetCheckBoxId(object taskId)
{
if (sbAvailable == null)
{
sbAvailable = new StringBuilder();
}
else
{
sbAvailable.Append(";");
}
sbAvailable.Append(taskId);
return taskPreposition + taskId;
}
///
/// Returns the name for the checkbox.
///
/// Task ID
protected string GetCheckBoxName(object taskId)
{
return taskPreposition + taskId;
}
///
/// Bind the data.
///
public void Bind()
{
if (!string.IsNullOrEmpty(ObjectType))
{
pnlGrid.Visible = true;
selectionEnabled = ((ObjectType != LicenseObjectType.LICENSEKEY) || !Settings.IsOlderVersion);
lblTasks.Text = GetString("Export.Tasks");
if (selectionEnabled)
{
// Initilaize strings
btnAllTasks.Text = GetString("ImportExport.All");
btnNoneTasks.Text = GetString("export.none");
}
pnlTaskLinks.Visible = selectionEnabled;
// Get object info
GeneralizedInfo info = CMSObjectHelper.GetReadOnlyObject(ObjectType);
if (info != null)
{
plcGrid.Visible = true;
codeNameColumnName = info.CodeNameColumn;
displayNameColumnName = info.DisplayNameColumn;
// Task fields
TemplateField taskCheckBoxField = (TemplateField)gvTasks.Columns[0];
taskCheckBoxField.HeaderText = GetString("General.Process");
BoundField titleField = (BoundField)gvTasks.Columns[1];
titleField.HeaderText = GetString("Export.TaskTitle");
BoundField typeField = (BoundField)gvTasks.Columns[2];
typeField.HeaderText = GetString("general.type");
BoundField timeField = (BoundField)gvTasks.Columns[3];
timeField.HeaderText = GetString("Export.TaskTime");
// Load tasks
DataSet ds = DataSource;
if (!DataHelper.DataSourceIsEmpty(ds) && !DataHelper.DataSourceIsEmpty(ds.Tables["Export_Task"]) && (ValidationHelper.GetBoolean(Settings.GetSettings(ImportExportHelper.SETTINGS_TASKS), true)))
{
plcTasks.Visible = true;
gvTasks.DataSource = ds.Tables["Export_Task"];
// Set correct ID for direct page contol
pagerElem.DirectPageControlID = ((float)ds.Tables["Export_Task"].Rows.Count / pagerElem.CurrentPageSize > 20.0f) ? "txtPage" : "drpPage";
// Call page binding event
if (OnPageBinding != null)
{
OnPageBinding(this, null);
}
gvTasks.DataBind();
}
else
{
plcTasks.Visible = false;
}
}
else
{
plcGrid.Visible = false;
}
// Disable license selection
bool enable = !((ObjectType == LicenseObjectType.LICENSEKEY) && Settings.IsOlderVersion);
gvTasks.Enabled = enable;
pnlTaskLinks.Enabled = enable;
}
else
{
pnlGrid.Visible = false;
}
}
///
/// Ensure tasks preselection.
///
/// Task ID
protected string IsTaskChecked(object taskId)
{
string value = "";
int id = ValidationHelper.GetInteger(taskId, 0);
if (Settings.IsTaskSelected(ObjectType, id, SiteObject))
{
value += " checked=\"checked\" ";
}
if (!selectionEnabled)
{
value += " disabled=\"disabled\" ";
}
return value;
}
protected void btnAll_Click(object sender, EventArgs e)
{
// Load all selection
Settings.LoadDefaultSelection(ObjectType, SiteObject, ImportTypeEnum.All, false, true);
}
protected void btnNone_Click(object sender, EventArgs e)
{
// Load none selection
Settings.LoadDefaultSelection(ObjectType, SiteObject, ImportTypeEnum.None, false, true);
}
#region "IUniPageable Members"
///
/// Pager data item.
///
public object PagerDataItem
{
get
{
return gvTasks.DataSource;
}
set
{
gvTasks.DataSource = value;
}
}
///
/// Pager control.
///
public UniPager UniPagerControl
{
get;
set;
}
public int PagerForceNumberOfResults
{
get
{
return -1;
}
set
{
}
}
///
/// Occurs when the control bind data.
///
public event EventHandler OnPageBinding;
///
/// Occurs when the pager change the page and current mode is postback => reload data
///
public event EventHandler OnPageChanged;
///
/// Evokes control databind.
///
public virtual void ReBind()
{
if (OnPageChanged != null)
{
OnPageChanged(this, null);
}
}
#endregion
}