/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using Everest.Library.ExtensionMethod;
namespace Everest.Forms.ExtForm{
public class ExtSimpleFormGenerator : ExtformGenerator
{
const string DefaultFormType = "Everest.FormPanel";
public ExtSimpleFormGenerator(ISchemaProvider provider) : base(provider) { }
public override string Generate(string schemaName, NameValueCollection config)
{
ISchema schema = SchemaProvider.GetSchema(schemaName, config);
StringBuilder stringBuilder = new StringBuilder();
/**************************
*
@"
cms.ContentForm.{0} = function(config){{
config = config || {{}};
Ext.apply(config,{{
labelWidth: 75,
url:'save-form.php',
frame:true,
title: 'Content Form',
bodyStyle:'padding:5px 5px 0',
defaults: {{width: 230}},
defaultType: 'textfield',
items: [{1}]
}});
cms.ContentForm.{0}.superclass.constructor.call(this,config);
}};
Ext.extend(cms.ContentForm.{0},Ext.FormPanel,{{
}});"
*
****************************/
bool fileUpload;
NameValueCollection formConfig = new NameValueCollection(schema.FormAttributes);
if (config != null)
{
foreach (var key in config.AllKeys)
{
formConfig[key] = config[key];
}
}
string attributes = GetAttributes(formConfig);
IColumn[] detailsColumns = schema.Columns.Where(c => !string.IsNullOrEmpty(c.DataIndexInDetail)).ToArray();
string columns = GetItemsJSON(schema, detailsColumns, config, out fileUpload);
stringBuilder.AppendFormat(@"
new {6}({{
{7}
labelWidth: 100,
url:'{3}',
loadUrl:'{4}',
frame:true,
containerType:'form',
title: '{0}',
bodyStyle:'padding:5px 5px 0',
defaults: {{width: 230}},
defaultType: 'textfield',
schemaName:'{2}',
fileUpload: {5},
items: [{1}],
childForms:[{8}]
}})
", schema.Title, columns, schemaName, schema.SubmitUrl, schema.DetailsUrl, fileUpload.ToString().ToLower(),
string.IsNullOrEmpty(schema.FormType) ? DefaultFormType : schema.FormType, attributes, GeneratorChildSchemas(schema, this.SchemaProvider, config));
return stringBuilder.ToString();
}
private string GetItemsJSON(ISchema schema, IColumn[] columns, NameValueCollection config, out bool fileUpload)
{
StringBuilder stringBuilder = new StringBuilder();
IOrderedEnumerable<IColumn> orderedColumn = columns.OrderBy<IColumn, int>((IColumn column) => column.Order);
fileUpload = false;
foreach (IColumn column in orderedColumn)
{
fileUpload = fileUpload || column.Control.FileUpload;
stringBuilder.AppendFormat("{0},", column.Control.Render(column));
}
stringBuilder.RemoveLastSpecifiedChar(',');
return stringBuilder.ToString();
}
}
}
|