oct_blogarticle.php
11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
/**
* @copyright OCTemplates
* @support https://octemplates.net/
* @license LICENSE.txt
*/
class ModelOCTemplatesBlogOCTBlogArticle extends Model {
public function updateViewed($blogarticle_id) {
$this->db->query("
UPDATE
" . DB_PREFIX . "oct_blogarticle
SET
viewed =
(
viewed + 1
)
WHERE
blogarticle_id = '" . (int)$blogarticle_id . "'
");
}
public function getArticle($blogarticle_id) {
$query = $this->db->query("
SELECT DISTINCT
*,
ad.name AS name,
a.image,
(
SELECT
COUNT(*) AS total
FROM
" . DB_PREFIX . "oct_blogcomments r2
WHERE
r2.blogarticle_id = a.blogarticle_id
AND r2.status = '1'
GROUP BY
r2.blogarticle_id
)
AS comments_total,
a.sort_order
FROM
" . DB_PREFIX . "oct_blogarticle a
LEFT JOIN
" . DB_PREFIX . "oct_blogarticle_description ad
ON (a.blogarticle_id = ad.blogarticle_id)
LEFT JOIN
" . DB_PREFIX . "oct_blogarticle_to_store a2s
ON (a.blogarticle_id = a2s.blogarticle_id)
WHERE
a.blogarticle_id = '" . (int)$blogarticle_id . "'
AND ad.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND a.status = '1'
AND a.date_available <= NOW()
AND a2s.store_id = '" . (int)$this->config->get('config_store_id') . "'
");
if ($query->num_rows) {
$articles = [
'blogarticle_id' => $query->row['blogarticle_id'],
'name' => $query->row['name'],
'shot_description' => $query->row['shot_description'],
'description' => $query->row['description'],
'meta_title' => $query->row['meta_title'],
'meta_description' => $query->row['meta_description'],
'meta_keyword' => $query->row['meta_keyword'],
'tag' => $query->row['tag'],
'image' => $query->row['image'],
'comments_total' => $query->row['comments_total'] ? $query->row['comments_total'] : 0,
'date_added' => $query->row['date_added'],
'date_modified' => $query->row['date_modified'],
'viewed' => $query->row['viewed']
];
return $articles;
} else {
return false;
}
}
public function getArticles($data = []) {
$sql = "
SELECT
a.blogarticle_id
";
if (isset($data['filter_blogcategory_id']) && !empty($data['filter_blogcategory_id'])) {
if (isset($data['filter_sub_blogcategory']) && !empty($data['filter_sub_blogcategory'])) {
$sql .= " FROM " . DB_PREFIX . "oct_blogcategory_path bcp LEFT JOIN " . DB_PREFIX . "oct_blogarticle_to_category a2c ON (bcp.blogcategory_id = a2c.blogcategory_id)";
} else {
$sql .= " FROM " . DB_PREFIX . "oct_blogarticle_to_category a2c";
}
$sql .= " LEFT JOIN " . DB_PREFIX . "oct_blogarticle a ON (a2c.blogarticle_id = a.blogarticle_id)";
} else {
$sql .= " FROM " . DB_PREFIX . "oct_blogarticle a";
}
$sql .= "
LEFT JOIN
" . DB_PREFIX . "oct_blogarticle_description ad
ON (a.blogarticle_id = ad.blogarticle_id)
LEFT JOIN
" . DB_PREFIX . "oct_blogarticle_to_store a2s
ON (a.blogarticle_id = a2s.blogarticle_id)
WHERE
ad.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND a.status = '1'
AND a.date_available <= NOW()
AND a2s.store_id = '" . (int)$this->config->get('config_store_id') . "'
";
if (isset($data['filter_blogcategory_id']) && !empty($data['filter_blogcategory_id'])) {
if (is_array($data['filter_blogcategory_id'])) {
$implode = [];
foreach ($data['filter_blogcategory_id'] as $blogcategory_id) {
$implode[] = (int)$blogcategory_id;
}
$sql .= " AND a2c.blogcategory_id IN (" . implode(',', $implode) . ")";
} else {
if (isset($data['filter_sub_blogcategory']) && !empty($data['filter_sub_blogcategory'])) {
$sql .= " AND bcp.blog_path_id = '" . (int)$data['filter_blogcategory_id'] . "'";
} else {
$sql .= " AND a2c.blogcategory_id = '" . (int)$data['filter_blogcategory_id'] . "'";
}
}
}
if (!empty($data['filter_name']) || !empty($data['filter_tag'])) {
$sql .= " AND (";
if (!empty($data['filter_name'])) {
$implode = [];
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_name'])));
foreach ($words as $word) {
$implode[] = "ad.name LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" AND ", $implode) . "";
}
if (!empty($data['filter_description'])) {
$sql .= " OR ad.description LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
}
}
if (!empty($data['filter_name']) && !empty($data['filter_tag'])) {
$sql .= " OR ";
}
if (!empty($data['filter_tag'])) {
$implode = [];
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_tag'])));
foreach ($words as $word) {
$implode[] = "ad.tag LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" AND ", $implode) . "";
}
}
$sql .= ")";
}
$sql .= " GROUP BY a.blogarticle_id";
$sort_data = [
'ad.name',
'a.sort_order',
'a.date_added'
];
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
if ($data['sort'] == 'ad.name') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
} else {
$sql .= " ORDER BY " . $data['sort'];
}
} else {
$sql .= " ORDER BY a.sort_order";
}
if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC, LCASE(ad.name) DESC";
} else {
$sql .= " ASC, LCASE(ad.name) ASC";
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$article_data = [];
$query = $this->db->query($sql);
foreach ($query->rows as $result) {
$article_data[$result['blogarticle_id']] = $this->getArticle($result['blogarticle_id']);
}
return $article_data;
}
public function getArticleImages($blogarticle_id) {
$query = $this->db->query("
SELECT
*
FROM
" . DB_PREFIX . "oct_blogarticle_image
WHERE
blogarticle_id = '" . (int)$blogarticle_id . "'
ORDER BY
sort_order ASC
");
return $query->rows;
}
public function getArticleRelated($blogarticle_id) {
$article_data = [];
$query = $this->db->query("
SELECT
*
FROM
" . DB_PREFIX . "oct_blogarticle_related ar
LEFT JOIN
" . DB_PREFIX . "oct_blogarticle a
ON (ar.related_id = a.blogarticle_id)
LEFT JOIN
" . DB_PREFIX . "oct_blogarticle_to_store a2s
ON (a.blogarticle_id = a2s.blogarticle_id)
WHERE
ar.blogarticle_id = '" . (int)$blogarticle_id . "'
AND a.status = '1'
AND a.date_available <= NOW()
AND a2s.store_id = '" . (int)$this->config->get('config_store_id') . "'
");
foreach ($query->rows as $result) {
$article_data[$result['related_id']] = $this->getArticle($result['related_id']);
}
return $article_data;
}
public function getArticleRelatedProduct($blogarticle_id) {
$this->load->model('catalog/product');
$product_data = [];
$query = $this->db->query("
SELECT
*
FROM
" . DB_PREFIX . "oct_blogarticle_related_product arp
WHERE
arp.blogarticle_id = '" . (int)$blogarticle_id . "'
");
foreach ($query->rows as $result) {
$product_info = $this->model_catalog_product->getProduct($result['product_id']);
if ($product_info) {
$product_data[$result['product_id']] = $product_info;
}
}
return $product_data;
}
public function getArticleRelatedProductPage($product_id) {
$product_related_blog_data = [];
$query = $this->db->query("
SELECT blogarticle_id as article_id
FROM
" . DB_PREFIX . "oct_blogarticle_related_product
WHERE
product_id = '" . (int)$product_id . "'
");
if ($query->num_rows) {
foreach ($query->rows as $result) {
$product_related_blog_data[] = $this->model_octemplates_blog_oct_blogarticle->getArticle($result['article_id']);
}
return $product_related_blog_data;
} else {
return 0;
}
}
public function getArticleLayoutId($blogarticle_id) {
$query = $this->db->query("
SELECT
*
FROM
" . DB_PREFIX . "oct_blogarticle_to_layout
WHERE
blogarticle_id = '" . (int)$blogarticle_id . "'
AND store_id = '" . (int)$this->config->get('config_store_id') . "'
");
if ($query->num_rows) {
return (int)$query->row['layout_id'];
} else {
return 0;
}
}
public function getArticleBlogCategories($blogarticle_id) {
$query = $this->db->query("
SELECT
*
FROM
" . DB_PREFIX . "oct_blogarticle_to_category ba2c
JOIN
" . DB_PREFIX . "oct_blogcategory bc ON (bc.blogcategory_id = ba2c.blogcategory_id)
WHERE
blogarticle_id = '" . (int)$blogarticle_id . "'
AND bc.status = 1
");
return $query->rows;
}
public function getTotalArticles($data = []) {
$sql = "SELECT COUNT(DISTINCT a.blogarticle_id) AS total";
if (!empty($data['filter_blogcategory_id'])) {
if (!empty($data['filter_sub_blogcategory'])) {
$sql .= " FROM " . DB_PREFIX . "oct_blogcategory_path bcp LEFT JOIN " . DB_PREFIX . "oct_blogarticle_to_category a2c ON (bcp.blogcategory_id = a2c.blogcategory_id)";
} else {
$sql .= " FROM " . DB_PREFIX . "oct_blogarticle_to_category a2c";
}
$sql .= " LEFT JOIN " . DB_PREFIX . "oct_blogarticle a ON (a2c.blogarticle_id = a.blogarticle_id)";
} else {
$sql .= " FROM " . DB_PREFIX . "oct_blogarticle a";
}
$sql .= "
LEFT JOIN
" . DB_PREFIX . "oct_blogarticle_description ad
ON (a.blogarticle_id = ad.blogarticle_id)
LEFT JOIN
" . DB_PREFIX . "oct_blogarticle_to_store a2s
ON (a.blogarticle_id = a2s.blogarticle_id)
WHERE
ad.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND a.status = '1'
AND a.date_available <= NOW()
AND a2s.store_id = '" . (int)$this->config->get('config_store_id') . "'
";
if (!empty($data['filter_blogcategory_id'])) {
if (!empty($data['filter_sub_blogcategory'])) {
$sql .= " AND bcp.blog_path_id = '" . (int)$data['filter_blogcategory_id'] . "'";
} else {
$sql .= " AND a2c.blogcategory_id = '" . (int)$data['filter_blogcategory_id'] . "'";
}
}
if (!empty($data['filter_name']) || !empty($data['filter_tag'])) {
$sql .= " AND (";
if (!empty($data['filter_name'])) {
$implode = [];
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_name'])));
foreach ($words as $word) {
$implode[] = "ad.name LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" AND ", $implode) . "";
}
if (!empty($data['filter_description'])) {
$sql .= " OR ad.description LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
}
}
if (!empty($data['filter_name']) && !empty($data['filter_tag'])) {
$sql .= " OR ";
}
if (!empty($data['filter_tag'])) {
$implode = [];
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_tag'])));
foreach ($words as $word) {
$implode[] = "ad.tag LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" AND ", $implode) . "";
}
}
$sql .= ")";
}
$query = $this->db->query($sql);
return (int)$query->row['total'];
}
}