1515use SolutionForest \InspireCms \InspireCmsConfig ;
1616use SolutionForest \InspireCms \Models \Contracts \Content ;
1717use SolutionForest \InspireCms \Support \Helpers \TreeNodeActionHelper ;
18+ use SolutionForest \InspireCms \Support \TreeNode \Concerns \CanCacheRecords ;
1819use SolutionForest \InspireCms \Support \TreeNode \Livewire \ServerSideTreeComponent ;
1920
2021class BaseContentTreeNode extends ServerSideTreeComponent
2122{
23+ use CanCacheRecords {
24+ getModel as protected getBaseModel;
25+ getElquentQuery as protected getBaseElquentQuery;
26+ }
27+
2228 protected static bool $ showNodeActions = false ;
2329
24- public ? string $ activeLocale = null ;
30+ protected static bool $ enableSelection = true ;
2531
26- protected array $ nodeRecordsCache = []; // Cache model records by node ID
32+ protected static bool $ skipChildrenIfTableView = true ;
33+
34+ public ?string $ activeLocale = null ;
2735
2836 public array $ redirectUrlParameters = []; // Additional URL parameters for resource links
2937
@@ -38,21 +46,6 @@ public function mount()
3846 }
3947 }
4048
41- // public function getExtraAlpineAttributes()
42- // {
43- // $attributes = [];
44-
45- // if ($this->isContentPickerModal()) {
46- // if (isset($this->modalConfig['modelable']) && is_array($this->modalConfig['modelable'])) {
47-
48- // $attributes['x-modelable'] = array_key_first($this->modalConfig['modelable']);
49- // $attributes['x-model'] = Arr::first($this->modalConfig['modelable']);
50- // }
51- // }
52-
53- // return new \Illuminate\View\ComponentAttributeBag($attributes);
54- // }
55-
5649 protected function getRootNodes (): array
5750 {
5851 // Return array of root nodes
@@ -118,11 +111,20 @@ protected function transformRecordIntoNode($record)
118111 );
119112 }
120113
114+ $ hasChildren = $ record ->children_count > 0 ;
115+
116+ if (static ::$ skipChildrenIfTableView
117+ && ($ documentType = $ record ?->documentType)
118+ && $ documentType ->show_as_table === true
119+ ) {
120+ $ hasChildren = false ;
121+ }
122+
121123 $ node = [
122124 'id ' => $ record ->getKey (),
123125 'name ' => $ record ->title ,
124126 'icon ' => $ record ->documentType ?->icon,
125- 'has_children ' => $ record -> children_count > 0 ,
127+ 'has_children ' => $ hasChildren ,
126128 'parent_id ' => $ record ->getParentId (),
127129 'depth ' => $ depth ,
128130
@@ -169,7 +171,9 @@ protected function resolveTreeNodeAction(array $action, array $parentActions): ?
169171
170172 if ($ recordKey && $ resolvedAction && is_null ($ resolvedAction ->getRecord ())) {
171173
172- $ resolvedAction = $ resolvedAction ->record ($ this ->retrieveRecordById ($ recordKey ));
174+ $ record = $ this ->retrieveRecordById ($ recordKey );
175+
176+ $ resolvedAction ->getRootGroup ()?->record($ record ) ?? $ resolvedAction ->record ($ record );
173177
174178 }
175179
@@ -191,6 +195,7 @@ public function getNodeItemActionsHtml($id)
191195 ->all (),
192196 livewireActions: $ actions ,
193197 model: $ this ->getModel (),
198+ livewire: $ this ,
194199 resolveRecordUsing: function ($ arguments , $ key ) {
195200 if ($ key instanceof Model) {
196201 return $ key ;
@@ -215,7 +220,8 @@ public function getNodeItemActionsHtml($id)
215220 public function getNodeLabel ($ node ): array
216221 {
217222 $ currentLocale = $ this ->activeLocale ;
218- $ currentLocaleTitle = $ node ['translatable ' ]['title ' ][$ currentLocale ] ?? null ;
223+ $ fallbackLocale = InspireCms::getFallbackLanguage ()?->code ?? app ()->getLocale ();
224+ $ currentLocaleTitle = $ node ['translatable ' ]['title ' ][$ currentLocale ] ?? $ node ['translatable ' ]['title ' ][$ fallbackLocale ] ?? null ;
219225
220226 return [
221227 'title ' => $ currentLocaleTitle ?? $ node ['title ' ] ?? 'Untitled ' ,
@@ -241,52 +247,6 @@ public function getNodeUrl(array $node): ?string
241247 return $ node ['url ' ] ?? null ;
242248 }
243249
244- protected function cacheRecordAppend ($ record , $ key = null )
245- {
246- $ key = $ key ?? $ record ->getKey ();
247- if (! isset ($ this ->nodeRecordsCache [$ key ])) {
248- $ this ->nodeRecordsCache [$ key ] = $ record ;
249- }
250- }
251-
252- protected function retrieveRecordById ($ recordKey )
253- {
254- if (isset ($ this ->nodeRecordsCache [$ recordKey ])) {
255- return $ this ->nodeRecordsCache [$ recordKey ];
256- }
257-
258- $ record = $ this ->getElquentQuery ()->find ($ recordKey );
259-
260- $ this ->cacheRecordAppend ($ record );
261-
262- return $ record ;
263- }
264-
265- /**
266- * Clear all caches - useful when data changes
267- */
268- public function clearRecordCaches (): void
269- {
270- $ this ->nodeRecordsCache = [];
271- }
272-
273- /**
274- * Clear cache for specific nodes
275- */
276- public function clearNodeRecordCache (array $ nodeIds ): void
277- {
278- // Clear record cache for specific nodes
279- foreach ($ nodeIds as $ nodeId ) {
280- unset($ this ->nodeRecordsCache [$ nodeId ]);
281- }
282- }
283-
284- public function refreshTree (): void
285- {
286- $ this ->clearRecordCaches (); // Clear record caches when refreshing
287- parent ::refreshTree ();
288- }
289-
290250 /**
291251 * @return class-string<Model>
292252 */
0 commit comments