コード例 #1
0
    /// <summary>
    /// Grid external data bound handler.
    /// </summary>
    protected object gridFiles_OnExternalDataBound(object sender, string sourceName, object parameter)
    {
        // Get the data row view from parameter
        DataRowView drv = null;

        if (parameter is DataRowView)
        {
            drv = (DataRowView)parameter;
        }
        else if (parameter is GridViewRow)
        {
            // Get data from the grid view row
            GridViewRow gvr = (parameter as GridViewRow);
            if (gvr != null)
            {
                drv = (DataRowView)gvr.DataItem;
            }
        }

        // Get the action button
        CMSGridActionButton btn = null;

        if (sender is CMSGridActionButton)
        {
            btn = (CMSGridActionButton)sender;
        }

        switch (sourceName)
        {
        case "delete":
        {
            // Delete action
            int    siteId   = ValidationHelper.GetInteger(drv["AttachmentSiteID"], 0);
            string siteName = GetSiteName(siteId);

            Guid   guid      = ValidationHelper.GetGuid(drv["AttachmentGUID"], Guid.Empty);
            string extension = ValidationHelper.GetString(drv["AttachmentExtension"], "");

            // Check if the file is in DB
            bool db = ValidationHelper.GetBoolean(drv["HasBinary"], false);

            // Check if the file is in the file system
            bool   fs   = false;
            string path = AttachmentInfoProvider.GetFilePhysicalPath(siteName, guid.ToString(), extension);
            if (File.Exists(path))
            {
                fs = true;
            }

            // If the file is present in both file system and database, delete is allowed
            if (fs && db)
            {
                // If the files are stored in file system, delete is allowed in database
                if (StoreInFileSystem(siteId))
                {
                    btn.OnClientClick = btn.OnClientClick.Replace("'delete'", "'deleteindatabase'");
                    btn.ToolTip       = "Delete from database";
                    return(parameter);
                }
                // If the files are stored in database, delete is allowed in file system
                if (StoreInDatabase(siteId))
                {
                    btn.OnClientClick = btn.OnClientClick.Replace("'delete'", "'deleteinfilesystem'");
                    btn.ToolTip       = "Delete from file system";
                    return(parameter);
                }
            }

            btn.Visible = false;
        }
        break;

        case "copy":
        {
            // Delete action
            int    siteId   = ValidationHelper.GetInteger(drv["AttachmentSiteID"], 0);
            string siteName = GetSiteName(siteId);

            Guid   guid      = ValidationHelper.GetGuid(drv["AttachmentGUID"], Guid.Empty);
            string extension = ValidationHelper.GetString(drv["AttachmentExtension"], "");

            // Check if the file is in DB
            bool db = ValidationHelper.GetBoolean(drv["HasBinary"], false);

            // Check if the file is in the file system
            bool   fs   = false;
            string path = AttachmentInfoProvider.GetFilePhysicalPath(siteName, guid.ToString(), extension);
            if (File.Exists(path))
            {
                fs = true;
            }

            // If the file is stored in file system and the file is not present in database, copy to database is allowed
            if (fs && !db && StoreInDatabase(siteId) && StoreInFileSystem(siteId))
            {
                btn.OnClientClick = btn.OnClientClick.Replace("'copy'", "'copytodatabase'");
                btn.ToolTip       = "Copy to database";
                //btn.ImageUrl =
                return(parameter);
            }
            // If the file is stored in database and the file is not present in file system, copy to file system is allowed
            if (db && !fs && StoreInFileSystem(siteId))
            {
                btn.OnClientClick = btn.OnClientClick.Replace("'copy'", "'copytofilesystem'");
                btn.ToolTip       = "Copy to file system";
                //btn.ImageUrl =
                return(parameter);
            }

            btn.Visible = false;
        }
        break;

        case "name":
        {
            // Attachment name
            string name      = ValidationHelper.GetString(drv["AttachmentName"], "");
            Guid   guid      = ValidationHelper.GetGuid(drv["AttachmentGUID"], Guid.Empty);
            int    siteId    = ValidationHelper.GetInteger(drv["AttachmentSiteID"], 0);
            string extension = ValidationHelper.GetString(drv["AttachmentExtension"], "");

            // File name
            name = Path.GetFileNameWithoutExtension(name);

            string url = ResolveUrl("~/CMSPages/GetFile.aspx?guid=") + guid;
            if (siteId != currentSiteId)
            {
                // Add the site name to the URL if not current site
                SiteInfo si = SiteInfoProvider.GetSiteInfo(siteId);
                if (si != null)
                {
                    url += "&sitename=" + si.SiteName;
                }
            }

            string tooltipSpan = name;
            bool   isImage     = ImageHelper.IsImage(extension);
            if (isImage)
            {
                int imageWidth  = ValidationHelper.GetInteger(DataHelper.GetDataRowViewValue(drv, "AttachmentImageWidth"), 0);
                int imageHeight = ValidationHelper.GetInteger(DataHelper.GetDataRowViewValue(drv, "AttachmentImageHeight"), 0);

                string tooltip = UIHelper.GetTooltipAttributes(url, imageWidth, imageHeight, null, name, extension, null, null, 300);
                tooltipSpan = "<span id=\"" + guid + "\" " + tooltip + ">" + name + "</span>";
            }

            return(UIHelper.GetFileIcon(Page, extension, tooltip: name) + "&nbsp;<a href=\"" + url + "\" target=\"_blank\">" + tooltipSpan + "</a>");
        }

        case "size":
            // File size
            return(DataHelper.GetSizeString(ValidationHelper.GetInteger(parameter, 0)));

        case "yesno":
            // Yes / No
            return(UniGridFunctions.ColoredSpanYesNo(parameter));

        case "site":
        {
            int siteId = ValidationHelper.GetInteger(parameter, 0);
            if (siteId > 0)
            {
                SiteInfo si = SiteInfoProvider.GetSiteInfo(siteId);
                if (si != null)
                {
                    return(si.DisplayName);
                }
            }
            return(null);
        }

        case "storedinfilesystem":
        {
            // Delete action
            int    siteId   = ValidationHelper.GetInteger(drv["AttachmentSiteID"], 0);
            string siteName = GetSiteName(siteId);

            Guid   guid      = ValidationHelper.GetGuid(drv["AttachmentGUID"], Guid.Empty);
            string extension = ValidationHelper.GetString(drv["AttachmentExtension"], "");

            // Check if the file is in DB
            bool db = ValidationHelper.GetBoolean(drv["HasBinary"], false);

            // Check if the file is in the file system
            bool   fs   = false;
            string path = AttachmentInfoProvider.GetFilePhysicalPath(siteName, guid.ToString(), extension);
            if (File.Exists(path))
            {
                fs = true;
            }

            return(UniGridFunctions.ColoredSpanYesNo(fs));
        }
        }

        return(parameter);
    }