/// <summary> Refresh the list of icon/wordmarks by pulling the data back from the database </summary>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public static bool RefreshIcons()
        {
            try
            {
                lock (iconListLock)
                {
                    if (iconList == null)
                    {
                        iconList = new Dictionary <string, Wordmark_Icon>(StringComparer.OrdinalIgnoreCase);
                    }

                    Engine_Database.Populate_Icon_List(iconList, null);
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
コード例 #2
0
        /// <summary> Constructor for a new instance of the Wordmarks_AdminViewer class </summary>
        /// <param name="RequestSpecificValues"> All the necessary, non-global data specific to the current request </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(RequestCache RequestSpecificValues)  : base(RequestSpecificValues)
        {
            RequestSpecificValues.Tracer.Add_Trace("Wordmarks_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 == null) || ((!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;
            }

            // 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>();
            Engine_Database.Populate_Icon_List(wordmarks, RequestSpecificValues.Tracer);

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

                    string new_wordmark_code = String.Empty;
                    if (form["admin_wordmark_code"] != null)
                    {
                        new_wordmark_code = form["admin_wordmark_code"].ToUpper().Trim();
                    }

                    // Was this a reset request?
                    if ((action_value == "DELETE") && (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)
                        {
                            RequestSpecificValues.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, RequestSpecificValues.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>();
                                Engine_Database.Populate_Icon_List(wordmarks, RequestSpecificValues.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 (action_value == "NEW")
                        {
                            bool alphaNumericTest = save_value.All(C => Char.IsLetterOrDigit(C) || C == '_');
                            if (save_value.Length == 0)
                            {
                                actionMessage = "ERROR: New wordmark code is a required field";
                            }
                            else if (!alphaNumericTest)
                            {
                                actionMessage = "ERROR: New wordmark code must be only letters and numbers";
                                save_value    = save_value.Replace("\"", "");
                            }
                            else
                            {
                                RequestSpecificValues.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, RequestSpecificValues.Tracer) > 0)
                                    {
                                        actionMessage = "Saved new wordmark <i>" + save_value + "</i>";
                                    }
                                    else
                                    {
                                        actionMessage = "Unable to save new wordmark <i>" + save_value + "</i>";
                                    }
                                }
                            }
                        }

                        if (action_value == "EDIT")
                        {
                            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, RequestSpecificValues.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>();
                        Engine_Database.Populate_Icon_List(wordmarks, RequestSpecificValues.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());
                }
            }
        }