26
26
use function Vvveb \__ ;
27
27
use Vvveb \Controller \Base ;
28
28
use function Vvveb \getUrl ;
29
+ use function Vvveb \model ;
29
30
use function Vvveb \sanitizeFileName ;
30
31
use function Vvveb \slugify ;
31
32
use Vvveb \Sql \PostSQL ;
@@ -106,7 +107,7 @@ private function loadEditorData() {
106
107
107
108
//menu list
108
109
$ menuSql = new \Vvveb \Sql \menuSQL ();
109
- $ results = $ menuSql ->getMenusList ($ this ->global );
110
+ $ results = $ menuSql ->getAll ($ this ->global );
110
111
111
112
$ data += $ results ;
112
113
@@ -167,7 +168,7 @@ function index() {
167
168
$ results = $ this ->posts ->getAll ($ options );
168
169
$ posts = [];
169
170
170
- foreach ($ results ['posts ' ] as $ post ) {
171
+ foreach ($ results ['post ' ] as $ post ) {
171
172
$ slug = $ post ['slug ' ];
172
173
$ url = url ('content/page/index ' ,['slug ' => $ slug , 'post_id ' => $ post ['post_id ' ]]);
173
174
@@ -187,28 +188,55 @@ function index() {
187
188
}
188
189
189
190
if (isset ($ this ->request ->get ['url ' ])) {
190
- $ name = $ url = $ this ->request ->get ['url ' ];
191
- $ template = $ this ->request ->get ['template ' ] ?? \Vvveb \getUrlTemplate ($ url ) ?? 'index.html ' ;
192
- $ folder = $ this ->request ->get ['folder ' ] ?? false ;
193
- $ filename = $ template ;
194
- $ file = $ template ;
195
- $ title = \Vvveb \humanReadable (str_replace ('.html ' , '' , $ url ));
191
+ $ url = $ this ->request ->get ['url ' ];
192
+ $ name = $ this ->request ->get ['name ' ] ?? '' ;
193
+ $ template = $ this ->request ->get ['template ' ] ?? '' ; //\Vvveb\getUrlTemplate($url) ?? 'index.html';
194
+ $ folder = $ this ->request ->get ['folder ' ] ?? false ;
195
+ $ route = \Vvveb \getUrlRoute ($ url );
196
+ $ file = $ template ;
197
+ $ className = 'url ' ;
198
+ $ current_page = [];
199
+
200
+ switch ($ route ['module ' ]) {
201
+ case 'product/product/index ' :
202
+ $ className = 'product ' ;
203
+ $ current_page ['product_id ' ] = $ route ['product_id ' ];
204
+
205
+ break ;
206
+
207
+ case 'content/post/index ' :
208
+ case 'content/page/index ' :
209
+ $ className = 'page ' ;
210
+ $ current_page ['post_id ' ] = $ route ['post_id ' ];
211
+
212
+ break ;
213
+ }
214
+
215
+ $ key = slugify ($ url );
216
+ $ slug = slugify (str_replace ('.html ' , '' , $ template ));
196
217
197
218
if ($ url == '/ ' ) {
198
- $ title = __ ('Homepage ' );
199
- $ name = 'index ' ;
219
+ $ key = $ slug = 'index ' ;
200
220
}
201
221
202
- $ current_page = [
203
- 'name ' => $ name ,
222
+ if (! $ name ) {
223
+ //if in page list get pretty name
224
+ if (isset ($ view ->pages [$ slug ])) {
225
+ $ name = $ view ->pages [$ slug ]['title ' ];
226
+ } else {
227
+ $ name = \Vvveb \humanReadable ($ url );
228
+ }
229
+ }
230
+ $ current_page += [
231
+ 'name ' => $ key ,
204
232
'file ' => $ file ,
205
233
'url ' => $ url . ($ theme ? '?theme= ' . $ theme : '' ),
206
- 'title ' => $ title ,
234
+ 'title ' => $ name ,
207
235
'folder ' => '' ,
208
- 'className ' => ' page ' ,
236
+ 'className ' => $ className ,
209
237
];
210
238
211
- $ view ->pages = [$ name => $ current_page ] + $ view ->pages ;
239
+ $ view ->pages = [$ key => $ current_page ] + $ view ->pages ;
212
240
}
213
241
214
242
$ admin_path = \Vvveb \adminPath ();
@@ -224,6 +252,8 @@ function index() {
224
252
$ this ->view ->renameUrl = "$ mediaControllerPath&action=rename " ;
225
253
226
254
//editor endpoints
255
+ $ this ->view ->namespaceUrl = $ admin_path . 'index.php?module=editor ' ;
256
+ $ this ->view ->editorUrl = $ controllerPath ;
227
257
$ this ->view ->saveUrl = "$ controllerPath&action=save " ;
228
258
$ this ->view ->deleteFileUrl = "$ controllerPath&action=delete " ;
229
259
$ this ->view ->renameFileUrl = "$ controllerPath&action=rename " ;
@@ -368,38 +398,54 @@ function delete() {
368
398
369
399
function rename () {
370
400
$ post_id = $ this ->request ->post ['post_id ' ] ?? false ;
401
+ $ product_id = $ this ->request ->post ['product_id ' ] ?? false ;
371
402
$ file = sanitizeFileName ($ this ->request ->post ['file ' ]);
372
403
$ newfile = sanitizeFileName ($ this ->request ->post ['newfile ' ]);
373
404
$ duplicate = $ this ->request ->post ['duplicate ' ] ?? 'false ' ;
374
405
$ themeFolder = $ this ->getThemeFolder ();
406
+ $ name = $ this ->request ->post ['name ' ] ?? $ newfile ;
407
+ $ theme = $ this ->getTheme ();
408
+ $ dir = dirname ($ file );
375
409
376
- if (strpos ($ newfile , '.html ' ) === false ) {
377
- $ newfile .= '.html ' ;
410
+ if ($ newfile ) {
411
+ $ slug = sanitizeFileName (str_ireplace ('.html ' , '' , (basename ($ newfile ))));
412
+ $ newfile = $ slug . '.html ' ;
378
413
}
379
414
380
415
$ currentFile = $ themeFolder . DS . $ file ;
381
- $ targetFile = dirname ($ currentFile ) . DS . slugify ( basename ( $ newfile)) ; //save in same folder
416
+ $ targetFile = dirname ($ currentFile ) . DS . $ newfile ; //save in same folder
382
417
383
418
$ message = ['success ' => false , 'message ' => __ ('Error! ' )];
384
419
385
- if ($ post_id ) {
420
+ if ($ post_id || $ product_id ) {
421
+ $ model = 'post ' ;
422
+ $ type = 'page ' ;
423
+ $ namespace = 'content ' ;
424
+ $ model_id = $ post_id ;
425
+
426
+ if ($ product_id ) {
427
+ $ model_id = $ product_id ;
428
+ $ namespace = 'product ' ;
429
+ $ model = 'product ' ;
430
+ $ type = 'product ' ;
431
+ }
432
+
386
433
if ($ newfile ) {
387
- $ type = 'page ' ;
388
434
$ name = sanitizeFileName ($ this ->request ->post ['name ' ]);
389
435
$ slug = slugify ($ name );
390
436
391
- $ this ->posts = new PostSQL ( );
392
- $ data = $ this ->posts ->get ([' post_id ' => $ post_id ]);
437
+ $ this ->posts = model ( $ model );
438
+ $ data = $ this ->posts ->get ([$ model . ' _id ' => $ model_id ]);
393
439
394
440
if ($ duplicate === 'true ' ) {
395
- $ data = $ this ->posts ->get ([' post_id ' => $ post_id ]);
441
+ $ data = $ this ->posts ->get ([$ model . ' _id ' => $ model_id ]);
396
442
397
443
if ($ data ) {
398
- unset($ data [' post_id ' ]);
444
+ unset($ data [$ model . ' _id ' ]);
399
445
$ id = rand (1 , 1000 );
400
446
401
- foreach ($ data [' post_content ' ] as &$ content ) {
402
- unset($ content [' post_id ' ]);
447
+ foreach ($ data [$ model . ' _content ' ] as &$ content ) {
448
+ unset($ content [$ model . ' _id ' ]);
403
449
404
450
if ($ content ['language_id ' ] == $ this ->global ['language_id ' ]) {
405
451
$ content ['name ' ] = $ name ;
@@ -410,65 +456,68 @@ function rename() {
410
456
}
411
457
}
412
458
413
- if (isset ($ data [' post_to_taxonomy_item ' ])) {
414
- foreach ($ data [' post_to_taxonomy_item ' ] as &$ item ) {
459
+ if (isset ($ data [$ model . ' _to_taxonomy_item ' ])) {
460
+ foreach ($ data [$ model . ' _to_taxonomy_item ' ] as &$ item ) {
415
461
$ taxonomy_item [] = $ item ['taxonomy_item_id ' ];
416
462
}
417
463
}
418
464
419
- if (isset ($ data [' post_to_site ' ])) {
420
- foreach ($ data [' post_to_site ' ] as &$ item ) {
465
+ if (isset ($ data [$ model . ' _to_site ' ])) {
466
+ foreach ($ data [$ model . ' _to_site ' ] as &$ item ) {
421
467
$ site_id [] = $ item ['site_id ' ];
422
468
}
423
469
}
424
470
425
- $ startTemplateUrl = $ data ['template ' ] ?? "content /$ type.html " ;
426
- $ template = "content /$ slug.html " ;
471
+ $ startTemplateUrl = $ data ['template ' ] ?? "$ namespace / $ type.html " ;
472
+ $ template = "$ namespace / $ slug.html " ;
427
473
428
474
if (! @copy ($ themeFolder . DS . $ startTemplateUrl , $ themeFolder . DS . $ template )) {
429
475
$ template = $ data ['template ' ] ?? '' ;
430
476
}
431
477
432
478
$ result = $ this ->posts ->add ([
433
- ' post ' => [
434
- ' post_content ' => $ data [' post_content ' ],
435
- 'taxonomy_item ' => $ taxonomy_item ?? [],
436
- 'template ' => $ template ,
479
+ $ model => [
480
+ $ model . ' _content ' => $ data [$ model . ' _content ' ],
481
+ 'taxonomy_item ' => $ taxonomy_item ?? [],
482
+ 'template ' => $ template ,
437
483
] + $ data ,
438
484
'site_id ' => $ site_id ,
439
485
]);
440
486
441
- if ($ result && isset ($ result ['post ' ])) {
442
- $ message = ['success ' => true , 'url ' => url ('content/page/index ' , ['slug ' => $ slug , 'post_id ' => $ post_id ]), 'message ' => ucfirst ($ type ) . ' ' . __ ('duplicated ' ) . '! ' ];
487
+ if ($ result && isset ($ result [$ model ])) {
488
+ $ model_id = $ result [$ model ];
489
+ $ message = ['success ' => true , 'name ' => $ name , 'slug ' => $ slug , $ model . '_id ' => $ model_id , 'url ' => url ("$ namespace/ $ type/index " , ['slug ' => $ slug , $ model . '_id ' => $ model_id ]), 'message ' => ucfirst ($ type ) . ' ' . __ ('duplicated ' ) . '! ' ];
443
490
} else {
444
491
$ message = ['success ' => false , 'message ' => sprintf (__ ('Error duplicating %s! ' ), $ type )];
445
492
}
446
493
}
447
494
} else {
448
495
$ data = [
449
- ' post_content ' => ['name ' => $ name , 'slug ' => $ slug ],
450
- ' post_id ' => $ post_id ,
451
- 'language_id ' => $ this ->global ['language_id ' ],
496
+ $ model . ' _content ' => ['name ' => $ name , 'slug ' => $ slug ],
497
+ $ model . ' _id ' => $ model_id ,
498
+ 'language_id ' => $ this ->global ['language_id ' ],
452
499
];
453
500
$ result = $ this ->posts ->editContent ($ data );
454
501
455
- if ($ result && isset ($ result [' post_content ' ])) {
456
- $ message = ['success ' => true , 'url ' => url (' content/page /index' , ['slug ' => $ slug , ' post_id ' => $ post_id ]), 'message ' => ucfirst ($ type ) . ' ' . __ ('renamed ' ) . '! ' ];
502
+ if ($ result && isset ($ result [$ model . ' _content ' ])) {
503
+ $ message = ['success ' => true , 'name ' => $ name , ' slug ' => $ slug , $ model . ' _id ' => $ model_id , ' url ' => url (" $ namespace / $ type /index " , ['slug ' => $ slug , $ model . ' _id ' => $ model_id ]), 'message ' => ucfirst ($ type ) . ' ' . __ ('renamed ' ) . '! ' ];
457
504
} else {
458
505
$ message = ['success ' => false , 'message ' => sprintf (__ ('Error renaming %s! ' ), $ type )];
459
506
}
460
507
}
461
508
}
462
509
} else {
510
+ $ newfile = PUBLIC_PATH . "themes/ $ theme/ " . ($ dir ? $ dir . '/ ' : '' ) . $ newfile ;
511
+
463
512
if ($ duplicate === 'true ' ) {
464
513
if (@copy ($ currentFile , $ targetFile )) {
465
- $ message = ['success ' => true , 'newfile ' => $ newfile , 'message ' => __ ('File copied! ' ), 'url ' => $ newfile ];
514
+ $ message = ['success ' => true , 'newfile ' => $ newfile , ' name ' => $ name , 'message ' => __ ('File copied! ' ), 'url ' => $ newfile ];
466
515
} else {
467
516
$ message = ['success ' => false , 'message ' => __ ('Error copying file! ' )];
468
517
}
469
518
} else {
470
519
if (rename ($ currentFile , $ targetFile )) {
471
- $ message = ['success ' => true , 'newfile ' => $ newfile , 'message ' => __ ('File renamed! ' )];
520
+ $ message = ['success ' => true , 'newfile ' => $ newfile , 'name ' => $ name , ' message ' => __ ('File renamed! ' )];
472
521
} else {
473
522
$ message = ['success ' => false , 'message ' => __ ('Error renaming file! ' )];
474
523
}
@@ -520,9 +569,10 @@ function save() {
520
569
'site_id ' => [$ this ->global ['site_id ' ]], ] + $ this ->global );
521
570
522
571
if ($ result ['post ' ]) {
572
+ $ post_id = $ result ['post ' ];
523
573
$ success = true ;
524
574
$ route = "content/ {$ type }/index " ;
525
- $ url = \Vvveb \url ($ route , ['slug ' => $ slug ]);
575
+ $ url = \Vvveb \url ($ route , ['slug ' => $ slug, ' post_id ' => $ post_id ]);
526
576
}
527
577
528
578
break ;
@@ -550,9 +600,10 @@ function save() {
550
600
'site_id ' => [$ this ->global ['site_id ' ]], ] + $ this ->global );
551
601
552
602
if ($ result ['product ' ]) {
603
+ $ product_id = $ result ['product ' ];
553
604
$ success = true ;
554
605
$ route = "product/ {$ type }/index " ;
555
- $ url = \Vvveb \url ($ route , ['slug ' => $ slug ]);
606
+ $ url = \Vvveb \url ($ route , ['slug ' => $ slug, ' product_id ' => $ product_id ]);
556
607
}
557
608
558
609
break ;
0 commit comments