Skip to content

Commit 68c6887

Browse files
committed
Post custom fields make available for new unsaved post
1 parent 3deab18 commit 68c6887

11 files changed

Lines changed: 143 additions & 52 deletions

File tree

BlogEngine/BlogEngine.Core/Data/Contracts/ICustomFieldRepository.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,11 @@ public interface ICustomFieldRepository
4343
/// <param name="key">Key in the key/value for a field</param>
4444
/// <returns>True on success</returns>
4545
bool Remove(string type, string id, string key);
46+
/// <summary>
47+
/// Crear fields for object type (post, theme etc)
48+
/// </summary>
49+
/// <param name="type">Custom type</param>
50+
/// <param name="id">Object id</param>
51+
void ClearCustomFields(string type, string id);
4652
}
4753
}

BlogEngine/BlogEngine.Core/Data/CustomFieldRepository.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public bool Update(CustomField item)
111111
/// <returns>True on success</returns>
112112
public bool Remove(string type, string id, string key)
113113
{
114-
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.AccessAdminPages))
115-
throw new System.UnauthorizedAccessException();
114+
if (!Security.IsAuthorizedTo(Rights.AccessAdminPages))
115+
throw new UnauthorizedAccessException();
116116
try
117117
{
118118
var item = new CustomField
@@ -123,7 +123,7 @@ public bool Remove(string type, string id, string key)
123123
Key = key
124124
};
125125

126-
BlogEngine.Core.Providers.BlogService.DeleteCustomField(item);
126+
Providers.BlogService.DeleteCustomField(item);
127127
CustomFieldsParser.ClearCache();
128128
return true;
129129
}
@@ -134,6 +134,26 @@ public bool Remove(string type, string id, string key)
134134
}
135135
}
136136

137+
/// <summary>
138+
/// Crear fields for object type (post, theme etc)
139+
/// </summary>
140+
/// <param name="type">Custom type</param>
141+
/// <param name="id">Object id</param>
142+
public void ClearCustomFields(string type, string id)
143+
{
144+
if (!Security.IsAuthorizedTo(Rights.AccessAdminPages))
145+
throw new UnauthorizedAccessException();
146+
try
147+
{
148+
Providers.BlogService.ClearCustomFields(Blog.CurrentInstance.BlogId.ToString(), type, id);
149+
CustomFieldsParser.ClearCache();
150+
}
151+
catch (Exception ex)
152+
{
153+
Utils.Log("Error updaging custom field", ex);
154+
}
155+
}
156+
137157
bool AlreadyExists(CustomField item)
138158
{
139159
var field = CustomFieldsParser.CachedFields.Where(f => f.BlogId == item.BlogId

BlogEngine/BlogEngine.Core/Providers/BlogProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,13 @@ public abstract class BlogProvider : ProviderBase
469469
/// </summary>
470470
/// <param name="field">Object field</param>
471471
public abstract void DeleteCustomField(BlogEngine.Core.Data.Models.CustomField field);
472+
/// <summary>
473+
/// Clear custom fields for a type (post, theme etc)
474+
/// </summary>
475+
/// <param name="blogId">Blog id</param>
476+
/// <param name="customType">Custom type</param>
477+
/// <param name="objectType">Custom object</param>
478+
public abstract void ClearCustomFields(string blogId, string customType, string objectType);
472479

473480
#endregion
474481

BlogEngine/BlogEngine.Core/Providers/BlogService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,17 @@ public static void DeleteCustomField(BlogEngine.Core.Data.Models.CustomField fie
950950
Provider.DeleteCustomField(field);
951951
}
952952

953+
/// <summary>
954+
/// Clear custom fields for a type (post, theme etc)
955+
/// </summary>
956+
/// <param name="blogId">Blog id</param>
957+
/// <param name="customType">Custom type</param>
958+
/// <param name="objectType">Custom object</param>
959+
public static void ClearCustomFields(string blogId, string customType, string objectType)
960+
{
961+
Provider.ClearCustomFields(blogId, customType, objectType);
962+
}
963+
953964
#endregion
954965

955966
#endregion

BlogEngine/BlogEngine.Core/Providers/DbProvider/DbBlogProvider.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,6 +3115,36 @@ public override void DeleteCustomField(CustomField field)
31153115
}
31163116
}
31173117

