using System;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using CMS.CMSHelper;
using CMS.ExtendedControls;
using CMS.FormControls;
using CMS.GlobalHelper;
using CMS.SettingsProvider;
using CMS.DocumentEngine;
using CMS.UIControls;
using CMS.SiteProvider;
public partial class CMSModules_Content_Controls_Attachments_DocumentAttachments_DocumentAttachmentsList : AttachmentsControl
{
#region "Variables"
private string mInnerDivClass = "NewAttachment";
private int? mFilterLimit = null;
#endregion
#region "Properties"
///
/// Messages placeholder
///
public override MessagesPlaceHolder MessagesPlaceHolder
{
get
{
return plcMess;
}
}
///
/// Minimal count of entries for display filter.
///
public int FilterLimit
{
get
{
if (mFilterLimit == null)
{
mFilterLimit = ValidationHelper.GetInteger(SettingsHelper.AppSettings["CMSDefaultListingFilterLimit"], 25);
}
return (int)mFilterLimit;
}
set
{
mFilterLimit = value;
}
}
///
/// CSS class of the new attachment link.
///
public string InnerDivClass
{
get
{
return mInnerDivClass;
}
set
{
mInnerDivClass = value;
if (lblDisabled != null)
{
lblDisabled.CssClass = value + "Disabled";
}
}
}
///
/// Info label.
///
public Label InfoLabel
{
get
{
return MessagesPlaceHolder.InfoLabel;
}
}
///
/// Indicates whether grouped attachments should be displayed.
///
public override Guid GroupGUID
{
get
{
return base.GroupGUID;
}
set
{
base.GroupGUID = value;
if ((dsAttachments != null) && (newAttachmentElem != null))
{
dsAttachments.AttachmentGroupGUID = value;
newAttachmentElem.AttachmentGroupGUID = value;
}
}
}
///
/// Indicates if paging is allowed.
///
public override bool AllowPaging
{
get
{
return base.AllowPaging;
}
set
{
base.AllowPaging = value;
if (gridAttachments != null)
{
gridAttachments.Pager.DisplayPager = value;
gridAttachments.GridView.AllowPaging = value;
}
}
}
///
/// Defines size of the page for paging.
///
public override string PageSize
{
get
{
return base.PageSize;
}
set
{
base.PageSize = value;
if (gridAttachments != null)
{
gridAttachments.PageSize = value;
}
}
}
///
/// Default page size.
///
public override int DefaultPageSize
{
get
{
return base.DefaultPageSize;
}
set
{
base.DefaultPageSize = value;
if (gridAttachments != null)
{
gridAttachments.Pager.DefaultPageSize = value;
}
}
}
///
/// Width of the attachment.
///
public override int ResizeToWidth
{
get
{
return base.ResizeToWidth;
}
set
{
base.ResizeToWidth = value;
if (newAttachmentElem != null)
{
newAttachmentElem.ResizeToWidth = value;
}
}
}
///
/// Height of the attachment.
///
public override int ResizeToHeight
{
get
{
return base.ResizeToHeight;
}
set
{
base.ResizeToHeight = value;
if (newAttachmentElem != null)
{
newAttachmentElem.ResizeToHeight = value;
}
}
}
///
/// Maximal side size of the attachment.
///
public override int ResizeToMaxSideSize
{
get
{
return base.ResizeToMaxSideSize;
}
set
{
base.ResizeToMaxSideSize = value;
if (newAttachmentElem != null)
{
newAttachmentElem.ResizeToMaxSideSize = value;
}
}
}
///
/// List of allowed extensions.
///
public override string AllowedExtensions
{
get
{
return base.AllowedExtensions;
}
set
{
base.AllowedExtensions = value;
if (newAttachmentElem != null)
{
newAttachmentElem.AllowedExtensions = value;
}
}
}
///
/// Specifies the document for which the attachments should be displayed.
///
public override int DocumentID
{
get
{
return base.DocumentID;
}
set
{
base.DocumentID = value;
if (newAttachmentElem != null)
{
newAttachmentElem.DocumentID = value;
}
}
}
///
/// Specifies the version of the document (optional).
///
public int VersionHistoryID
{
get
{
if ((Node != null) && (Node.DocumentID > 0))
{
return Node.DocumentCheckedOutVersionHistoryID;
}
return 0;
}
}
///
/// Defines the form GUID; indicates that the temporary attachment will be handled.
///
public override Guid FormGUID
{
get
{
return base.FormGUID;
}
set
{
base.FormGUID = value;
if ((dsAttachments != null) && (newAttachmentElem != null))
{
dsAttachments.AttachmentFormGUID = value;
newAttachmentElem.FormGUID = value;
}
}
}
///
/// If true, control does not process the data.
///
public override bool StopProcessing
{
get
{
return base.StopProcessing;
}
set
{
base.StopProcessing = value;
if ((dsAttachments != null) && (newAttachmentElem != null))
{
dsAttachments.StopProcessing = value;
newAttachmentElem.StopProcessing = value;
}
}
}
///
/// Content container
///
public Panel Container
{
get
{
return pnlCont;
}
}
///
/// Label for workflow information
///
public Label WorkflowLabel
{
get
{
return lblWf;
}
}
#endregion
#region "Page events"
protected void Page_Load(object sender, EventArgs e)
{
if (dsAttachments == null)
{
StopProcessing = true;
}
Visible = !StopProcessing;
if (StopProcessing)
{
if (dsAttachments != null)
{
dsAttachments.StopProcessing = true;
}
if (newAttachmentElem != null)
{
newAttachmentElem.StopProcessing = true;
}
// Do nothing
}
else
{
// Register script for tooltips
if (ShowTooltip)
{
ScriptHelper.RegisterTooltip(Page);
}
// Ensure info message
if ((Request["__EVENTTARGET"] == hdnPostback.ClientID) || Request["__EVENTTARGET"] == hdnFullPostback.ClientID)
{
string action = Request["__EVENTARGUMENT"];
switch (action)
{
case "insert":
ShowConfirmation(GetString("attach.inserted"));
break;
case "update":
ShowConfirmation(GetString("attach.updated"));
break;
case "delete":
ShowConfirmation(GetString("attach.deleted"));
break;
default:
if (action != "")
{
UpdateEditParameters(action);
}
break;
}
}
#region "Scripts"
// Refresh script
string script = String.Format(@"
function RefreshUpdatePanel_{0}(hiddenFieldID, action) {{
var hiddenField = document.getElementById(hiddenFieldID);
if (hiddenField) {{
__doPostBack(hiddenFieldID, action);
}}
}}
function FullPageRefresh_{0}(action) {{
if(RefreshTree != null)
{{
RefreshTree();
}}
var hiddenField = document.getElementById('{1}');
if (hiddenField) {{
__doPostBack('{1}', action);
}}
}}
function InitRefresh_{0}(msg, fullRefresh, refreshTree, action)
{{
if((msg != null) && (msg != '')){{
alert(msg); action='error';
}}
if (fullRefresh) {{
FullPageRefresh_{0}(action);
}}
else {{
RefreshUpdatePanel_{0}('{2}', action);
}}
}}
function DeleteConfirmation() {{
return confirm({3});;
}}
", ClientID, hdnFullPostback.ClientID, hdnPostback.ClientID, ScriptHelper.GetString(GetString("attach.deleteconfirmation")));
ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "AttachmentScripts_" + ClientID, ScriptHelper.GetScript(script));
// Register dialog script
ScriptHelper.RegisterDialogScript(Page);
// Register jQuery script for thumbnails updating
ScriptHelper.RegisterJQuery(Page);
#endregion
// Initialize button for adding attachments
newAttachmentElem.ImageUrl = ResolveUrl(GetImageUrl("Design/Controls/DirectUploader/upload_new.png"));
newAttachmentElem.ImageWidth = 16;
newAttachmentElem.ImageHeight = 16;
newAttachmentElem.SourceType = MediaSourceEnum.DocumentAttachments;
newAttachmentElem.DocumentID = DocumentID;
newAttachmentElem.NodeParentNodeID = NodeParentNodeID;
newAttachmentElem.NodeClassName = NodeClassName;
newAttachmentElem.ResizeToWidth = ResizeToWidth;
newAttachmentElem.ResizeToHeight = ResizeToHeight;
newAttachmentElem.ResizeToMaxSideSize = ResizeToMaxSideSize;
newAttachmentElem.AttachmentGroupGUID = GroupGUID;
newAttachmentElem.FormGUID = FormGUID;
newAttachmentElem.AllowedExtensions = AllowedExtensions;
newAttachmentElem.ParentElemID = ClientID;
newAttachmentElem.ForceLoad = true;
newAttachmentElem.InnerDivHtml = GetString("attach.newattachment");
newAttachmentElem.InnerDivClass = InnerDivClass;
newAttachmentElem.InnerLoadingDivHtml = GetString("attach.loading");
newAttachmentElem.InnerLoadingDivClass = InnerLoadingDivClass;
newAttachmentElem.IsLiveSite = IsLiveSite;
newAttachmentElem.CheckPermissions = CheckPermissions;
lblDisabled.CssClass = InnerDivClass + "Disabled";
imgDisabled.ImageUrl = ResolveUrl(GetImageUrl("Design/Controls/DirectUploader/upload_newdisabled.png"));
// Grid initialization
gridAttachments.OnExternalDataBound += gridAttachments_OnExternalDataBound;
gridAttachments.OnDataReload += gridAttachments_OnDataReload;
gridAttachments.OnAction += gridAttachments_OnAction;
gridAttachments.OnBeforeDataReload += gridAttachments_OnBeforeDataReload;
gridAttachments.ZeroRowsText = GetString("attach.nodatafound");
gridAttachments.IsLiveSite = IsLiveSite;
gridAttachments.ShowActionsMenu = !IsLiveSite;
gridAttachments.Columns = "AttachmentID, AttachmentGUID, AttachmentImageWidth, AttachmentImageHeight, AttachmentExtension, AttachmentName, AttachmentSize, AttachmentOrder, AttachmentTitle, AttachmentDescription";
gridAttachments.OrderBy = "AttachmentOrder, AttachmentName, AttachmentID";
// Get all possible column names from appropriate info
IDataContainer info = (VersionHistoryID == 0) ? (IDataContainer)new AttachmentInfo() : new AttachmentHistoryInfo();
gridAttachments.AllColumns = SqlHelperClass.MergeColumns(info.ColumnNames).Replace("AttachmentHistoryID", "AttachmentID");
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (StopProcessing)
{
// Do nothing
}
else
{
lblWf.Visible = (lblWf.Text != string.Empty);
// Ensure uploader button
plcUploader.Visible = Enabled;
plcUploaderDisabled.Visible = !Enabled;
// Hide actions
gridAttachments.GridView.Columns[0].Visible = !HideActions;
gridAttachments.GridView.Columns[1].Visible = !HideActions;
newAttachmentElem.Visible = !HideActions && Enabled;
plcUploaderDisabled.Visible = !HideActions && !Enabled;
if (!RequestHelper.IsPostBack())
{
// Hide filter
if ((FilterLimit > 0) && (gridAttachments.RowsCount <= FilterLimit))
{
pnlFilter.Visible = false;
}
else
{
pnlFilter.Visible = true;
}
}
// Ensure correct layout
bool gridHasData = !DataHelper.DataSourceIsEmpty(gridAttachments.DataSource);
Visible = !HideActions || pnlFilter.Visible;
lblNoData.Visible = (!gridHasData && pnlFilter.Visible);
// Dialog for editing attachment
StringBuilder sb = new StringBuilder();
sb.AppendLine(String.Format(@"
function Edit_{0}(attachmentGUID, formGUID, versionHistoryID, parentId, hash, image) {{
var form = '';
if (formGUID != '') {{
form = '&formguid=' + formGUID + '&parentid=' + parentId;
}}
{1}
if (image) {{
modalDialog('{2}, 'editorDialog', 905, 670);
}}
else {{
modalDialog('{3}, 'editorDialog', 500, 350);
}}
return false;
}}",
ClientID,
(((Node != null) ? String.Format("else{{ form = '&siteid=' + {0}; }}", Node.NodeSiteID) : string.Empty)),
ResolveUrl((IsLiveSite ? "~/CMSFormControls/LiveSelectors/ImageEditor.aspx" : "~/CMSModules/Content/CMSDesk/Edit/ImageEditor.aspx") + "?attachmentGUID=' + attachmentGUID + '&versionHistoryID=' + versionHistoryID + form + '&clientid=" + ClientID + "&refresh=1&hash=' + hash"),
CMSContext.ResolveDialogUrl(String.Format("{0}?attachmentGUID=' + attachmentGUID + '&versionHistoryID=' + versionHistoryID + form + '&clientid={1}&refresh=1&hash=' + hash", (IsLiveSite ? "~/CMSModules/Content/Attachments/CMSPages/MetaDataEditor.aspx" : "~/CMSModules/Content/Attachments/Dialogs/MetaDataEditor.aspx"), ClientID))));
// Register script for editing attachment
ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "AttachmentEditScripts_" + ClientID, ScriptHelper.GetScript(sb.ToString()));
}
}
#endregion
#region "Public methods"
///
/// Indicates if the control contains some data.
///
public override bool HasData()
{
return (dsAttachments != null && !DataHelper.DataSourceIsEmpty(dsAttachments.DataSource));
}
#endregion
#region "Overridden methods"
///
/// Reloads data.
///
public override void ReloadData()
{
gridAttachments.ReloadData();
}
#endregion
#region "Private methods"
///
/// Updates parameters used by Edit button when displaying image editor.
///
/// GUID identifying attachment
private void UpdateEditParameters(string attachmentGuidString)
{
if (ShowTooltip)
{
// Try to get attachment GUID
Guid attGuid = ValidationHelper.GetGuid(attachmentGuidString, Guid.Empty);
if (attGuid != null)
{
// Get attachment
AttachmentInfo attInfo = AttachmentInfoProvider.GetAttachmentInfoWithoutBinary(attGuid, SiteName);
if (attInfo != null)
{
string attName = attInfo.AttachmentName;
int documentId = DocumentID;
// Get attachment URL
string attachmentUrl = null;
if (Node != null)
{
if (IsLiveSite && (documentId > 0))
{
attachmentUrl = CMSContext.ResolveUIUrl(TreePathUtils.GetAttachmentUrl(attGuid, URLHelper.GetSafeFileName(attName, CMSContext.CurrentSiteName), 0, null));
}
else
{
attachmentUrl = CMSContext.ResolveUIUrl(TreePathUtils.GetAttachmentUrl(attGuid, URLHelper.GetSafeFileName(attName, CMSContext.CurrentSiteName), VersionHistoryID, null));
}
}
else
{
attachmentUrl = ResolveUrl(DocumentHelper.GetAttachmentUrl(attGuid, VersionHistoryID));
}
attachmentUrl = URLHelper.UpdateParameterInUrl(attachmentUrl, "chset", Guid.NewGuid().ToString());
// Ensure correct URL for non-temporary attachments
if ((OriginalNodeSiteName != CMSContext.CurrentSiteName) && (documentId > 0))
{
attachmentUrl = URLHelper.AddParameterToUrl(attachmentUrl, "sitename", OriginalNodeSiteName);
}
// Generate new tooltip command
string newToolTip = null;
string title = attInfo.AttachmentTitle;
string description = attInfo.AttachmentDescription;
// Optionally trim attachment name
string attachmentName = TextHelper.LimitLength(attInfo.AttachmentName, ATTACHMENT_NAME_LIMIT, "...");
int imageWidth = attInfo.AttachmentImageWidth;
int imageHeight = attInfo.AttachmentImageHeight;
bool isImage = ImageHelper.IsImage(attInfo.AttachmentExtension);
int tooltipWidth = 300;
string url = isImage ? attachmentUrl : null;
string tooltipBody = UIHelper.GetTooltip(url, imageWidth, imageHeight, title, attachmentName, description, null, ref tooltipWidth);
if (!string.IsNullOrEmpty(tooltipBody))
{
newToolTip = String.Format("Tip('{0}', WIDTH, -300)", tooltipBody);
}
// Get update script
string updateScript = String.Format("$j(\"#{0}\").attr('onmouseover', '').unbind('mouseover').mouseover(function(){{ {1} }});", attGuid, newToolTip);
// Execute update
ScriptHelper.RegisterStartupScript(Page, typeof(Page), "AttachmentUpdateEdit", ScriptHelper.GetScript(updateScript));
}
}
}
}
private string GetWhereConditionInternal()
{
return string.IsNullOrEmpty(txtFilter.Text) ? null : String.Format("AttachmentName LIKE '%{0}%'", SqlHelperClass.GetSafeQueryString(txtFilter.Text, false));
}
#endregion
#region "Grid events"
protected DataSet gridAttachments_OnDataReload(string completeWhere, string currentOrder, int currentTopN, string columns, int currentOffset, int currentPageSize, ref int totalRecords)
{
if (Node != null)
{
dsAttachments.Path = Node.NodeAliasPath;
// Prefer culture from the node
dsAttachments.CultureCode = !string.IsNullOrEmpty(Node.DocumentCulture) ? Node.DocumentCulture : CMSContext.PreferredCultureCode;
dsAttachments.SiteName = SiteInfoProvider.GetSiteName(Node.OriginalNodeSiteID);
}
// Versioned attachments
dsAttachments.DocumentVersionHistoryID = VersionHistoryID;
dsAttachments.AttachmentGroupGUID = GroupGUID;
dsAttachments.AttachmentFormGUID = FormGUID;
dsAttachments.TopN = currentTopN;
dsAttachments.WhereCondition = GetWhereConditionInternal();
dsAttachments.SelectedColumns = columns;
dsAttachments.OrderBy = currentOrder;
dsAttachments.LoadData(true);
// Ensure right column name (for attachments under workflow)
if (!DataHelper.DataSourceIsEmpty(dsAttachments.DataSource))
{
DataSet ds = (DataSet)dsAttachments.DataSource;
if (ds != null)
{
DataTable dt = (ds).Tables[0];
if (!dt.Columns.Contains("AttachmentFormGUID"))
{
dt.Columns.Add("AttachmentFormGUID");
}
}
}
return (DataSet)dsAttachments.DataSource;
}
protected void gridAttachments_OnBeforeDataReload()
{
gridAttachments.IsLiveSite = IsLiveSite;
gridAttachments.GridView.AllowPaging = AllowPaging;
if (!AllowPaging)
{
gridAttachments.PageSize = "0";
}
gridAttachments.GridView.AllowSorting = false;
}
///
/// UniGrid action buttons event handler.
///
protected void gridAttachments_OnAction(string actionName, object actionArgument)
{
if (Enabled && !HideActions)
{
#region "Check permissions"
if (CheckPermissions)
{
if (FormGUID != Guid.Empty)
{
if (!RaiseOnCheckPermissions("Create", this))
{
if (!CMSContext.CurrentUser.IsAuthorizedToCreateNewDocument(NodeParentNodeID, NodeClassName))
{
ShowError(GetString("attach.actiondenied"));
return;
}
}
}
else
{
if (!RaiseOnCheckPermissions("Modify", this))
{
if (CMSContext.CurrentUser.IsAuthorizedPerDocument(Node, NodePermissionsEnum.Modify) == AuthorizationResultEnum.Denied)
{
ShowError(GetString("attach.actiondenied"));
return;
}
}
}
}
#endregion
Guid attachmentGuid = Guid.Empty;
// Get action argument (Guid or int)
if (ValidationHelper.IsGuid(actionArgument))
{
attachmentGuid = ValidationHelper.GetGuid(actionArgument, Guid.Empty);
}
// Process proper action
switch (actionName.ToLowerCSafe())
{
case "moveup":
if (attachmentGuid != Guid.Empty)
{
// Move attachment up
if (FormGUID == Guid.Empty)
{
PerformAttachmentAction("moveup", attachmentGuid, () => { DocumentHelper.MoveAttachmentUp(attachmentGuid, Node); });
}
else
{
AttachmentInfoProvider.MoveAttachmentUp(attachmentGuid, 0);
}
}
break;
case "movedown":
if (attachmentGuid != Guid.Empty)
{
// Move attachment down
if (FormGUID == Guid.Empty)
{
PerformAttachmentAction("movedown", attachmentGuid, () => { DocumentHelper.MoveAttachmentDown(attachmentGuid, Node); });
}
else
{
AttachmentInfoProvider.MoveAttachmentDown(attachmentGuid, 0);
}
}
break;
case "delete":
if (attachmentGuid != Guid.Empty)
{
// Delete attachment
if (FormGUID == Guid.Empty)
{
PerformAttachmentAction("delete", attachmentGuid, () => { DocumentHelper.DeleteAttachment(Node, attachmentGuid, TreeProvider); });
}
else
{
AttachmentInfoProvider.DeleteTemporaryAttachment(attachmentGuid, CMSContext.CurrentSiteName);
}
ShowConfirmation(GetString("attach.deleted"));
}
break;
}
}
}
private void PerformAttachmentAction(string actionName, Guid attachmentGuid, Action action)
{
// Store original values
var originalStep = Node.WorkflowStep;
var wasArchived = Node.IsArchived;
var wasInPublishedStep = Node.IsInPublishStep;
// Ensure automatic check-in/ check-out
VersionManager vm = null;
bool checkin = false;
// Check out the document
if (AutoCheck)
{
vm = VersionManager.GetInstance(TreeProvider);
var step = vm.CheckOut(Node, Node.IsPublished, true);
// Do not check-in document if not under a workflow anymore
checkin = (step != null);
}
// Perform action
if (action != null)
{
action();
}
// Check in the document
if (AutoCheck)
{
if ((vm != null) && checkin && (Node.DocumentWorkflowStepID != 0))
{
vm.CheckIn(Node, null, null);
}
// Document state changed
bool fullRefresh = (originalStep == null) || originalStep.StepIsArchived || originalStep.StepIsPublished ||
(wasInPublishedStep != Node.IsInPublishStep) || (wasArchived != Node.IsArchived) || (WorkflowManager.GetNodeWorkflow(Node) == null);
// Ensure full page refresh
ScriptHelper.RegisterStartupScript(Page, typeof(Page), actionName + "Refresh", ScriptHelper.GetScript(String.Format("InitRefresh_{0}('', {2}, false, '{1}');", ClientID, actionName, fullRefresh ? "true" : "false")));
// Clear document manager properties
DocumentManager.ClearProperties();
}
// Log synchronization task if not under workflow
if (!UsesWorkflow)
{
DocumentSynchronizationHelper.LogDocumentChange(Node, TaskTypeEnum.UpdateDocument, TreeProvider);
}
}
///
/// UniGrid external data bound.
///
protected object gridAttachments_OnExternalDataBound(object sender, string sourceName, object parameter)
{
DataRowView rowView = null;
string attName = null;
string attachmentExt = null;
switch (sourceName.ToLowerCSafe())
{
case "clone":
ImageButton imgClone = sender as ImageButton;
if (imgClone != null)
{
if (this.IsLiveSite)
{
// Hide cloning on live site
imgClone.Visible = false;
return imgClone;
}
string objectType = null;
if (VersionHistoryID > 0)
{
objectType = PredefinedObjectType.ATTACHMENTHISTORY;
}
else
{
objectType = PredefinedObjectType.ATTACHMENT;
}
int id = ValidationHelper.GetInteger(((DataRowView)((GridViewRow)parameter).DataItem).Row["AttachmentID"], 0);
imgClone.PreRender += new EventHandler(imgClone_PreRender);
imgClone.OnClientClick = "modalDialog('" + URLHelper.ResolveUrl("~/CMSModules/Objects/Dialogs/CloneObjectDialog.aspx?objectType=" + objectType + "&objectId=" + id) + "', 'CloneObject', 750, 400); return false;";
}
break;
case "update":
Panel pnlBlock = new Panel { ID = "pnlBlock" };
bool isWebDAVEnabled = CMSContext.IsWebDAVEnabled(SiteName);
bool isWindowsAuthentication = RequestHelper.IsWindowsAuthentication();
pnlBlock.Style.Add("margin", "0 auto");
pnlBlock.Width = ((isWebDAVEnabled && isWindowsAuthentication) ? 32 : 16);
// Add disabled image
ImageButton imgUpdate = new ImageButton { ID = "imgUpdate" };
imgUpdate.PreRender += imgUpdate_PreRender;
pnlBlock.Controls.Add(imgUpdate);
// Add update control
// Dynamically load uploader control
DirectFileUploader dfuElem = Page.LoadUserControl("~/CMSModules/Content/Controls/Attachments/DirectFileUploader/DirectFileUploader.ascx") as DirectFileUploader;
rowView = parameter as DataRowView;
// Set uploader's properties
if (dfuElem != null)
{
dfuElem.SourceType = MediaSourceEnum.DocumentAttachments;
dfuElem.ID = "dfuElem" + DocumentID;
dfuElem.IsLiveSite = IsLiveSite;
dfuElem.EnableSilverlightUploader = false;
dfuElem.ControlGroup = "update";
dfuElem.AttachmentGUID = GetAttachmentGuid(rowView);
dfuElem.DisplayInline = true;
dfuElem.UploadMode = MultifileUploaderModeEnum.DirectSingle;
dfuElem.InnerLoadingDivHtml = " ";
dfuElem.MaxNumberToUpload = 1;
dfuElem.Height = 16;
dfuElem.Width = 16;
dfuElem.PreRender += dfuElem_PreRender;
pnlBlock.Controls.Add(dfuElem);
}
attName = GetAttachmentName(rowView);
attachmentExt = GetAttachmentExtension(rowView);
int nodeGroupId = (Node != null) ? Node.GetIntegerValue("NodeGroupID") : 0;
// Check if WebDAV allowed by the form
bool allowWebDAV = (Form == null) || Form.AllowWebDAV;
// Add WebDAV edit control
if (allowWebDAV && isWebDAVEnabled && isWindowsAuthentication && (FormGUID == Guid.Empty) && WebDAVSettings.IsExtensionAllowedForEditMode(attachmentExt, SiteName))
{
// Dynamically load control
WebDAVEditControl webdavElem = Page.LoadUserControl("~/CMSModules/WebDAV/Controls/AttachmentWebDAVEditControl.ascx") as WebDAVEditControl;
// Set editor's properties
if (webdavElem != null)
{
webdavElem.Enabled = Enabled;
webdavElem.ID = "webdavElem" + DocumentID;
// Ensure form identification
if ((Form != null) && (Form.Parent != null))
{
webdavElem.FormID = Form.Parent.ClientID;
}
webdavElem.SiteName = SiteName;
webdavElem.FileName = attName;
webdavElem.NodeAliasPath = Node.NodeAliasPath;
webdavElem.NodeCultureCode = Node.DocumentCulture;
webdavElem.PreRender += webdavElem_PreRender;
if (FieldInfo != null)
{
webdavElem.AttachmentFieldName = FieldInfo.Name;
}
// Set Group ID for live site
webdavElem.GroupID = IsLiveSite ? nodeGroupId : 0;
webdavElem.IsLiveSite = IsLiveSite;
// Align left if WebDAV is enabled and windows authentication is enabled
bool isRTL = (IsLiveSite && CultureHelper.IsPreferredCultureRTL()) || (!IsLiveSite && CultureHelper.IsUICultureRTL());
pnlBlock.Style.Add("text-align", isRTL ? "right" : "left");
pnlBlock.Controls.Add(webdavElem);
}
}
return pnlBlock;
case "edit":
// Get file extension
string extension = ValidationHelper.GetString(((DataRowView)((GridViewRow)parameter).DataItem).Row["AttachmentExtension"], string.Empty).ToLowerCSafe();
Guid guid = ValidationHelper.GetGuid(((DataRowView)((GridViewRow)parameter).DataItem).Row["AttachmentGUID"], Guid.Empty);
ImageButton img = sender as ImageButton;
if (img != null)
{
img.AlternateText = String.Format("{0}|{1}", extension, guid);
img.PreRender += img_PreRender;
}
break;
case "delete":
ImageButton imgDelete = sender as ImageButton;
if (imgDelete != null)
{
// Turn off validation
imgDelete.CausesValidation = false;
imgDelete.PreRender += imgDelete_PreRender;
// Explicitly initialize confirmation
imgDelete.OnClientClick = "if(DeleteConfirmation() == false){return false;}";
}
break;
case "moveup":
ImageButton imgUp = sender as ImageButton;
if (imgUp != null)
{
// Turn off validation
imgUp.CausesValidation = false;
imgUp.PreRender += imgUp_PreRender;
}
break;
case "movedown":
ImageButton imgDown = sender as ImageButton;
if (imgDown != null)
{
// Turn off validation
imgDown.CausesValidation = false;
imgDown.PreRender += imgDown_PreRender;
}
break;
case "attachmentname":
{
rowView = parameter as DataRowView;
// Get attachment GUID
Guid attachmentGuid = GetAttachmentGuid(rowView);
// Get attachment extension
attachmentExt = GetAttachmentExtension(rowView);
bool isImage = ImageHelper.IsImage(attachmentExt);
string iconUrl = GetFileIconUrl(attachmentExt, "List");
// Get link for attachment
string attachmentUrl = null;
attName = ValidationHelper.GetString(rowView["AttachmentName"], string.Empty);
int documentId = DocumentID;
if (Node != null)
{
if (IsLiveSite && (documentId > 0))
{
attachmentUrl = CMSContext.ResolveUIUrl(TreePathUtils.GetAttachmentUrl(attachmentGuid, URLHelper.GetSafeFileName(attName, CMSContext.CurrentSiteName), 0, null));
}
else
{
attachmentUrl = CMSContext.ResolveUIUrl(TreePathUtils.GetAttachmentUrl(attachmentGuid, URLHelper.GetSafeFileName(attName, CMSContext.CurrentSiteName), VersionHistoryID, null));
}
}
else
{
attachmentUrl = ResolveUrl(DocumentHelper.GetAttachmentUrl(attachmentGuid, VersionHistoryID));
}
attachmentUrl = URLHelper.UpdateParameterInUrl(attachmentUrl, "chset", Guid.NewGuid().ToString());
// Ensure correct URL for non-temporary attachments
if ((OriginalNodeSiteName != CMSContext.CurrentSiteName) && (documentId > 0))
{
attachmentUrl = URLHelper.AddParameterToUrl(attachmentUrl, "sitename", OriginalNodeSiteName);
}
// Add latest version requirement for live site
if (IsLiveSite && (documentId > 0))
{
// Add requirement for latest version of files for current document
string newparams = "latestfordocid=" + documentId;
newparams += "&hash=" + ValidationHelper.GetHashString("d" + documentId);
attachmentUrl += "&" + newparams;
}
// Optionally trim attachment name
string attachmentName = TextHelper.LimitLength(attName, ATTACHMENT_NAME_LIMIT, "...");
// Tooltip
string tooltip = null;
if (ShowTooltip)
{
string title = ValidationHelper.GetString(DataHelper.GetDataRowViewValue(rowView, "AttachmentTitle"), string.Empty);
string description = ValidationHelper.GetString(DataHelper.GetDataRowViewValue(rowView, "AttachmentDescription"), string.Empty);
int imageWidth = ValidationHelper.GetInteger(DataHelper.GetDataRowViewValue(rowView, "AttachmentImageWidth"), 0);
int imageHeight = ValidationHelper.GetInteger(DataHelper.GetDataRowViewValue(rowView, "AttachmentImageHeight"), 0);
tooltip = UIHelper.GetTooltipAttributes(attachmentUrl, imageWidth, imageHeight, title, attachmentName, attachmentExt, description, null, 300);
}
// Icon
string imageTag = String.Format("
", iconUrl, attachmentName);
if (isImage)
{
return String.Format("{3}{4}", attachmentUrl, attachmentGuid, tooltip, imageTag, attachmentName);
}
else
{
attachmentUrl = URLHelper.AddParameterToUrl(attachmentUrl, "disposition", "attachment");
return String.Format("{3}{4}", attachmentUrl, attachmentGuid, tooltip, imageTag, attachmentName);
}
}
case "attachmentsize":
long size = ValidationHelper.GetLong(parameter, 0);
return DataHelper.GetSizeString(size);
}
return parameter;
}
#endregion
#region "Grid actions' events"
protected void dfuElem_PreRender(object sender, EventArgs e)
{
DirectFileUploader dfuElem = (DirectFileUploader)sender;
if (Enabled)
{
dfuElem.ForceLoad = true;
dfuElem.FormGUID = FormGUID;
dfuElem.AttachmentGroupGUID = GroupGUID;
dfuElem.DocumentID = DocumentID;
dfuElem.NodeParentNodeID = NodeParentNodeID;
dfuElem.NodeClassName = NodeClassName;
dfuElem.ResizeToWidth = ResizeToWidth;
dfuElem.ResizeToHeight = ResizeToHeight;
dfuElem.ResizeToMaxSideSize = ResizeToMaxSideSize;
dfuElem.AllowedExtensions = AllowedExtensions;
dfuElem.ImageUrl = ResolveUrl(GetImageUrl("Design/Controls/DirectUploader/upload.png"));
dfuElem.ImageHeight = 16;
dfuElem.ImageWidth = 16;
dfuElem.InsertMode = false;
dfuElem.ParentElemID = ClientID;
dfuElem.CheckPermissions = CheckPermissions;
dfuElem.IsLiveSite = IsLiveSite;
dfuElem.UploadMode = MultifileUploaderModeEnum.DirectSingle;
dfuElem.MaxNumberToUpload = 1;
dfuElem.Height = 16;
dfuElem.Width = 16;
}
else
{
dfuElem.Visible = false;
}
}
protected void webdavElem_PreRender(object sender, EventArgs e)
{
WebDAVEditControl wdc = sender as WebDAVEditControl;
if (wdc != null)
{
wdc.Enabled = Enabled;
}
}
void imgClone_PreRender(object sender, EventArgs e)
{
ImageButton imgClone = (ImageButton)sender;
if (!Enabled || (DocumentID <= 0))
{
imgClone.Style.Add("cursor", "default");
imgClone.Enabled = false;
}
}
protected void imgUpdate_PreRender(object sender, EventArgs e)
{
ImageButton imgUpdate = (ImageButton)sender;
if (!Enabled)
{
imgUpdate.ImageUrl = ResolveUrl(GetImageUrl("Design/Controls/DirectUploader/uploaddisabled.png"));
imgUpdate.Style.Add("cursor", "default");
imgUpdate.CssClass = "InlineIcon";
imgUpdate.Enabled = false;
}
else
{
imgUpdate.Visible = false;
}
}
protected void img_PreRender(object sender, EventArgs e)
{
ImageButton img = (ImageButton)sender;
if (CMSContext.CurrentUser.IsAuthenticated())
{
if (!Enabled)
{
// Disable edit icon
img.ImageUrl = GetImageUrl("Design/Controls/UniGrid/Actions/editdisabled.png");
img.Enabled = false;
img.Style.Add("cursor", "default");
}
else
{
string[] args = img.AlternateText.Split('|');
string strForm = (FormGUID == Guid.Empty) ? string.Empty : FormGUID.ToString();
string form = null;
if (!String.IsNullOrEmpty(strForm))
{
form = String.Format("&formguid={0}&parentid={1}", strForm, NodeParentNodeID);
}
else
{
if (Node != null)
{
form += "&siteid=" + Node.NodeSiteID;
}
}
string isImage = ImageHelper.IsSupportedByImageEditor(args[0]) ? "true" : "false";
// Prepare parameters
string parameters = String.Format("?attachmentGUID={0}&versionHistoryID={1}{2}&clientid={3}&refresh=1", args[1], VersionHistoryID, form, ClientID);
// Create security hash
string validationHash = QueryHelper.GetHash(parameters);
img.Attributes.Add("onclick", String.Format("Edit_{0}('{1}', '{2}', '{3}', {4}, '{5}', {6});return false;", ClientID, args[1], strForm, VersionHistoryID, NodeParentNodeID, validationHash, isImage));
}
img.AlternateText = GetString("general.edit");
}
else
{
img.Visible = false;
}
}
protected void imgDown_PreRender(object sender, EventArgs e)
{
ImageButton imgDown = (ImageButton)sender;
if (!Enabled || !AllowChangeOrder)
{
// Disable move down icon in case that editing is not allowed
imgDown.ImageUrl = GetImageUrl("Design/Controls/UniGrid/Actions/downdisabled.png");
imgDown.Enabled = false;
imgDown.Style.Add("cursor", "default");
}
}
protected void imgUp_PreRender(object sender, EventArgs e)
{
ImageButton imgUp = (ImageButton)sender;
if (!Enabled || !AllowChangeOrder)
{
// Disable move up icon in case that editing is not allowed
imgUp.ImageUrl = GetImageUrl("Design/Controls/UniGrid/Actions/updisabled.png");
imgUp.Enabled = false;
imgUp.Style.Add("cursor", "default");
}
}
protected void imgDelete_PreRender(object sender, EventArgs e)
{
ImageButton imgDelete = (ImageButton)sender;
if (!Enabled)
{
// Disable delete icon in case that editing is not allowed
imgDelete.ImageUrl = GetImageUrl("Design/Controls/UniGrid/Actions/deletedisabled.png");
imgDelete.Enabled = false;
imgDelete.Style.Add("cursor", "default");
}
}
#endregion
#region "Helper methods"
///
/// Gets GUID value from DataRowView.
///
/// Row cell
/// GUID of attachment
protected static Guid GetAttachmentGuid(DataRowView drv)
{
// Get GUID of attachment
return ValidationHelper.GetGuid(DataHelper.GetDataRowViewValue(drv, "AttachmentGUID"), Guid.Empty);
}
///
/// Gets attachment name from DataRowView.
///
/// Row cell
/// Attachment name
protected static string GetAttachmentName(DataRowView drv)
{
// Get GUID of attachment
return ValidationHelper.GetString(DataHelper.GetDataRowViewValue(drv, "AttachmentName"), string.Empty);
}
///
/// Gets extension value from DataRowView.
///
/// Row view
/// Extension of attachment
protected static string GetAttachmentExtension(DataRowView drv)
{
// Get extension of attachment
return ValidationHelper.GetString(DataHelper.GetDataRowViewValue(drv, "AttachmentExtension"), string.Empty);
}
#endregion
}