Skip to content

Commit f6fc936

Browse files
committed
Added duplicate option for posts/pages and products
1 parent c7c39be commit f6fc936

23 files changed

+413
-166
lines changed

admin/controller/content/posts.php

+85-31
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Vvveb\System\Cache;
3030
use Vvveb\System\Images;
3131
use Vvveb\System\User\Admin;
32+
use function Vvveb\url;
3233

3334
class Posts extends Base {
3435
protected $type = 'post';
@@ -44,6 +45,59 @@ function init() {
4445
return parent::init();
4546
}
4647

48+
function duplicate() {
49+
$post_id = $this->request->post['post_id'] ?? $this->request->get['post_id'] ?? false;
50+
51+
if ($post_id) {
52+
$this->posts = new PostSQL();
53+
$data = $this->posts->get(['post_id' => $post_id, 'type' => $this->type]);
54+
55+
unset($data['post_id']);
56+
$id = rand(1, 1000);
57+
58+
foreach ($data['post_content'] as &$content) {
59+
unset($content['post_id']);
60+
$content['name'] .= ' [' . __('duplicate') . ']';
61+
$content['slug'] .= '-' . __('duplicate') . "-$id";
62+
}
63+
64+
if (isset($data['post_to_taxonomy_item'])) {
65+
foreach ($data['post_to_taxonomy_item'] as &$item) {
66+
$taxonomy_item[] = $item['taxonomy_item_id'];
67+
}
68+
}
69+
70+
if (isset($data['post_to_site'])) {
71+
foreach ($data['post_to_site'] as &$item) {
72+
$site_id[] = $item['site_id'];
73+
}
74+
}
75+
76+
if ($data) {
77+
$result = $this->posts->add([
78+
'post' => [
79+
'post_content' => $data['post_content'],
80+
'taxonomy_item' => $taxonomy_item ?? [],
81+
] + $data,
82+
'site_id' => $site_id,
83+
]);
84+
85+
if ($result && isset($result['post'])) {
86+
$post_id = $result['post'];
87+
$url = url(['module' => 'content/post', 'post_id' => $post_id, 'type' => $this->type]);
88+
89+
$success = ucfirst($this->type) . __(' duplicated!');
90+
$success .= sprintf(' <a href="%s">%s</a>', $url, __('Edit') . " {$this->type}");
91+
$this->view->success[] = $success;
92+
} else {
93+
$this->view->errors[] = sprintf(__('Error duplicating %s!'), $this->type);
94+
}
95+
}
96+
}
97+
98+
return $this->index();
99+
}
100+
47101
function delete() {
48102
$post_id = $this->request->post['post_id'] ?? $this->request->get['post_id'] ?? false;
49103

@@ -108,31 +162,31 @@ function archives($options) {
108162

109163
$return[$archive['year'] . '/' . $archive['month']] = $name;
110164
}
111-
/*
112-
$return = [];
165+
/*
166+
$return = [];
113167
114-
if (isset($archives['archives'])) {
115-
foreach ($archives['archives'] as $index => &$archive) {
116-
if (isset($archive['month'])) {
117-
$monthNum = $archive['month'];
168+
if (isset($archives['archives'])) {
169+
foreach ($archives['archives'] as $index => &$archive) {
170+
if (isset($archive['month'])) {
171+
$monthNum = $archive['month'];
118172
119-
$archive['month_text'] = $monthNum;
173+
$archive['month_text'] = $monthNum;
120174
121-
if ($df) {
122-
$dt->setDate(0, $archive['month'], 0);
123-
$archive['month_text'] = ucfirst(datefmt_format($df, $dt));
124-
}
125-
}
175+
if ($df) {
176+
$dt->setDate(0, $archive['month'], 0);
177+
$archive['month_text'] = ucfirst(datefmt_format($df, $dt));
178+
}
179+
}
126180
127-
$archive['month'] = sprintf('%02d', $archive['month']);
128-
$name =
129-
(isset($archive['month']) ? $archive['month_text'] . ' ' : '') .
130-
(isset($archive['year']) ? $archive['year'] . ' ' : '');
181+
$archive['month'] = sprintf('%02d', $archive['month']);
182+
$name =
183+
(isset($archive['month']) ? $archive['month_text'] . ' ' : '') .
184+
(isset($archive['year']) ? $archive['year'] . ' ' : '');
131185
132-
$return[$archive['year'] . '/' . $archive['month']] = $name;
133-
}
134-
}
135-
*/
186+
$return[$archive['year'] . '/' . $archive['month']] = $name;
187+
}
188+
}
189+
*/
136190
return $return;
137191
}
138192

@@ -177,16 +231,16 @@ function index() {
177231
$post['image'] = Images::image($post['image'], 'post');
178232
}
179233

