22
33use EvolutionCMS \Models ;
44use EvolutionCMS \Interfaces \ManagerTheme ;
5+ use EvolutionCMS \Support \TemplateFileEngines ;
6+ use EvolutionCMS \TemplateProcessor ;
57use Illuminate \Support \Collection ;
68use Illuminate \Database \Eloquent ;
79
810class Template extends AbstractController implements ManagerTheme \PageControllerInterface
911{
1012 protected $ view = 'page.template ' ;
1113
14+ /**
15+ * A template file larger than this is not handed to the editor for
16+ * switching. Templates are not this big; something else is going on, and a
17+ * megabyte of it does not belong inline in the form.
18+ */
19+ private const MAX_EDITABLE_FILE_BYTES = 524288 ;
20+
1221 protected $ events = [
1322 'OnTempFormPrerender ' ,
1423 'OnTempFormRender '
@@ -51,6 +60,48 @@ public function canView(): bool
5160 public function process () : bool
5261 {
5362 $ this ->object = $ this ->parameterData ();
63+ $ engines = TemplateFileEngines::make ();
64+ $ existingFiles = $ engines ->existing ((string ) $ this ->object ->templatealias );
65+ $ winningExtension = $ engines ->winner (
66+ (string ) $ this ->object ->templatealias ,
67+ (string ) $ this ->object ->templatefileextension
68+ );
69+
70+ // A template that reads from a file shows the file in the editor, not
71+ // the database column - editing what is not rendered would be a trap.
72+ // The database copy stays where it is as the fallback it already is.
73+ // "Automatic" only describes templates that predate the setting. A
74+ // template being created now has no old behaviour to preserve, so it
75+ // starts where its code is being typed.
76+ $ templateSource = (string ) $ this ->object ->templatesource ;
77+ if ($ templateSource === '' && !$ this ->object ->getKey ()) {
78+ $ templateSource = TemplateProcessor::SOURCE_DATABASE ;
79+ }
80+
81+ // The editor has to be able to show whatever the selectors point at, the
82+ // moment they are moved - otherwise it keeps displaying code that the
83+ // save is not going to write, which is the one thing an editor must
84+ // never do. The database column and every file already on disk are
85+ // handed to the page so the swap needs no round trip.
86+ $ databaseContent = (string ) $ this ->object ->content ;
87+ $ fileContents = [];
88+ foreach ($ existingFiles as $ extension => $ path ) {
89+ if (is_readable ($ path ) && filesize ($ path ) <= self ::MAX_EDITABLE_FILE_BYTES ) {
90+ $ fileContents [$ extension ] = (string ) file_get_contents ($ path );
91+ }
92+ }
93+
94+ $ sourceFile = null ;
95+ if ($ templateSource === TemplateProcessor::SOURCE_FILE ) {
96+ $ sourceFile = $ engines ->pathFor (
97+ (string ) $ this ->object ->templatealias ,
98+ (string ) $ this ->object ->templatefileextension
99+ );
100+ $ pinned = ltrim ((string ) $ this ->object ->templatefileextension , '. ' );
101+ if ($ sourceFile !== null && isset ($ fileContents [$ pinned ])) {
102+ $ this ->object ->content = $ fileContents [$ pinned ];
103+ }
104+ }
54105 $ this ->parameters = [
55106 'data ' => $ this ->object ,
56107 'categories ' => $ this ->parameterCategories (),
@@ -69,7 +120,24 @@ function (Models\SiteTmplvar $item) {
69120 ),
70121 'action ' => $ this ->getIndex (),
71122 'events ' => $ this ->parameterEvents (),
72- 'actionButtons ' => $ this ->parameterActionButtons ()
123+ 'actionButtons ' => $ this ->parameterActionButtons (),
124+ 'templateFileEngines ' => $ engines ->all (),
125+ // A new template follows [(chunk_processor)]; one that already
126+ // recorded an engine keeps showing it, so that opening a template
127+ // and saving it cannot quietly re-point it at another engine.
128+ 'templateFileDefault ' => $ engines ->isRegistered ($ this ->object ->templatefileextension )
129+ ? ltrim ((string ) $ this ->object ->templatefileextension , '. ' )
130+ : $ engines ->defaultExtension (
131+ (string ) $ this ->managerTheme ->getCore ()->getConfig ('chunk_processor ' )
132+ ),
133+ // Only one file can render an alias; the rest are shadowed, so the
134+ // form says which ones are already there and which of them wins.
135+ 'templateFileExisting ' => $ existingFiles ,
136+ 'templateFileWinner ' => $ winningExtension ,
137+ 'templateSource ' => $ templateSource ,
138+ 'templateSourceFile ' => $ sourceFile ,
139+ 'templateDbContent ' => $ databaseContent ,
140+ 'templateFileContents ' => $ fileContents
73141 ];
74142
75143 return true ;
0 commit comments