/// <summary> Constructor for a new instance of the Wordmarks_AdminViewer class </summary>
        /// <param name="User"> Authenticated user information </param>
        /// <param name="Current_Mode"> Mode / navigation information for the current request</param>
        /// <param name="Tracer">Trace object keeps a list of each method executed and important milestones in rendering</param>
        /// <remarks> Postback from editing an existing wordmark, deleting a wordmark, or creating a new wordmark is handled here in the constructor </remarks>
        public Wordmarks_AdminViewer(User_Object User, SobekCM_Navigation_Object Current_Mode, Custom_Tracer Tracer) : base(User)
        {
            Tracer.Add_Trace("Wordmarks_AdminViewer.Constructor", String.Empty);

            // Save the mode and settings  here
            currentMode = Current_Mode;

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

            // If the user cannot edit this, go back
            if ((user == null) || ((!user.Is_System_Admin) && (!user.Is_Portal_Admin)))
            {
                currentMode.Mode          = Display_Mode_Enum.My_Sobek;
                currentMode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                currentMode.Redirect();
                return;
            }

            // Get the wordmark directory and ensure it exists
            wordmarkDirectory = HttpContext.Current.Server.MapPath("design/wordmarks");
            if (!Directory.Exists(wordmarkDirectory))
            {
                Directory.CreateDirectory(wordmarkDirectory);
            }

            // Get the list of all wordmarks
            wordmarks = new Dictionary <string, Wordmark_Icon>();
            SobekCM_Database.Populate_Icon_List(wordmarks, Tracer);

            // If this is a postback, handle any events first
            // if (currentMode.isPostBack)
            // {
            try
            {
                // Pull the standard values
                NameValueCollection form = HttpContext.Current.Request.Form;
                if (form["admin_wordmark_code_delete"] != null)
                {
                    string delete_value      = form["admin_wordmark_code_delete"].ToUpper().Trim();
                    string save_value        = form["admin_wordmark_code_tosave"].ToUpper().Trim();
                    string new_wordmark_code = form["admin_wordmark_code"].ToUpper().Trim();

                    // Was this a reset request?
                    if (delete_value.Length > 0)
                    {
                        // If the value to delete does not have a period, then it has no extension,
                        // so this is to delete a USED wordmark which is both a file AND in the database
                        if (delete_value.IndexOf(".") < 0)
                        {
                            Tracer.Add_Trace("Wordmarks_AdminViewer.Constructor", "Delete wordmark '" + delete_value + "' from the database");

                            // Get the wordmark, so we can also delete the file
                            Wordmark_Icon deleteIcon = wordmarks[delete_value];

                            // Delete from the database
                            if (SobekCM_Database.Delete_Icon(delete_value, Tracer))
                            {
                                // Set the deleted wordmark message
                                actionMessage = "Deleted wordmark <i>" + delete_value + "</i>";

                                // Try to delete the file related to this wordmark now
                                if ((deleteIcon != null) && (File.Exists(wordmarkDirectory + "\\" + deleteIcon.Image_FileName)))
                                {
                                    try
                                    {
                                        File.Delete(wordmarkDirectory + "\\" + deleteIcon.Image_FileName);
                                    }
                                    catch (Exception)
                                    {
                                        actionMessage = "Deleted wordmark <i>" + delete_value + "</i> but unable to delete the file <i>" + deleteIcon.Image_FileName + "</i>";
                                    }
                                }

                                // Repull the wordmark list now
                                wordmarks = new Dictionary <string, Wordmark_Icon>();
                                SobekCM_Database.Populate_Icon_List(wordmarks, Tracer);
                            }
                            else
                            {
                                // Report the error
                                if (SobekCM_Database.Last_Exception == null)
                                {
                                    actionMessage = "Unable to delete wordmark <i>" + delete_value + "</i> since it is in use";
                                }
                                else
                                {
                                    actionMessage = "Unknown error while deleting wordmark <i>" + delete_value + "</i>";
                                }
                            }
                        }
                        else
                        {
                            // This is to delete just a file, which presumably is unused by the system
                            // and does not appear in the database
                            // Try to delete the file related to this wordmark now
                            if (File.Exists(wordmarkDirectory + "\\" + delete_value))
                            {
                                try
                                {
                                    File.Delete(wordmarkDirectory + "\\" + delete_value);
                                    actionMessage = "Deleted unused image file <i>" + delete_value + "</i>";
                                }
                                catch (Exception)
                                {
                                    actionMessage = "Unable to delete unused image <i>" + delete_value + "</i>";
                                }
                            }
                        }
                    }
                    else
                    {
                        // Or.. was this a save request
                        if (save_value.Length > 0)
                        {
                            Tracer.Add_Trace("Wordmarks_AdminViewer.Constructor", "Save wordmark '" + save_value + "'");

                            // Was this to save a new interface (from the main page) or edit an existing (from the popup form)?
                            if (save_value == new_wordmark_code)
                            {
                                string new_file  = form["admin_wordmark_file"].Trim();
                                string new_link  = form["admin_wordmark_link"].Trim();
                                string new_title = form["admin_wordmark_title"].Trim();

                                // Save this new wordmark
                                if (SobekCM_Database.Save_Icon(new_wordmark_code, new_file, new_link, new_title, Tracer) > 0)
                                {
                                    actionMessage = "Saved new wordmark <i>" + save_value + "</i>";
                                }
                                else
                                {
                                    actionMessage = "Unable to save new wordmark <i>" + save_value + "</i>";
                                }
                            }
                            else
                            {
                                string edit_file  = form["form_wordmark_file"].Trim();
                                string edit_link  = form["form_wordmark_link"].Trim();
                                string edit_title = form["form_wordmark_title"].Trim();

                                // Save this existing wordmark
                                if (SobekCM_Database.Save_Icon(save_value, edit_file, edit_link, edit_title, Tracer) > 0)
                                {
                                    actionMessage = "Edited existing wordmark <i>" + save_value + "</i>";
                                }
                                else
                                {
                                    actionMessage = "Unable to edit existing wordmark <i>" + save_value + "</i>";
                                }
                            }

                            // Repull the wordmark list now
                            wordmarks = new Dictionary <string, Wordmark_Icon>();
                            SobekCM_Database.Populate_Icon_List(wordmarks, Tracer);
                        }
                    }
                }
            }
            catch (Exception)
            {
                actionMessage = "Unknown error caught while handing request.";
            }
            //}

            // Get the list of wordmarks in the directory
            string[] allFiles = SobekCM_File_Utilities.GetFiles(wordmarkDirectory, "*.jpg|*.jpeg|*.png|*.gif|*.bmp");
            loweredFiles = allFiles.Select(ThisFileName => new FileInfo(ThisFileName)).Select(ThisFileInfo => ThisFileInfo.Name.ToLower()).ToList();
            loweredFiles.Sort();

            // Get the list of all assigned wordmark files
            foreach (Wordmark_Icon thisWordmark in wordmarks.Values)
            {
                if (loweredFiles.Contains(thisWordmark.Image_FileName.ToLower()))
                {
                    loweredFiles.Remove(thisWordmark.Image_FileName.ToLower());
                }
            }
        }
