/
PublishedContentExtensions.cs
1965 lines (1673 loc) · 88.4 KB
/
PublishedContentExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using Examine.LuceneEngine.SearchCriteria;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Services;
using Umbraco.Web.Models;
using Umbraco.Core;
using Umbraco.Web.Routing;
using ContentType = umbraco.cms.businesslogic.ContentType;
namespace Umbraco.Web
{
/// <summary>
/// Provides extension methods for <c>IPublishedContent</c>.
/// </summary>
public static class PublishedContentExtensions
{
#region Key
public static Guid GetKey(this IPublishedContent content)
{
var contentWithKey = content as IPublishedContentWithKey;
return contentWithKey == null ? Guid.Empty : contentWithKey.Key;
}
#endregion
#region Urls
/// <summary>
/// Gets the url for the content.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>The url for the content.</returns>
[Obsolete("NiceUrl() is obsolete, use the Url() method instead")]
public static string NiceUrl(this IPublishedContent content)
{
return content.Url();
}
/// <summary>
/// Gets the url for the content.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>The url for the content.</returns>
/// <remarks>Better use the <c>Url</c> property but that method is here to complement <c>UrlAbsolute()</c>.</remarks>
public static string Url(this IPublishedContent content)
{
return content.Url;
}
/// <summary>
/// Gets the absolute url for the content.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>The absolute url for the content.</returns>
[Obsolete("NiceUrlWithDomain() is obsolete, use the UrlAbsolute() method instead.")]
public static string NiceUrlWithDomain(this IPublishedContent content)
{
return content.UrlAbsolute();
}
/// <summary>
/// Gets the absolute url for the content.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>The absolute url for the content.</returns>
//[Obsolete("UrlWithDomain() is obsolete, use the UrlAbsolute() method instead.")]
public static string UrlWithDomain(this IPublishedContent content)
{
return content.UrlAbsolute();
}
/// <summary>
/// Gets the absolute url for the content.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>The absolute url for the content.</returns>
public static string UrlAbsolute(this IPublishedContent content)
{
// adapted from PublishedContentBase.Url
switch (content.ItemType)
{
case PublishedItemType.Content:
if (UmbracoContext.Current == null)
throw new InvalidOperationException("Cannot resolve a Url for a content item when UmbracoContext.Current is null.");
if (UmbracoContext.Current.UrlProvider == null)
throw new InvalidOperationException("Cannot resolve a Url for a content item when UmbracoContext.Current.UrlProvider is null.");
return UmbracoContext.Current.UrlProvider.GetUrl(content.Id, true);
case PublishedItemType.Media:
throw new NotSupportedException("AbsoluteUrl is not supported for media types.");
default:
throw new ArgumentOutOfRangeException();
}
}
#endregion
#region Template
/// <summary>
/// Returns the current template Alias
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string GetTemplateAlias(this IPublishedContent content)
{
var template = ApplicationContext.Current.Services.FileService.GetTemplate(content.TemplateId);
return template == null ? string.Empty : template.Alias;
}
#endregion
#region IsComposedOf
/// <summary>
/// Gets a value indicating whether the content is of a content type composed of the given alias
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The content type alias.</param>
/// <returns>A value indicating whether the content is of a content type composed of a content type identified by the alias.</returns>
public static bool IsComposedOf(this IPublishedContent content, string alias)
{
return content.ContentType.CompositionAliases.Contains(alias);
}
#endregion
#region HasProperty
/// <summary>
/// Gets a value indicating whether the content has a property identified by its alias.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <returns>A value indicating whether the content has the property identified by the alias.</returns>
/// <remarks>The content may have a property, and that property may not have a value.</remarks>
public static bool HasProperty(this IPublishedContent content, string alias)
{
return content.ContentType.GetPropertyType(alias) != null;
}
#endregion
#region HasValue
/// <summary>
/// Gets a value indicating whether the content has a value for a property identified by its alias.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <returns>A value indicating whether the content has a value for the property identified by the alias.</returns>
/// <remarks>Returns true if <c>GetProperty(alias)</c> is not <c>null</c> and <c>GetProperty(alias).HasValue</c> is <c>true</c>.</remarks>
public static bool HasValue(this IPublishedContent content, string alias)
{
return content.HasValue(alias, false);
}
/// <summary>
/// Gets a value indicating whether the content has a value for a property identified by its alias.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="recurse">A value indicating whether to navigate the tree upwards until a property with a value is found.</param>
/// <returns>A value indicating whether the content has a value for the property identified by the alias.</returns>
/// <remarks>Returns true if <c>GetProperty(alias, recurse)</c> is not <c>null</c> and <c>GetProperty(alias, recurse).HasValue</c> is <c>true</c>.</remarks>
public static bool HasValue(this IPublishedContent content, string alias, bool recurse)
{
var prop = content.GetProperty(alias, recurse);
return prop != null && prop.HasValue;
}
/// <summary>
/// Returns one of two strings depending on whether the content has a value for a property identified by its alias.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="valueIfTrue">The value to return if the content has a value for the property.</param>
/// <param name="valueIfFalse">The value to return if the content has no value for the property.</param>
/// <returns>Either <paramref name="valueIfTrue"/> or <paramref name="valueIfFalse"/> depending on whether the content
/// has a value for the property identified by the alias.</returns>
public static IHtmlString HasValue(this IPublishedContent content, string alias,
string valueIfTrue, string valueIfFalse = null)
{
return content.HasValue(alias, false)
? new HtmlString(valueIfTrue)
: new HtmlString(valueIfFalse ?? string.Empty);
}
/// <summary>
/// Returns one of two strings depending on whether the content has a value for a property identified by its alias.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="recurse">A value indicating whether to navigate the tree upwards until a property with a value is found.</param>
/// <param name="valueIfTrue">The value to return if the content has a value for the property.</param>
/// <param name="valueIfFalse">The value to return if the content has no value for the property.</param>
/// <returns>Either <paramref name="valueIfTrue"/> or <paramref name="valueIfFalse"/> depending on whether the content
/// has a value for the property identified by the alias.</returns>
public static IHtmlString HasValue(this IPublishedContent content, string alias, bool recurse,
string valueIfTrue, string valueIfFalse = null)
{
return content.HasValue(alias, recurse)
? new HtmlString(valueIfTrue)
: new HtmlString(valueIfFalse ?? string.Empty);
}
#endregion
#region GetPropertyValue
/// <summary>
/// Gets the value of a content's property identified by its alias.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <returns>The value of the content's property identified by the alias.</returns>
/// <remarks>
/// <para>The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering content.</para>
/// <para>If no property with the specified alias exists, or if the property has no value, returns <c>null</c>.</para>
/// <para>If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.</para>
/// <para>The alias is case-insensitive.</para>
/// </remarks>
public static object GetPropertyValue(this IPublishedContent content, string alias)
{
var property = content.GetProperty(alias);
return property == null ? null : property.Value;
}
/// <summary>
/// Gets the value of a content's property identified by its alias, if it exists, otherwise a default value.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the content's property identified by the alias, if it exists, otherwise a default value.</returns>
/// <remarks>
/// <para>The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering content.</para>
/// <para>If no property with the specified alias exists, or if the property has no value, returns <paramref name="defaultValue"/>.</para>
/// <para>If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.</para>
/// <para>The alias is case-insensitive.</para>
/// </remarks>
public static object GetPropertyValue(this IPublishedContent content, string alias, string defaultValue)
{
var property = content.GetProperty(alias);
return property == null || property.HasValue == false ? defaultValue : property.Value;
}
/// <summary>
/// Gets the value of a content's property identified by its alias, if it exists, otherwise a default value.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the content's property identified by the alias, if it exists, otherwise a default value.</returns>
/// <remarks>
/// <para>The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering content.</para>
/// <para>If no property with the specified alias exists, or if the property has no value, returns <paramref name="defaultValue"/>.</para>
/// <para>If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.</para>
/// <para>The alias is case-insensitive.</para>
/// </remarks>
public static object GetPropertyValue(this IPublishedContent content, string alias, object defaultValue)
{
var property = content.GetProperty(alias);
return property == null || property.HasValue == false ? defaultValue : property.Value;
}
/// <summary>
/// Recursively gets the value of a content's property identified by its alias.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="recurse">A value indicating whether to recurse.</param>
/// <returns>The recursive value of the content's property identified by the alias.</returns>
/// <remarks>
/// <para>Recursively means: walking up the tree from <paramref name="content"/>, get the first value that can be found.</para>
/// <para>The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering content.</para>
/// <para>If no property with the specified alias exists, or if the property has no value, returns <c>null</c>.</para>
/// <para>If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.</para>
/// <para>The alias is case-insensitive.</para>
/// </remarks>
public static object GetPropertyValue(this IPublishedContent content, string alias, bool recurse)
{
var property = content.GetProperty(alias, recurse);
return property == null ? null : property.Value;
}
/// <summary>
/// Recursively the value of a content's property identified by its alias, if it exists, otherwise a default value.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="recurse">A value indicating whether to recurse.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the content's property identified by the alias, if it exists, otherwise a default value.</returns>
/// <remarks>
/// <para>Recursively means: walking up the tree from <paramref name="content"/>, get the first value that can be found.</para>
/// <para>The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering content.</para>
/// <para>If no property with the specified alias exists, or if the property has no value, returns <paramref name="defaultValue"/>.</para>
/// <para>If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.</para>
/// <para>The alias is case-insensitive.</para>
/// </remarks>
public static object GetPropertyValue(this IPublishedContent content, string alias, bool recurse, object defaultValue)
{
var property = content.GetProperty(alias, recurse);
return property == null || property.HasValue == false ? defaultValue : property.Value;
}
#endregion
#region GetPropertyValue<T>
/// <summary>
/// Gets the value of a content's property identified by its alias, converted to a specified type.
/// </summary>
/// <typeparam name="T">The target property type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <returns>The value of the content's property identified by the alias, converted to the specified type.</returns>
/// <remarks>
/// <para>The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering content.</para>
/// <para>If no property with the specified alias exists, or if the property has no value, or if it could not be converted, returns <c>default(T)</c>.</para>
/// <para>If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.</para>
/// <para>The alias is case-insensitive.</para>
/// </remarks>
public static T GetPropertyValue<T>(this IPublishedContent content, string alias)
{
return content.GetPropertyValue(alias, false, false, default(T));
}
/// <summary>
/// Gets the value of a content's property identified by its alias, converted to a specified type, if it exists, otherwise a default value.
/// </summary>
/// <typeparam name="T">The target property type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the content's property identified by the alias, converted to the specified type, if it exists, otherwise a default value.</returns>
/// <remarks>
/// <para>The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering content.</para>
/// <para>If no property with the specified alias exists, or if the property has no value, or if it could not be converted, returns <paramref name="defaultValue"/>.</para>
/// <para>If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.</para>
/// <para>The alias is case-insensitive.</para>
/// </remarks>
public static T GetPropertyValue<T>(this IPublishedContent content, string alias, T defaultValue)
{
return content.GetPropertyValue(alias, false, true, defaultValue);
}
/// <summary>
/// Recursively gets the value of a content's property identified by its alias, converted to a specified type.
/// </summary>
/// <typeparam name="T">The target property type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="recurse">A value indicating whether to recurse.</param>
/// <returns>The value of the content's property identified by the alias, converted to the specified type.</returns>
/// <remarks>
/// <para>Recursively means: walking up the tree from <paramref name="content"/>, get the first value that can be found.</para>
/// <para>The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering content.</para>
/// <para>If no property with the specified alias exists, or if the property has no value, or if it could not be converted, returns <c>default(T)</c>.</para>
/// <para>If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.</para>
/// <para>The alias is case-insensitive.</para>
/// </remarks>
public static T GetPropertyValue<T>(this IPublishedContent content, string alias, bool recurse)
{
return content.GetPropertyValue(alias, recurse, false, default(T));
}
/// <summary>
/// Recursively gets the value of a content's property identified by its alias, converted to a specified type, if it exists, otherwise a default value.
/// </summary>
/// <typeparam name="T">The target property type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="recurse">A value indicating whether to recurse.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the content's property identified by the alias, converted to the specified type, if it exists, otherwise a default value.</returns>
/// <remarks>
/// <para>Recursively means: walking up the tree from <paramref name="content"/>, get the first value that can be found.</para>
/// <para>The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering content.</para>
/// <para>If no property with the specified alias exists, or if the property has no value, or if it could not be converted, returns <paramref name="defaultValue"/>.</para>
/// <para>If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.</para>
/// <para>The alias is case-insensitive.</para>
/// </remarks>
public static T GetPropertyValue<T>(this IPublishedContent content, string alias, bool recurse, T defaultValue)
{
return content.GetPropertyValue(alias, recurse, true, defaultValue);
}
internal static T GetPropertyValue<T>(this IPublishedContent content, string alias, bool recurse, bool withDefaultValue, T defaultValue)
{
var property = content.GetProperty(alias, recurse);
if (property == null) return defaultValue;
return property.GetValue(withDefaultValue, defaultValue);
}
#endregion
// copied over from Core.PublishedContentExtensions - should be obsoleted
[Obsolete("GetRecursiveValue() is obsolete, use GetPropertyValue().")]
public static string GetRecursiveValue(this IPublishedContent content, string alias)
{
var value = content.GetPropertyValue(alias, true);
return value == null ? string.Empty : value.ToString();
}
#region Search
public static IEnumerable<IPublishedContent> Search(this IPublishedContent content, string term, bool useWildCards = true, string searchProvider = null)
{
var searcher = Examine.ExamineManager.Instance.DefaultSearchProvider;
if (string.IsNullOrEmpty(searchProvider) == false)
searcher = Examine.ExamineManager.Instance.SearchProviderCollection[searchProvider];
var t = term.Escape().Value;
if (useWildCards)
t = term.MultipleCharacterWildcard().Value;
var luceneQuery = "+__Path:(" + content.Path.Replace("-", "\\-") + "*) +" + t;
var crit = searcher.CreateSearchCriteria().RawQuery(luceneQuery);
return content.Search(crit, searcher);
}
public static IEnumerable<IPublishedContent> SearchDescendants(this IPublishedContent content, string term, bool useWildCards = true, string searchProvider = null)
{
return content.Search(term, useWildCards, searchProvider);
}
public static IEnumerable<IPublishedContent> SearchChildren(this IPublishedContent content, string term, bool useWildCards = true, string searchProvider = null)
{
var searcher = Examine.ExamineManager.Instance.DefaultSearchProvider;
if (string.IsNullOrEmpty(searchProvider) == false)
searcher = Examine.ExamineManager.Instance.SearchProviderCollection[searchProvider];
var t = term.Escape().Value;
if (useWildCards)
t = term.MultipleCharacterWildcard().Value;
var luceneQuery = "+parentID:" + content.Id + " +" + t;
var crit = searcher.CreateSearchCriteria().RawQuery(luceneQuery);
return content.Search(crit, searcher);
}
public static IEnumerable<IPublishedContent> Search(this IPublishedContent content, Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null)
{
var s = Examine.ExamineManager.Instance.DefaultSearchProvider;
if (searchProvider != null)
s = searchProvider;
var results = s.Search(criteria);
return results.ConvertSearchResultToPublishedContent(UmbracoContext.Current.ContentCache);
}
#endregion
#region ToContentSet
/// <summary>
/// Returns the content enumerable as a content set.
/// </summary>
/// <param name="source">The content enumerable.</param>
/// <returns>A content set wrapping the content enumerable.</returns>
public static PublishedContentSet<T> ToContentSet<T>(this IEnumerable<T> source)
where T : class, IPublishedContent
{
return new PublishedContentSet<T>(source);
}
/// <summary>
/// Returns the ordered content enumerable as an ordered content set.
/// </summary>
/// <param name="source">The ordered content enumerable.</param>
/// <returns>A ordered content set wrapping the ordered content enumerable.</returns>
public static PublishedContentOrderedSet<T> ToContentSet<T>(this IOrderedEnumerable<T> source)
where T : class, IPublishedContent
{
return new PublishedContentOrderedSet<T>(source);
}
#endregion
#region Dynamic Linq Extensions
// todo - we should keep this file clean and remove dynamic linq stuff from it
public static IQueryable<IPublishedContent> OrderBy(this IEnumerable<IPublishedContent> source, string predicate)
{
var dList = new DynamicPublishedContentList(source);
return dList.OrderBy<DynamicPublishedContent>(predicate);
}
public static IQueryable<IPublishedContent> Where(this IEnumerable<IPublishedContent> list, string predicate)
{
// wrap in DynamicPublishedContentList so that the ContentSet is correct
// though that code is somewhat ugly.
var dlist = new DynamicPublishedContentList(new DynamicPublishedContentList(list)
.Where<DynamicPublishedContent>(predicate));
return dlist.AsQueryable<IPublishedContent>();
}
public static IEnumerable<IGrouping<object, IPublishedContent>> GroupBy(this IEnumerable<IPublishedContent> list, string predicate)
{
var dList = new DynamicPublishedContentList(list);
return dList.GroupBy(predicate);
}
public static IQueryable Select(this IEnumerable<IPublishedContent> list, string predicate, params object[] values)
{
var dList = new DynamicPublishedContentList(list);
return dList.Select(predicate);
}
public static HtmlString Where(this IPublishedContent content, string predicate, string valueIfTrue)
{
if (content == null) throw new ArgumentNullException("content");
return content.Where(predicate, valueIfTrue, string.Empty);
}
public static HtmlString Where(this IPublishedContent content, string predicate, string valueIfTrue, string valueIfFalse)
{
if (content == null) throw new ArgumentNullException("content");
return new HtmlString(content.Where(predicate) ? valueIfTrue : valueIfFalse);
}
public static bool Where(this IPublishedContent content, string predicate)
{
if (content == null) throw new ArgumentNullException("content");
var dynamicDocumentList = new DynamicPublishedContentList { content.AsDynamicOrNull() };
var filtered = dynamicDocumentList.Where<DynamicPublishedContent>(predicate);
return filtered.Count() == 1;
}
#endregion
#region AsDynamic
// it is ok to have dynamic here
// content should NOT be null
public static dynamic AsDynamic(this IPublishedContent content)
{
if (content == null) throw new ArgumentNullException("content");
return new DynamicPublishedContent(content);
}
// content CAN be null
internal static DynamicPublishedContent AsDynamicOrNull(this IPublishedContent content)
{
return content == null ? null : new DynamicPublishedContent(content);
}
#endregion
#region ContentSet
public static int Position(this IPublishedContent content)
{
return content.GetIndex();
}
public static int Index(this IPublishedContent content)
{
return content.GetIndex();
}
private static int GetIndex(this IPublishedContent content, IEnumerable<IPublishedContent> set)
{
var index = set.FindIndex(n => n.Id == content.Id);
if (index < 0)
throw new IndexOutOfRangeException("Could not find content in the content set.");
return index;
}
#endregion
#region IsSomething: misc.
/// <summary>
/// Gets a value indicating whether the content is visible.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>A value indicating whether the content is visible.</returns>
/// <remarks>A content is not visible if it has an umbracoNaviHide property with a value of "1". Otherwise,
/// the content is visible.</remarks>
public static bool IsVisible(this IPublishedContent content)
{
// note: would be better to ensure we have an IPropertyEditorValueConverter for booleans
// and then treat the umbracoNaviHide property as a boolean - vs. the hard-coded "1".
// rely on the property converter - will return default bool value, ie false, if property
// is not defined, or has no value, else will return its value.
return content.GetPropertyValue<bool>(Constants.Conventions.Content.NaviHide) == false;
}
/// <summary>
/// Determines whether the specified content is a specified content type.
/// </summary>
/// <param name="content">The content to determine content type of.</param>
/// <param name="docTypeAlias">The alias of the content type to test against.</param>
/// <returns>True if the content is of the specified content type; otherwise false.</returns>
public static bool IsDocumentType(this IPublishedContent content, string docTypeAlias)
{
return content.DocumentTypeAlias.InvariantEquals(docTypeAlias);
}
/// <summary>
/// Determines whether the specified content is a specified content type or it's derived types.
/// </summary>
/// <param name="content">The content to determine content type of.</param>
/// <param name="docTypeAlias">The alias of the content type to test against.</param>
/// <param name="recursive">When true, recurses up the content type tree to check inheritance; when false just calls IsDocumentType(this IPublishedContent content, string docTypeAlias).</param>
/// <returns>True if the content is of the specified content type or a derived content type; otherwise false.</returns>
public static bool IsDocumentType(this IPublishedContent content, string docTypeAlias, bool recursive)
{
if (content.IsDocumentType(docTypeAlias))
return true;
if (recursive)
return IsDocumentTypeRecursive(content, docTypeAlias);
return false;
}
private static bool IsDocumentTypeRecursive(IPublishedContent content, string docTypeAlias)
{
var contentTypeService = UmbracoContext.Current.Application.Services.ContentTypeService;
var type = contentTypeService.GetContentType(content.DocumentTypeAlias);
while (type != null && type.ParentId > 0)
{
type = contentTypeService.GetContentType(type.ParentId);
if (type.Alias.InvariantEquals(docTypeAlias))
return true;
}
return false;
}
public static bool IsNull(this IPublishedContent content, string alias, bool recurse)
{
return content.HasValue(alias, recurse) == false;
}
public static bool IsNull(this IPublishedContent content, string alias)
{
return content.HasValue(alias) == false;
}
#endregion
#region IsSomething: position in set
public static bool IsFirst(this IPublishedContent content)
{
return content.GetIndex() == 0;
}
public static HtmlString IsFirst(this IPublishedContent content, string valueIfTrue)
{
return content.IsFirst(valueIfTrue, string.Empty);
}
public static HtmlString IsFirst(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsFirst() ? valueIfTrue : valueIfFalse);
}
public static bool IsNotFirst(this IPublishedContent content)
{
return content.IsFirst() == false;
}
public static HtmlString IsNotFirst(this IPublishedContent content, string valueIfTrue)
{
return content.IsNotFirst(valueIfTrue, string.Empty);
}
public static HtmlString IsNotFirst(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsNotFirst() ? valueIfTrue : valueIfFalse);
}
public static bool IsPosition(this IPublishedContent content, int index)
{
return content.GetIndex() == index;
}
public static HtmlString IsPosition(this IPublishedContent content, int index, string valueIfTrue)
{
return content.IsPosition(index, valueIfTrue, string.Empty);
}
public static HtmlString IsPosition(this IPublishedContent content, int index, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsPosition(index) ? valueIfTrue : valueIfFalse);
}
public static bool IsModZero(this IPublishedContent content, int modulus)
{
return content.GetIndex() % modulus == 0;
}
public static HtmlString IsModZero(this IPublishedContent content, int modulus, string valueIfTrue)
{
return content.IsModZero(modulus, valueIfTrue, string.Empty);
}
public static HtmlString IsModZero(this IPublishedContent content, int modulus, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsModZero(modulus) ? valueIfTrue : valueIfFalse);
}
public static bool IsNotModZero(this IPublishedContent content, int modulus)
{
return content.IsModZero(modulus) == false;
}
public static HtmlString IsNotModZero(this IPublishedContent content, int modulus, string valueIfTrue)
{
return content.IsNotModZero(modulus, valueIfTrue, string.Empty);
}
public static HtmlString IsNotModZero(this IPublishedContent content, int modulus, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsNotModZero(modulus) ? valueIfTrue : valueIfFalse);
}
public static bool IsNotPosition(this IPublishedContent content, int index)
{
return content.IsPosition(index) == false;
}
public static HtmlString IsNotPosition(this IPublishedContent content, int index, string valueIfTrue)
{
return content.IsNotPosition(index, valueIfTrue, string.Empty);
}
public static HtmlString IsNotPosition(this IPublishedContent content, int index, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsNotPosition(index) ? valueIfTrue : valueIfFalse);
}
public static bool IsLast(this IPublishedContent content)
{
return content.GetIndex() == content.ContentSet.Count() - 1;
}
public static HtmlString IsLast(this IPublishedContent content, string valueIfTrue)
{
return content.IsLast(valueIfTrue, string.Empty);
}
public static HtmlString IsLast(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsLast() ? valueIfTrue : valueIfFalse);
}
public static bool IsNotLast(this IPublishedContent content)
{
return content.IsLast() == false;
}
public static HtmlString IsNotLast(this IPublishedContent content, string valueIfTrue)
{
return content.IsNotLast(valueIfTrue, string.Empty);
}
public static HtmlString IsNotLast(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsNotLast() ? valueIfTrue : valueIfFalse);
}
public static bool IsEven(this IPublishedContent content)
{
return content.GetIndex() % 2 == 0;
}
public static HtmlString IsEven(this IPublishedContent content, string valueIfTrue)
{
return content.IsEven(valueIfTrue, string.Empty);
}
public static HtmlString IsEven(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsEven() ? valueIfTrue : valueIfFalse);
}
public static bool IsOdd(this IPublishedContent content)
{
return content.GetIndex() % 2 == 1;
}
public static HtmlString IsOdd(this IPublishedContent content, string valueIfTrue)
{
return content.IsOdd(valueIfTrue, string.Empty);
}
public static HtmlString IsOdd(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsOdd() ? valueIfTrue : valueIfFalse);
}
#endregion
#region IsSomething: equality
public static bool IsEqual(this IPublishedContent content, IPublishedContent other)
{
return content.Id == other.Id;
}
public static HtmlString IsEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue)
{
return content.IsEqual(other, valueIfTrue, string.Empty);
}
public static HtmlString IsEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsEqual(other) ? valueIfTrue : valueIfFalse);
}
public static bool IsNotEqual(this IPublishedContent content, IPublishedContent other)
{
return content.IsEqual(other) == false;
}
public static HtmlString IsNotEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue)
{
return content.IsNotEqual(other, valueIfTrue, string.Empty);
}
public static HtmlString IsNotEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsNotEqual(other) ? valueIfTrue : valueIfFalse);
}
#endregion
#region IsSomething: ancestors and descendants
public static bool IsDescendant(this IPublishedContent content, IPublishedContent other)
{
return content.Ancestors().Any(x => x.Id == other.Id);
}
public static HtmlString IsDescendant(this IPublishedContent content, IPublishedContent other, string valueIfTrue)
{
return content.IsDescendant(other, valueIfTrue, string.Empty);
}
public static HtmlString IsDescendant(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsDescendant(other) ? valueIfTrue : valueIfFalse);
}
public static bool IsDescendantOrSelf(this IPublishedContent content, IPublishedContent other)
{
return content.AncestorsOrSelf().Any(x => x.Id == other.Id);
}
public static HtmlString IsDescendantOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue)
{
return content.IsDescendantOrSelf(other, valueIfTrue, string.Empty);
}
public static HtmlString IsDescendantOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsDescendantOrSelf(other) ? valueIfTrue : valueIfFalse);
}
public static bool IsAncestor(this IPublishedContent content, IPublishedContent other)
{
// avoid using Descendants(), that's expensive
return other.Ancestors().Any(x => x.Id == content.Id);
}
public static HtmlString IsAncestor(this IPublishedContent content, IPublishedContent other, string valueIfTrue)
{
return content.IsAncestor(other, valueIfTrue, string.Empty);
}
public static HtmlString IsAncestor(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsAncestor(other) ? valueIfTrue : valueIfFalse);
}
public static bool IsAncestorOrSelf(this IPublishedContent content, IPublishedContent other)
{
// avoid using DescendantsOrSelf(), that's expensive
return other.AncestorsOrSelf().Any(x => x.Id == content.Id);
}
public static HtmlString IsAncestorOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue)
{
return content.IsAncestorOrSelf(other, valueIfTrue, string.Empty);
}
public static HtmlString IsAncestorOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse)
{
return new HtmlString(content.IsAncestorOrSelf(other) ? valueIfTrue : valueIfFalse);
}
#endregion
#region Axes: ancestors, ancestors-or-self
// as per XPath 1.0 specs §2.2,
// - the ancestor axis contains the ancestors of the context node; the ancestors of the context node consist
// of the parent of context node and the parent's parent and so on; thus, the ancestor axis will always
// include the root node, unless the context node is the root node.
// - the ancestor-or-self axis contains the context node and the ancestors of the context node; thus,
// the ancestor axis will always include the root node.
//
// as per XPath 2.0 specs §3.2.1.1,
// - the ancestor axis is defined as the transitive closure of the parent axis; it contains the ancestors
// of the context node (the parent, the parent of the parent, and so on) - The ancestor axis includes the
// root node of the tree in which the context node is found, unless the context node is the root node.
// - the ancestor-or-self axis contains the context node and the ancestors of the context node; thus,
// the ancestor-or-self axis will always include the root node.
//
// the ancestor and ancestor-or-self axis are reverse axes ie they contain the context node or nodes that
// are before the context node in document order.
//
// document order is defined by §2.4.1 as:
// - the root node is the first node.
// - every node occurs before all of its children and descendants.
// - the relative order of siblings is the order in which they occur in the children property of their parent node.
// - children and descendants occur before following siblings.
/// <summary>
/// Gets the ancestors of the content.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>The ancestors of the content, in down-top order.</returns>
/// <remarks>Does not consider the content itself.</remarks>
public static IEnumerable<IPublishedContent> Ancestors(this IPublishedContent content)
{
return content.AncestorsOrSelf(false, null);
}
/// <summary>
/// Gets the ancestors of the content, at a level lesser or equal to a specified level.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="maxLevel">The level.</param>
/// <returns>The ancestors of the content, at a level lesser or equal to the specified level, in down-top order.</returns>
/// <remarks>Does not consider the content itself. Only content that are "high enough" in the tree are returned.</remarks>
public static IEnumerable<IPublishedContent> Ancestors(this IPublishedContent content, int maxLevel)
{
return content.AncestorsOrSelf(false, n => n.Level <= maxLevel);
}
/// <summary>
/// Gets the ancestors of the content, of a specified content type.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="contentTypeAlias">The content type.</param>
/// <returns>The ancestors of the content, of the specified content type, in down-top order.</returns>
/// <remarks>Does not consider the content itself. Returns all ancestors, of the specified content type.</remarks>
public static IEnumerable<IPublishedContent> Ancestors(this IPublishedContent content, string contentTypeAlias)
{
return content.AncestorsOrSelf(false, n => n.DocumentTypeAlias == contentTypeAlias);
}
/// <summary>
/// Gets the ancestors of the content, of a specified content type.
/// </summary>
/// <typeparam name="T">The content type.</typeparam>
/// <param name="content">The content.</param>
/// <returns>The ancestors of the content, of the specified content type, in down-top order.</returns>
/// <remarks>Does not consider the content itself. Returns all ancestors, of the specified content type.</remarks>
public static IEnumerable<T> Ancestors<T>(this IPublishedContent content)
where T : class, IPublishedContent
{
return content.Ancestors().OfType<T>();
}
/// <summary>
/// Gets the ancestors of the content, at a level lesser or equal to a specified level, and of a specified content type.
/// </summary>
/// <typeparam name="T">The content type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="maxLevel">The level.</param>
/// <returns>The ancestors of the content, at a level lesser or equal to the specified level, and of the specified
/// content type, in down-top order.</returns>
/// <remarks>Does not consider the content itself. Only content that are "high enough" in the trees, and of the
/// specified content type, are returned.</remarks>
public static IEnumerable<T> Ancestors<T>(this IPublishedContent content, int maxLevel)
where T : class, IPublishedContent
{
return content.Ancestors(maxLevel).OfType<T>();
}