using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using Cuyahoga.Web.UI; using Cuyahoga.Web.Util; using Cuyahoga.Core; using Cuyahoga.Core.Domain; using Cuyahoga.Core.Service; using Cuyahoga.Core.Search; using Cuyahoga.Core.Util; using Cuyahoga.Modules.ImageControl; namespace Cuyahoga.Modules.ImageControl { public partial class ImageControl : BaseModuleControl { private ImageControlModule _module; //protected string _path; private Cuyahoga.Web.UI.PageEngine _page; protected void Page_Load(object sender, EventArgs e) { //add stuff here this._module = this.Module as ImageControlModule; if (this.Page is PageEngine) { this._page = (PageEngine)this.Page; Node n = this._page.ActiveNode; string filename = traverse(n); HyperLink1.ImageUrl = Page.ResolveUrl(@"~/Images/header/" + filename); HyperLink1.NavigateUrl = UrlHelper.GetSiteUrl(); HyperLink1.Text = "Lory Student Center - Colorado State University"; } } private string traverse( Node no ) { string basepath = Server.MapPath(Page.ResolveUrl(@"~/Images/header/") + no.ShortDescription); string image_default = "index/" + System.Configuration.ConfigurationManager.AppSettings["defaultImage"]; //this._path += " | " + basepath; /***** If image directory exists for this node, find a picture there *****/ if (System.IO.Directory.Exists( basepath ) ) { //select random image //return basepath; //get directory files DirectoryInfo di = new DirectoryInfo(basepath); FileInfo[] fi = di.GetFiles("*.jpg"); int num = fi.Length; //this._path += " num: " + num; //make sure directory isn't empty /* * if case: no files found in dir and dir is at the root so return default * else if case: no files found, but it's not the root so we traverse up * else case: dir found and there are files so pick a random one */ if (num == 0 && no.IsRootNode) return image_default; else if (num == 0 && !no.IsRootNode) { return traverse(no.ParentNode); } else { //select random image from files Random randobj = new Random(); int randnum = randobj.Next(num); //this._path += no.ShortDescription + "/" + fi[randnum].Name; return no.ShortDescription + "/" + fi[randnum].Name; } } else { /*** if it's the root and there's still no dir found, return default * else traverse up to find dir */ if (no.IsRootNode) return image_default; else return traverse((Node)no.ParentNode); } } } }