Skip to content

Commit 1f9d8eb

Browse files
committed
Include id for post and product urls to have working urls if slug changes to avoid adding redirects
1 parent 576a312 commit 1f9d8eb

File tree

13 files changed

+91
-96
lines changed

13 files changed

+91
-96
lines changed

admin/controller/content/autocomplete-trait.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ function categoriesAutocomplete() {
4747

4848
$this->response->setType('json');
4949
$this->response->output($search);
50-
51-
return false;
5250
}
5351

5452
function manufacturersAutocomplete() {
@@ -72,8 +70,6 @@ function manufacturersAutocomplete() {
7270

7371
$this->response->setType('json');
7472
$this->response->output($search);
75-
76-
return false;
7773
}
7874

7975
function vendorsAutocomplete() {
@@ -97,8 +93,6 @@ function vendorsAutocomplete() {
9793

9894
$this->response->setType('json');
9995
$this->response->output($search);
100-
101-
return false;
10296
}
10397

10498
function urlAutocomplete() {
@@ -122,7 +116,7 @@ function urlAutocomplete() {
122116
'type' => 'cardimage',
123117
'src' => $product['image'],
124118
'text' => $product['name'],
125-
'value'=> '<a href="' . url('product/product/index', ['slug'=> $product['slug']]) . '">' . $product['name'] . '</a>',
119+
'value'=> '<a href="' . url('product/product/index', ['slug'=> $product['slug'], 'product_id' => $post['product_id']]) . '">' . $product['name'] . '</a>',
126120
];
127121
}
128122
}
@@ -138,12 +132,12 @@ function urlAutocomplete() {
138132
'type' => 'cardimage',
139133
'src' => $post['image'],
140134
'text' => $post['name'],
141-
'value'=> '<a href="' . url('content/post/index', ['slug'=> $post['slug']]) . '">' . $post['name'] . '</a>',
135+
'value'=> '<a href="' . url('content/post/index', ['slug'=> $post['slug'], 'post_id' => $post['post_id']]) . '">' . $post['name'] . '</a>',
142136
];
143137
}
144138
}
145139
}
146-
140+
147141
$this->response->setType('json');
148142
$this->response->output($search);
149143
}

