Ejemplo n.º 1
0
    /// <summary>
    /// Generates the web part code.
    /// </summary>
    protected void GenerateCode()
    {
        string templateFile = MapPath("~/App_Data/CodeTemplates/WebPart");
        string baseControl  = txtBaseControl.Text.Trim();

        WebPartInfo wpi = WebPartInfoProvider.GetWebPartInfo(webpartId);

        if (wpi != null)
        {
            // Generate the ASCX
            string ascx = File.ReadAllText(templateFile + ".ascx.template");

            // Prepare the path
            string path = URLHelper.UnResolveUrl(WebPartInfoProvider.GetWebPartUrl(wpi), SettingsKeyProvider.ApplicationPath);

            // Prepare the class name
            string className = path.Trim('~', '/');
            if (className.EndsWith(".ascx"))
            {
                className = className.Substring(0, className.Length - 5);
            }
            className = className.Replace("/", "_");

            ascx = Regex.Replace(ascx, "(Inherits)=\"[^\"]+\"", "$1=\"" + className + "\"");
            ascx = Regex.Replace(ascx, "(CodeFile|CodeBehind)=\"[^\"]+\"", "$1=\"" + path + "\"");

            this.txtASCX.Text = ascx;

            // Generate the code
            string code = File.ReadAllText(templateFile + ".ascx.cs.template");

            code = Regex.Replace(code, "( class\\s+)[^\\s]+", "$1" + className);

            // Prepare the properties
            FormInfo fi = new FormInfo(wpi.WebPartProperties);

            StringBuilder sbInit = new StringBuilder();

            string propertiesCode = CodeGenerator.GetPropertiesCode(fi, true, baseControl, sbInit);

            // Replace in code
            code = code.Replace("// ##PROPERTIES##", propertiesCode);
            code = code.Replace("// ##SETUP##", sbInit.ToString());

            this.txtCS.Text = code;
        }
    }