180-
$url = ['module' => 'content/post', 'post_id' => $post['post_id'], 'type' => $post['type']];
181-
$admin_path = \Vvveb\adminPath();
182-
$template = $post['template'] ? $post['template'] : $defaultTemplate;
183-
$post['url'] = \Vvveb\url($url);
184-
$post['edit-url'] = $post['url'];
185-
186-
$post['admin-url'] = \Vvveb\url(['module' => 'content/posts']) . '&filter[admin_id_text]=' . $post['username'] . ' &filter[admin_id]=' . $post['admin_id'];
187-
$post['delete-url'] = \Vvveb\url(['module' => 'content/posts', 'action' => 'delete'] + $url + ['post_id[]' => $post['post_id']]);
188-
$post['view-url'] = \Vvveb\url("content/{$this->type}/index", $post + $url + ['host' => $this->global['site_url']]);
189-
$post['design-url'] = \Vvveb\url(['module' => 'editor/editor', 'url' => $post['view-url'], 'template' => $template, 'host' => $this->global['site_url'] . $admin_path], false, false);
234+
$url = ['module' => 'content/post', 'post_id' => $post['post_id'], 'type' => $post['type']];
235+
$admin_path = \Vvveb\adminPath();
236+
$template = $post['template'] ? $post['template'] : $defaultTemplate;
237+
$post['url'] = url($url);
238+
$post['edit-url'] = $post['url'];
239+
$post['admin-url'] = url(['module' => 'content/posts']) . '&filter[admin_id_text]=' . $post['username'] . ' &filter[admin_id]=' . $post['admin_id'];
240+
$post['delete-url'] = url(['module' => 'content/posts', 'action' => 'delete'] + $url + ['post_id[]' => $post['post_id']]);
241+
$post['duplicate-url'] = url(['module' => 'content/posts', 'action' => 'duplicate'] + $url + ['post_id' => $post['post_id']]);
242+
$post['view-url'] = url("content/{$this->type}/index", $post + $url + ['host' => $this->global['site_url']]);
243+
$post['design-url'] = url(['module' => 'editor/editor', 'url' => $post['view-url'], 'template' => $template, 'host' => $this->global['site_url'] . $admin_path], false, false);
190244
}
191245
}
192246

@@ -203,7 +257,7 @@ function () use (&$options) {
203257
$view->filter = $this->filter;
204258
$view->limit = $options['limit'];
205259
$view->type = $this->type;
206-
$view->addUrl = \Vvveb\url(['module' => 'content/post', 'type' => $this->type]);
260+
$view->addUrl = url(['module' => 'content/post', 'type' => $this->type]);
207261
$view->type_name = humanReadable(__($this->type));
208262
$view->type_name_plural = humanReadable(__($view->type . 's'));
209263
}

admin/controller/editor/editor.php

