22
33namespace Statikbe \FilamentFlexibleContentBlockPages \Http \Controllers ;
44
5- use Artesaos \SEOTools \Facades \SEOTools ;
65use Artesaos \SEOTools \Facades \SEOMeta ;
7- use Carbon \ Carbon ;
6+ use Artesaos \ SEOTools \ Facades \ SEOTools ;
87use Illuminate \Foundation \Validation \ValidatesRequests ;
98use Illuminate \Http \Response ;
109use Illuminate \Routing \Controller ;
1110use Illuminate \Support \Facades \Auth ;
1211use Illuminate \Support \Facades \Cache ;
13- use Illuminate \Support \Facades \Storage ;
1412use Mcamara \LaravelLocalization \Facades \LaravelLocalization ;
1513use Spatie \MediaLibrary \Conversions \ConversionCollection ;
14+ use Spatie \MediaLibrary \MediaCollections \Models \Media ;
1615use Statikbe \FilamentFlexibleContentBlockPages \Facades \FilamentFlexibleContentBlockPages ;
1716use Statikbe \FilamentFlexibleContentBlockPages \Models \Page ;
1817use Statikbe \FilamentFlexibleContentBlockPages \Models \Settings ;
@@ -22,10 +21,11 @@ class PageController extends Controller
2221 use ValidatesRequests;
2322
2423 const CACHE_SEO_IMAGE_DIMENSIONS = 'seo_image_dimensions:%s ' ;
24+
2525 const CACHE_SEO_IMAGE_TTL = 60 * 60 * 8 ; // in seconds
26- const TEMPLATE_PATH = 'filament-flexible-content-block-pages::pages. ' ;
2726
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+
2929 public function index (Page $ page )
3030 {
3131 // check if page is published:
@@ -41,11 +41,41 @@ public function index(Page $page)
4141 $ this ->setSEOLocalisationAndCanonicalUrl ();
4242 $ this ->setSEOImage ($ page );
4343
44- return view (self ::TEMPLATE_PATH . ' index ' , [
44+ return view (self ::TEMPLATE_PATH , [
4545 'page ' => $ page ,
4646 ]);
4747 }
4848
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+
4979 protected function getSEOTitlePostfix ()
5080 {
5181 return sprintf (' | %s ' , flexiblePagesSetting (Settings::SETTING_SITE_TITLE ));
@@ -98,37 +128,35 @@ protected function setBasicSEO(Page $page)
98128
99129 protected function setSEOImage (Page $ page )
100130 {
101- //TODO copy getSEOMedia in HasSEOAttributesTrait
102131 $ seoMedia = $ page ->getFallbackImageMedia ($ page ->SEOImage ()->first (), $ page ->getSEOImageCollection ());
103132 $ seoUrl = null ;
104133 $ imageDimensions = null ;
105134
106- /*if ($seoMedia) {
135+ // 1. try SEO image of the page
136+ if ($ seoMedia ) {
107137 $ seoUrl = $ seoMedia ->getUrl ($ page ->getSEOImageConversionName ());
108138 $ 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+ }
113155 }
114156
115- if($seoUrl && $imageDimensions) {
157+ if ($ seoUrl && $ imageDimensions ) {
116158 SEOTools::opengraph ()->addImage ($ seoUrl , $ imageDimensions );
117159 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 );
132160 }
133161 }
134162
@@ -143,7 +171,7 @@ protected function getSEOImageDimensions(Media $seoMedia, string $conversion)
143171
144172 return Cache::remember ($ cacheKey ,
145173 self ::CACHE_SEO_IMAGE_TTL ,
146- function () use ($ seoMedia , $ conversion ) {
174+ function () use ($ seoMedia , $ conversion ) {
147175 $ conversionCollection = ConversionCollection::createForMedia ($ seoMedia );
148176 $ conversion = $ conversionCollection ->getByName ($ conversion );
149177
0 commit comments