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[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);
        }
    }
Пример #2
0
    /// <summary>
    /// Sets time controls (dropdown with interval and textbox with interval value). Returns true if time controls are to be hided.
    /// </summary>
    private bool SetTimeControls()
    {
        HitsIntervalEnum interval = HitsIntervalEnumFunctions.StringToHitsConversion(mIntervalStr);

        DateTime from = DateTimeHelper.ZERO_TIME;
        DateTime to   = DateTimeHelper.ZERO_TIME;

        object dcFrom = null;
        object dcTo   = null;

        if (mParameters != null)
        {
            // Load fromdate and todate from report parameters (passed from query string)
            dcFrom = mParameters.Table.Columns["FromDate"];
            dcTo   = mParameters.Table.Columns["ToDate"];

            if (dcFrom != null)
            {
                from = ValidationHelper.GetDateTime(mParameters["FromDate"], DateTimeHelper.ZERO_TIME);
            }

            if (dcTo != null)
            {
                to = ValidationHelper.GetDateTime(mParameters["ToDate"], DateTimeHelper.ZERO_TIME);
            }
        }

        // If one contains zero time, set all time radio button. In such situation, report can maintain unlimited fromdate or todate.
        if ((from == DateTimeHelper.ZERO_TIME) || (to == DateTimeHelper.ZERO_TIME))
        {
            mCheckLast = false;
        }

        // If one is not set, hide limitdata panel
        if ((dcFrom == null) || (dcTo == null))
        {
            ucInterval.DefaultPeriod = SchedulingHelper.PERIOD_DAY;
            return(true);
        }

        int  diff        = 0;
        bool noAddToDiff = false;

        // If interval is not known, but 'from' and 'to' is set (f.e. preview, webpart,..) - compute interval from date values
        if (interval == HitsIntervalEnum.None)
        {
            String sFrom = ValidationHelper.GetString(mParameters["FromDate"], String.Empty).ToLowerCSafe();
            String sTo   = ValidationHelper.GetString(mParameters["ToDate"], String.Empty).ToLowerCSafe();
            mCheckLast = true;

            if (MacroProcessor.ContainsMacro(sFrom) && MacroProcessor.ContainsMacro(sTo))
            {
                if (sFrom.Contains("addhours"))
                {
                    interval = HitsIntervalEnum.Hour;
                }
                else if (sFrom.Contains("adddays"))
                {
                    interval = HitsIntervalEnum.Day;
                }
                else if (sFrom.Contains("addweeks"))
                {
                    interval = HitsIntervalEnum.Week;
                }
                else if (sFrom.Contains("addmonths"))
                {
                    interval = HitsIntervalEnum.Month;
                }
                else if (sFrom.Contains("addyears"))
                {
                    interval = HitsIntervalEnum.Year;
                }

                var macroResolverSettings = new MacroSettings
                {
                    AvoidInjection = false,
                    Culture        = CultureHelper.EnglishCulture.Name
                };
                to          = DateTime.Now;
                from        = ValidationHelper.GetDateTime(MacroResolver.Resolve(sFrom, macroResolverSettings), DateTime.Now, macroResolverSettings.Culture);
                noAddToDiff = true;
            }
            else if ((from != DateTimeHelper.ZERO_TIME) && (to != DateTimeHelper.ZERO_TIME))
            {
                // Set interval as greatest possible interval (365+ days -> years, 30+days->months ,...)
                diff = (int)(to - from).TotalDays;
                if (diff >= 365)
                {
                    interval = HitsIntervalEnum.Year;
                }
                else if (diff >= 30)
                {
                    interval = HitsIntervalEnum.Month;
                }
                else if (diff >= 7)
                {
                    interval = HitsIntervalEnum.Week;
                }
                else if (diff >= 1)
                {
                    interval = HitsIntervalEnum.Day;
                }
                else
                {
                    interval = HitsIntervalEnum.Hour;
                }
            }
        }

        // Set default period and diff based on interval
        switch (interval)
        {
        case HitsIntervalEnum.Year:
            diff = to.Year - from.Year;
            ucInterval.DefaultPeriod = SchedulingHelper.PERIOD_MONTH;
            break;

        case HitsIntervalEnum.Month:
            diff = ((to.Year - from.Year) * 12) + to.Month - from.Month;
            ucInterval.DefaultPeriod = SchedulingHelper.PERIOD_MONTH;
            break;

        case HitsIntervalEnum.Week:
            diff = (int)(to - from).TotalDays / 7;
            ucInterval.DefaultPeriod = SchedulingHelper.PERIOD_WEEK;
            break;

        case HitsIntervalEnum.Day:
            diff = (int)(to - from).TotalDays;
            ucInterval.DefaultPeriod = SchedulingHelper.PERIOD_DAY;
            break;

        case HitsIntervalEnum.Hour:
            diff = (int)(to - from).TotalHours;
            ucInterval.DefaultPeriod = SchedulingHelper.PERIOD_HOUR;
            break;

        case HitsIntervalEnum.None:
            mCheckLast = false;
            break;
        }

        // Add current
        if (!noAddToDiff)
        {
            diff++;
        }

        if (interval != HitsIntervalEnum.None)
        {
            drpLast.SelectedValue = HitsIntervalEnumFunctions.HitsConversionToString(interval);
        }

        if (!mCheckLast)
        {
            // Defaul settings for no time
            ucInterval.DefaultPeriod = SchedulingHelper.PERIOD_DAY;
        }

        if (diff != 0)
        {
            txtLast.Text = diff.ToString();
        }

        return(false);
    }
Пример #3
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);
        }
    }