admin/controller/content/edit.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ function index() {
181181
if (isset($post[$this->object . '_content'])) {
182182
foreach ($post[$this->object . '_content'] as &$content) {
183183
if (! isset($post['url'])) {
184-
$post['url'] = \Vvveb\url($route, ['slug'=> $content['slug']] + $url);
184+
$post['url'] = \Vvveb\url($route, ['slug'=> $content['slug'], 'post_id'=> $post_id] + $url);
185185

186186
if (! $post['url']) {
187-
$post['url'] = \Vvveb\url($altRoute, ['slug'=> $content['slug']] + $url);
187+
$post['url'] = \Vvveb\url($altRoute, ['slug'=> $content['slug'], 'post_id'=> $post_id] + $url);
188188
}
189189
}
190190
$language = [];

app/controller/content/page.php

-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,4 @@
2424

2525
class Page extends Post {
2626
public $type = 'page';
27-
28-
function index() {
29-
parent::index();
30-
}
3127
}

app/controller/content/post.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,18 @@ function index() {
4646
}
4747

4848
$language = $this->request->get['language'] ?? $this->global['language'] ?? $this->global['default_language'];
49+
$post_id = $this->request->get['post_id'] ?? '';
4950
$slug = $this->request->get['slug'] ?? '';
5051
$created_at = $this->request->get['created_at'] ?? ''; //revision preview
5152

52-
if ($slug) {
53+
if ($post_id || $slug) {
5354
$contentSql = new PostSQL();
54-
$options = $this->global + ['slug' => $slug, 'type' => $this->type];
55+
$options = $this->global + ['post_id' => $post_id, 'slug' => $slug, 'type' => $this->type];
5556
$content = $contentSql->getContent($options) ?? [];
5657

58+
$class = __NAMESPACE__ . '\\' . ucfirst($this->type); //__CLASS__ is always Post
5759
$error = __('Post not found!');
58-
list($content, $language, $slug) = Event :: trigger(__CLASS__,__FUNCTION__, $content, $language, $slug);
60+
list($content, $language, $slug) = Event :: trigger($class,__FUNCTION__, $content, $language, $slug);
5961

6062
if ($content) {
6163
if (isset($content[$language])) {
@@ -72,7 +74,7 @@ function index() {
7274
if ($this->global['language'] != $languageContent['code']) {
7375
setLanguage($languageContent['code']);
7476
}
75-
77+
7678
$this->global['language'] = $languageContent['code'];
7779
$this->global['language_id'] = $languageContent['language_id'];
7880

@@ -85,7 +87,7 @@ function index() {
8587
$this->request->request['name'] = $languageContent['name'];
8688
$this->request->request['code'] = $languageContent['code'];
8789
$this->request->request['language_id'] = $languageContent['language_id'];
88-
90+
8991
if ($created_at) {
9092
//check if admin user to allow revision preview
9193
$admin = Admin::current();
@@ -106,13 +108,15 @@ function index() {
106108
$this->view->tplFile("content/{$this->type}.tpl");
107109
}
108110
} else {
109-
$this->notFound(true, ['message' => $error, 'title' => $error]);
111+
return $this->notFound(true, ['message' => $error, 'title' => $error]);
110112
}
113+
114+
list($content, $languageContent, $language, $slug) = Event :: trigger($class, __FUNCTION__ . ':after', $content, $languageContent, $language, $slug);
111115
} else {
112-
$this->notFound(true, ['message' => $error, 'title' => $error]);
116+
return $this->notFound(true, ['message' => $error, 'title' => $error]);
113117
}
114118

115-
$this->view->post = $languageContent;
119+
$this->view->post = $languageContent;
116120
$this->view->content = $content;
117121
}
118122
}

app/sql/mysqli/post.sql

+11-11
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@
4141
LEFT JOIN admin ad ON (_.admin_id = ad.admin_id)
4242
WHERE 1 = 1
4343

44-
@IF isset(:slug)
45-
THEN
46-
AND pd.slug = :slug
44+
@IF isset(:slug) && !(isset(:post_id) && :post_id)
45+
THEN
46+
AND pd.slug = :slug
4747
END @IF
4848

49-
@IF isset(:post_id)
50-
THEN
51-
AND _.post_id = :post_id
49+
@IF isset(:post_id)
50+
THEN
51+
AND _.post_id = :post_id
5252
END @IF
5353

54-
@IF isset(:type)
55-
THEN
56-
AND _.type = :type
54+
@IF isset(:type)
55+
THEN
56+
AND _.type = :type
5757
END @IF
5858

5959
LIMIT 1;
@@ -87,12 +87,12 @@
8787

8888
WHERE 1 = 1
8989

90-
@IF isset(:slug)
90+
@IF isset(:slug) && !(isset(:post_id) && :post_id)
9191
THEN
9292
AND _.post_id = (SELECT post_id FROM post_content WHERE slug = :slug LIMIT 1)
9393
END @IF
9494

95-
@IF isset(:post_id)
95+
@IF isset(:post_id) && :post_id > 0
9696
THEN
9797
AND _.post_id = :post_id
9898
END @IF

app/sql/mysqli/product.sql

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@
1313
)
1414
BEGIN
1515

16-
SELECT _.product_id,_.slug,_.name,_.meta_keywords,_.meta_description,_.language_id,product.template,language.code,language.code as array_key
16+
SELECT product.*, _.product_id,_.slug,_.name,_.meta_keywords,_.meta_description,_.language_id,product.template,language.code,language.code as array_key
1717
FROM product_content AS _
1818
LEFT JOIN language ON (language.language_id = _.language_id)
1919
LEFT JOIN product ON (product.product_id = _.product_id)
2020
WHERE 1 = 1
2121

22-
@IF isset(:status)
23-
THEN
24-
AND product.status = :status
22+
@IF isset(:status)
23+
THEN
24+
AND product.status = :status
2525
END @IF
2626

27-
@IF isset(:slug)
28-
THEN
29-
AND _.product_id = (SELECT product_id FROM product_content WHERE slug = :slug LIMIT 1)
27+
@IF isset(:slug) && !(isset(:product_id) && :product_id)
28+
THEN
29+
AND _.product_id = (SELECT product_id FROM product_content WHERE slug = :slug LIMIT 1)
3030
END @IF
3131

32-
@IF isset(:product_id)
33-
THEN
34-
AND _.product_id = :product_id
32+
@IF isset(:product_id) && :product_id > 0
33+
THEN
34+
AND _.product_id = :product_id
3535
END @IF
3636
END

app/sql/pgsql/post.sql

+9-9
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
LEFT JOIN admin ad ON (_.admin_id = ad.admin_id)
4242
WHERE 1 = 1
4343

44-
@IF isset(:slug)
45-
THEN
46-
AND pd.slug = :slug
44+
@IF isset(:slug) && !(isset(:post_id) && :post_id)
45+
THEN
46+
AND pd.slug = :slug
4747
END @IF
4848

4949
@IF isset(:post_id)
@@ -87,14 +87,14 @@
8787

8888
WHERE 1 = 1
8989

90-
@IF isset(:slug)
91-
THEN
92-
AND _.post_id = (SELECT post_id FROM post_content WHERE slug = :slug LIMIT 1)
90+
@IF isset(:slug) && !(isset(:post_id) && :post_id)
91+
THEN
92+
AND _.post_id = (SELECT post_id FROM post_content WHERE slug = :slug LIMIT 1)
9393
END @IF
9494

95-
@IF isset(:post_id)
96-
THEN
97-
AND _.post_id = :post_id
95+
@IF isset(:post_id) && :post_id > 0
96+
THEN
97+
AND _.post_id = :post_id
9898
END @IF
9999

100100
@IF isset(:site_id)

app/sql/pgsql/product.sql

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
LEFT JOIN product ON (product.product_id = _.product_id)
2020
WHERE 1 = 1
2121

22-
@IF isset(:status)
23-
THEN
24-
AND product.status = :status
22+
@IF isset(:status)
23+
THEN
24+
AND product.status = :status
2525
END @IF
2626

27-
@IF isset(:slug)
28-
THEN
29-
AND _.product_id = (SELECT product_id FROM product_content WHERE slug = :slug LIMIT 1)
27+
@IF isset(:slug) && !(isset(:product_id) && :product_id)
28+
THEN
29+
AND _.product_id = (SELECT product_id FROM product_content WHERE slug = :slug LIMIT 1)
3030
END @IF
3131

3232
@IF isset(:product_id)

app/sql/sqlite/post.sql

+9-9
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
LEFT JOIN admin ad ON (_.admin_id = ad.admin_id)
4242
WHERE 1 = 1
4343

44-
@IF isset(:slug)
45-
THEN
46-
AND pd.slug = :slug
44+
@IF isset(:slug) && !(isset(:post_id) && :post_id)
45+
THEN
46+
AND pd.slug = :slug
4747
END @IF
4848

4949
@IF isset(:post_id)
@@ -87,14 +87,14 @@
8787

8888
WHERE 1 = 1
8989

90-
@IF isset(:slug)
91-
THEN
92-
AND _.post_id = (SELECT post_id FROM post_content WHERE slug = :slug LIMIT 1)
90+
@IF isset(:slug) && !(isset(:post_id) && :post_id)
91+
THEN
92+
AND _.post_id = (SELECT post_id FROM post_content WHERE slug = :slug LIMIT 1)
9393
END @IF
9494

95-
@IF isset(:post_id)
96-
THEN
97-
AND _.post_id = :post_id
95+
@IF isset(:post_id) && :post_id > 0
96+
THEN
97+
AND _.post_id = :post_id
9898
END @IF
9999

100100
@IF isset(:site_id)

app/sql/sqlite/product.sql

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
AND product.status = :status
2525
END @IF
2626

27-
@IF isset(:slug)
28-
THEN
29-
AND _.product_id = (SELECT product_id FROM product_content WHERE slug = :slug LIMIT 1)
27+
@IF isset(:slug) && !(isset(:product_id) && :product_id)
28+
THEN
29+
AND _.product_id = (SELECT product_id FROM product_content WHERE slug = :slug LIMIT 1)
3030
END @IF
3131

32-
@IF isset(:product_id)
33-
THEN
34-
AND _.product_id = :product_id
32+
@IF isset(:product_id) && :product_id > 0
33+
THEN
34+
AND _.product_id = :product_id
3535
END @IF
3636
END

app/template/components/post.tpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $post = $this->_component['post'][$post_idx] ?? [];
1717

1818

1919
//manual echo to avoid html escape
20-
@post [data-v-post-content] = <?php if (isset($post['content'])) echo $post['content'];?>
20+
@post [data-v-post-content] = <?php if (isset($post['content'])) echo($post['content']);?>
2121
//@post [data-v-post-content] = $post['content']
2222

2323

@@ -42,4 +42,4 @@ foreach ($post['images'] as $image) { ?>
4242
4343
@image|after = <?php
4444
}
45-
?>
45+
?>

app/template/components/product.tpl

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ $_pagination_limit = isset($product['limit']) ? $product['limit'] : 5;
2222
@product|data-v-id = $product['product_id']
2323
@product|data-v-type = 'product'
2424

25+
//manual echo to avoid html escape
26+
@product [data-v-product-content] = <?php echo($product['content']);?>
27+
2528
//catch all data attributes
2629
@product [data-v-product-*]|innerText = $product['@@__data-v-product-(*)__@@']
2730
@product input[data-v-product-*]|value = $product['@@__data-v-product-(*)__@@']
2831
@product a[data-v-product-*]|href = $product['@@__data-v-product-(*)__@@']
2932

30-
//manual echo to avoid html escape
31-
@product [data-v-product-content] = <?php echo $product['content'];?>
3233

3334
@product button[data-v-product-*]|formaction = $product['@@__data-v-product-(*)__@@']
3435
@product a[data-v-product-*]|href = $product['@@__data-v-product-(*)__@@']

0 commit comments

Comments
 (0)