-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
matt4446
committed
Mar 19, 2012
1 parent
9a1ac32
commit 88a4d7e
Showing
12 changed files
with
970 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Web.Mvc; | ||
using Orchard; | ||
using Orchard.DisplayManagement; | ||
using Orchard.Localization; | ||
using Orchard.Projections.Services; | ||
using Orchard.Security; | ||
using Orchard.Settings; | ||
using Orchard.ContentManagement; | ||
using Orchard.Tokens; | ||
using JsonProjection.Feeds; | ||
using Orchard.UI.Admin; | ||
using Orchard.Themes; | ||
using System.Net; | ||
|
||
namespace JsonProjection.Controllers | ||
{ | ||
public class ServiceController : Controller | ||
{ | ||
private readonly IJsonFeedService jsonFeed; | ||
|
||
public ServiceController( | ||
IOrchardServices services, | ||
IShapeFactory shapeFactory, | ||
IJsonFeedService jsonFeed) | ||
{ | ||
|
||
this.jsonFeed = jsonFeed; | ||
|
||
Services = services; | ||
|
||
T = NullLocalizer.Instance; | ||
Shape = shapeFactory; | ||
} | ||
|
||
public IOrchardServices Services { get; set; } | ||
|
||
public Localizer T { get; set; } | ||
|
||
public dynamic Shape { get; set; } | ||
|
||
[Admin] | ||
public ActionResult Index() | ||
{ | ||
var viewModel = new ViewModel.AdminIndexViewModel(); | ||
viewModel.Items = this.Services.ContentManager.Query<Orchard.Projections.Models.QueryPart>().List().ToList(); | ||
|
||
return View(viewModel); | ||
} | ||
|
||
public ActionResult Feed(int id) | ||
{ | ||
var feed = this.jsonFeed.Execute(id); | ||
|
||
return Content(feed, "JSON", Encoding.UTF8); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
[Themed] | ||
public ActionResult Preview(int id) { | ||
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage queries"))) | ||
return new HttpUnauthorizedResult(); | ||
|
||
var feed = this.jsonFeed.Execute(id); | ||
|
||
return Content(feed); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using ClaySharp; | ||
using Newtonsoft.Json; | ||
using Orchard.DisplayManagement; | ||
|
||
namespace JsonProjection.Converter | ||
{ | ||
//public class ClayJsonConverter : JsonConverter | ||
//{ | ||
// public override bool CanRead | ||
// { | ||
// get | ||
// { | ||
// // TODO: read Clay objects | ||
// return false; | ||
// } | ||
// } | ||
|
||
// public override void WriteJson(JsonWriter writer, IShape value, JsonSerializer serializer) | ||
// { | ||
// //var clay = (IClayBehaviorProvider)value; | ||
|
||
// var members = new Dictionary<string, object>(); | ||
// ((IClayBehaviorProvider)value).Behavior.GetMembers(() => null, value, members); | ||
|
||
|
||
// // Parse Clay Array | ||
// var e = ((dynamic)value).GetEnumerator(); | ||
// if (e != null && e.MoveNext()) | ||
// { | ||
// writer.WriteStartArray(); | ||
// do | ||
// { | ||
// serializer.Serialize(writer, e.Current); | ||
// } while (e.MoveNext()); | ||
// writer.WriteEndArray(); | ||
// } | ||
|
||
// var members = new Dictionary<string, object>(); | ||
// clay.Behavior.GetMembers(() => null, clay, members); | ||
|
||
// var memberKeys = members.Keys.Where(key => !key.StartsWith("_")).ToList(); | ||
|
||
// if (memberKeys.Count > 0) | ||
// { | ||
// writer.WriteStartObject(); | ||
// foreach (var key in memberKeys) | ||
// { | ||
// writer.WritePropertyName(key); | ||
// serializer.Serialize(writer, members[key]); | ||
// } | ||
|
||
// writer.WriteEndObject(); | ||
// } | ||
// } | ||
|
||
// public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
// { | ||
// throw new NotImplementedException(); | ||
// } | ||
|
||
// public override bool CanConvert(Type objectType) | ||
// { | ||
// return typeof(Clay).IsAssignableFrom(objectType); | ||
// } | ||
//} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using JsonProjection.Converter; | ||
using Newtonsoft.Json; | ||
using Orchard.ContentManagement; | ||
using Orchard.DisplayManagement; | ||
using Orchard.Localization; | ||
using Orchard.Projections.Descriptors.Layout; | ||
using Orchard.Projections.Models; | ||
using Orchard.Projections.Services; | ||
|
||
namespace JsonProjection.Layouts | ||
{ | ||
public class JsonLayout : ILayoutProvider | ||
{ | ||
private readonly IContentManager _contentManager; | ||
protected dynamic Shape { get; set; } | ||
|
||
public JsonLayout(IShapeFactory shapeFactory, IContentManager contentManager) | ||
{ | ||
_contentManager = contentManager; | ||
Shape = shapeFactory; | ||
T = NullLocalizer.Instance; | ||
} | ||
|
||
public Localizer T { get; set; } | ||
|
||
public void Describe(DescribeLayoutContext describe) | ||
{ | ||
describe.For("JSON", T("JSON"), T("JSON Layouts")) | ||
.Element("List", T("JSON List"), T("Organizes content items inside a json array."), | ||
DisplayLayout, | ||
RenderLayout, | ||
"JsonLayout" | ||
); | ||
} | ||
|
||
public LocalizedString DisplayLayout(LayoutContext context) | ||
{ | ||
string order = context.State.Order; | ||
|
||
switch (order) | ||
{ | ||
case "ordered": | ||
return T("Ordered Html List"); | ||
case "unordered": | ||
return T("Unordered Html List"); | ||
default: | ||
throw new ArgumentOutOfRangeException("order"); | ||
} | ||
} | ||
|
||
public dynamic RenderLayout(LayoutContext context, IEnumerable<LayoutComponentResult> layoutComponentResults) | ||
{ | ||
int columns = 2;//Convert.ToInt32(context.State.Columns); | ||
bool horizontal = Convert.ToString(context.State.Alignment) != "vertical"; | ||
string rowClass = context.State.RowClass; | ||
string gridClass = context.State.GridClass; | ||
string gridId = context.State.GridId; | ||
|
||
IEnumerable<dynamic> shapes = | ||
//context.LayoutRecord.Display == (int)LayoutRecord.Displays.Content | ||
// ? layoutComponentResults.Select(x => _contentManager.BuildDisplay(x.ContentItem, context.LayoutRecord.DisplayType)) | ||
layoutComponentResults.Select(x => x.Properties); | ||
|
||
//ClayJsonConverter converter = new ClayJsonConverter(); | ||
|
||
//StringBuilder sb = new StringBuilder(); | ||
//StringWriter sw = new StringWriter(sb); | ||
//using (JsonWriter jsonWriter = new JsonTextWriter(sw)) | ||
//{ | ||
// converter.WriteJson(jsonWriter, shapes, new JsonSerializer() { }); | ||
//} | ||
|
||
return Shape.JsonList(Id: gridId, Horizontal: horizontal, Columns: columns, Items: shapes, Classes: new[] { gridClass }, RowClasses: new[] { rowClass }); | ||
} | ||
|
||
//public dynamic RenderLayout(LayoutContext context, IEnumerable<LayoutComponentResult> layoutComponentResults) | ||
//{ | ||
// string order = context.State.Order; | ||
// string itemClass = context.State.ItemClass; | ||
// string listClass = context.State.ListClass; | ||
// string listId = context.State.ListId; | ||
|
||
// string listTag = order == "ordered" ? "ol" : "ul"; | ||
|
||
// IEnumerable<dynamic> shapes; | ||
// if (context.LayoutRecord.Display == (int)LayoutRecord.Displays.Content) | ||
// { | ||
// shapes = layoutComponentResults.Select(x => _contentManager.BuildDisplay(x.ContentItem, context.LayoutRecord.DisplayType)); | ||
// } | ||
// else | ||
// { | ||
// shapes = layoutComponentResults.Select(x => x.Properties); | ||
// } | ||
|
||
// var classes = String.IsNullOrEmpty(listClass) ? Enumerable.Empty<string>() : new[] { listClass }; | ||
// var itemClasses = String.IsNullOrEmpty(itemClass) ? Enumerable.Empty<string>() : new[] { itemClass }; | ||
|
||
// return Shape.List(Id: listId, Items: shapes, Tag: listTag, Classes: classes, ItemClasses: itemClasses); | ||
//} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Orchard.Core.Contents.Rules; | ||
using Orchard.DisplayManagement; | ||
using Orchard.Forms.Services; | ||
using Orchard.Localization; | ||
|
||
namespace JsonProjection.Layouts | ||
{ | ||
public class JsonLayoutForms : Orchard.Forms.Services.IFormProvider { | ||
protected dynamic Shape { get; set; } | ||
public Localizer T { get; set; } | ||
|
||
public JsonLayoutForms( | ||
IShapeFactory shapeFactory) { | ||
Shape = shapeFactory; | ||
T = NullLocalizer.Instance; | ||
} | ||
|
||
public void Describe(DescribeContext context) { | ||
//todo: add options: | ||
//1. Include Query definition in json | ||
//2. Require Authorization | ||
//3. Permission stereotype | ||
|
||
Func<IShapeFactory, object> form = | ||
shape => { | ||
|
||
var f = | ||
Shape.Form( | ||
Id: "JsonLayout", | ||
_Options: Shape.Fieldset( | ||
Title: T("Fields"), | ||
_ValueTrue: | ||
Shape.Checkbox( | ||
Id: "IncludeQueryDefinition", | ||
Title: T("Include Query Definition"), | ||
Checked: true, | ||
Description: T("Include the title of the query in the json results") | ||
), | ||
_ValueFalse: | ||
Shape.Checkbox( | ||
Id: "RequireAuthorization", | ||
Title: T("Require Authorization"), | ||
Checked: false, | ||
Description: T("Require user to be authorized") | ||
) | ||
) | ||
//_Options: Shape.Fieldset( | ||
//Title: T("Alignment"), | ||
//_ValueTrue: Shape.Radio( | ||
// Id: "horizontal", Name: "Alignment", | ||
// Title: T("Horizontal"), Value: "horizontal", | ||
// Checked: true, | ||
// Description: T("Horizontal alignment will place items starting in the upper left and moving right.") | ||
// ), | ||
//_ValueFalse: Shape.Radio( | ||
// Id: "vertical", Name: "Alignment", | ||
// Title: T("Vertical"), Value: "vertical", | ||
// Description: T("Vertical alignment will place items starting in the upper left and moving dowmn.") | ||
// ), | ||
//_Colums: Shape.TextBox( | ||
// Id: "columns", Name: "Columns", | ||
// Title: T("Number of columns/lines "), | ||
// Value: 3, | ||
// Description: T("How many columns (in Horizontal mode) or lines (in Vertical mode) to display in the grid."), | ||
// Classes: new[] { "small-text", "tokenized" } | ||
// ) | ||
//), | ||
|
||
); | ||
|
||
return f; | ||
}; | ||
|
||
context.Form("JsonLayout", form); | ||
|
||
} | ||
} | ||
|
||
public class JsonLayoutFormsValitator : FormHandler | ||
{ | ||
public Localizer T { get; set; } | ||
|
||
public override void Validating(ValidatingContext context) { | ||
if (context.FormName == "JsonLayout") | ||
{ | ||
//if (context.ValueProvider.GetValue("Order") == null) { | ||
// context.ModelState.AddModelError("Order", T("You must provide an Order").Text); | ||
//} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.