Esempio n. 1
0
    /// <summary>
    /// Gets and bulk updates web part layouts. Called when the "Get and bulk update layouts" button is pressed.
    /// Expects the CreateWebPartLayout method to be run first.
    /// </summary>
    private bool GetAndBulkUpdateWebPartLayouts()
    {
        // Prepare the parameters
        string where = "WebPartLayoutCodeName LIKE N'MyNewLayout%'";

        // Get the data
        DataSet layouts = WebPartLayoutInfoProvider.GetWebPartLayouts(where, null);

        if (!DataHelper.DataSourceIsEmpty(layouts))
        {
            // Loop through the individual items
            foreach (DataRow layoutDr in layouts.Tables[0].Rows)
            {
                // Create object from DataRow
                WebPartLayoutInfo modifyLayout = new WebPartLayoutInfo(layoutDr);

                // Update the properties
                modifyLayout.WebPartLayoutDisplayName = modifyLayout.WebPartLayoutDisplayName.ToUpper();

                // Save the changes
                WebPartLayoutInfoProvider.SetWebPartLayoutInfo(modifyLayout);
            }

            return(true);
        }

        return(false);
    }
    /// <summary>
    /// Save layout code.
    /// </summary>
    protected bool SaveData()
    {
        // Remove "." due to virtual path provider replacement
        txtCodeName.Text = txtCodeName.Text.Replace(".", "");

        txtDisplayName.Text = txtDisplayName.Text.Trim();
        txtCodeName.Text    = txtCodeName.Text.Trim();

        string errorMessage = new Validator()
                              .NotEmpty(txtCodeName.Text, rfvCodeName.ErrorMessage)
                              .NotEmpty(txtDisplayName.Text, rfvDisplayName.ErrorMessage)
                              .IsCodeName(txtCodeName.Text, GetString("general.invalidcodename")).Result;

        int         webPartId   = ValidationHelper.GetInteger(Request.QueryString["webpartId"], 0);
        WebPartInfo webPartInfo = WebPartInfoProvider.GetWebPartInfo(webPartId);

        if (webPartInfo == null)
        {
            errorMessage = GetString("WebPartEditLayoutEdit.InvalidWebPartID");
        }

        if (errorMessage != String.Empty)
        {
            lblError.Text    = errorMessage;
            lblError.Visible = true;
            return(false);
        }

        // Get layout info
        WebPartLayoutInfo webPartLayoutInfo = WebPartLayoutInfoProvider.GetWebPartLayoutInfo(layoutId);

        if (webPartLayoutInfo != null)
        {
            // Get layout info using its code name - layout code name must be unique
            DataSet ds = WebPartLayoutInfoProvider.GetWebPartLayouts("WebPartLayoutCodeName = '" + WebPartLayoutInfoProvider.GetWebPartLayoutFullCodeName(webPartInfo.WebPartName, txtCodeName.Text) + "'", null);

            // Find anything?
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                WebPartLayoutInfo temp = new WebPartLayoutInfo(ds.Tables[0].Rows[0]);
                // Is it the same layout?
                if ((ds.Tables[0].Rows.Count > 1) || (temp.WebPartLayoutID != webPartLayoutInfo.WebPartLayoutID))
                {
                    lblError.Text    = String.Format(GetString("WebPartEditLayoutEdit.CodeNameAlreadyExist"), txtCodeName.Text);
                    lblError.Visible = true;
                    return(false);
                }
            }

            webPartLayoutInfo.WebPartLayoutCodeName    = txtCodeName.Text;
            webPartLayoutInfo.WebPartLayoutDisplayName = txtDisplayName.Text;
            webPartLayoutInfo.WebPartLayoutDescription = txtDescription.Text;

            if (!webPartLayoutInfo.Generalized.IsCheckedOut)
            {
                webPartLayoutInfo.WebPartLayoutCode = etaCode.Text;
                webPartLayoutInfo.WebPartLayoutCSS  = tbCSS.Text;
            }

            WebPartLayoutInfoProvider.SetWebPartLayoutInfo(webPartLayoutInfo);

            // Reload header if changes were saved
            if (TabMode)
            {
                ScriptHelper.RefreshTabHeader(Page, null);
            }
        }
        return(true);
    }
    protected void btnSaveAll_Click(object sender, EventArgs e)
    {
        try
        {
            // Save all the document transformations
            DataSet ds = DataClassInfoProvider.GetAllDataClass();
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int    classId        = ValidationHelper.GetInteger(dr["ClassID"], 0);
                    string className      = ValidationHelper.GetString(dr["ClassName"], "");
                    bool   isDocumentType = ValidationHelper.GetBoolean(dr["ClassIsDocumentType"], false);

                    if (isDocumentType)
                    {
                        ProcessTransformations(classId, className);
                    }
                }
            }

            // Save all the custom table transformations
            ds = DataClassInfoProvider.GetCustomTableClasses(null, null, 0, "ClassID,ClassName,ClassIsCustomTable");
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int    classId       = ValidationHelper.GetInteger(dr["ClassID"], 0);
                    string className     = ValidationHelper.GetString(dr["ClassName"], "");
                    bool   isCustomTable = ValidationHelper.GetBoolean(dr["ClassIsCustomTable"], false);

                    if (isCustomTable)
                    {
                        ProcessTransformations(classId, className);
                    }
                }
            }

            // Save all the layouts
            ds = LayoutInfoProvider.GetAllLayouts();
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    string layoutName = ValidationHelper.GetString(dr["LayoutCodeName"], "");
                    string layoutCode = ValidationHelper.GetString(dr["LayoutCode"], "");

                    int    checkedOutByUserId    = ValidationHelper.GetInteger(dr["LayoutCheckedOutByUserID"], 0);
                    string checkedOutMachineName = ValidationHelper.GetString(dr["LayoutCheckedOutMachineName"], "");

                    if ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower()))
                    {
                        string filename = LayoutInfoProvider.GetLayoutUrl(layoutName, null);

                        // Prepare the code
                        string code = layoutCode;
                        code = LayoutInfoProvider.AddLayoutDirectives(code);

                        SiteManagerFunctions.SaveCodeFile(filename, code);
                    }
                }
            }

            // Save all the page template layouts
            ds = PageTemplateInfoProvider.GetAllTemplates();
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    string templateName   = ValidationHelper.GetString(dr["PageTemplateCodeName"], "");
                    string templateLayout = ValidationHelper.GetString(dr["PageTemplateLayout"], "");
                    bool   isReusable     = ValidationHelper.GetBoolean(dr["PageTemplateIsReusable"], false);

                    int    checkedOutByUserId    = ValidationHelper.GetInteger(dr["PageTemplateLayoutCheckedOutByUserID"], 0);
                    string checkedOutMachineName = ValidationHelper.GetString(dr["PageTemplateLayoutCheckedOutMachineName"], "");
                    bool   isPortalTemplate      = ValidationHelper.GetBoolean(dr["PageTemplateIsPortal"], false);
                    string pageTemplateType      = ValidationHelper.GetString(dr["PageTemplateType"], "");
                    bool   isDashboard           = pageTemplateType.Equals(PageTemplateInfoProvider.GetPageTemplateTypeCode(PageTemplateTypeEnum.Dashboard));

                    if ((isPortalTemplate || isDashboard) && !String.IsNullOrEmpty(templateLayout) && ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower())))
                    {
                        string filename = null;
                        if (isReusable)
                        {
                            filename = PageTemplateInfoProvider.GetLayoutUrl(templateName, null);
                        }
                        else
                        {
                            filename = PageTemplateInfoProvider.GetAdhocLayoutUrl(templateName, null);
                        }

                        // Prepare the code
                        string code = templateLayout;
                        code = LayoutInfoProvider.AddLayoutDirectives(code);

                        SiteManagerFunctions.SaveCodeFile(filename, code);
                    }
                }
            }

            // Save all the web part layouts
            ds = WebPartLayoutInfoProvider.GetWebPartLayouts(null, null);
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    string webPartLayoutCodeName = ValidationHelper.GetString(dr["WebPartLayoutCodeName"], "");
                    string webPartLayoutCode     = ValidationHelper.GetString(dr["WebPartLayoutCode"], "");

                    WebPartInfo wpi = WebPartInfoProvider.GetWebPartInfo(ValidationHelper.GetInteger(dr["WebPartLayoutWebPartID"], 0));

                    if (wpi != null)
                    {
                        int    checkedOutByUserId    = ValidationHelper.GetInteger(dr["WebPartLayoutCheckedOutByUserID"], 0);
                        string checkedOutMachineName = ValidationHelper.GetString(dr["WebPartLayoutCheckedOutMachineName"], "");

                        if (!String.IsNullOrEmpty(webPartLayoutCode) && ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower())))
                        {
                            // Get layout filename
                            string filename = WebPartLayoutInfoProvider.GetWebPartLayoutUrl(wpi.WebPartName, webPartLayoutCodeName, "");
                            // Get path to the code file
                            string codeFilePath = URLHelper.GetVirtualPath(filename) + ".cs";

                            // Get path to the original code behind file
                            string originalCodeFilePath = String.Empty;
                            if (codeFileRegex.IsMatch(webPartLayoutCode, 0))
                            {
                                originalCodeFilePath = codeFileRegex.Match(webPartLayoutCode).Result("$1");
                            }

                            // Get original class name
                            string originalClassName = String.Empty;
                            if (inheritsRegex.IsMatch(webPartLayoutCode))
                            {
                                originalClassName = inheritsRegex.Match(webPartLayoutCode).Result("$1");
                            }

                            if (codeFileRegex.IsMatch(webPartLayoutCode))
                            {
                                webPartLayoutCode = codeFileRegex.Replace(webPartLayoutCode, "CodeFile=\"" + codeFilePath + "\"");
                            }

                            if (inheritsRegex.IsMatch(webPartLayoutCode))
                            {
                                webPartLayoutCode = inheritsRegex.Replace(webPartLayoutCode, "Inherits=\"$1_Web_Deployment\"");
                            }

                            // Read original codefile and change classname
                            string codeFileCode = String.Empty;
                            if (!String.IsNullOrEmpty(originalCodeFilePath) && FileHelper.FileExists(originalCodeFilePath))
                            {
                                codeFileCode = File.ReadAllText(Server.MapPath(originalCodeFilePath));
                                codeFileCode = codeFileCode.Replace(originalClassName, originalClassName + "_Web_Deployment");

                                // Save code file
                                SiteManagerFunctions.SaveCodeFile(filename, webPartLayoutCode);

                                // Save code behind file
                                SiteManagerFunctions.SaveCodeFile(codeFilePath, codeFileCode);
                            }
                        }
                    }
                }
            }

            lblResult.Text = GetString("Deployment.ObjectsSaved");
        }
        catch (Exception ex)
        {
            CMS.EventLog.EventLogProvider ep = new CMS.EventLog.EventLogProvider();
            ep.LogEvent("System deployment", "E", ex);

            lblError.Visible = true;
            lblError.Text    = ex.Message;
        }
    }
