Skip to content

Commit 7adbd88

Browse files
committed
Adde new API for blog
1 parent 7ff7204 commit 7adbd88

File tree

6 files changed

+153
-14
lines changed

6 files changed

+153
-14
lines changed

model/blog/category.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,15 @@ public function getTotalCategories($data = array())
118118
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
119119
return $result['count(*)'];
120120
}
121+
122+
public function getCategoryByPostId($post_id)
123+
{
124+
$categories = CorrespondancesCategoriesClass::getCategoriesListeName(
125+
(int)$post_id,
126+
(int)$this->context->cookie->id_lang,
127+
1
128+
);
129+
130+
return $categories;
131+
}
121132
}

model/blog/post.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ class ModelBlogPost extends Model
3131
public function getPost($post_id)
3232
{
3333
$post = new NewsClass((int) $post_id, (int) $this->context->language->id, $this->context->shop->id);
34-
3534
return array(
3635
'id' => $post->id,
3736
'title' => $post->title,
37+
'datePublished' => $post->date,
3838
'description' => html_entity_decode($post->content, ENT_QUOTES, 'UTF-8'),
3939
'shortDescription' => strip_tags(html_entity_decode($post->paragraph, ENT_QUOTES, 'UTF-8')),
4040
'image' => $this->getImage($post->id),
@@ -45,16 +45,26 @@ public function getPost($post_id)
4545

4646
public function getImage($post_id)
4747
{
48-
$uri = __PS_BASE_URI__ . 'modules/prestablog/views/img/' . Configuration::get('prestablog_theme') .
48+
if (file_exists(_PS_ROOT_DIR_. '/modules/prestablog/views/img/' . Configuration::get('prestablog_theme') .
49+
'/up-img/' . $post_id . '.jpg')) {
50+
$uri = __PS_BASE_URI__ . 'modules/prestablog/views/img/' . Configuration::get('prestablog_theme') .
4951
'/up-img/' . $post_id . '.jpg';
50-
return $this->context->link->protocol_content . Tools::getMediaServer($uri) . $uri;
52+
return $this->context->link->protocol_content . Tools::getMediaServer($uri) . $uri;
53+
} else {
54+
return '';
55+
}
5156
}
5257

5358
public function getImageLazy($post_id)
5459
{
55-
$uri = __PS_BASE_URI__ . 'modules/prestablog/views/img/' . Configuration::get('prestablog_theme') .
60+
if (file_exists(_PS_ROOT_DIR_. '/modules/prestablog/views/img/' . Configuration::get('prestablog_theme') .
61+
'/up-img/thumb_' . $post_id . '.jpg')) {
62+
$uri = __PS_BASE_URI__ . 'modules/prestablog/views/img/' . Configuration::get('prestablog_theme') .
5663
'/up-img/thumb_' . $post_id . '.jpg';
57-
return $this->context->link->protocol_content . Tools::getMediaServer($uri) . $uri;
64+
return $this->context->link->protocol_content . Tools::getMediaServer($uri) . $uri;
65+
} else {
66+
return '';
67+
}
5868
}
5969

6070
public function getPosts($data = array())
@@ -138,4 +148,34 @@ public function getTotalPosts($data = array())
138148
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
139149
return $result['count(*)'];
140150
}
151+
152+
public function getNextPost($post_id)
153+
{
154+
$sql = new DbQuery();
155+
$sql->select('*');
156+
$sql->from('prestablog_news', 'pn');
157+
$sql->leftJoin('prestablog_news_lang', 'pnl', 'pnl.`id_prestablog_news` = pn.`id_prestablog_news`');
158+
$sql->where('pn.`actif` = 1');
159+
$sql->where('pnl.`id_lang` = ' . (int) $this->context->language->id);
160+
$sql->orderBy('pn.date ASC');
161+
$sql->where("pn.id_prestablog_news > '" . (int)$post_id . "'");
162+
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
163+
164+
return $result;
165+
}
166+
public function getPrevPost($post_id)
167+
{
168+
$sql = new DbQuery();
169+
$sql->select('*');
170+
$sql->from('prestablog_news', 'pn');
171+
$sql->leftJoin('prestablog_news_lang', 'pnl', 'pnl.`id_prestablog_news` = pn.`id_prestablog_news`');
172+
$sql->where('pn.`actif` = 1');
173+
$sql->where('pnl.`id_lang` = ' . (int) $this->context->language->id);
174+
$sql->orderBy('pn.id_prestablog_news DESC');
175+
$sql->where("pn.id_prestablog_news < '" . (int)$post_id . "'");
176+
177+
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
178+
179+
return $result;
180+
}
141181
}

resolver/blog/post.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,43 @@ public function get($args)
2525

2626
$post = $this->model_blog_post->getPost($args['id']);
2727