3118+
/// <summary>
3119+
/// Clear custom fields for a type (post, theme etc)
3120+
/// </summary>
3121+
/// <param name="blogId">Blog id</param>
3122+
/// <param name="customType">Custom type</param>
3123+
/// <param name="objectType">Custom object</param>
3124+
public override void ClearCustomFields(string blogId, string customType, string objectType)
3125+
{
3126+
using (var conn = this.CreateConnection())
3127+
{
3128+
if (conn.HasConnection)
3129+
{
3130+
string conPrv = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BlogEngine"].ProviderName;
3131+
var sqlQuery = "delete from {0}CustomFields where CustomType = {1}customtype and BlogId = {1}blogid and ObjectId = {1}objectid";
3132+
if (conPrv == "MySql.Data.MySqlClient")
3133+
{
3134+
sqlQuery = "delete from {0}CustomFields where CustomType = {1}customtype and BlogId = {1}blogid and ObjectId = {1}objectid";
3135+
}
3136+
using (var cmd = conn.CreateTextCommand(string.Format(sqlQuery, this.tablePrefix, this.parmPrefix)))
3137+
{
3138+
var p = cmd.Parameters;
3139+
p.Add(conn.CreateParameter(FormatParamName("customtype"), customType));
3140+
p.Add(conn.CreateParameter(FormatParamName("blogid"), blogId));
3141+
p.Add(conn.CreateParameter(FormatParamName("objectid"), objectType));
3142+
cmd.ExecuteNonQuery();
3143+
}
3144+
}
3145+
}
3146+
}
3147+
31183148
#endregion
31193149

31203150
/// <summary>

