using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Cms.Engine;
using Telerik.Web.UI;
using System.Data;
using Telerik.Libraries;
using System.Collections;
using System.ComponentModel;
namespace edu.yale.som.Sitefinity.libraries
{
public partial class SlideShow : System.Web.UI.UserControl
{
#region UserControl Properties
//Name of Sitefinity Gallery
[Description("Name of Sitefinity Gallery to display.")]
public string Gallery
{
get { return ViewState["_Gallery"] as String; }
set { ViewState["_Gallery"] = value; }
}
//Control Height
[Category("Appearance")]
[Description("Default is 200")]
public string Height
{
get { return ViewState["_Height"] as String; }
set { ViewState["_Height"] = value; }
}
//Control Width
[Category("Appearance")]
[Description("Default is 200")]
public string Width
{
get { return ViewState["_Width"] as String; }
set { ViewState["_Width"] = value; }
}
//enum values for Animation Slide Show Type Drop down.
//public enum AnimationType { Select = 0, None = 1, Fade = 2, Pulse = 3 }
public enum AnimationType
{
Select = 0,
None = 1,
Fade = 2,
Pulse = 3
}
//Animation property that uses the enum valuies
[Category("Animation Settings")]
public AnimationType Animation { get; set; }
//Time in millseconds to run the transition animation
[Category("Animation Settings")]
[Description("Time in milliseconds for fade animation.")]
public string Duration
{
get { return ViewState["_Duration"] as String; }
set { ViewState["_Duration"] = value; }
}
//time in milliseconds that each each image will appear in the control
[Description("Time in milliseconds each image will appear.")]
public string FrameDuration
{
get { return ViewState["_FrameDuration"] as String; }
set { ViewState["_FrameDuration"] = value; }
}
//enum values for the pause on mouse over drop down
//public enum PauseType { True, False }
public enum PauseType
{
True,
False
}
//property to set the pause on mouse over setting.
[Category("Animation Settings")]
public PauseType PauseOnMouseOver { get; set; }
#endregion
protected void Page_Load(object sender, EventArgs e)
{
//Check and set properties
if (Gallery == null && Gallery.ToString() == string.Empty)
throw new Exception("Error: Gallery name not set");
if (Height != null && Height.ToString() != string.Empty)
rotator.Height = new Unit(Height);
if (Width != null && Width.ToString() != string.Empty)
rotator.Width = new Unit(Width);
//Animation Time
if (Duration != null && Duration.ToString() != string.Empty)
rotator.SlideShowAnimation.Duration = int.Parse(Duration);
//Length of Time for each image
if (FrameDuration != null && FrameDuration.ToString() != string.Empty)
rotator.FrameDuration = int.Parse(FrameDuration);
if (PauseOnMouseOver.ToString() == "True")
rotator.PauseOnMouseOver = true;
else
rotator.PauseOnMouseOver = false;
//set rotator type by casting Animation enum value to Telerik.Web.UI.Rotator.AnimationType
if (Animation != 0)
rotator.SlideShowAnimation.Type = (Telerik.Web.UI.Rotator.AnimationType)Animation;
//call bind rotator code
BindRotator();
}
#region Private Methods
//method for building datatable from gallery info and binding it to rotator control
private void BindRotator()
{
//Create data table
DataTable rotatorData = new DataTable();
rotatorData.Columns.Add("Image");
rotatorData.Columns.Add("Width");
rotatorData.Columns.Add("Height");
rotatorData.Columns.Add("LinkUrl");
//get Images from gallery
IList listOfImages = getImages(Gallery);
//check if there are any images
if (listOfImages.Count > 0)
{
//loop through each image and store url into datatable
foreach (IContent tempInfo in listOfImages)
{
//remove the ~ from the url, resize the images to match the dimensions of the rotator control
rotatorData.Rows.Add(new string[] { tempInfo.UrlWithExtension.Replace("~", ""), rotator.Width.ToString(), rotator.Height.ToString(),tempInfo.GetMetaData("Description").ToString() });
}
rotator.DataSource = rotatorData;
rotator.DataBind();
}
//if there are no images, hide the title and hide the rotator
else
{
rotator.Visible = false;
}
}
private IList getImages(string libName)
{
//create Sitefinity library manager
LibraryManager libManager = new LibraryManager("Libraries");
//try to access images, if the gallery does not exist the GetLibrary method throws an unhandled exception. The method will throw an error stating the library doesn't exist.
try
{
//get library object
ILibrary lib = libManager.GetLibrary(libName);
//get Library ID
Guid[] parentIDs = new Guid[1];
parentIDs[0] = lib.ID;
//get all images for specific library
IList listOfImages = libManager.GetContent(0, 0, "Publication_Date DESC", parentIDs);
return listOfImages;
}
catch
{
throw new Exception("Error: Gallery does not exist");
}
}
#endregion
}
}