1010use Illuminate \Database \Eloquent \Relations \HasMany ;
1111use Illuminate \Support \Arr ;
1212use Illuminate \Support \Carbon ;
13- use Illuminate \Support \Facades \Auth ;
14- use Illuminate \Support \Facades \Route ;
1513use Illuminate \Support \Str ;
14+ use Novius \LaravelLinkable \Configs \LinkableConfig ;
15+ use Novius \LaravelLinkable \Traits \Linkable ;
1616use Novius \LaravelMeta \Enums \IndexFollow ;
1717use Novius \LaravelMeta \MetaModelConfig ;
1818use Novius \LaravelMeta \Traits \HasMeta ;
1919use Novius \LaravelPublishable \Enums \PublicationStatus ;
2020use Novius \LaravelPublishable \Traits \Publishable ;
2121use Novius \LaravelTranslatable \Traits \Translatable ;
22- use Psr \Container \ContainerExceptionInterface ;
23- use Psr \Container \NotFoundExceptionInterface ;
2422use RuntimeException ;
2523use Spatie \Sluggable \HasSlug ;
2624use Spatie \Sluggable \SlugOptions ;
@@ -69,6 +67,7 @@ class Page extends Model
6967{
7068 use HasMeta;
7169 use HasSlug;
70+ use Linkable;
7271 use Publishable;
7372 use Translatable;
7473
@@ -111,95 +110,11 @@ public function children(): HasMany
111110 return $ this ->hasMany (static ::class, 'parent_id ' , 'id ' );
112111 }
113112
114- public function url (): ?string
115- {
116- $ routeName = config ('laravel-nova-page-manager.front_route_name ' );
117- $ parameter = $ this ->getUrlParameter ();
118-
119- if ($ routeName === null || ! $ this ->exists || ! $ parameter ) {
120- return null ;
121- }
122-
123- return route ($ routeName , [
124- $ parameter => $ this ->slug ,
125- ]);
126- }
127-
128- public function previewUrl (): ?string
129- {
130- $ routeName = config ('laravel-nova-page-manager.front_route_name ' );
131- $ parameter = $ this ->getUrlParameter ();
132-
133- if ($ routeName === null || ! $ this ->exists || ! $ parameter ) {
134- return null ;
135- }
136-
137- $ params = [
138- $ parameter => $ this ->slug ,
139- ];
140-
141- $ guard = config ('laravel-nova-page-manager.guard_preview ' );
142- if (empty ($ guard ) && ! $ this ->isPublished ()) {
143- $ params ['previewToken ' ] = $ this ->preview_token ;
144- }
145-
146- return route ($ routeName , $ params );
147- }
148-
149- protected function getUrlParameter (): ?string
150- {
151- $ parameter = config ('laravel-nova-page-manager.front_route_parameter ' );
152-
153- if (! empty ($ parameter )) {
154- return $ parameter ;
155- }
156-
157- $ routeName = config ('laravel-nova-page-manager.front_route_name ' );
158- if (empty ($ routeName )) {
159- return null ;
160- }
161-
162- $ route = Route::getRoutes ()->getByName ($ routeName );
163- if (! $ route ) {
164- return null ;
165- }
166-
167- if (! preg_match ('/({\w+})/ ' , $ route ->uri (), $ matches )) {
168- return null ;
169- }
170-
171- return substr ($ matches [0 ], 1 , -1 );
172- }
173-
174113 public function getRouteKeyName (): string
175114 {
176115 return 'slug ' ;
177116 }
178117
179- public function resolveRouteBinding ($ value , $ field = null )
180- {
181- $ guard = config ('laravel-nova-page-manager.guard_preview ' );
182- $ query = static ::where ('locale ' , app ()->currentLocale ());
183-
184- if (! empty ($ guard ) && Auth::guard ($ guard )->check ()) {
185- return $ this ->resolveRouteBindingQuery ($ query , $ value , $ field )->first ();
186- }
187-
188- if (request ()->has ('previewToken ' )) {
189- $ query ->where (/**
190- * @throws ContainerExceptionInterface
191- * @throws NotFoundExceptionInterface
192- */ function (Builder $ query ) {
193- $ query ->published ()
194- ->orWhere ('preview_token ' , request ()->get ('previewToken ' ));
195- });
196-
197- return $ this ->resolveRouteBindingQuery ($ query , $ value , $ field )->first ();
198- }
199-
200- return $ this ->resolveRouteBindingQuery ($ query ->published (), $ value , $ field )->first ();
201- }
202-
203118 public function getSlugOptions (): SlugOptions
204119 {
205120 return SlugOptions::create ()
@@ -221,6 +136,29 @@ public function getMetaConfig(): MetaModelConfig
221136 return $ this ->metaConfig ;
222137 }
223138
139+ protected LinkableConfig $ _linkableConfig ;
140+
141+ public function linkableConfig (): LinkableConfig
142+ {
143+ if (! isset ($ this ->_linkableConfig )) {
144+ $ this ->_linkableConfig = new LinkableConfig (
145+ routeName: config ('laravel-nova-page-manager.front_route_name ' ),
146+ routeParameterName: config ('laravel-nova-page-manager.front_route_parameter ' ),
147+ optionLabel: 'title ' ,
148+ optionGroup: trans ('laravel-nova-page-manager::page.linkableGroup ' ),
149+ resolveQuery: function (Builder |Page $ query ) {
150+ $ query ->where ('locale ' , app ()->currentLocale ());
151+ },
152+ resolveNotPreviewQuery: function (Builder |Page $ query ) {
153+ $ query ->published ();
154+ },
155+ previewTokenField: 'preview_token '
156+ );
157+ }
158+
159+ return $ this ->_linkableConfig ;
160+ }
161+
224162 protected function seoCanonicalUrl (): Attribute
225163 {
226164 return Attribute::make (
0 commit comments