Exemplo n.º 2
0
        /// <summary> Constructor for a new instance of the Wordmarks_AdminViewer class </summary>
        /// <param name="User"> Authenticated user information </param>
        /// <param name="Current_Mode"> Mode / navigation information for the current request</param>
        /// <param name="Tracer">Trace object keeps a list of each method executed and important milestones in rendering</param>
        /// <remarks> Postback from editing an existing wordmark, deleting a wordmark, or creating a new wordmark is handled here in the constructor </remarks>
        public Wordmarks_AdminViewer(User_Object User, SobekCM_Navigation_Object Current_Mode, Custom_Tracer Tracer)
            : base(User)
        {
            Tracer.Add_Trace("Wordmarks_AdminViewer.Constructor", String.Empty);

            // Save the mode and settings  here
            currentMode = Current_Mode;

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

            // If the user cannot edit this, go back
            if ((!user.Is_System_Admin) && (!user.Is_Portal_Admin))
            {
                currentMode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                HttpContext.Current.Response.Redirect(currentMode.Redirect_URL());
            }

            // If this is a postback, handle any events first
            if (currentMode.isPostBack)
            {
                try
                {
                    // Pull the standard values
                    NameValueCollection form = HttpContext.Current.Request.Form;

                    string delete_value      = form["admin_wordmark_code_delete"].ToUpper().Trim();
                    string save_value        = form["admin_wordmark_code_tosave"].ToUpper().Trim();
                    string new_wordmark_code = form["admin_wordmark_code"].ToUpper().Trim();

                    // Was this a reset request?
                    if (delete_value.Length > 0)
                    {
                        Tracer.Add_Trace("Wordmarks_AdminViewer.Constructor", "Delete wordmark '" + delete_value + "'");
                        if (SobekCM_Database.Delete_Icon(delete_value, Tracer))
                        {
                            actionMessage = "Deleted wordmark <i>" + delete_value + "</i>";
                        }
                        else
                        {
                            if (SobekCM_Database.Last_Exception == null)
                            {
                                actionMessage = "Unable to delete wordmark <i>" + delete_value + "</i> since it is in use";
                            }
                            else
                            {
                                actionMessage = "Unknown error while deleting wordmark <i>" + delete_value + "</i>";
                            }
                        }
                    }
                    else
                    {
                        // Or.. was this a save request
                        if (save_value.Length > 0)
                        {
                            Tracer.Add_Trace("Wordmarks_AdminViewer.Constructor", "Save wordmark '" + save_value + "'");

                            // Was this to save a new interface (from the main page) or edit an existing (from the popup form)?
                            if (save_value == new_wordmark_code)
                            {
                                string new_file  = form["admin_wordmark_file"].Trim();
                                string new_link  = form["admin_wordmark_link"].Trim();
                                string new_title = form["admin_wordmark_title"].Trim();

                                // Save this new wordmark
                                if (SobekCM_Database.Save_Icon(new_wordmark_code, new_file, new_link, new_title, Tracer) > 0)
                                {
                                    actionMessage = "Saved new wordmark <i>" + save_value + "</i>";
                                }
                                else
                                {
                                    actionMessage = "Unable to save new wordmark <i>" + save_value + "</i>";
                                }
                            }
                            else
                            {
                                string edit_file  = form["form_wordmark_file"].Trim();
                                string edit_link  = form["form_wordmark_link"].Trim();
                                string edit_title = form["form_wordmark_title"].Trim();

                                // Save this existing wordmark
                                if (SobekCM_Database.Save_Icon(save_value, edit_file, edit_link, edit_title, Tracer) > 0)
                                {
                                    actionMessage = "Edited existing wordmark <i>" + save_value + "</i>";
                                }
                                else
                                {
                                    actionMessage = "Unable to edit existing wordmark <i>" + save_value + "</i>";
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    actionMessage = "Unknown error caught while handing request.";
                }
            }
        }