Skip to content

Commit 46a425e

Browse files
committed
Rename and duplicate for products and current url
1 parent 3e62ea5 commit 46a425e

File tree

1 file changed

+98
-47
lines changed

1 file changed

+98
-47
lines changed

admin/controller/editor/editor.php

+98-47
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use function Vvveb\__;
2727
use Vvveb\Controller\Base;
2828
use function Vvveb\getUrl;
29+
use function Vvveb\model;
2930
use function Vvveb\sanitizeFileName;
3031
use function Vvveb\slugify;
3132
use Vvveb\Sql\PostSQL;
@@ -106,7 +107,7 @@ private function loadEditorData() {
106107

107108
//menu list
108109
$menuSql = new \Vvveb\Sql\menuSQL();
109-
$results = $menuSql->getMenusList($this->global);
110+
$results = $menuSql->getAll($this->global);
110111

111112
$data += $results;
112113

@@ -167,7 +168,7 @@ function index() {
167168
$results = $this->posts->getAll($options);
168169
$posts = [];
169170

170-
foreach ($results['posts'] as $post) {
171+
foreach ($results['post'] as $post) {
171172
$slug = $post['slug'];
172173
$url = url('content/page/index',['slug' => $slug, 'post_id' => $post['post_id']]);
173174

@@ -187,28 +188,55 @@ function index() {
187188
}
188189

189190
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));
196217

197218
if ($url == '/') {
198-
$title = __('Homepage');
199-
$name = 'index';
219+
$key = $slug = 'index';
200220
}
201221

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,
204232
'file' => $file,
205233
'url' => $url . ($theme ? '?theme=' . $theme : ''),
206-
'title' => $title,
234+
'title' => $name,
207235
'folder' => '',
208-
'className' => 'page',
236+
'className' => $className,
209237
];
210238

211-
$view->pages = [$name => $current_page] + $view->pages;
239+
$view->pages = [$key => $current_page] + $view->pages;
212240
}
213241

214242
$admin_path = \Vvveb\adminPath();
@@ -224,6 +252,8 @@ function index() {
224252
$this->view->renameUrl = "$mediaControllerPath&action=rename";
225253

226254
//editor endpoints
255+
$this->view->namespaceUrl = $admin_path . 'index.php?module=editor';
256+
$this->view->editorUrl = $controllerPath;
227257
$this->view->saveUrl = "$controllerPath&action=save";
228258
$this->view->deleteFileUrl = "$controllerPath&action=delete";
229259
$this->view->renameFileUrl = "$controllerPath&action=rename";
@@ -368,38 +398,54 @@ function delete() {
368398

369399
function rename() {
370400
$post_id = $this->request->post['post_id'] ?? false;
401+
$product_id = $this->request->post['product_id'] ?? false;
371402
$file = sanitizeFileName($this->request->post['file']);
372403
$newfile = sanitizeFileName($this->request->post['newfile']);
373404
$duplicate = $this->request->post['duplicate'] ?? 'false';
374405
$themeFolder = $this->getThemeFolder();
406+
$name = $this->request->post['name'] ?? $newfile;
407+
$theme = $this->getTheme();
408+
$dir = dirname($file);
375409

376-
if (strpos($newfile, '.html') === false) {
377-
$newfile .= '.html';
410+
if ($newfile) {
411+
$slug = sanitizeFileName(str_ireplace('.html', '', (basename($newfile))));
412+
$newfile = $slug . '.html';
378413
}
379414

380415
$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
382417

383418
$message = ['success' => false, 'message' => __('Error!')];
384419

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+
386433
if ($newfile) {
387-
$type = 'page';
388434
$name = sanitizeFileName($this->request->post['name']);
389435
$slug = slugify($name);
390436

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]);
393439

394440
if ($duplicate === 'true') {
395-
$data = $this->posts->get(['post_id' => $post_id]);
441+
$data = $this->posts->get([$model . '_id' => $model_id]);
396442

397443
if ($data) {
398-
unset($data['post_id']);
444+
unset($data[$model . '_id']);
399445
$id = rand(1, 1000);
400446

401-
foreach ($data['post_content'] as &$content) {
402-
unset($content['post_id']);
447+
foreach ($data[$model . '_content'] as &$content) {
448+
unset($content[$model . '_id']);
403449

404450
if ($content['language_id'] == $this->global['language_id']) {
405451
$content['name'] = $name;
@@ -410,65 +456,68 @@ function rename() {
410456
}
411457
}
412458

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) {
415461
$taxonomy_item[] = $item['taxonomy_item_id'];
416462
}
417463
}
418464

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) {
421467
$site_id[] = $item['site_id'];
422468
}
423469
}
424470

425-
$startTemplateUrl = $data['template'] ?? "content/$type.html";
426-
$template = "content/$slug.html";
471+
$startTemplateUrl = $data['template'] ?? "$namespace/$type.html";
472+
$template = "$namespace/$slug.html";
427473

428474
if (! @copy($themeFolder . DS . $startTemplateUrl, $themeFolder . DS . $template)) {
429475
$template = $data['template'] ?? '';
430476
}
431477

432478
$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,
437483
] + $data,
438484
'site_id' => $site_id,
439485
]);
440486

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') . '!'];
443490
} else {
444491
$message = ['success' => false, 'message' => sprintf(__('Error duplicating %s!'), $type)];
445492
}
446493
}
447494
} else {
448495
$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'],
452499
];
453500
$result = $this->posts->editContent($data);
454501

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') . '!'];
457504
} else {
458505
$message = ['success' => false, 'message' => sprintf(__('Error renaming %s!'), $type)];
459506
}
460507
}
461508
}
462509
} else {
510+
$newfile = PUBLIC_PATH . "themes/$theme/" . ($dir ? $dir . '/' : '') . $newfile;
511+
463512
if ($duplicate === 'true') {
464513
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];
466515
} else {
467516
$message = ['success' => false, 'message' => __('Error copying file!')];
468517
}
469518
} else {
470519
if (rename($currentFile, $targetFile)) {
471-
$message = ['success' => true, 'newfile' => $newfile, 'message' => __('File renamed!')];
520+
$message = ['success' => true, 'newfile' => $newfile, 'name' => $name, 'message' => __('File renamed!')];
472521
} else {
473522
$message = ['success' => false, 'message' => __('Error renaming file!')];
474523
}
@@ -520,9 +569,10 @@ function save() {
520569
'site_id' => [$this->global['site_id']], ] + $this->global);
521570

522571
if ($result['post']) {
572+
$post_id = $result['post'];
523573
$success = true;
524574
$route = "content/{$type}/index";
525-
$url = \Vvveb\url($route, ['slug'=> $slug]);
575+
$url = \Vvveb\url($route, ['slug'=> $slug, 'post_id'=> $post_id]);
526576
}
527577

528578
break;
@@ -550,9 +600,10 @@ function save() {
550600
'site_id' => [$this->global['site_id']], ] + $this->global);
551601

552602
if ($result['product']) {
603+
$product_id = $result['product'];
553604
$success = true;
554605
$route = "product/{$type}/index";
555-
$url = \Vvveb\url($route, ['slug'=> $slug]);
606+
$url = \Vvveb\url($route, ['slug' => $slug, 'product_id'=> $product_id]);
556607
}
557608

558609
break;

0 commit comments

Comments
 (0)