Ejemplo n.º 2
0
    protected void Page_Load(object sender, EventArgs e)
    {
        StringSafeDictionary <object> decodedProperties = new StringSafeDictionary <object>();

        foreach (DictionaryEntry param in Properties)
        {
            // Decode special CK editor char
            String str = String.Empty;
            if (param.Value != null)
            {
                str = param.Value.ToString().Replace("%25", "%");
            }

            decodedProperties[(string)param.Key] = HttpUtility.UrlDecode(str);
        }
        Properties = decodedProperties;

        string widgetName = ValidationHelper.GetString(Properties["name"], String.Empty);

        // Widget name must be specified
        if (String.IsNullOrEmpty(widgetName))
        {
            AddErrorWebPart("widgets.invalidname", null);
            return;
        }

        WidgetInfo wi = WidgetInfoProvider.GetWidgetInfo(widgetName);

        if (wi == null)
        {
            AddErrorWebPart("widget.failedtoload", null);
            return;
        }

        WebPartInfo wpi = WebPartInfoProvider.GetWebPartInfo(wi.WidgetWebPartID);

        if (wpi == null)
        {
            return;
        }

        //no widgets can be used as inline
        if (!wi.WidgetForInline)
        {
            AddErrorWebPart("widgets.cantbeusedasinline", null);
            return;
        }

        try
        {
            FormInfo fi = null;
            DataRow  dr = null;

            using (var cs = new CachedSection <FormInfo>(ref fi, 1440, (PortalContext.ViewMode == ViewModeEnum.LiveSite), null, "inlinewidgetcontrol", wi.WidgetID, wpi.WebPartID))
            {
                if (cs.LoadData)
                {
                    // Merge widget and it's parent webpart properties
                    string props = FormHelper.MergeFormDefinitions(wpi.WebPartProperties, wi.WidgetProperties);

                    // Prepare form
                    const WidgetZoneTypeEnum zoneType = WidgetZoneTypeEnum.Editor;
                    fi = PortalFormHelper.GetWidgetFormInfo(wi.WidgetName, zoneType, props, true, wi.WidgetDefaultValues);

                    // Apply changed values
                    dr = fi.GetDataRow();
                    fi.LoadDefaultValues(dr);

                    if (cs.Cached)
                    {
                        cs.CacheDependency = CacheHelper.GetCacheDependency("cms.webpart|byid|" + wpi.WebPartID + "\n" + "cms.widget|byid|" + wi.WidgetID);
                    }

                    cs.Data = fi;
                }
            }

            // Get datarow
            if (dr == null)
            {
                dr = fi.GetDataRow();
                fi.LoadDefaultValues(dr);
            }

            // Incorporate inline parameters to datarow
            string publicFields = ";containertitle;container;";
            if (wi.WidgetPublicFileds != null)
            {
                publicFields += ";" + wi.WidgetPublicFileds.ToLowerCSafe() + ";";
            }

            // Load the widget control
            string url = null;

            // Set widget layout
            WebPartLayoutInfo wpli = WebPartLayoutInfoProvider.GetWebPartLayoutInfo(wi.WidgetLayoutID);

            if (wpli != null)
            {
                // Load specific layout through virtual path
                url = wpli.Generalized.GetVirtualFileRelativePath(WebPartLayoutInfo.EXTERNAL_COLUMN_CODE, wpli.WebPartLayoutVersionGUID);
            }
            else
            {
                // Load regularly
                url = WebPartInfoProvider.GetWebPartUrl(wpi, false);
            }

            CMSAbstractWebPart control = (CMSAbstractWebPart)Page.LoadUserControl(url);
            control.PartInstance = new WebPartInstance();

            // Use current page placeholder
            control.PagePlaceholder = PortalHelper.FindParentPlaceholder(this);

            // Set all form values to webpart
            foreach (DataColumn column in dr.Table.Columns)
            {
                object value      = dr[column];
                string columnName = column.ColumnName.ToLowerCSafe();

                //Resolve set values by user
                if (Properties.Contains(columnName))
                {
                    FormFieldInfo ffi = fi.GetFormField(columnName);
                    if ((ffi != null) && ffi.Visible && ((ffi.DisplayIn == null) || !ffi.DisplayIn.Contains(FormInfo.DISPLAY_CONTEXT_DASHBOARD)))
                    {
                        value = Properties[columnName];
                    }
                }

                // Resolve macros in defined in default values
                if (!String.IsNullOrEmpty(value as string))
                {
                    // Do not resolve macros for public fields
                    if (publicFields.IndexOfCSafe(";" + columnName + ";") < 0)
                    {
                        // Check whether current column
                        bool avoidInjection = control.SQLProperties.Contains(";" + columnName + ";");

                        // Resolve macros
                        MacroSettings settings = new MacroSettings()
                        {
                            AvoidInjection = avoidInjection,
                        };
                        value = control.ContextResolver.ResolveMacros(value.ToString(), settings);
                    }
                }

                control.PartInstance.SetValue(column.ColumnName, value);
            }

            control.PartInstance.IsWidget = true;

            // Load webpart content
            control.OnContentLoaded();

            // Add webpart to controls collection
            Controls.Add(control);

            // Handle DisableViewstate setting
            control.EnableViewState = !control.DisableViewState;
        }

        catch (Exception ex)
        {
            AddErrorWebPart("widget.failedtoload", ex);
        }
    }
