Beispiel #1
0
        /// <summary> Constructor for a new instance of the Thematic_Headings_AdminViewer class </summary>
        /// <param name="RequestSpecificValues"> All the necessary, non-global data specific to the current request </param>
        /// <remarks> Postback from handling an edit or new thematic heading is handled here in the constructor </remarks>
        public Thematic_Headings_AdminViewer(RequestCache RequestSpecificValues) : base(RequestSpecificValues)
        {
            RequestSpecificValues.Tracer.Add_Trace("Thematic_Headings_AdminViewer.Constructor", String.Empty);

            // Set action message to nothing to start
            actionMessage = String.Empty;

            // If the RequestSpecificValues.Current_User cannot edit this, go back
            if ((!RequestSpecificValues.Current_User.Is_System_Admin) && (!RequestSpecificValues.Current_User.Is_Portal_Admin))
            {
                RequestSpecificValues.Current_Mode.Mode          = Display_Mode_Enum.My_Sobek;
                RequestSpecificValues.Current_Mode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // Handle any post backs
            if (RequestSpecificValues.Current_Mode.isPostBack)
            {
                try
                {
                    // Pull the standard values from the form
                    NameValueCollection form = HttpContext.Current.Request.Form;
                    string save_value        = form["admin_heading_tosave"];
                    string action_value      = form["admin_heading_action"];

                    // Switch, depending on the request
                    if (action_value != null)
                    {
                        switch (action_value.Trim().ToLower())
                        {
                        case "edit":
                            string new_name = form["form_heading_name"];
                            if (new_name != null)
                            {
                                int id    = Convert.ToInt32(save_value);
                                int order = 1 + UI_ApplicationCache_Gateway.Thematic_Headings.TakeWhile(ThisHeading => ThisHeading.ID != id).Count();
                                if (Engine_Database.Edit_Thematic_Heading(id, order, new_name, RequestSpecificValues.Tracer) < 1)
                                {
                                    actionMessage = "Unable to edit existing thematic heading";
                                }
                                else
                                {
                                    // For thread safety, lock the thematic headings list
                                    Engine_ApplicationCache_Gateway.RefreshThematicHeadings();

                                    actionMessage = "Thematic heading edits saved";
                                }
                            }
                            break;

                        case "delete":
                            int thematicDeleteId = Convert.ToInt32(save_value);
                            if (!Engine_Database.Delete_Thematic_Heading(thematicDeleteId, RequestSpecificValues.Tracer))
                            {
                                // Set action message
                                actionMessage = "Unable to delete existing thematic heading";
                            }
                            else
                            {
                                // For thread safety, lock the thematic headings list
                                lock (UI_ApplicationCache_Gateway.Thematic_Headings)
                                {
                                    // Remove this thematic heading from the list
                                    int i = 0;
                                    while (i < UI_ApplicationCache_Gateway.Thematic_Headings.Count)
                                    {
                                        if (UI_ApplicationCache_Gateway.Thematic_Headings[i].ID == thematicDeleteId)
                                        {
                                            UI_ApplicationCache_Gateway.Thematic_Headings.RemoveAt(i);
                                        }
                                        else
                                        {
                                            i++;
                                        }
                                    }
                                }

                                // Set action message
                                actionMessage = "Thematic heading deleted";
                            }
                            break;


                        case "new":
                            int new_order = UI_ApplicationCache_Gateway.Thematic_Headings.Count + 1;
                            int newid     = Engine_Database.Edit_Thematic_Heading(-1, new_order, save_value, RequestSpecificValues.Tracer);
                            if (newid < 1)
                            {
                                actionMessage = "Unable to save new thematic heading";
                            }
                            else
                            {
                                // For thread safety, lock the thematic headings list
                                lock (UI_ApplicationCache_Gateway.Thematic_Headings)
                                {
                                    // Repopulate the thematic headings list
                                    Engine_Database.Populate_Thematic_Headings(UI_ApplicationCache_Gateway.Thematic_Headings, RequestSpecificValues.Tracer);
                                }

                                // Add this blank thematic heading to the code manager
                                UI_ApplicationCache_Gateway.Aggregations.Add_Blank_Thematic_Heading(newid);

                                actionMessage = "New thematic heading saved";
                            }
                            break;

                        case "moveup":
                            string[] moveup_split = save_value.Split(",".ToCharArray());
                            int      moveup_id    = Convert.ToInt32(moveup_split[0]);
                            int      moveup_order = Convert.ToInt32(moveup_split[1]);
                            if (moveup_order > 1)
                            {
                                Thematic_Heading themeHeading = UI_ApplicationCache_Gateway.Thematic_Headings[moveup_order - 1];
                                if (themeHeading.ID == moveup_id)
                                {
                                    // For thread safety, lock the thematic headings list
                                    lock (UI_ApplicationCache_Gateway.Thematic_Headings)
                                    {
                                        // Move this thematic heading up
                                        UI_ApplicationCache_Gateway.Thematic_Headings.Remove(themeHeading);
                                        UI_ApplicationCache_Gateway.Thematic_Headings.Insert(moveup_order - 2, themeHeading);
                                        int current_order = 1;
                                        foreach (Thematic_Heading thisTheme in UI_ApplicationCache_Gateway.Thematic_Headings)
                                        {
                                            Engine_Database.Edit_Thematic_Heading(thisTheme.ID, current_order, thisTheme.Text, RequestSpecificValues.Tracer);
                                            current_order++;
                                        }

                                        // Repopulate the thematic headings list
                                        Engine_Database.Populate_Thematic_Headings(UI_ApplicationCache_Gateway.Thematic_Headings, RequestSpecificValues.Tracer);
                                    }
                                }
                            }
                            break;

                        case "movedown":
                            string[] movedown_split = save_value.Split(",".ToCharArray());
                            int      movedown_id    = Convert.ToInt32(movedown_split[0]);
                            int      movedown_order = Convert.ToInt32(movedown_split[1]);
                            if (movedown_order < UI_ApplicationCache_Gateway.Thematic_Headings.Count)
                            {
                                Thematic_Heading themeHeading = UI_ApplicationCache_Gateway.Thematic_Headings[movedown_order - 1];
                                if (themeHeading.ID == movedown_id)
                                {
                                    // For thread safety, lock the thematic headings list
                                    lock (UI_ApplicationCache_Gateway.Thematic_Headings)
                                    {
                                        // Move this thematic heading down
                                        UI_ApplicationCache_Gateway.Thematic_Headings.Remove(themeHeading);
                                        UI_ApplicationCache_Gateway.Thematic_Headings.Insert(movedown_order, themeHeading);
                                        int current_order = 1;
                                        foreach (Thematic_Heading thisTheme in UI_ApplicationCache_Gateway.Thematic_Headings)
                                        {
                                            Engine_Database.Edit_Thematic_Heading(thisTheme.ID, current_order, thisTheme.Text, RequestSpecificValues.Tracer);
                                            current_order++;
                                        }

                                        // Repopulate the thematic headings list
                                        Engine_Database.Populate_Thematic_Headings(UI_ApplicationCache_Gateway.Thematic_Headings, RequestSpecificValues.Tracer);
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                catch (Exception)
                {
                    actionMessage = "Unknown error caught while handling your reqeust";
                }
            }
        }
        /// <summary> Add a new aggregation to the system </summary>
        /// <param name="NewAggregation"> Information for the new aggregation </param>
        /// <returns> Message indicating success or any errors encountered </returns>
        public static RestResponseMessage add_new_aggregation(New_Aggregation_Arguments NewAggregation)
        {
            // Convert to the integer id for the parent and begin to do checking
            List <string> errors   = new List <string>();
            int           parentid = -1;

            if (NewAggregation.ParentCode.Length > 0)
            {
                Item_Aggregation_Related_Aggregations parentAggr = Engine_ApplicationCache_Gateway.Codes[NewAggregation.ParentCode];
                if (parentAggr != null)
                {
                    parentid = parentAggr.ID;
                }
                else
                {
                    errors.Add("Parent code is not valid");
                }
            }
            else
            {
                errors.Add("You must select a PARENT for this new aggregation");
            }

            // Validate the code

            if (NewAggregation.Code.Length > 20)
            {
                errors.Add("New aggregation code must be twenty characters long or less");
            }
            else if (NewAggregation.Code.Length == 0)
            {
                errors.Add("You must enter a CODE for this item aggregation");
            }
            else if (Engine_ApplicationCache_Gateway.Codes[NewAggregation.Code.ToUpper()] != null)
            {
                errors.Add("New code must be unique... <i>" + NewAggregation.Code + "</i> already exists");
            }
            else if (Engine_ApplicationCache_Gateway.Settings.Static.Reserved_Keywords.Contains(NewAggregation.Code.ToLower()))
            {
                errors.Add("That code is a system-reserved keyword.  Try a different code.");
            }
            else
            {
                bool alphaNumericTest = NewAggregation.Code.All(C => Char.IsLetterOrDigit(C) || C == '_' || C == '-');
                if (!alphaNumericTest)
                {
                    errors.Add("New aggregation code must be only letters and numbers");
                    NewAggregation.Code = NewAggregation.Code.Replace("\"", "");
                }
            }

            // Was there a type and name
            if (NewAggregation.Type.Length == 0)
            {
                errors.Add("You must select a TYPE for this new aggregation");
            }
            if (NewAggregation.Description.Length == 0)
            {
                errors.Add("You must enter a DESCRIPTION for this new aggregation");
            }
            if (NewAggregation.Name.Length == 0)
            {
                errors.Add("You must enter a NAME for this new aggregation");
            }
            else
            {
                if (NewAggregation.ShortName.Length == 0)
                {
                    NewAggregation.ShortName = NewAggregation.Name;
                }
            }

            // Check for the thematic heading
            int thematicHeadingId = -1;

            if (!String.IsNullOrEmpty(NewAggregation.Thematic_Heading))
            {
                // Look for the matching thematic heading
                foreach (Thematic_Heading thisHeading in Engine_ApplicationCache_Gateway.Thematic_Headings)
                {
                    if (String.Compare(thisHeading.Text, NewAggregation.Thematic_Heading, StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        thematicHeadingId = thisHeading.ID;
                        break;
                    }
                }

                // If there was no match, the thematic heading was invalid, unless it was new
                if (thematicHeadingId < 0)
                {
                    if ((!NewAggregation.NewThematicHeading.HasValue) || (!NewAggregation.NewThematicHeading.Value))
                    {
                        errors.Add("Invalid thematic heading indicated");
                    }
                    else if (errors.Count == 0)
                    {
                        // Add the thematic heading first
                        if ((thematicHeadingId < 0) && (NewAggregation.NewThematicHeading.HasValue) && (NewAggregation.NewThematicHeading.Value))
                        {
                            thematicHeadingId = Engine_Database.Edit_Thematic_Heading(-1, 10, NewAggregation.Thematic_Heading, null);
                        }
                    }
                }
            }

            if (errors.Count > 0)
            {
                // Create the error message
                StringBuilder actionMessage = new StringBuilder("ERROR: Invalid entry for new item aggregation.<br />");
                foreach (string error in errors)
                {
                    actionMessage.Append("<br />" + error);
                }

                return(new RestResponseMessage(ErrorRestTypeEnum.InputError, actionMessage.ToString()));
            }



            string language = Web_Language_Enum_Converter.Enum_To_Code(Engine_ApplicationCache_Gateway.Settings.System.Default_UI_Language);

            // Try to save the new item aggregation
            if (!Engine_Database.Save_Item_Aggregation(NewAggregation.Code, NewAggregation.Name, NewAggregation.ShortName, NewAggregation.Description, thematicHeadingId, NewAggregation.Type, NewAggregation.Active, NewAggregation.Hidden, NewAggregation.External_Link, parentid, NewAggregation.User, language, null))
            {
                return(new RestResponseMessage(ErrorRestTypeEnum.Exception, "ERROR saving the new item aggregation to the database"));
            }
            // Ensure a folder exists for this, otherwise create one
            try
            {
                string folder = Engine_ApplicationCache_Gateway.Settings.Servers.Base_Design_Location + "aggregations\\" + NewAggregation.Code.ToLower();
                if (!Directory.Exists(folder))
                {
                    // Create this directory and all the subdirectories
                    Directory.CreateDirectory(folder);
                    Directory.CreateDirectory(folder + "/html");
                    Directory.CreateDirectory(folder + "/images");
                    Directory.CreateDirectory(folder + "/html/home");
                    Directory.CreateDirectory(folder + "/html/custom/home");
                    Directory.CreateDirectory(folder + "/images/buttons");
                    Directory.CreateDirectory(folder + "/images/banners");
                    Directory.CreateDirectory(folder + "/uploads");

                    // Get the parent name
                    string link_to_parent = String.Empty;
                    Item_Aggregation_Related_Aggregations parentAggr = Engine_ApplicationCache_Gateway.Codes.Aggregation_By_ID(parentid);
                    if (parentAggr != null)
                    {
                        if (String.Compare(parentAggr.Code, "all", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            link_to_parent = "<p>&larr; Back to <a href=\"<%BASEURL%>\" alt=\"Return to parent collection\">" + parentAggr.Name + "</a></p>" + Environment.NewLine;
                        }
                        else
                        {
                            link_to_parent = "<p>&larr; Back to <a href=\"<%BASEURL%>" + parentAggr.Code + "\" alt=\"Return to parent collection\">" + parentAggr.Name + "</a></p>" + Environment.NewLine;
                        }
                    }

                    // Create a default home text file
                    StreamWriter writer = new StreamWriter(folder + "/html/home/text.html");
                    writer.WriteLine(link_to_parent + "<h3>About " + NewAggregation.Name + "</h3>" + Environment.NewLine + "<p>" + NewAggregation.Description + "</p>" + Environment.NewLine + "<p>To edit this, log on as the aggregation admin and hover over this text to edit it.</p>" + Environment.NewLine);

                    writer.Flush();
                    writer.Close();

                    // Was a button indicated, and does it exist?
                    if ((!String.IsNullOrEmpty(NewAggregation.ButtonFile)) && (File.Exists(NewAggregation.ButtonFile)))
                    {
                        File.Copy(NewAggregation.ButtonFile, folder + "/images/buttons/coll.gif");
                    }
                    else
                    {
                        // Copy the default banner and buttons from images
                        if (File.Exists(Engine_ApplicationCache_Gateway.Settings.Servers.Base_Directory + "design/aggregations/default_button.png"))
                        {
                            File.Copy(Engine_ApplicationCache_Gateway.Settings.Servers.Base_Directory + "design/aggregations/default_button.png", folder + "/images/buttons/coll.png");
                        }
                        if (File.Exists(Engine_ApplicationCache_Gateway.Settings.Servers.Base_Directory + "design/aggregations/default_button.gif"))
                        {
                            File.Copy(Engine_ApplicationCache_Gateway.Settings.Servers.Base_Directory + "design/aggregations/default_button.gif", folder + "/images/buttons/coll.gif");
                        }
                    }

                    // Was a banner indicated, and does it exist?
                    string banner_file = String.Empty;
                    if ((!String.IsNullOrEmpty(NewAggregation.BannerFile)) && (File.Exists(NewAggregation.BannerFile)))
                    {
                        banner_file = "images/banners/" + Path.GetFileName(NewAggregation.BannerFile);
                        File.Copy(NewAggregation.BannerFile, folder + "//" + banner_file, true);
                    }
                    else
                    {
                        // Try to create a new custom banner
                        bool custom_banner_created = false;

                        // Create the banner with the name of the collection
                        if (Directory.Exists(Engine_ApplicationCache_Gateway.Settings.Servers.Application_Server_Network + "\\default\\banner_images"))
                        {
                            try
                            {
                                string[] banners = Directory.GetFiles(Engine_ApplicationCache_Gateway.Settings.Servers.Application_Server_Network + "\\default\\banner_images", "*.jpg");
                                if (banners.Length > 0)
                                {
                                    Random randomizer    = new Random();
                                    string banner_to_use = banners[randomizer.Next(0, banners.Length - 1)];
                                    Bitmap bitmap        = (Bitmap)(Image.FromFile(banner_to_use));

                                    RectangleF rectf = new RectangleF(30, bitmap.Height - 55, bitmap.Width - 40, 40);
                                    Graphics   g     = Graphics.FromImage(bitmap);
                                    g.SmoothingMode     = SmoothingMode.AntiAlias;
                                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                    g.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                                    g.DrawString(NewAggregation.Name, new Font("Tahoma", 25, FontStyle.Bold), Brushes.Black, rectf);
                                    g.Flush();

                                    string new_file = folder + "/images/banners/coll.jpg";
                                    if (!File.Exists(new_file))
                                    {
                                        bitmap.Save(new_file, ImageFormat.Jpeg);
                                        custom_banner_created = true;
                                        banner_file           = "images/banners/coll.jpg";
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                // Suppress this Error...
                            }
                        }

                        if ((!custom_banner_created) && (!File.Exists(folder + "/images/banners/coll.jpg")))
                        {
                            if (File.Exists(Engine_ApplicationCache_Gateway.Settings.Servers.Base_Directory + "design/aggregations/default_banner.jpg"))
                            {
                                banner_file = "images/banners/coll.jpg";
                                File.Copy(Engine_ApplicationCache_Gateway.Settings.Servers.Base_Directory + "design/aggregations/default_banner.jpg", folder + "/images/banners/coll.jpg", true);
                            }
                        }
                    }

                    // Now, try to create the item aggregation and write the configuration file
                    Custom_Tracer             tracer          = new Custom_Tracer();
                    Complete_Item_Aggregation itemAggregation = SobekEngineClient.Aggregations.Get_Complete_Aggregation(NewAggregation.Code, true, tracer);
                    if (banner_file.Length > 0)
                    {
                        itemAggregation.Banner_Dictionary.Clear();
                        itemAggregation.Add_Banner_Image(banner_file, Engine_ApplicationCache_Gateway.Settings.System.Default_UI_Language);
                    }
                    itemAggregation.Write_Configuration_File(Engine_ApplicationCache_Gateway.Settings.Servers.Base_Design_Location + itemAggregation.ObjDirectory);

                    // If an email shoudl be sent, do that now
                    if (String.Compare(Engine_ApplicationCache_Gateway.Settings.Email.Send_On_Added_Aggregation, "always", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        string user = String.Empty;
                        if (!String.IsNullOrEmpty(NewAggregation.User))
                        {
                            user = NewAggregation.User;
                        }

                        string body = "New aggregation added to this system:\n\n\tCode:\t" + itemAggregation.Code + "\n\tType:\t" + itemAggregation.Type + "\n\tName:\t" + itemAggregation.Name + "\n\tShort:\t" + itemAggregation.ShortName + "\n\tUser:\t" + user + "\n\n" + Engine_ApplicationCache_Gateway.Settings.Servers.Application_Server_URL + "/" + itemAggregation.Code;
                        Email_Helper.SendEmail(Engine_ApplicationCache_Gateway.Settings.Email.System_Email, "New " + itemAggregation.Type + " - " + itemAggregation.ShortName, body, false, Engine_ApplicationCache_Gateway.Settings.System.System_Name);
                    }
                }
            }
            catch
            {
                // Reload the list of all codes, to include this new one and the new hierarchy
                lock (Engine_ApplicationCache_Gateway.Codes)
                {
                    Engine_Database.Populate_Code_Manager(Engine_ApplicationCache_Gateway.Codes, null);
                }

                return(new RestResponseMessage(ErrorRestTypeEnum.Exception, "ERROR completing the new aggregation add"));
            }

            // Reload the list of all codes, to include this new one and the new hierarchy
            lock (Engine_ApplicationCache_Gateway.Codes)
            {
                Engine_Database.Populate_Code_Manager(Engine_ApplicationCache_Gateway.Codes, null);
            }

            // Clear all aggregation information (and thematic heading info) from the cache as well
            CachedDataManager.Aggregations.Clear();

            return(new RestResponseMessage(ErrorRestTypeEnum.Successful, null));
        }