Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / Experiments / Generator / Generators / Scaffold / Templates / ScaffoldHelper.cs
blob909ae41a1778ae7704a6dde5bb086c69bd0c167d
1 using System;
2 using System.IO;
3 using System.Collections;
4 using System.Text.RegularExpressions;
5 using System.Reflection;
6 using Castle.ActiveRecord;
7 using Castle.ActiveRecord.Framework.Internal;
8 using Castle.MonoRail.Framework;
9 using Castle.MonoRail.Framework.Helpers;
11 namespace <%= HelpersNamespace %>
13 /// <summary>
14 /// Helper for displaying cool things on web pages!
15 /// </summary>
16 public class ScaffoldHelper : AbstractHelper
18 private HtmlHelper _htmlHelper = new HtmlHelper();
20 #region Formating helpers
21 public string ToHumanName(string name)
23 return Regex.Replace(name, "([a-z])([A-Z])", new MatchEvaluator(HumanNameMatchEvaluator));
25 private string HumanNameMatchEvaluator(Match m)
27 return m.Groups[1] + " " + m.Groups[2].ToString().ToLower();
29 public string ToHumanName(PropertyModel prop)
31 return ToHumanName(prop.Property.Name);
34 public string ToColumnName(string name)
36 if (Controller.Params["order"] == name)
37 return ToHumanName(name);
38 return string.Format("<a href=\"?order={0}\">{1}</a>", name, ToHumanName(name));
40 public string ToColumnName(PropertyModel prop)
42 if (prop.PropertyAtt.ColumnType == "StringClob") // Can't order by!
43 return ToHumanName(prop.Property.Name);
44 return ToColumnName(prop.Property.Name);
46 #endregion
48 #region Properties helpers
49 public PropertyModel[] GetProperties(IEnumerable enumerable)
51 IEnumerator en = enumerable.GetEnumerator();
52 en.MoveNext();
53 return GetProperties(en.Current.GetType());
56 public PropertyModel[] GetProperties(Type arType)
58 ArrayList props = new ArrayList();
59 ActiveRecordModel model = ActiveRecordModel.GetModel(arType);
61 props.AddRange(model.Properties);
63 foreach (BelongsToModel belong in model.BelongsTo)
65 props.Add(new PropertyModel(belong.Property, new PropertyAttribute()));
68 return (PropertyModel[]) props.ToArray(typeof(PropertyModel));
71 public object GetPropertyValue(object obj, PropertyModel prop)
73 return prop.Property.GetValue(obj, null);
76 public bool HasItems(IEnumerable enumerable)
78 return enumerable.GetEnumerator().MoveNext();
80 #endregion
82 #region Input helpers
83 public string InputFor(string prefix, object ar, PropertyModel prop)
85 string name = string.Format("{0}.{1}", prefix, prop.Property.Name);
86 Type type = prop.Property.PropertyType;
87 object value = GetPropertyValue(ar, prop);
89 if (typeof(ActiveRecordBase).IsAssignableFrom(type))
91 return Select(name, value as ActiveRecordBase, type);
93 else if (type == typeof(DateTime))
95 return HtmlHelper.DateTime(name, (DateTime) value);
97 else if (type == typeof(string) && prop.PropertyAtt.ColumnType == "StringClob")
99 return HtmlHelper.TextArea(name, 40, 10, (string) value);
102 if (value == null)
103 return HtmlHelper.InputText(name, "");
104 else
105 return HtmlHelper.InputText(name, value.ToString());
108 public string Select(string name, object ar)
110 return Select(name, ar, ar.GetType());
113 public string Select(string name, object ar, Type arType)
115 HtmlHelper helper = new HtmlHelper();
116 ActiveRecordModel model = ActiveRecordModel.GetModel(arType);
117 PropertyInfo prop = model.Key.Property;
118 object selected = null;
120 if (ar != null && prop != null)
122 selected = prop.GetValue(ar, null);
125 return helper.Select(name + ".Id")
126 + helper.CreateOptions(ActiveRecordMediator.FindAll(arType), "ToString", "Id", selected)
127 + helper.EndSelect();
129 #endregion
131 #region Errors helpers
132 public string ErrorsFor(ActiveRecordValidationBase ar)
134 if (this.Controller.Context.RequestType == "GET")
135 return null;
136 return String.Join("<br>", ar.ValidationErrorMessages);
139 public bool HasError(ActiveRecordValidationBase ar)
141 if (this.Controller.Context.RequestType == "GET")
142 return false;
143 return ar.ValidationErrorMessages.Length > 0;
145 #endregion
147 #region Pagination helpers
148 public string PageBrowser(Page page)
150 StringWriter output = new StringWriter();
151 PaginationHelper helper = new PaginationHelper();
152 helper.SetController(this.Controller);
154 if (page.HasFirst)
155 output.Write(helper.CreatePageLink(1, "First"));
156 else
157 output.Write("First");
159 output.Write(" | ");
161 if (page.HasPrevious)
162 output.Write(helper.CreatePageLink(page.PreviousIndex, "Previous"));
163 else
164 output.Write("Previous");
166 output.Write(" | ");
168 if (page.HasNext)
169 output.Write(helper.CreatePageLink(page.NextIndex, "Next"));
170 else
171 output.Write("Next");
173 output.Write(" | ");
175 if (page.HasLast)
176 output.Write(helper.CreatePageLink(page.LastIndex, "Last"));
177 else
178 output.Write("Last");
180 return output.ToString();
182 #endregion
184 #region Debugging helpers
185 public string Debug(object ar)
187 return "<code><pre>" + Debug(ar, 0) + "</code></pre>";
189 private string Debug(object ar, int indent)
191 if (ar == null) return null;
193 StringWriter output = new StringWriter();
194 string tab = new string('\t', indent);
195 object value;
197 output.WriteLine("{0}(#{1}):", tab, ar.GetType().Name);
198 foreach (PropertyModel prop in GetProperties(ar.GetType()))
200 value = GetPropertyValue(ar, prop);
202 output.WriteLine("{0}\t{1} ({2}) = {3}", tab, prop.Property.Name, prop.Property.PropertyType.Name, value);
203 if (typeof(ActiveRecordBase).IsAssignableFrom(prop.Property.PropertyType))
205 output.WriteLine(Debug(value as ActiveRecordBase, indent+1));
209 return output.ToString();
211 #endregion
213 #region Sub helpers
214 public HtmlHelper HtmlHelper {
215 get {
216 return _htmlHelper;
219 #endregion