Ejemplo n.º 3
0
    protected void Page_Load(object sender, EventArgs e)
    {
        Hashtable decodedProperties = new Hashtable();

        foreach (DictionaryEntry param in mProperties)
        {
            // Decode special CK editor char
            String str = String.Empty;
            if (param.Value != null)
            {
                str = param.Value.ToString().Replace("%25", "%");
            }

            decodedProperties[param.Key] = HttpUtility.UrlDecode(str);
        }
        mProperties = decodedProperties;

        string widgetName = ValidationHelper.GetString(mProperties["name"], String.Empty);

        // Widget name must be specified
        if (String.IsNullOrEmpty(widgetName))
        {
            AddErrorWebPart("widgets.invalidname", null);
            return;
        }

        WidgetInfo wi = WidgetInfoProvider.GetWidgetInfo(widgetName);

        if (wi == null)
        {
            AddErrorWebPart("widget.failedtoload", null);
            return;
        }

        WebPartInfo wpi = WebPartInfoProvider.GetWebPartInfo(wi.WidgetWebPartID);

        if (wpi == null)
        {
            return;
        }

        //no widgets can be used as inline
        if (!wi.WidgetForInline)
        {
            AddErrorWebPart("widgets.cantbeusedasinline", null);
            return;
        }

        try
        {
            // Merge widget and it's parent webpart properties
            string properties = FormHelper.MergeFormDefinitions(wpi.WebPartProperties, wi.WidgetProperties);

            // Prepare form
            WidgetZoneTypeEnum zoneType           = WidgetZoneTypeEnum.Editor;
            FormInfo           zoneTypeDefinition = PortalHelper.GetPositionFormInfo(zoneType);
            FormInfo           fi = FormHelper.GetWidgetFormInfo(wi.WidgetName, Enum.GetName(typeof(WidgetZoneTypeEnum), zoneType), properties, zoneTypeDefinition, true);

            // Apply changed values
            DataRow dr = fi.GetDataRow();
            fi.LoadDefaultValues(dr);

            // Incorporate inline parameters to datarow
            string publicFields = ";containertitle;container;";
            if (wi.WidgetPublicFileds != null)
            {
                publicFields += ";" + wi.WidgetPublicFileds.ToLower() + ";";
            }

            // Load the webpart(widget) control
            string url = WebPartInfoProvider.GetWebPartUrl(wpi, false);

            CMSAbstractWebPart control = (CMSAbstractWebPart)Page.LoadControl(url);
            control.PartInstance = new WebPartInstance();

            // Set all form values to webpart
            foreach (DataColumn column in dr.Table.Columns)
            {
                object value      = dr[column];
                string columnName = column.ColumnName.ToLower();

                //Resolve set values by user
                if (mProperties.Contains(columnName))
                {
                    FormFieldInfo ffi = fi.GetFormField(columnName);
                    if ((ffi != null) && ffi.Visible && (!ffi.DisplayIn.Contains(FormInfo.DISPLAY_CONTEXT_DASHBOARD)))
                    {
                        value = mProperties[columnName];
                    }
                }

                // Resolve macros in defined in default values
                if (!String.IsNullOrEmpty(value as string))
                {
                    // Do not resolve macros for public fields
                    if (publicFields.IndexOf(";" + columnName + ";") < 0)
                    {
                        // Check whether current column
                        bool avoidInjection = control.SQLProperties.Contains(";" + columnName + ";");

                        // Resolve macros
                        value = control.ContextResolver.ResolveMacros(value.ToString(), avoidInjection);
                    }
                }

                control.PartInstance.SetValue(column.ColumnName, value);
            }

            // Load webpart content
            control.OnContentLoaded();

            // Add webpart to controls collection
            this.Controls.Add(control);
        }

        catch (Exception ex)
        {
            AddErrorWebPart("widget.failedtoload", ex);
        }
    }
    /// <summary>
    /// Generates the web part code.
    /// </summary>
    /// <param name="wpi">Web part info</param>
    /// <param name="baseControl">Base control nested within the web part</param>
    /// <param name="ascx">Returning ASCX code</param>
    /// <param name="code">Returning code behind</param>
    /// <param name="designer">Returning designer file</param>
    public static void GenerateWebPartCode(WebPartInfo wpi, string baseControl, out string ascx, out string code, out string designer)
    {
        code     = "";
        ascx     = "";
        designer = "";

        if (wpi != null)
        {
            string templateFile = HttpContext.Current.Server.MapPath("~/App_Data/CodeTemplates/WebPart");

            // Generate the ASCX
            ascx = File.ReadAllText(templateFile + ".ascx.template");

            // Prepare the path
            string path = URLHelper.UnResolveUrl(WebPartInfoProvider.GetWebPartUrl(wpi), SettingsKeyProvider.ApplicationPath);

            // Prepare the class name
            string className = path.Trim('~', '/');
            if (className.EndsWithCSafe(".ascx"))
            {
                className = className.Substring(0, className.Length - 5);
            }
            className = ValidationHelper.GetIdentifier(className, "_");

            ascx = Regex.Replace(ascx, "(Inherits)=\"[^\"]+\"", "$1=\"" + className + "\"");

            // Replace the code file / code behind
            bool webApp = CMSContext.IsWebApplication;

            string fileAttr = "CodeFile";
            //if (webApp)
            //{
            //    fileAttr = "CodeBehind";
            //}

            ascx = Regex.Replace(ascx, "(CodeFile|CodeBehind)=\"[^\"]+\"", fileAttr + "=\"" + path + ".cs\"");

            // Generate the code
            code = File.ReadAllText(templateFile + ".ascx.cs.template");

            code = Regex.Replace(code, "( class\\s+)[^\\s]+", "$1" + className);

            // Prepare the properties
            FormInfo fi = new FormInfo(wpi.WebPartProperties);

            StringBuilder sbInit = new StringBuilder();

            string propertiesCode = CodeGenerator.GetPropertiesCode(fi, true, baseControl, sbInit, true);

            // Replace in code
            code = code.Replace("// ##PROPERTIES##", propertiesCode);
            code = code.Replace("// ##SETUP##", sbInit.ToString());

            // Generate the designer
            if (webApp)
            {
                designer = File.ReadAllText(templateFile + ".ascx.designer.cs.template");

                designer = Regex.Replace(designer, "( class\\s+)[^\\s]+", "$1" + className);
            }
        }
    }