2
2
3
3
namespace Statikbe \FilamentFlexibleContentBlockPages \Http \Controllers ;
4
4
5
- use Artesaos \SEOTools \Facades \SEOTools ;
6
5
use Artesaos \SEOTools \Facades \SEOMeta ;
7
- use Carbon \ Carbon ;
6
+ use Artesaos \ SEOTools \ Facades \ SEOTools ;
8
7
use Illuminate \Foundation \Validation \ValidatesRequests ;
9
8
use Illuminate \Http \Response ;
10
9
use Illuminate \Routing \Controller ;
11
10
use Illuminate \Support \Facades \Auth ;
12
11
use Illuminate \Support \Facades \Cache ;
13
- use Illuminate \Support \Facades \Storage ;
14
12
use Mcamara \LaravelLocalization \Facades \LaravelLocalization ;
15
13
use Spatie \MediaLibrary \Conversions \ConversionCollection ;
14
+ use Spatie \MediaLibrary \MediaCollections \Models \Media ;
16
15
use Statikbe \FilamentFlexibleContentBlockPages \Facades \FilamentFlexibleContentBlockPages ;
17
16
use Statikbe \FilamentFlexibleContentBlockPages \Models \Page ;
18
17
use Statikbe \FilamentFlexibleContentBlockPages \Models \Settings ;
@@ -22,10 +21,11 @@ class PageController extends Controller
22
21
use ValidatesRequests;
23
22
24
23
const CACHE_SEO_IMAGE_DIMENSIONS = 'seo_image_dimensions:%s ' ;
24
+
25
25
const CACHE_SEO_IMAGE_TTL = 60 * 60 * 8 ; // in seconds
26
- const TEMPLATE_PATH = 'filament-flexible-content-block-pages::pages. ' ;
27
26
28
- // TODO make an abstract model with the table name to use as class to resolve in the route param
27
+ const TEMPLATE_PATH = 'filament-flexible-content-block-pages::pages.index ' ;
28
+
29
29
public function index (Page $ page )
30
30
{
31
31
// check if page is published:
@@ -41,11 +41,41 @@ public function index(Page $page)
41
41
$ this ->setSEOLocalisationAndCanonicalUrl ();
42
42
$ this ->setSEOImage ($ page );
43
43
44
- return view (self ::TEMPLATE_PATH . ' index ' , [
44
+ return view (self ::TEMPLATE_PATH , [
45
45
'page ' => $ page ,
46
46
]);
47
47
}
48
48
49
+ public function homeIndex ()
50
+ {
51
+ $ page = Page::code (Page::HOME_PAGE )
52
+ ->firstOrFail ();
53
+
54
+ return $ this ->index ($ page );
55
+ }
56
+
57
+ public function childIndex (Page $ parent , Page $ page )
58
+ {
59
+ // check if the page is a child of the parent
60
+ if (! $ parent ->isParentOf ($ page )) {
61
+ abort (Response::HTTP_NOT_FOUND );
62
+ }
63
+
64
+ // render the page with the regular page index function of the controller, or invoke the correct controller here:
65
+ return $ this ->index ($ page );
66
+ }
67
+
68
+ public function grandchildIndex (Page $ grandparent , Page $ parent , Page $ page )
69
+ {
70
+ // check if the page is a child of the parent
71
+ if (! $ parent ->isParentOf ($ page ) || ! $ grandparent ->isParentOf ($ parent )) {
72
+ abort (Response::HTTP_NOT_FOUND );
73
+ }
74
+
75
+ // render the page with the regular page index function of the controller, or invoke the correct controller here:
76
+ return $ this ->index ($ page );
77
+ }
78
+
49
79
protected function getSEOTitlePostfix ()
50
80
{
51
81
return sprintf (' | %s ' , flexiblePagesSetting (Settings::SETTING_SITE_TITLE ));
@@ -98,37 +128,35 @@ protected function setBasicSEO(Page $page)
98
128
99
129
protected function setSEOImage (Page $ page )
100
130
{
101
- //TODO copy getSEOMedia in HasSEOAttributesTrait
102
131
$ seoMedia = $ page ->getFallbackImageMedia ($ page ->SEOImage ()->first (), $ page ->getSEOImageCollection ());
103
132
$ seoUrl = null ;
104
133
$ imageDimensions = null ;
105
134
106
- /*if ($seoMedia) {
135
+ // 1. try SEO image of the page
136
+ if ($ seoMedia ) {
107
137
$ seoUrl = $ seoMedia ->getUrl ($ page ->getSEOImageConversionName ());
108
138
$ imageDimensions = $ this ->getSEOImageDimensions ($ seoMedia , $ page ->getSEOImageConversionName ());
109
- } else if()
110
- $seoUrl = $seoMedia->getUrl($page->getSEOImageConversionName());
111
- $imageDimensions = $this->getSEOImageDimensions($seoMedia, $page->getSEOImageConversionName());
112
- $this->setSEODefaultImage($page);
139
+ } else {
140
+ // 2. try the hero image of the page
141
+ $ seoMedia = $ page ->getFallbackImageMedia ($ page ->heroImage ()->first (), $ page ->getHeroImageCollection ());
142
+ if ($ seoMedia ) {
143
+ $ seoUrl = $ seoMedia ->getUrl ($ page ->getSEOImageConversionName ());
144
+ $ imageDimensions = $ this ->getSEOImageDimensions ($ seoMedia , $ page ->getSEOImageConversionName ());
145
+ }
146
+
147
+ // 3. try the default SEO image in the settings
148
+ if (! $ seoMedia || ! $ seoUrl ) {
149
+ /** @var Settings $settings */
150
+ $ settings = FilamentFlexibleContentBlockPages::config ()->getSettingsModel ()::getSettings ();
151
+ $ seoMedia = $ settings ->getFallbackImageMedia ($ settings ->defaultSeoImage ()->first (), $ settings ::COLLECTION_DEFAULT_SEO );
152
+ $ seoUrl = $ seoMedia ->getUrl ($ settings ::CONVERSION_DEFAULT_SEO );
153
+ $ imageDimensions = $ this ->getSEOImageDimensions ($ seoMedia , $ settings ::CONVERSION_DEFAULT_SEO );
154
+ }
113
155
}
114
156
115
- if($seoUrl && $imageDimensions) {
157
+ if ($ seoUrl && $ imageDimensions ) {
116
158
SEOTools::opengraph ()->addImage ($ seoUrl , $ imageDimensions );
117
159
SEOTools::twitter ()->addValue ('image ' , $ seoUrl );
118
- }*/
119
- }
120
-
121
- protected function setSEODefaultImage (Page $ page )
122
- {
123
- $ defaultSeoImage = flexiblePagesSettingImageUrl (Settings::COLLECTION_DEFAULT_SEO , Settings::CONVERSION_DEFAULT_SEO );
124
- // $test = Storage::get($defaultSeoImage);
125
- if ($ defaultSeoImage && trim ($ defaultSeoImage ) != '' ) {
126
- $ seoParams = $ this ->getSEOImageDimensions ($ defaultSeoImage , true );
127
- $ this ->setSEOImage ($ defaultSeoImage , $ seoParams );
128
- } else {
129
- $ imageUrl = $ page ->getHeroImageUrl ($ page ->getSEOImageConversionName ());
130
- $ seoParams = $ this ->getSEOImageDimensions ($ imageUrl , true );
131
- $ this ->setSEOImage ($ imageUrl , $ seoParams );
132
160
}
133
161
}
134
162
@@ -143,7 +171,7 @@ protected function getSEOImageDimensions(Media $seoMedia, string $conversion)
143
171
144
172
return Cache::remember ($ cacheKey ,
145
173
self ::CACHE_SEO_IMAGE_TTL ,
146
- function () use ($ seoMedia , $ conversion ) {
174
+ function () use ($ seoMedia , $ conversion ) {
147
175
$ conversionCollection = ConversionCollection::createForMedia ($ seoMedia );
148
176
$ conversion = $ conversionCollection ->getByName ($ conversion );
149
177
0 commit comments