Created
July 31, 2013 02:18
-
-
Save sitefinitysteve/6118815 to your computer and use it in GitHub Desktop.
Replacement for the sitefinity image asset field control, allows you to set a microdata attribute as well (among other things)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Web; | |
using Telerik.Sitefinity.Configuration; | |
using Telerik.Sitefinity.DynamicModules.Model; | |
using System.Web.UI; | |
using Telerik.Sitefinity.Model; | |
using Telerik.Sitefinity.Model.ContentLinks; | |
using Telerik.Sitefinity.Modules.Libraries; | |
using Telerik.Sitefinity.SitefinityExceptions; | |
using Telerik.Sitefinity.Web.UI.ContentUI; | |
using Telerik.Sitefinity; | |
using Telerik.Web.UI; | |
using System.Web.UI.WebControls; | |
using SitefinityWebApp.Code.Configuration; | |
namespace SitefinityWebApp.Usercontrols.Widgets { | |
/// <summary> | |
/// Sample: | |
/// <gs:SimpleImage runat="server" ImageDataField='<%# Eval("MainImage") %>' IsThumbnail="True" ShowTitle="True" IsLink="True" Type="dictionary" Title='<%# Eval("Title") %>' UrlName='<%# Eval("UrlName") %>' /> | |
/// </summary> | |
public class SimpleImage : System.Web.UI.UserControl { | |
protected void Page_PreRender(object sender, EventArgs e) { | |
LibrariesManager manager = LibrariesManager.GetManager(); | |
LiteralControl imageLiteral = new LiteralControl(); | |
var imageLink = this.ImageDataField; | |
if (imageLink != null) | |
{ | |
if (imageLink.Count() > 0) | |
{ | |
var link = imageLink.First(); | |
if (link != null) | |
{ | |
try { | |
var image = manager.GetImage(link.ChildItemId); | |
SetupImage(image, imageLiteral); | |
} | |
catch (ItemNotFoundException iex) { | |
this.AssignDefault(imageLiteral); | |
} | |
} | |
} | |
else { | |
//Get the default image from the config | |
this.AssignDefault(imageLiteral); | |
} | |
//Add wrapper | |
imageLiteral.Text = "<div class='simple-image-wrapper'>" + imageLiteral.Text + "</div>"; | |
this.Controls.Add(imageLiteral); | |
} | |
if(this.ImageModel != null){ | |
try | |
{ | |
SetupImage(this.ImageModel, imageLiteral); | |
} | |
catch (ItemNotFoundException iex) | |
{ | |
this.AssignDefault(imageLiteral); | |
} | |
} | |
} | |
private void SetupImage(Telerik.Sitefinity.Libraries.Model.Image image, LiteralControl imageLiteral) { | |
var imageUrl = image.Url; | |
if (this.IsThumbnail) { | |
var thumb = image.Thumbnails.FirstOrDefault(x => x.Name == this.ThumbnailName); | |
if (thumb == null) { | |
imageUrl = image.ThumbnailUrl; | |
} | |
else { | |
imageUrl = thumb.ResolveMediaUrl(); | |
} | |
} | |
if (this.RequiredHeight != -1) { | |
if (image.Height < this.RequiredHeight) { | |
imageUrl = imageUrl + ((imageUrl.Contains("?")) ? "&size=" + this.RequiredHeight : "?size=" + this.RequiredHeight); | |
} | |
} | |
if (this.IsLink) { | |
var detailUrl = String.Empty; | |
switch (this.Type) { | |
case "Dictionary": | |
detailUrl = String.Format("/ingredients/{0}/{1}", this.UrlName[0].ToString(), this.UrlName); | |
break; | |
default: | |
//Simple Link | |
detailUrl = string.Format("/{0}/{1}", this.Type.ToLower(), this.UrlName); | |
break; | |
} | |
imageLiteral.Text = String.Format(@"<a href='{2}'> | |
<img src='{0}' {1} {4} {5} {6} /> | |
{3} | |
</a>", | |
imageUrl, | |
(!String.IsNullOrEmpty(image.AlternativeText)) ? "alt='" + image.AlternativeText + "'" : "", | |
detailUrl, | |
(this.ShowTitle) ? "<span class='title'>" + this.Title + "</span>" : "", | |
(!String.IsNullOrEmpty(this.ItemProp)) ? "itemprop='" + this.ItemProp + "'" : "", | |
(this.Width.HasValue) ? " width='" + this.Width.Value + "px'" : "", | |
(!String.IsNullOrEmpty(this.CssClass)) ? "class='" + this.CssClass + "'" : ""); | |
} | |
else if (this.AsBackgroundImage) { | |
imageLiteral.Text = String.Format("<div style='background: url(\"{0}\") no-repeat center center; background-size: cover' {1}></div>", | |
imageUrl, | |
(!String.IsNullOrEmpty(this.CssClass)) ? "class='" + this.CssClass + "'" : ""); | |
} | |
else { | |
imageLiteral.Text = String.Format(@"<img src='{0}' {1} {3} {4} /> | |
{2}", | |
imageUrl, | |
(!String.IsNullOrEmpty(image.AlternativeText)) ? "alt='" + image.AlternativeText + "'" : "", | |
(this.ShowTitle) ? "<span class='title'>" + this.Title + "</span>" : "", | |
(!String.IsNullOrEmpty(this.ItemProp)) ? "itemprop='" + this.ItemProp + "'" : "", | |
(!String.IsNullOrEmpty(this.CssClass)) ? "class='" + this.CssClass + "'" : "" | |
); | |
} | |
if (this.ShowCredit) | |
this.AssignCredit(imageLiteral, image); | |
} | |
private void AssignDefault(LiteralControl imageLiteral) { | |
//Get the default image from the config | |
if (!String.IsNullOrEmpty(this.DefaultImageKey)) { | |
var defaultImageKey = Config.Get<GourmetSleuthConfig>().DefaultImages[this.DefaultImageKey]; | |
if (defaultImageKey != null) { | |
LibrariesManager manager = LibrariesManager.GetManager(); | |
var image = manager.GetImages().FirstOrDefault(x => x.ItemDefaultUrl == defaultImageKey.ImageUrl); | |
if (image != null) { | |
string imageUrl = (this.IsThumbnail) ? image.ThumbnailUrl : image.Url; | |
imageLiteral.Text = String.Format(@"<img src='{0}' {1} {3} {4} /> {2}", | |
imageUrl, | |
(!String.IsNullOrEmpty(image.AlternativeText)) ? "alt='" + image.AlternativeText + "'" : "", | |
(this.ShowTitle) ? "<span class='title'>" + this.Title + "</span>" : "", | |
(!String.IsNullOrEmpty(this.ItemProp)) ? "itemprop='" + this.ItemProp + "'" : "", | |
(!String.IsNullOrEmpty(this.CssClass)) ? "class='" + this.CssClass + "'" : ""); | |
} | |
} | |
} | |
} | |
private void AssignCredit(LiteralControl imageLiteral, Telerik.Sitefinity.Libraries.Model.Image image){ | |
bool hasText = false; | |
string creditText = String.Empty; | |
var creditName = image.GetValue<string>("CreditName"); | |
var creditUrl = image.GetValue<string>("CreditUrl"); | |
//Has name and link | |
if(!String.IsNullOrEmpty(creditName) && !String.IsNullOrEmpty(creditUrl)){ | |
creditText = String.Format("<div class='image-credit'><a href='{0}'>{2} {1}</a></div>", creditUrl, creditName, this.PhotoCreditText); | |
hasText = true; | |
}else{ | |
//Has name OR link | |
if(!String.IsNullOrEmpty(creditName)) | |
creditText = String.Format("<div class='image-credit'>{1} {0}</div>", creditName, this.PhotoCreditText); | |
else if(!String.IsNullOrEmpty(creditUrl)) | |
creditText = String.Format("<div class='image-credit'>{1} <a href='{0}'>{0}</a></div>", creditUrl, this.PhotoCreditText); | |
hasText = true; | |
} | |
if (hasText) | |
imageLiteral.Text += creditText; | |
} | |
public string DataFieldName { get; set; } | |
public string DefaultImageKey { get; set; } | |
private bool _isThumb = true; | |
public bool IsThumbnail | |
{ | |
get { return _isThumb; } | |
set { _isThumb = value; } | |
} | |
private string _thumbName = "medium"; | |
public string ThumbnailName { | |
get { return _thumbName; } | |
set { _thumbName = value; } | |
} | |
private bool _showTitle = false; | |
public bool ShowTitle | |
{ | |
get { return _showTitle; } | |
set { _showTitle = value; } | |
} | |
private bool _isLink = false; | |
public bool IsLink | |
{ | |
get { return _isLink; } | |
set { _isLink = value; } | |
} | |
private bool _showCredit = false; | |
public bool ShowCredit { | |
get { return _showCredit; } | |
set { _showCredit = value; } | |
} | |
private int _requiredHeight = -1; | |
public int RequiredHeight { | |
get { return _requiredHeight; } | |
set { _requiredHeight = value; } | |
} | |
private int _maxheight = -1; | |
public int MaxHeight { | |
get { return _maxheight; } | |
set { _maxheight = value; } | |
} | |
private bool _asBackgroundImage = false; | |
public bool AsBackgroundImage { | |
get { return _asBackgroundImage; } | |
set { _asBackgroundImage = value; } | |
} | |
public string ItemProp { get; set; } | |
public ContentLink[] ImageDataField { get; set; } | |
public Telerik.Sitefinity.Libraries.Model.Image ImageModel { get; set; } | |
private string _type = String.Empty; | |
public string Type | |
{ | |
get | |
{ | |
if (_type == String.Empty) | |
_type = HttpContext.Current.Request.Url.Segments[1].ToLower(); | |
return _type; | |
} | |
set{ | |
_type = value; | |
} | |
} | |
public string Title { get; set; } | |
public string UrlName { get; set; } | |
public int? Width { get; set; } | |
public int? Height { get; set; } | |
public string CssClass { get; set; } | |
private string _photoText = "photo credit:"; | |
public string PhotoCreditText { | |
get { return _photoText; } | |
set { _photoText = value; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment