Skip to content

Commit

Permalink
Merge pull request #20 from kjac/max-submissions
Browse files Browse the repository at this point in the history
Support max submissions
  • Loading branch information
kjac committed Mar 30, 2016
2 parents 101e9a0 + ec88c84 commit 7975605
Show file tree
Hide file tree
Showing 20 changed files with 239 additions and 120 deletions.
2 changes: 1 addition & 1 deletion Grunt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"grunt-template": "^0.2.3"
},
"meta": {
"version": "0.11.1.1"
"version": "0.12.0.1"
}
}
4 changes: 2 additions & 2 deletions Samples/Advanced custom field/My.Fields.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Examine.0.1.68.0\lib\Examine.dll</HintPath>
</Reference>
<Reference Include="FormEditor, Version=0.11.0.2, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="FormEditor, Version=0.12.0.1, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\FormEditor.Binaries.0.11.0.2\lib\net40\FormEditor.dll</HintPath>
<HintPath>..\packages\FormEditor.Binaries.0.12.0.1\lib\net40\FormEditor.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack, Version=1.4.9.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
2 changes: 1 addition & 1 deletion Samples/Advanced custom field/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<package id="ClientDependency" version="1.8.4" targetFramework="net45" />
<package id="ClientDependency-Mvc5" version="1.8.0.0" targetFramework="net45" />
<package id="Examine" version="0.1.68.0" targetFramework="net45" />
<package id="FormEditor.Binaries" version="0.11.0.2" targetFramework="net45" />
<package id="FormEditor.Binaries" version="0.12.0.1" targetFramework="net45" />
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="net45" />
<package id="ImageProcessor" version="2.3.0.0" targetFramework="net45" />
<package id="ImageProcessor.Web" version="4.4.0.0" targetFramework="net45" />
Expand Down
4 changes: 2 additions & 2 deletions Samples/Custom validation condition/My.Conditions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
<HintPath>..\packages\Examine.0.1.68.0\lib\Examine.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FormEditor, Version=0.11.0.2, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="FormEditor, Version=0.12.0.1, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\FormEditor.Binaries.0.11.0.2\lib\net40\FormEditor.dll</HintPath>
<HintPath>..\packages\FormEditor.Binaries.0.12.0.1\lib\net40\FormEditor.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack">
<HintPath>..\packages\HtmlAgilityPack.1.4.9\lib\Net45\HtmlAgilityPack.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion Samples/Custom validation condition/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<package id="ClientDependency" version="1.8.4" targetFramework="net45" />
<package id="ClientDependency-Mvc5" version="1.8.0.0" targetFramework="net45" />
<package id="Examine" version="0.1.68.0" targetFramework="net45" />
<package id="FormEditor.Binaries" version="0.11.0.2" targetFramework="net45" />
<package id="FormEditor.Binaries" version="0.12.0.1" targetFramework="net45" />
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="net45" />
<package id="ImageProcessor" version="2.3.0.0" targetFramework="net45" />
<package id="ImageProcessor.Web" version="4.4.0.0" targetFramework="net45" />
Expand Down
4 changes: 2 additions & 2 deletions Samples/Elastic storage index/FormEditor.ElasticIndex.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@
<HintPath>..\packages\Examine.0.1.68.0\lib\Examine.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FormEditor, Version=0.11.0.2, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="FormEditor, Version=0.12.0.1, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\FormEditor.Binaries.0.11.0.2\lib\net40\FormEditor.dll</HintPath>
<HintPath>..\packages\FormEditor.Binaries.0.12.0.1\lib\net40\FormEditor.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack, Version=1.4.9.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.4.9\lib\Net45\HtmlAgilityPack.dll</HintPath>
Expand Down
18 changes: 6 additions & 12 deletions Samples/Elastic storage index/Storage/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,24 +190,18 @@ public bool SaveFile(HttpPostedFile file, string filename, Guid rowId)

public void Delete()
{
// make sure the index exists before we attempt to delete it.
//var response = Client.Search<Entry>(s => s.Fields(f => f.Id).Take(1));
//if(response.ServerError != null)
//{
// // - 404 = index was not found. that's ok.
// if(response.ServerError.Status != NotFound)
// {
// Log.Warning("An error occurred trying to delete the index: {0}. Server error: {1} ({2}).", IndexName(), response.ServerError.Error, response.ServerError.Status);
// }
// return;
//}

// create a client for the base index uri and delete the entire index by name.
var uri = GetConnectionUri();
var client = new ElasticClient(new ConnectionSettings(uri));
client.DeleteIndex(IndexName());
}

public int Count()
{
var response = Client.Count<Entry>();
return response.ServerError == null ? (int)response.Count : 0;
}

private static StorageRow ToFormRow(Entry entry)
{
return new StorageRow(Guid.Parse(entry.Id), entry.CreatedDate, entry.FieldValues);
Expand Down
2 changes: 1 addition & 1 deletion Samples/Elastic storage index/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<package id="ClientDependency-Mvc5" version="1.8.0.0" targetFramework="net45" />
<package id="Elasticsearch.Net" version="1.6.0" targetFramework="net45" />
<package id="Examine" version="0.1.68.0" targetFramework="net45" />
<package id="FormEditor.Binaries" version="0.11.0.2" targetFramework="net45" />
<package id="FormEditor.Binaries" version="0.12.0.1" targetFramework="net45" />
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="net45" />
<package id="ImageProcessor" version="2.3.0.0" targetFramework="net45" />
<package id="ImageProcessor.Web" version="4.4.0.0" targetFramework="net45" />
Expand Down
4 changes: 2 additions & 2 deletions Samples/Event handling/My.Events.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
<Reference Include="Examine">
<HintPath>..\packages\Examine.0.1.68.0\lib\Examine.dll</HintPath>
</Reference>
<Reference Include="FormEditor, Version=0.11.0.2, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="FormEditor, Version=0.12.0.1, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\FormEditor.Binaries.0.11.0.2\lib\net40\FormEditor.dll</HintPath>
<HintPath>..\packages\FormEditor.Binaries.0.12.0.1\lib\net40\FormEditor.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack">
<HintPath>..\packages\HtmlAgilityPack.1.4.9\lib\Net45\HtmlAgilityPack.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion Samples/Event handling/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<package id="ClientDependency" version="1.8.4" targetFramework="net45" />
<package id="ClientDependency-Mvc5" version="1.8.0.0" targetFramework="net45" />
<package id="Examine" version="0.1.68.0" targetFramework="net45" />
<package id="FormEditor.Binaries" version="0.11.0.2" targetFramework="net45" />
<package id="FormEditor.Binaries" version="0.12.0.1" targetFramework="net45" />
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="net45" />
<package id="ImageProcessor" version="2.3.0.0" targetFramework="net45" />
<package id="ImageProcessor.Web" version="4.4.0.0" targetFramework="net45" />
Expand Down
4 changes: 2 additions & 2 deletions Samples/SQL storage index/FormEditor.SqlIndex.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Examine.0.1.68.0\lib\Examine.dll</HintPath>
</Reference>
<Reference Include="FormEditor, Version=0.11.0.2, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="FormEditor, Version=0.12.0.1, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\FormEditor.Binaries.0.11.0.2\lib\net40\FormEditor.dll</HintPath>
<HintPath>..\packages\FormEditor.Binaries.0.12.0.1\lib\net40\FormEditor.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack, Version=1.4.9.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
13 changes: 12 additions & 1 deletion Samples/SQL storage index/Storage/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public Result Get(string sortField, bool sortDescending, int count, int skip)
// the field values were serialized into one column when they were added to the index, so we
// can't sort on sortField. instead we'll sort on Id DESC so we always return the newest entries first.
var pageNumber = (skip / count) + 1;
var page = Database.Page<Entry>(pageNumber, count, "WHERE ContentId=@0 ORDER BY Id DESC", _contentId);
var page = GetPage(pageNumber, count);

var rows = page != null & page.Items.Any()
? page.Items.Select(ToFormRow).Where(r => r != null).ToList()
Expand All @@ -89,6 +89,11 @@ public Result Get(string sortField, bool sortDescending, int count, int skip)
return new Result((int)page.TotalItems, rows, "Id", true);
}

private Page<Entry> GetPage(int pageNumber, int count)
{
return Database.Page<Entry>(pageNumber, count, "WHERE ContentId=@0 ORDER BY Id DESC", _contentId);
}

public Stream GetFile(string filename, Guid rowId)
{
var file = Database.SingleOrDefault<File>("WHERE Filename=@0", filename);
Expand Down Expand Up @@ -130,6 +135,12 @@ public void Delete()
Database.Delete<Entry>("WHERE ContentId=@0", _contentId);
}

public int Count()
{
var page = GetPage(1, 1);
return (int)page.TotalItems;
}

private Database Database
{
get { return UmbracoContext.Current.Application.DatabaseContext.Database; }
Expand Down
2 changes: 1 addition & 1 deletion Samples/SQL storage index/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<package id="ClientDependency" version="1.8.4" targetFramework="net45" />
<package id="ClientDependency-Mvc5" version="1.8.0.0" targetFramework="net45" />
<package id="Examine" version="0.1.68.0" targetFramework="net45" />
<package id="FormEditor.Binaries" version="0.11.0.2" targetFramework="net45" />
<package id="FormEditor.Binaries" version="0.12.0.1" targetFramework="net45" />
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="net45" />
<package id="ImageProcessor" version="2.3.0.0" targetFramework="net45" />
<package id="ImageProcessor.Web" version="4.4.0.0" targetFramework="net45" />
Expand Down
61 changes: 55 additions & 6 deletions Source/Solution/FormEditor/FormModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public IEnumerable<Row> Rows
public int SuccessPageId { get; set; }
public string ReceiptHeader { get; set; }
public string ReceiptBody { get; set; }
public int? MaxSubmissions { get; set; }
public string MaxSubmissionsExceededHeader { get; set; }
public string MaxSubmissionsExceededText { get; set; }

#endregion

Expand All @@ -79,11 +82,11 @@ public IEnumerable<Row> Rows

public bool CollectSubmittedValues(bool redirect = true)
{
if (UmbracoContext.Current == null || UmbracoContext.Current.PublishedContentRequest == null || UmbracoContext.Current.PublishedContentRequest.PublishedContent == null)
if(RequestedContent == null)
{
return false;
}
return CollectSubmittedValues(UmbracoContext.Current.PublishedContentRequest.PublishedContent, redirect);
return CollectSubmittedValues(RequestedContent, redirect);
}

public bool CollectSubmittedValues(IPublishedContent content, bool redirect = true)
Expand All @@ -94,6 +97,12 @@ public bool CollectSubmittedValues(IPublishedContent content, bool redirect = tr
return false;
}

// are we able to accept any submissions for this form?
if(MaxSubmissionsExceeded(content))
{
return false;
}

// first collect the submitted values
var fields = CollectSubmittedValuesFromRequest(content);

Expand Down Expand Up @@ -134,6 +143,10 @@ public bool CollectSubmittedValues(IPublishedContent content, bool redirect = tr

// before add to index event handling did not cancel - add to index
var rowId = AddSubmittedValuesToIndex(content, valueFields);
if(rowId == Guid.Empty)
{
return false;
}

// tell everyone that something was added
RaiseAfterAddToIndex(rowId);
Expand All @@ -150,11 +163,11 @@ public bool CollectSubmittedValues(IPublishedContent content, bool redirect = tr

public FormData GetSubmittedValues(int page = 1, int perPage = 10, string sortField = null, bool sortDescending = false)
{
if(UmbracoContext.Current == null || UmbracoContext.Current.PublishedContentRequest == null || UmbracoContext.Current.PublishedContentRequest.PublishedContent == null)
if(RequestedContent == null)
{
return new FormData();
}
return GetSubmittedValues(UmbracoContext.Current.PublishedContentRequest.PublishedContent, page, perPage, sortField, sortDescending);
return GetSubmittedValues(RequestedContent, page, perPage, sortField, sortDescending);
}

public FormData GetSubmittedValues(IPublishedContent content, int page = 1, int perPage = 10, string sortField = null, bool sortDescending = false)
Expand Down Expand Up @@ -185,11 +198,47 @@ public IEnumerable<FieldWithValue> AllValueFields()
return AllFields().OfType<FieldWithValue>();
}

public bool MaxSubmissionsExceeded()
{
if(RequestedContent == null)
{
return true;
}
return MaxSubmissionsExceeded(RequestedContent);
}

public bool MaxSubmissionsExceeded(IPublishedContent content)
{
if(MaxSubmissions.HasValue == false)
{
return false;
}
var index = IndexHelper.GetIndex(content.Id);
return index.Count() >= MaxSubmissions.Value;
}

private HttpRequest Request
{
get { return HttpContext.Current.Request; }
}

private UmbracoContext Context
{
get { return UmbracoContext.Current; }
}

private IPublishedContent RequestedContent
{
get
{
if(Context == null || Context.PublishedContentRequest == null || Context.PublishedContentRequest.PublishedContent == null)
{
return null;
}
return Context.PublishedContentRequest.PublishedContent;
}
}

#region Stuff for backwards compatibility with v0.10.0.2 (before introducing form pages) - should probably be removed at some point

private void EnsurePagesForBackwardsCompatibility()
Expand Down Expand Up @@ -503,7 +552,7 @@ private void LoadPreValues(IPublishedContent content)
{
return;
}
var preValues = UmbracoContext.Current.Application.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(property.DataTypeId);
var preValues = Context.Application.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(property.DataTypeId);
if(preValues == null)
{
return;
Expand Down Expand Up @@ -535,7 +584,7 @@ private static bool GetPreValueAsBoolean(string key, IDictionary<string, PreValu

private void RedirectToSuccesPage()
{
var helper = new UmbracoHelper(UmbracoContext.Current);
var helper = new UmbracoHelper(Context);
var redirectTo = helper.TypedContent(SuccessPageId);
if(redirectTo != null)
{
Expand Down
4 changes: 2 additions & 2 deletions Source/Solution/FormEditor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("0.11.1.1")]
[assembly: AssemblyFileVersion("0.11.1.1")]
[assembly: AssemblyVersion("0.12.0.1")]
[assembly: AssemblyFileVersion("0.12.0.1")]
6 changes: 6 additions & 0 deletions Source/Solution/FormEditor/Storage/IIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,11 @@ public interface IIndex
/// Deletes the entire index
/// </summary>
void Delete();

/// <summary>
/// Gets the number of entries in the index
/// </summary>
/// <returns>The number of entries</returns>
int Count();
}
}
8 changes: 8 additions & 0 deletions Source/Solution/FormEditor/Storage/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ public void Delete()
}
}

public int Count()
{
var reader = GetIndexReader();
var count = reader.NumDocs();
reader.Close();
return count;
}

private LuceneDirectory IndexDirectory
{
get
Expand Down
22 changes: 22 additions & 0 deletions Source/Umbraco/Plugin/editor/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,28 @@ <h4 form-editor-localize key="receipt.message.header">...or enter a message</h4>
<!-- ## data ##-->
<div id="tabFormEditorData" class="tab-pane">
<div ng-include="'/App_Plugins/FormEditor/editor/data.html'"></div>
<h5 form-editor-localize key="data.maxSubmissions.header">Limit the number of submissions</h5>
<p form-editor-localize key="data.maxSubmissions.text">
If you want to limit the number of submissions for this form, enter the maximum allowed number of submissions and a message to show the users when no more submissions are allowed.
</p>
<label class="control-label" for="maxSubmissions" form-editor-localize key="data.maxSubmissions">
Allowed submissions
</label>
<div class="controls controls-row">
<input type="number" class="umb-editor" id="maxSubmissions" ng-model="model.value.maxSubmissions" min="1" />
</div>
<label class="control-label" for="maxSubmissionsExceededHeader" form-editor-localize key="data.maxSubmissions.message.heading">
Heading
</label>
<div class="controls controls-row">
<input type="text" class="umb-editor" id="maxSubmissionsExceededHeader" ng-model="model.value.maxSubmissionsExceededHeader" />
</div>
<label class="control-label" for="maxSubmissionsExceededText" form-editor-localize key="data.maxSubmissions.message.body">
Text
</label>
<div class="controls controls-row">
<textarea class="umb-editor" id="maxSubmissionsExceededText" ng-model="model.value.maxSubmissionsExceededText" rows="5"></textarea>
</div>
</div>
</div>

Expand Down
Loading

0 comments on commit 7975605

Please sign in to comment.