28+
$date_format = '%A %d %B %Y';
29+
2830
$that = $this;
2931
return array(
3032
'id' => $post['id'],
33+
'name' => $post['title'],
3134
'title' => $post['title'],
3235
'shortDescription' => $post['shortDescription'],
3336
'description' => $post['description'],
3437
'keyword' => $post['keyword'],
3538
'image' => $post['image'],
3639
'imageLazy' => $post['imageLazy'],
40+
'rating' => null,
41+
'datePublished' => iconv(mb_detect_encoding(strftime($date_format, strtotime($post['datePublished']))), "utf-8//IGNORE", strftime($date_format, strtotime($post['datePublished']))),
3742
'reviews' => function ($root, $args) use ($that) {
3843
return $that->load->resolver('blog/review/get', array(
3944
'parent' => $root,
4045
'args' => $args
4146
));
47+
},
48+
'categories' => function ($root, $args) use ($that) {
49+
return $that->load->resolver('blog/post/categories', array(
50+
'parent' => $root,
51+
'args' => $args
52+
));
53+
},
54+
'next' => function ($root, $args) use ($that) {
55+
return $that->load->resolver('blog/post/next', array(
56+
'parent' => $root,
57+
'args' => $args
58+
));
59+
},
60+
'prev' => function ($root, $args) use ($that) {
61+
return $that->load->resolver('blog/post/prev', array(
62+
'parent' => $root,
63+
'args' => $args
64+
));
4265
}
4366
);
4467
} else {
@@ -86,4 +109,52 @@ public function getList($args)
86109
);
87110
}
88111
}
112+
113+
public function categories($args)
114+
{
115+
if ($this->status) {
116+
$this->load->model('blog/category');
117+
$post = $args['parent'];
118+
119+
$result = $this->model_blog_category->getCategoryByPostId($post['id']);
120+
$categories = array();
121+
foreach ($result as $category) {
122+
$categories[] =$this->load->resolver('blog/category/get', array('id' => $category['id_prestablog_categorie']));
123+
}
124+
return $categories;
125+
} else {
126+
return array();
127+
}
128+
}
129+
130+
public function prev($args)
131+
{
132+
if ($this->status) {
133+
$this->load->model('blog/post');
134+
$post = $args['parent'];
135+
$next_post_info = $this->model_blog_post->getNextPost($post['id']);
136+
if (empty($next_post_info)) {
137+
return null;
138+
}
139+
return $this->get(array('id' => $next_post_info['id_prestablog_news']));
140+
} else {
141+
return array();
142+
}
143+
}
144+
145+
public function next($args)
146+
{
147+
if ($this->status) {
148+
$this->load->model('blog/post');
149+
$post = $args['parent'];
150+
$prev_post_info = $this->model_blog_post->getPrevPost($post['id']);
151+
152+
if (empty($prev_post_info)) {
153+
return null;
154+
}
155+
return $this->get(array('id' => $prev_post_info['id_prestablog_news']));
156+
} else {
157+
return array();
158+
}
159+
}
89160
}

resolver/blog/review.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function get($data)
4040
$this->load->model('blog/review');
4141
$result = $this->model_blog_review->getReviews($post['id']);
4242

43-
return $result;
43+
return array(
44+
'content'=> $result,
45+
'totalElements' => count($result)
46+
);
4447
}
4548
}

resolver/common/page.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ public function get($args)
1818
$page_info = $this->model_common_page->getPage($args['id']);
1919

2020
return array(
21-
'id' => $page_info['id'],
22-
'title' => $page_info['title'],
21+
'id' => $page_info['id'],
22+
'name' => $page_info['title'],
23+
'title' => $page_info['title'],
2324
'description' => $page_info['description'],
24-
'sort_order' => (int)$page_info['sort_order'],
25-
'keyword' => $page_info['keyword']
25+
'sort_order' => (int)$page_info['sort_order'],
26+
'keyword' => $page_info['keyword']
2627
);
2728
}
2829

schema.graphql

100755100644
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ type CategoryResult {
7676
}
7777

7878
type Currency {
79-
title: String
79+
title: String @deprecated(reason: "Changed to name!")
80+
name: String
8081
code: String
8182
symbol_left: String
8283
symbol_right: String
@@ -120,7 +121,8 @@ type OptionValue {
120121

121122
type Page {
122123
id: ID
123-
title: String
124+
title: String @deprecated(reason: "Changed to name!")
125+
name: String
124126
description: String
125127
keyword: String
126128
sort_order: Int
@@ -137,15 +139,26 @@ type PageResult {
137139
totalElements: Int
138140
}
139141

142+
type postReviewResult {
143+
content: [postReview]
144+
totalElements: Int
145+
}
146+
140147
type Post {
141148
id: ID
142-
title: String
149+
title: String @deprecated(reason: "Changed to name!")
150+
name: String
143151
shortDescription: String
144152
description: String
145153
image: String
146154
imageLazy: String
147155
keyword: String
148-
reviews: [postReview]
156+
rating: Float
157+
reviews: postReviewResult
158+
categories: [categoryBlog]
159+
datePublished: String
160+
next: Post
161+
prev: Post
149162
}
150163

151164
type PostResult {

0 commit comments

Comments
 (0)