Exemplo n.º 1
0
        private static void ConfigureMyWorkViews()
        {
            SPWeb configWeb = Utils.GetConfigWeb();

            List <MyWorkGridView> myWorkGridViews = MyWork.GetGlobalViews(configWeb).ToList();

            MyWorkGridView defaultView = myWorkGridViews.FirstOrDefault(view => view.Id.Equals("dv"));

            if (defaultView != null)
            {
                if (!defaultView.Cols.Split(',').Any(col => col.Split(':')[0].ToLower().Equals("workingon")))
                {
                    defaultView.Cols = defaultView.Cols + ",WorkingOn:95";

                    MyWork.SaveGlobalViews(defaultView, configWeb);
                }
            }

            myWorkGridViews = MyWork.GetGlobalViews(configWeb).ToList();

            bool workingOnViewExists = myWorkGridViews.Any(view => view.Name.Trim().ToLower().Equals("working on it"));

            if (!workingOnViewExists)
            {
                MyWorkGridView workingOnView = myWorkGridViews.FirstOrDefault(view => view.Id.Equals("dv"));

                if (workingOnView != null)
                {
                    workingOnView.Name    = "Working On It";
                    workingOnView.Id      = workingOnView.Name.Md5().ToLower();
                    workingOnView.Filters = "0|WorkingOn:1:11";
                    workingOnView.Default = false;

                    MyWork.SaveGlobalViews(workingOnView, configWeb);
                }
            }
        }
        // Protected Methods (1) 

        /// <summary>
        ///     Sends the tool part content to the specified HtmlTextWriter object, which writes the content to be rendered on the client.
        /// </summary>
        /// <param name="output">The HtmlTextWriter object that receives the tool part content.</param>
        protected override void RenderToolPart(HtmlTextWriter output)
        {
            var myWorkWebPart = (MyWorkWebPart)ParentToolPane.SelectedWebPart;

            string myWorkWebPartHtmlCode = Resources.MyWorkToolPart.Replace("_ID__", UniqueID.Md5());

            #region Get Settings

            string selectedLists = string.Empty;

            if (myWorkWebPart.SelectedLists != null)
            {
                IOrderedEnumerable <string> selLists = (myWorkWebPart.SelectedLists.Where(
                                                            selectedList => !string.IsNullOrEmpty(selectedList))
                                                        .Select(
                                                            selectedList =>
                                                            string.Format(@"'{0}'", selectedList))).ToList()
                                                       .OrderBy(
                    l =>
                    l);

                selectedLists = string.Join(",", selLists.ToArray());
            }

            string selectedFields = string.Empty;

            if (myWorkWebPart.SelectedFields != null)
            {
                IOrderedEnumerable <string> selFields = (myWorkWebPart.SelectedFields.Where(
                                                             selectedField => !string.IsNullOrEmpty(selectedField))
                                                         .Select(
                                                             selectedField =>
                                                             string.Format(
                                                                 @"{{InternalName:'{0}',PrettyName:'{1}'}}",
                                                                 selectedField,
                                                                 selectedField.ToPrettierName()))).ToList().
                                                        OrderBy(
                    f =>
                    f);

                selectedFields = string.Join(",", selFields.ToArray());
            }

            List <myworksettings.MWList> myWorkLists = myworksettings.GetMyWorkListsFromDb(_web,
                                                                                           MyWork.GetArchivedWebs(
                                                                                               _web.Site.ID));

            var includedMWLists = new List <string>();
            List <myworksettings.MWList> excludedMWLists = myWorkLists.ToList();

            if (myWorkWebPart.MyWorkSelectedLists.Count() > 0)
            {
                foreach (
                    myworksettings.MWList myWorkList in
                    myWorkLists.Where(myWorkList => myWorkWebPart.MyWorkSelectedLists.Contains(myWorkList.Name)))
                {
                    includedMWLists.Add(string.Format(@"{{Id:'{0}',Name:'{1}'}}", myWorkList.Id, myWorkList.Name));
                    excludedMWLists.Remove(myWorkList);
                }
            }
            else
            {
                includedMWLists = excludedMWLists.Select(excludedMWList => string.Format(@"{{Id:'{0}',Name:'{1}'}}",
                                                                                         excludedMWList.Id,
                                                                                         excludedMWList.Name)).ToList();

                excludedMWLists = new List <myworksettings.MWList>();
            }

            string includedMyWorkLists = string.Join(",", includedMWLists.ToArray());

            IEnumerable <string> excludedLists =
                excludedMWLists.Select(
                    excludedMWList =>
                    string.Format(@"{{Id:'{0}',Name:'{1}'}}", excludedMWList.Id,
                                  SPHttpUtility.HtmlEncode(excludedMWList.Name)));
            string excludedMyWorkLists = string.Join(",", excludedLists.ToArray());

            string crossSiteUrls = string.Empty;

            if (myWorkWebPart.CrossSiteUrls != null)
            {
                IOrderedEnumerable <string> crossSites = myWorkWebPart.CrossSiteUrls.Where(
                    crossSiteUrl => !string.IsNullOrEmpty(crossSiteUrl))
                                                         .Select(
                    crossSiteUrl =>
                    string.Format(@"'{0}'", crossSiteUrl))
                                                         .OrderBy(s => s);
                crossSiteUrls = string.Join(",", crossSites.ToArray());
            }

            var defaultGlobalViews = new List <string>();

            bool defaultViewFound = false;

            List <MyWorkGridView> myWorkGridViews =
                MyWork.GetGlobalViews(Utils.GetConfigWeb()).OrderBy(v => v.Name).ToList();

            foreach (MyWorkGridView myWorkGridView in myWorkGridViews)
            {
                bool defaultView = myWorkWebPart.DefaultGlobalView.Equals(myWorkGridView.Id);
                if (defaultView)
                {
                    defaultViewFound = true;
                }

                defaultGlobalViews.Add(string.Format(@"{{Id:'{0}',Name:'{1}',Default:{2}}}", myWorkGridView.Id,
                                                     myWorkGridView.Name, defaultView.Lc()));
            }

            defaultGlobalViews.Insert(0,
                                      string.Format(@"{{Id:'',Name:'Do Not Set View',Default:{0}}}",
                                                    (!defaultViewFound).Lc()));

            string objDefaultGlobalViews = string.Join(",", defaultGlobalViews.ToArray());

            bool agoFilterEnabled   = false;
            int  agoFilterDays      = 0;
            bool afterFilterEnabled = false;
            int  afterFilterDays    = 0;

            bool indicatorActive = true;
            int  indicatorDays   = 2;

            if (!string.IsNullOrEmpty(myWorkWebPart.DueDayFilter))
            {
                string[] filters = myWorkWebPart.DueDayFilter.Split('|');

                bool.TryParse(filters[0], out agoFilterEnabled);
                int.TryParse(filters[1], out agoFilterDays);
                bool.TryParse(filters[2], out afterFilterEnabled);
                int.TryParse(filters[3], out afterFilterDays);
            }

            if (!string.IsNullOrEmpty(myWorkWebPart.NewItemIndicator))
            {
                string[] settings = myWorkWebPart.NewItemIndicator.Split('|');

                bool.TryParse(settings[0], out indicatorActive);
                int.TryParse(settings[1], out indicatorDays);
            }

            string myWorkObjString =
                string.Format(
                    @"useCentralizedSettings:{0}, selectedLists:[{1}], selectedFields:[{2}], crossSiteUrls:[{3}], performanceMode:{4}, hideNewButton:{5}, allowEditToggle:{6}, defaultToEditMode:{7}, defaultGlobalViews:[{8}], includedMyWorkLists:[{9}], excludedMyWorkLists:[{10}], daysAgoEnabled:{11}, daysAfterEnabled:{12}, newItemIndicatorEnabled:{13}, daysAgo:'{14}', daysAfter:'{15}', newItemIndicator:'{16}', showToolbar:{17}",
                    myWorkWebPart.UseCentralizedSettings.Lc(), selectedLists, selectedFields, crossSiteUrls,
                    myWorkWebPart.PerformanceMode.Lc(), myWorkWebPart.HideNewButton.Lc(),
                    myWorkWebPart.AllowEditToggle.Lc(), myWorkWebPart.DefaultToEditMode.Lc(), objDefaultGlobalViews,
                    includedMyWorkLists, excludedMyWorkLists, agoFilterEnabled.Lc(),
                    afterFilterEnabled.Lc(), indicatorActive.Lc(),
                    agoFilterDays, afterFilterDays, indicatorDays, myWorkWebPart.ShowToolbar.Lc());

            myWorkWebPartHtmlCode = myWorkWebPartHtmlCode.Replace("objMyWork__VAL__", myWorkObjString);

            #endregion

            #region Get All Lists and Fields

            var listsAndFields = new Dictionary <string, List <string> >();
            foreach (SPList list in _web.Lists)
            {
                try
                {
                    listsAndFields.Add(list.Title,
                                       (list.Fields.Cast <SPField>()
                                        .Where(spField => !spField.Hidden && spField.Reorderable)
                                        .Select(spField => spField.InternalName)).ToList());
                }
                catch { }
            }

            string allListsAndFieldsString =
                string.Format(@"{0}",
                              string.Join(",", listsAndFields.Select(listAndFields =>
                                                                     string.Format(@"{{List:'{0}',Fields:[{1}]}}",
                                                                                   listAndFields.Key,
                                                                                   string.Join(",",
                                                                                               listAndFields.Value.
                                                                                               Select(
                                                                                                   field
                                                                                                   =>
                                                                                                   string
                                                                                                   .Format
                                                                                                   (
                                                                                                       @"{{InternalName:'{0}',PrettyName:'{1}'}}",
                                                                                                       field,
                                                                                                       field
                                                                                                       .
                                                                                                       ToPrettierName
                                                                                                           ()))
                                                                                               .
                                                                                               ToArray())))
                                          .ToArray()));

            myWorkWebPartHtmlCode = myWorkWebPartHtmlCode.Replace("allListsAndFields__VAL__", allListsAndFieldsString);

            #endregion

            #region Get Field Lists

            var fieldLists = new Dictionary <string, List <string> >();

            foreach (var listAndFields in listsAndFields)
            {
                foreach (string field in listAndFields.Value)
                {
                    if (!fieldLists.ContainsKey(field))
                    {
                        fieldLists.Add(field, new List <string>());
                    }
                    fieldLists[field].Add(listAndFields.Key);
                }
            }

            List <string> fields = fieldLists.Select(fieldList => string.Format(@"{0}:[{1}]", fieldList.Key,
                                                                                string.Join(",",
                                                                                            fieldList.Value.Select(
                                                                                                list =>
                                                                                                string.Format(@"'{0}'",
                                                                                                              list)).
                                                                                            ToArray())))
                                   .ToList();

            string fieldListsString = string.Join(",", fields.ToArray());

            myWorkWebPartHtmlCode = myWorkWebPartHtmlCode.Replace("fieldLists__VAL__", fieldListsString);

            string listWebsString = string.Join(",", (from myWorkList in myWorkLists
                                                      let webs =
                                                          myWorkList.Webs.Select(web => string.Format(@"'{0}'", web))
                                                          select
                                                          string.Format(@"{0}:[{1}]", myWorkList.Id,
                                                                        string.Join(",", webs.ToArray()))).ToArray());

            myWorkWebPartHtmlCode = myWorkWebPartHtmlCode.Replace("listWebs__VAL__", listWebsString);

            #endregion

            output.Write(myWorkWebPartHtmlCode);
        }