+75-23
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private function loadThemeConfig() {
8787
private function loadTemplateList($theme = null) {
8888
$list = $this->themeConfig['pages'] ?? [];
8989

90-
$pages = $list + Cache::getInstance()->cache(APP,'template-list.' . $theme,
90+
$pages = $list + Cache::getInstance()->cache(APP, 'template-list.' . $theme,
9191
function () use ($theme) {
9292
return \Vvveb\getTemplateList($theme);
9393
}, 604800);
@@ -97,6 +97,10 @@ function () use ($theme) {
9797
return $pages;
9898
}
9999

100+
private function clearTemplateListCache($theme = null) {
101+
return Cache::getInstance()->delete(APP, 'template-list.' . $theme);
102+
}
103+
100104
private function loadEditorData() {
101105
$data = [];
102106

@@ -328,7 +332,6 @@ function delete() {
328332
$post_id = $this->request->post['post_id'] ?? false;
329333
$file = sanitizeFileName($this->request->post['file']);
330334
$themeFolder = $this->getThemeFolder();
331-
//echo $themeFolder . DS . $file;
332335

333336
if ($post_id) {
334337
$type = 'page';
@@ -375,7 +378,7 @@ function rename() {
375378
}
376379

377380
$currentFile = $themeFolder . DS . $file;
378-
$targetFile = $themeFolder . DS . $newfile;
381+
$targetFile = dirname($currentFile) . DS . basename($newfile); //save in same folder
379382

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

@@ -386,29 +389,75 @@ function rename() {
386389
$slug = slugify($name);
387390

388391
$this->posts = new PostSQL();
389-
$options = [
390-
'post_id' => $post_id,
391-
'post_content' => [
392-
'name' => $name,
393-
'slug' => $slug,
394-
],
395-
] + $this->global;
392+
$data = $this->posts->get(['post_id' => $post_id]);
396393

397394
if ($duplicate === 'true') {
395+
$data = $this->posts->get(['post_id' => $post_id]);
396+
397+
if ($data) {
398+
unset($data['post_id']);
399+
$id = rand(1, 1000);
400+
401+
foreach ($data['post_content'] as &$content) {
402+
unset($content['post_id']);
403+
404+
if ($content['language_id'] == $this->global['language_id']) {
405+
$content['name'] = $name;
406+
$content['slug'] = $slug;
407+
} else {
408+
$content['name'] .= ' [' . __('duplicate') . ']';
409+
$content['slug'] .= '-' . __('duplicate') . "-$id";
410+
}
411+
}
412+
413+
if (isset($data['post_to_taxonomy_item'])) {
414+
foreach ($data['post_to_taxonomy_item'] as &$item) {
415+
$taxonomy_item[] = $item['taxonomy_item_id'];
416+
}
417+
}
418+
419+
if (isset($data['post_to_site'])) {
420+
foreach ($data['post_to_site'] as &$item) {
421+
$site_id[] = $item['site_id'];
422+
}
423+
}
424+
425+
$startTemplateUrl = $data['template'] ?? "content/$type.html";
426+
$template = "content/$slug.html";
427+
428+
if (!@copy($themeFolder . DS . $startTemplateUrl, $themeFolder . DS . $template)) {
429+
$template = $data['template'] ?? '';
430+
}
431+
432+
$result = $this->posts->add([
433+
'post' => [
434+
'post_content' => $data['post_content'],
435+
'taxonomy_item' => $taxonomy_item ?? [],
436+
'template' => $template,
437+
] + $data,
438+
'site_id' => $site_id,
439+
]);
440+
441+
if ($result && isset($result['post'])) {
442+
$message = ['success' => true, 'url' => url('content/page/index', ['slug' => $slug]), 'message' => ucfirst($type) . __(' duplicated!')];
443+
} else {
444+
$message = ['success' => false, 'message' => sprintf(__('Error duplicating %s!'), $type)];
445+
}
446+
}
398447
} else {
399448
$result = $this->posts->editContent($options);
400449
}
401450

402451
if ($result && isset($result['post_content'])) {
403452
$message = ['success' => true, 'url' => url('content/page/index', ['slug' => $slug]), 'message' => ucfirst($type) . __(' renamed!')];
404453
} else {
405-
$message = ['success' => false, 'message' => sprintf(__('Error deleting %s!'), $type)];
454+
$message = ['success' => false, 'message' => sprintf(__('Error renaming %s!'), $type)];
406455
}
407456
}
408457
} else {
409458
if ($duplicate === 'true') {
410-
if (copy($currentFile, $targetFile)) {
411-
$message = ['success' => true, 'newfile' => $newfile, 'message' => __('File copied!')];
459+
if (@copy($currentFile, $targetFile)) {
460+
$message = ['success' => true, 'newfile' => $newfile, 'message' => __('File copied!'), 'url' => $newfile];
412461
} else {
413462
$message = ['success' => false, 'message' => __('Error copying file!')];
414463
}
@@ -421,6 +470,7 @@ function rename() {
421470
}
422471
}
423472

473+
$this->clearTemplateListCache(sanitizeFileName($this->request->get['theme'] ?? false));
424474
$this->response->setType('json');
425475
$this->response->output($message);
426476
}
@@ -520,13 +570,13 @@ function save() {
520570
$startTemplate = $themeFolder . DS . $startTemplateUrl;
521571

522572
if (file_exists($startTemplate)) {
523-
if (! ($html = @file_get_contents($startTemplate)) ) {
573+
if (! ($html = @file_get_contents($startTemplate))) {
524574
$text .= sprintf(__('%s is not readable!'), $startTemplate);
525575
}
526576
} else {
527577
$text .= sprintf(__('%s does not exist!'), $startTemplate);
528578
}
529-
579+
530580
$html = preg_replace('@<base href[^>]+>@', '<base href="' . $baseUrl . '">', $html);
531581
}
532582

@@ -546,7 +596,16 @@ function save() {
546596
}
547597
}
548598

549-
if (! $startTemplateUrl) {
599+
//if plugins template use public path
600+
$isPlugin = false;
601+
if (substr_compare($file,'/plugins/', 0, 9) === 0) {
602+
$fileName = DIR_PUBLIC . DS . ($folder ? $folder . DS : '') . $file;
603+
$isPlugin = true;
604+
} else {
605+
$fileName = $themeFolder . DS . ($folder ? $folder . DS : '') . $file;
606+
}
607+
608+
if (! $startTemplateUrl && !$isPlugin) {
550609
$backupFolder = $themeFolder . DS . 'backup' . DS;
551610

552611
if (is_writable($backupFolder)) {
@@ -581,13 +640,6 @@ function save() {
581640
}
582641
}
583642

584-
//if plugins template use public path
585-
if (substr_compare($file[0],'/plugins/', 0, 9) === 0) {
586-
$fileName = DIR_PUBLIC . DS . ($folder ? $folder . DS : '') . $file;
587-
} else {
588-
$fileName = $themeFolder . DS . ($folder ? $folder . DS : '') . $file;
589-
}
590-
591643
if ($html) {
592644
if (@file_put_contents($fileName, $html)) {
593645
$globalOptions = [];

0 commit comments

Comments
 (0)