Skip to content

Commit 1e77b5e

Browse files
committed
Merged PR 5859: Add options for allowed parents;
Add options for allowed parents; Clean up code with vs warnings
1 parent 5909d42 commit 1e77b5e

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

Infocaster.Umbraco.DateFolders.Website/appsettings.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
}
2525
},
2626
"DateFolders": {
27-
"ItemDateProperty": "",
27+
"ItemDateProperty": "publicationDate",
2828
"CreateDayFolders": false,
2929
"OrderByDescending": true,
3030
"FolderDocType": "dateFolder",
31-
"ItemDocTypes": [ "contentPage" ]
31+
"ItemDocTypes": [ "blogpost" ],
32+
"AllowedParentIds": [ 2222 ],
33+
"AllowedParentDocTypes": ["blog"]
3234
}
3335
}

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ After installing the package, just complete the configuration steps below and yo
3838
"CreateDayFolders": false,
3939
"OrderByDescending": true,
4040
"FolderDocType": "dateFolder",
41-
"ItemDocTypes": [ "contentPage" ]
41+
"ItemDocTypes": [ "contentPage" ],
42+
"AllowedParentIds": [ 2222 ],
43+
"AllowedParentDocTypes": ["blog"]
4244
}
4345
```
4446

@@ -47,6 +49,8 @@ After installing the package, just complete the configuration steps below and yo
4749
- **DateFolderDocType** | The doctype to use for creating the year/month/day folders. (e.g "DateFolder")
4850
- **CreateDayFolders** | Boolean indicating whether or not day folders should be created, if false only years and months are created.
4951
- **OrderByDecending** | Boolean indicating sort order for date folders (default: false)
52+
- **AllowedParentIds** | (Optional) The node id for the parent(s) to limit the creation of datefolders to. (e.g. 1234) - comma separated values are allowed for multiple note ids
53+
- **AllowedParentDocTypes** | (Optional) The doctype alias for the parent(s) to limit the creation of datefolders to. (e.g. "blog") - comma separated values are allowed for multiple doctype aliases
5054

5155
## Changelog
5256
Version 10.0.0

ReadMe.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ Add the following configuration to your appsettings.json file:
1515
"CreateDayFolders": false,
1616
"OrderByDescending": true,
1717
"FolderDocType": "dateFolder",
18-
"ItemDocTypes": [ "contentPage" ]
18+
"ItemDocTypes": [ "contentPage" ],
19+
"AllowedParentIds": [ 2222 ],
20+
"AllowedParentDocTypes": ["blog"]
1921
}
2022

2123
ItemDocType | The doctype alias to create datefolders for. (e.g. "contentPage") - comma separated values are allowed for multiple doctype aliases
2224
ItemDateProperty | The property of the itemDocType to read the date from. (e.g. "startDate") (don't add this key if you just want to use the document's create date)
2325
DateFolderDocType | The doctype to use for creating the year/month/day folders. (e.g "DateFolder")
2426
CreateDayFolders | Boolean indicating whether or not day folders should be created, if false only years and months are created.
2527
OrderByDecending | Boolean indicating sort order for date folders (default: false)
28+
AllowedParentIds | (Optional) The node id for the parent(s) to limit the creation of datefolders to. (e.g. 1234) - comma separated values are allowed for multiple note ids
29+
AllowedParentDocTypes | (Optional) The doctype alias for the parent(s) to limit the creation of datefolders to. (e.g. "blog") - comma separated values are allowed for multiple doctype aliases
30+
2631

2732
For more information, check out the links below:
2833
Github - https://github.com/Infocaster/Datefolders

src/Infocaster.Umbraco.DateFolders/Composers/DatefoldersComposer.cs

+30-12
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,16 @@ public DateFoldersContentSavedNotification(IOptions<DateFoldersConfigBase> optio
164164

165165
public void Handle(ContentSavedNotification notification)
166166
{
167-
foreach (var content in notification.SavedEntities)
167+
foreach (IContent savedContent in notification.SavedEntities)
168168
{
169+
// Fix for 'cannot save non-current version' error: https://our.umbraco.com/forum/using-umbraco-and-getting-started/99320-cannot-save-a-non-current-version
170+
// Error occurs when no datefolders available yet and multiple items are moved into datefolders
171+
IContent content = _contentService.GetById(savedContent.Id);
172+
169173
if (!_options.ItemDocTypes.Contains(content.ContentType.Alias)) continue;
170174

175+
if (!ParentValid(content)) continue;
176+
171177
IContentType folderDocType = _contentTypeService.Get(_options.FolderDocType);
172178
if (folderDocType is not null)
173179
{
@@ -176,9 +182,9 @@ public void Handle(ContentSavedNotification notification)
176182
var date = GetItemDate(content, _options.ItemDateProperty);
177183
IContent parent = _contentService.GetById(content.ParentId);
178184

179-
IContent? monthFolder = null;
180-
IContent? yearFolder = null;
181-
IContent? dayFolder = null;
185+
IContent monthFolder = null;
186+
IContent yearFolder = null;
187+
IContent dayFolder = null;
182188

183189
bool dayChanged = false;
184190
bool monthChanged;
@@ -196,9 +202,9 @@ public void Handle(ContentSavedNotification notification)
196202

197203
dayChanged = date.Day.ToString("00") != dayFolder.Name;
198204
}
199-
205+
200206
yearFolder = _contentService.GetById(monthFolder.ParentId);
201-
207+
202208
// Set item parent to source folder which contains the datefolders for sorting
203209
parent = _contentService.GetById(yearFolder.ParentId);
204210

@@ -214,7 +220,7 @@ public void Handle(ContentSavedNotification notification)
214220

215221
if (yearChanged || monthChanged || dayChanged)
216222
{
217-
IContent? newDayFolder = null;
223+
IContent newDayFolder = null;
218224

219225
IContent newYearFolder = yearChanged || yearFolder is null ? GetOrCreateAndPublishDateFolder(_contentService, parent, date.Year.ToString(), content.CreatorId) : yearFolder;
220226
IContent newMonthFolder = GetOrCreateAndPublishDateFolder(_contentService, newYearFolder, date.Month.ToString("00"), content.CreatorId);
@@ -256,7 +262,7 @@ public void Handle(ContentSavedNotification notification)
256262

257263
// Sort all content in folders by date
258264
OrderChildrenByDateProperty(orderParent, _options.OrderByDescending, !string.IsNullOrEmpty(_options.ItemDateProperty) ? _options.ItemDateProperty : null);
259-
265+
260266
// Sort all folders by name
261267
OrderChildrenByName(parent, _options.OrderByDescending);
262268
OrderChildrenByName(newYearFolder, _options.OrderByDescending);
@@ -292,7 +298,8 @@ private static DateTime GetItemDate(IContentBase content, string propertyAlias)
292298

293299
if (content.HasProperty(propertyAlias))
294300
{
295-
return content.GetValue<DateTime>(propertyAlias);
301+
DateTime propertyDate = content.GetValue<DateTime>(propertyAlias);
302+
return propertyDate == DateTime.MinValue ? content.CreateDate : propertyDate;
296303
}
297304
else
298305
{
@@ -310,7 +317,7 @@ private static DateTime GetItemDate(IContentBase content, string propertyAlias)
310317
/// <returns></returns>
311318
private IContent GetOrCreateAndPublishDateFolder(IContentService contentService, IContent parent, string nodeName, int currentUserId)
312319
{
313-
IContent? content = null;
320+
IContent content = null;
314321
var parentChildren = parent.GetAllChildren(_contentService);
315322

316323
// Get first child of FolderDocType if it exists
@@ -338,7 +345,7 @@ private void OrderChildrenByName(IContent parent, bool orderByDesc)
338345
{
339346
try
340347
{
341-
var allChildren = parent.GetAllChildren(_contentService);
348+
var allChildren = parent.GetAllChildren(_contentService);
342349

343350
var orderedChildren = (orderByDesc
344351
? allChildren.Where(c => int.TryParse(c.Name, out int i)).OrderByDescending(x => int.Parse(x.Name))
@@ -360,7 +367,7 @@ private void OrderChildrenByName(IContent parent, bool orderByDesc)
360367
/// <param name="parent"></param>
361368
/// <param name="orderByDesc"></param>
362369
/// <param name="propertyAlias"></param>
363-
private void OrderChildrenByDateProperty(IContent parent, bool orderByDesc, string? propertyAlias = null)
370+
private void OrderChildrenByDateProperty(IContent parent, bool orderByDesc, string propertyAlias = "")
364371
{
365372
try
366373
{
@@ -386,5 +393,16 @@ private void OrderChildrenByDateProperty(IContent parent, bool orderByDesc, stri
386393
_logger.LogError(ex, "DateFolders OrderChildrenByDateProperty exception");
387394
}
388395
}
396+
397+
private bool ParentValid(IContent content)
398+
{
399+
if (!_options.AllowedParentIds.Any() && !_options.AllowedParentDocTypes.Any()) return true;
400+
401+
IContent parentContentItem = _contentService.GetAncestors(content).Reverse().FirstOrDefault(x => !x.ContentType.Alias.Equals(_options.FolderDocType));
402+
if (_options.AllowedParentIds.Any() && _options.AllowedParentIds.Contains(parentContentItem.Id)) return true;
403+
if (_options.AllowedParentDocTypes.Any() && _options.AllowedParentDocTypes.Contains(parentContentItem.ContentType.Alias)) return true;
404+
405+
return false;
406+
}
389407
}
390408
}

src/Infocaster.Umbraco.DateFolders/Models/DateFoldersConfigBase.cs

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Infocaster.Umbraco.DateFolders.Models
55
public class DateFoldersConfigBase
66
{
77
public List<string> ItemDocTypes { get; set; } = new List<string>();
8+
public List<int> AllowedParentIds { get; set; } = new List<int>();
9+
public List<string> AllowedParentDocTypes { get; set; } = new List<string>();
810
public string FolderDocType { get; set; }
911
public bool OrderByDescending { get; set; } = true;
1012
public bool CreateDayFolders { get; set; } = false;

0 commit comments

Comments
 (0)