BlogEngine/BlogEngine.Core/Providers/XmlProvider/CustomFields.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public partial class XmlBlogProvider : BlogProvider
1919
/// Saves custom field
2020
/// </summary>
2121
/// <param name="field">Object custom field</param>
22-
public override void SaveCustomField(BlogEngine.Core.Data.Models.CustomField field)
22+
public override void SaveCustomField(Data.Models.CustomField field)
2323
{
2424
var fileName = Path.Combine(this.Folder, _customFieldsFile);
2525

2626
var x = FillCustomFields();
2727

28-
var items = FillCustomFields() ?? new List<BlogEngine.Core.Data.Models.CustomField>();
28+
var items = FillCustomFields() ?? new List<Data.Models.CustomField>();
2929
int idx = -1;
3030

3131
for (int index = 0; index < items.Count; index++)
@@ -71,18 +71,18 @@ public override void SaveCustomField(BlogEngine.Core.Data.Models.CustomField fie
7171
/// </summary>
7272
/// <param name="blog">Current blog</param>
7373
/// <returns>List of custom fields</returns>
74-
public override List<BlogEngine.Core.Data.Models.CustomField> FillCustomFields()
74+
public override List<Data.Models.CustomField> FillCustomFields()
7575
{
7676
var fileName = Path.Combine(this.Folder, _customFieldsFile);
7777

7878
if (!File.Exists(fileName))
79-
return new List<BlogEngine.Core.Data.Models.CustomField>();
79+
return new List<Data.Models.CustomField>();
8080

8181
var doc = new XmlDocument();
8282
doc.Load(fileName);
8383

8484
return (from XmlNode node in doc.SelectNodes("CustomFields/item")
85-
select new BlogEngine.Core.Data.Models.CustomField
85+
select new Data.Models.CustomField
8686
{
8787
CustomType = node.Attributes["customtype"].InnerText,
8888
ObjectId = node.Attributes["objectid"].InnerText,
@@ -97,7 +97,7 @@ public override void SaveCustomField(BlogEngine.Core.Data.Models.CustomField fie
9797
/// Deletes custom field
9898
/// </summary>
9999
/// <param name="field">Object field</param>
100-
public override void DeleteCustomField(BlogEngine.Core.Data.Models.CustomField field)
100+
public override void DeleteCustomField(Data.Models.CustomField field)
101101
{
102102
var fileName = Path.Combine(this.Folder, _customFieldsFile);
103103
var xmlDoc = new XmlDocument();
@@ -128,5 +128,41 @@ public override void DeleteCustomField(BlogEngine.Core.Data.Models.CustomField f
128128
}
129129
xmlDoc.Save(fileName);
130130
}
131+
132+
/// <summary>
133+
/// Clear custom fields for a type (post, theme etc)
134+
/// </summary>
135+
/// <param name="blogId">Blog id</param>
136+
/// <param name="customType">Custom type</param>
137+
/// <param name="objectType">Custom object</param>
138+
public override void ClearCustomFields(string blogId, string customType, string objectType)
139+
{
140+
var fileName = Path.Combine(this.Folder, _customFieldsFile);
141+
var xmlDoc = new XmlDocument();
142+
143+
xmlDoc.Load(fileName);
144+
var items = xmlDoc.SelectNodes("CustomFields/item");
145+
146+
if (items != null && items.Count > 0)
147+
{
148+
for (int i = 0; i < items.Count; i++)
149+
{
150+
if (items[i].Attributes != null
151+
&& items[i].Attributes["customtype"] != null
152+
&& items[i].Attributes["blogid"] != null
153+
&& items[i].Attributes["objectid"] != null)
154+
{
155+
if (items[i].Attributes["customtype"].InnerText == customType
156+
&& items[i].Attributes["blogid"].InnerText == blogId
157+
&& items[i].Attributes["objectid"].InnerText == objectType)
158+
{
159+
if (items[i].ParentNode != null)
160+
items[i].ParentNode.RemoveChild(items[i]);
161+
}
162+
}
163+
}
164+
}
165+
xmlDoc.Save(fileName);
166+
}
131167
}
132168
}

BlogEngine/BlogEngine.NET/AppCode/Api/CustomFieldsController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ public HttpResponseMessage Put([FromBody]List<CustomField> items)
4343
{
4444
if (items != null && items.Count > 0)
4545
{
46+
repository.ClearCustomFields(items[0].CustomType, items[0].ObjectId);
47+
4648
foreach (var item in items)
4749
{
48-
repository.Update(item);
50+
repository.Add(item);
4951
}
5052
}
5153
return Request.CreateResponse(HttpStatusCode.OK);

BlogEngine/BlogEngine.NET/App_Data/datastore/extensions/AIQiniu.xml

Lines changed: 0 additions & 11 deletions
This file was deleted.

BlogEngine/BlogEngine.NET/admin/app/controllers/postEditor.js

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
if (data.Id) {
127127
angular.copy(data, $scope.post);
128128
var x = $scope.post.Id;
129+
$scope.updateCustom();
129130
}
130131
$("#modal-form").modal('hide');
131132
spinOff();
@@ -255,7 +256,7 @@
255256
});
256257
}
257258

258-
$scope.saveCustom = function () {
259+
$scope.addCustom = function () {
259260
var customField = {
260261
"CustomType": "POST",
261262
"ObjectId": $scope.post.Id,
@@ -266,19 +267,22 @@
266267
toastr.error("Custom key is required");
267268
return false;
268269
}
269-
dataService.addItem("/api/customfields", customField)
270-
.success(function (data) {
271-
toastr.success('New item added');
272-
$scope.loadCustom();
273-
$("#modal-custom-fields").modal('hide');
274-
})
275-
.error(function () {
276-
toastr.error($rootScope.lbl.updateFailed);
277-
$("#modal-custom-fields").modal('hide');
270+
$scope.customFields.push(customField);
271+
$("#modal-custom-fields").modal('hide');
272+
}
273+
274+
$scope.deleteCustom = function (key, objId) {
275+
$.each($scope.customFields, function (index, result) {
276+
if (result["Key"] == key && result["ObjectId"] == objId) {
277+
$scope.customFields.splice(index, 1);
278+
}
278279
});
279280
}
280281

281282
$scope.updateCustom = function () {
283+
for (var i = 0; i < $scope.customFields.length; i++) {
284+
$scope.customFields[i].ObjectId = $scope.post.Id;
285+
}
282286
dataService.updateItem("/api/customfields", $scope.customFields)
283287
.success(function (data) {
284288
spinOff();
@@ -289,25 +293,6 @@
289293
});
290294
}
291295

292-
$scope.deleteCustom = function (key, objId) {
293-
var customField = {
294-
"CustomType": "POST",
295-
"Key": key,
296-
"ObjectId": objId
297-
};
298-
spinOn();
299-
dataService.deleteItem("/api/customfields", customField)
300-
.success(function (data) {
301-
toastr.success("Item deleted");
302-
spinOff();
303-
$scope.loadCustom();
304-
})
305-
.error(function () {
306-
toastr.error($rootScope.lbl.couldNotDeleteItem);
307-
spinOff();
308-
});
309-
}
310-
311296
$scope.loadProfileCustomFields = function () {
312297
$scope.profileCustomFields = [];
313298

BlogEngine/BlogEngine.NET/admin/editpost.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
</div>
7676
</div>
7777
<div class="modal-footer">
78-
<button type="button" data-ng-click="saveCustom()" class="btn btn-success btn-tabkey pull-right"><i class="fa fa-save"></i>{{lbl.save}}</button>
78+
<button type="button" data-ng-click="addCustom()" class="btn btn-success btn-tabkey pull-right"><i class="fa fa-save"></i>{{lbl.save}}</button>
7979
<button type="button" class="btn btn-default" data-dismiss="modal" ng-click="focusInput=false"><i class="fa fa-ban"></i>{{lbl.cancel}}</button>
8080
</div>
8181
</div>
@@ -135,7 +135,7 @@
135135
<label class="newpost-widget-title">
136136
{{lbl.customFields}}
137137
</label>
138-
<a ng-disabled="post.Id === null || post.Id === ''" class="btn btn-default btn-sm btn-block" id="btnCustomFields" ng-click="showCustom()" data-toggle="modal">{{lbl.add}}</a>
138+
<a class="btn btn-default btn-sm btn-block" id="btnCustomFields" ng-click="showCustom()" data-toggle="modal">{{lbl.add}}</a>
139139
<div id="frm-custom-edit" ng-if="customFields && customFields.length > 0" class="form-horizontal clearfix" style="padding: 5px 0">
140140
<div data-ng-repeat="item in customFields">
141141
<label class="control-label">{{item.Key}}</label>

0 commit comments

Comments
 (0)