Esempio n. 4
0
    /// <summary>
    /// Button OK click handler.
    /// </summary>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        // Trim text values
        txtWebPartName.Text        = txtWebPartName.Text.Trim();
        txtWebPartDisplayName.Text = txtWebPartDisplayName.Text.Trim();
        txtWebPartFileName.Text    = txtWebPartFileName.Text.Trim();

        // Validate the text box fields
        string errorMessage = new Validator()
                              .NotEmpty(txtWebPartName.Text, rfvWebPartName.ErrorMessage)
                              .NotEmpty(txtWebPartDisplayName.Text, rfvWebPartDisplayName.ErrorMessage)
                              .IsCodeName(txtWebPartName.Text, GetString("WebPart_Clone.InvalidCodeName"))
                              .Result;

        // Validate file name
        if (string.IsNullOrEmpty(errorMessage) && chckCloneWebPartFiles.Checked)
        {
            errorMessage = new Validator()
                           .NotEmpty(txtWebPartFileName.Text, rfvWebPartFileName.ErrorMessage)
                           .IsFileName(Path.GetFileName(txtWebPartFileName.Text.Trim('~')), GetString("WebPart_Clone.InvalidFileName")).Result;
        }

        // Check if webpart with same name exists
        if (WebPartInfoProvider.GetWebPartInfo(txtWebPartName.Text) != null)
        {
            errorMessage = GetString(String.Format("Development-WebPart_Clone.WebPartExists", txtWebPartName.Text));
        }

        // Check if webpart is not cloned to the root category
        WebPartCategoryInfo wci = WebPartCategoryInfoProvider.GetWebPartCategoryInfoByCodeName("/");

        if (wci.CategoryID == ValidationHelper.GetInteger(drpWebPartCategories.SelectedValue, -1))
        {
            errorMessage = GetString("Development-WebPart_Clone.cannotclonetoroot");
        }

        if (errorMessage != "")
        {
            lblError.Text    = errorMessage;
            lblError.Visible = true;
            return;
        }

        // get web part info object
        WebPartInfo wi = WebPartInfoProvider.GetWebPartInfo(webPartId);

        if (wi == null)
        {
            lblError.Text    = GetString("WebPart_Clone.InvalidWebPartID");
            lblError.Visible = true;
            return;
        }

        // Create new webpart with all properties from source webpart
        WebPartInfo nwpi = new WebPartInfo(wi, false);

        nwpi.WebPartID   = 0;
        nwpi.WebPartGUID = Guid.NewGuid();

        // Modify clone info
        nwpi.WebPartName        = txtWebPartName.Text;
        nwpi.WebPartDisplayName = txtWebPartDisplayName.Text;
        nwpi.WebPartCategoryID  = ValidationHelper.GetInteger(drpWebPartCategories.SelectedValue, -1);

        if (nwpi.WebPartParentID <= 0)
        {
            nwpi.WebPartFileName = txtWebPartFileName.Text;
        }

        string path     = String.Empty;
        string filename = String.Empty;
        string inher    = String.Empty;

        try
        {
            // Copy file if needed and webpart is not ihnerited
            if (chckCloneWebPartFiles.Checked && (wi.WebPartParentID == 0))
            {
                // Get source file path
                string srcFile = GetWebPartPhysicalPath(wi.WebPartFileName);

                // Get destination file path
                string dstFile = GetWebPartPhysicalPath(nwpi.WebPartFileName);

                // Ensure directory
                DirectoryHelper.EnsureDiskPath(Path.GetDirectoryName(DirectoryHelper.EnsurePathBackSlash(dstFile)), URLHelper.WebApplicationPhysicalPath);

                // Check if source and target file path are different
                if (File.Exists(dstFile))
                {
                    throw new Exception(GetString("general.fileexists"));
                }

                // Get file name
                filename = Path.GetFileName(dstFile);
                // Get path to the partial class name replace
                string wpPath = nwpi.WebPartFileName;

                if (!wpPath.StartsWith("~/"))
                {
                    wpPath = WebPartInfoProvider.WebPartsDirectory + "/" + wpPath.TrimStart('/');
                }
                path = Path.GetDirectoryName(wpPath);

                inher = path.Replace('\\', '_').Replace('/', '_') + "_" + Path.GetFileNameWithoutExtension(filename).Replace('.', '_');
                inher = inher.Trim('~');
                inher = inher.Trim('_');

                // Read .aspx file, replace classname and save as new file
                string text = File.ReadAllText(srcFile);
                File.WriteAllText(dstFile, ReplaceASCX(text, DirectoryHelper.CombinePath(path, filename), inher));

                // Read .aspx file, replace classname and save as new file
                if (File.Exists(srcFile + ".cs"))
                {
                    text = File.ReadAllText(srcFile + ".cs");
                    File.WriteAllText(dstFile + ".cs", ReplaceASCXCS(text, inher));
                }

                if (File.Exists(srcFile + ".vb"))
                {
                    text = File.ReadAllText(srcFile + ".vb");
                    File.WriteAllText(dstFile + ".vb", ReplaceASCXVB(text, inher));
                }

                // Copy web part subfolder
                string srcDirectory = srcFile.Remove(srcFile.Length - Path.GetFileName(srcFile).Length) + Path.GetFileNameWithoutExtension(srcFile) + "_files";
                if (Directory.Exists(srcDirectory))
                {
                    string dstDirectory = dstFile.Remove(dstFile.Length - Path.GetFileName(dstFile).Length) + Path.GetFileNameWithoutExtension(dstFile) + "_files";
                    if (srcDirectory.ToLower() != dstDirectory.ToLower())
                    {
                        DirectoryHelper.EnsureDiskPath(srcDirectory, URLHelper.WebApplicationPhysicalPath);
                        DirectoryHelper.CopyDirectory(srcDirectory, dstDirectory);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            lblError.Text    = ex.Message;
            lblError.Visible = true;
            return;
        }

        // Add new web part to database
        WebPartInfoProvider.SetWebPartInfo(nwpi);

        try
        {
            // Get and duplicate all webpart layouts associated to webpart
            DataSet ds = WebPartLayoutInfoProvider.GetWebPartLayouts(webPartId);
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    WebPartLayoutInfo wpli = new WebPartLayoutInfo(dr);
                    wpli.WebPartLayoutID                    = 0;              // Create new record
                    wpli.WebPartLayoutWebPartID             = nwpi.WebPartID; // Associate layout to new webpart
                    wpli.WebPartLayoutGUID                  = Guid.NewGuid();
                    wpli.WebPartLayoutCheckedOutByUserID    = 0;
                    wpli.WebPartLayoutCheckedOutFilename    = "";
                    wpli.WebPartLayoutCheckedOutMachineName = "";

                    // Replace classname and inherits attribut
                    wpli.WebPartLayoutCode = ReplaceASCX(wpli.WebPartLayoutCode, DirectoryHelper.CombinePath(path, filename), inher);
                    WebPartLayoutInfoProvider.SetWebPartLayoutInfo(wpli);
                }
            }

            // Duplicate associated thumbnail
            MetaFileInfoProvider.CopyMetaFiles(webPartId, nwpi.WebPartID, PortalObjectType.WEBPART, MetaFileInfoProvider.OBJECT_CATEGORY_THUMBNAIL, null);
        }
        catch (Exception ex)
        {
            lblError.Text    = ex.Message;
            lblError.Visible = true;
            return;
        }

        // Close clone window
        // Refresh web part tree and select/edit new widget
        string script      = String.Empty;
        string refreshLink = URLHelper.ResolveUrl("~/CMSModules/PortalEngine/UI/WebParts/Development/WebPart_Tree.aspx?webpartid=" + nwpi.WebPartID + "&reload=true");

        if (QueryHelper.Contains("reloadAll"))
        {
            script += "wopener.parent.parent.frames['webparttree'].location.href ='" + refreshLink + "';";
        }
        else
        {
            script = "wopener.location = '" + refreshLink + "';";
        }
        script += "window.close();";

        ltlScript.Text = ScriptHelper.GetScript(script);
    }