Skip to content

Commit b1f6096

Browse files
committed
Fix bugs
1 parent 8a06a40 commit b1f6096

File tree

20 files changed

+842
-1216
lines changed

20 files changed

+842
-1216
lines changed
Lines changed: 47 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,75 @@
11
<?php
22

3-
require_once VF_SYSTEM_DIR.'engine/model.php';
3+
use \Magento\Framework\App\ObjectManager;
44

5+
require_once VF_SYSTEM_DIR . 'engine/model.php';
6+
7+
/**
8+
* @property \Magefan\Blog\Model\ResourceModel\Category\CollectionFactory $_collectionFactory
9+
* @property Magefan\Blog\Model\CategoryFactory $_categoryFactory
10+
*/
511
class ModelBlogCategory extends Model
612
{
7-
public function getCategory($category_id)
8-
{
9-
$sql = "SELECT c.*, IFNULL(SUBSTRING_INDEX(c.path, '/', -1), 0) as parent_id
10-
FROM `".$this->db->getTableName('magefan_blog_category')."` c
11-
left join `".$this->db->getTableName('magefan_blog_category_store')."` cs on c.category_id = cs.category_id
12-
where c.is_active = '1' and c.`category_id` = '" . (int) $category_id . "'";
13+
private $_collectionFactory;
14+
private $_categoryFactory;
1315

14-
$sql .= " GROUP BY c.category_id";
16+
public function __construct($registry)
17+
{
18+
parent::__construct($registry);
1519

16-
$result = $this->db->fetchOne($sql);
20+
$objectManager = ObjectManager::getInstance();
21+
$this->_collectionFactory = $objectManager->get('\Magefan\Blog\Model\ResourceModel\Category\CollectionFactory');
22+
$this->_categoryFactory = $objectManager->get('Magefan\Blog\Model\CategoryFactory');
23+
}
1724

18-
return $result;
25+
public function getCategory($category_id)
26+
{
27+
return $this->_categoryFactory->create()->load($category_id);
1928
}
2029

2130
public function getCategories($data = array())
2231
{
23-
$sql = "SELECT c.category_id as ID
24-
FROM `".$this->db->getTableName('magefan_blog_category')."` c
25-
left join `".$this->db->getTableName('magefan_blog_category_store')."` cs on c.category_id = cs.category_id
26-
where c.is_active = '1'";
27-
28-
$implode = array();
29-
30-
if (isset($data['filter_parent_id'])) {
31-
$implode[] = "IFNULL(SUBSTRING_INDEX(c.path, '/', -1), 0) = '" . (int) $data['filter_parent_id'] . "'";
32+
/** @var $collection \Magefan\Blog\Model\ResourceModel\Category\Collection */
33+
$collection = $this->_collectionFactory->create();
34+
$collection->addActiveFilter();
35+
$collection->addStoreFilter($this->store->getStoreId());
36+
37+
if ($data['size'] != '-1') {
38+
$collection->setPageSize($data['size']);
39+
$collection->setCurPage($data['page']);
3240
}
3341

34-
if (count($implode) > 0) {
35-
$sql .= ' AND ' . implode(' AND ', $implode);
36-
}
37-
38-
$sql .= " GROUP BY c.category_id";
39-
40-
$sort_data = array(
41-
'ID'
42-
);
42+
if ($data['parent'] !== -1) {
43+
if ($data['parent'] != 0) {
44+
$collection->addFieldToFilter('category_id', ['in' => $this->_categoryFactory->create()->load($data['parent'])->getChildrenIds(false)]);
4345

44-
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
45-
$sql .= " ORDER BY " . $data['sort'];
46-
} else {
47-
$sql .= " ORDER BY ID";
46+
} else {
47+
$collection->addFieldToFilter('path', array('null' => true));
48+
}
4849
}
4950

5051
if (isset($data['order']) && ($data['order'] == 'DESC')) {
51-
$sql .= " DESC";
52+
$order = "DESC";
5253
} else {
53-
$sql .= " ASC";
54+
$order = "ASC";
5455
}
5556

56-
if (isset($data['start']) || isset($data['limit'])) {
57-
if ($data['start'] < 0) {
58-
$data['start'] = 0;
59-
}
60-
61-
if ($data['limit'] < 1) {
62-
$data['limit'] = 20;
63-
}
64-
65-
$sql .= " LIMIT " . (int) $data['start'] . "," . (int) $data['limit'];
66-
}
67-
68-
$results = $this->db->fetchAll($sql);
69-
70-
return $results;
71-
}
72-
73-
public function getTotalCategories($data = array())
74-
{
75-
$sql = "SELECT count(*) as total
76-
FROM `".$this->db->getTableName('magefan_blog_category')."` c
77-
left join `".$this->db->getTableName('magefan_blog_category_store')."` cs on c.category_id = cs.category_id
78-
where c.is_active = '1'";
79-
80-
$implode = array();
81-
82-
if (isset($data['filter_parent_id'])) {
83-
$implode[] = "IFNULL(SUBSTRING_INDEX(c.path, '/', -1), 0) = '" . (int) $data['filter_parent_id'] . "'";
84-
}
57+
$sort_data = array(
58+
'id' => 'category_id',
59+
'name' => 'title',
60+
'sort_order' => 'position'
61+
);
8562

86-
if (count($implode) > 0) {
87-
$sql .= ' AND ' . implode(' AND ', $implode);
63+
if (isset($data['sort']) && in_array($data['sort'], array_keys($sort_data))) {
64+
$sort = $sort_data[$data['sort']];
65+
} else {
66+
$sort = "category_id";
8867
}
8968

90-
$result = $this->db->fetchOne($sql);
91-
92-
return $result['total'];
93-
}
94-
95-
public function getCategoryByPostId($post_id)
96-
{
97-
$sql = "SELECT c.category_id
98-
FROM `".$this->db->getTableName('magefan_blog_category')."` c
99-
left join `".$this->db->getTableName('magefan_blog_category_store')."` cs on c.category_id = cs.category_id
100-
left join `".$this->db->getTableName('magefan_blog_post_category')."` pc on pc.category_id = c.category_id
101-
where c.is_active = '1' AND pc.post_id = '".$post_id."'";
69+
$collection->setOrder($sort, $order);
10270

103-
$results = $this->db->fetchAll($sql);
71+
$collection->load();
10472

105-
return $results;
73+
return $collection;
10674
}
10775
}
Lines changed: 51 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,91 @@
11
<?php
22

3-
require_once VF_SYSTEM_DIR.'engine/model.php';
3+
use Magento\Framework\App\ObjectManager;
44

5+
require_once VF_SYSTEM_DIR . 'engine/model.php';
6+
7+
/**
8+
* @property \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $_collectionFactory
9+
* @property Magefan\Blog\Model\PostFactory $_postFactory
10+
*/
511
class ModelBlogPost extends Model
612
{
7-
public function getPost($post_id)
8-
{
9-
$sql = "SELECT p.*
10-
FROM `".$this->db->getTableName('magefan_blog_post')."` p
11-
WHERE p.post_id = '".$post_id."'";
13+
private $_collectionFactory;
14+
private $_postFactory;
1215

13-
$result = $this->db->fetchOne($sql);
16+
public function __construct($registry)
17+
{
18+
parent::__construct($registry);
1419

15-
return $result;
20+
$objectManager = ObjectManager::getInstance();
21+
$this->_collectionFactory = $objectManager->get('\Magefan\Blog\Model\ResourceModel\Post\CollectionFactory');
22+
$this->_postFactory = $objectManager->get('Magefan\Blog\Model\PostFactory');
23+
$this->_categoryFactory = $objectManager->get('Magefan\Blog\Model\CategoryFactory');
1624
}
1725

18-
public function getPosts($data = array())
26+
public function getPost($post_id)
1927
{
20-
$sql = "SELECT p.post_id as ID
21-
FROM `".$this->db->getTableName('magefan_blog_post')."` p";
28+
return $this->_postFactory->create()->load($post_id);
29+
}
2230

23-
$implode = array();
31+
public function getPosts($data)
32+
{
33+
/** @var $collection \Magefan\Blog\Model\ResourceModel\Post\Collection */
34+
$collection = $this->_collectionFactory->create();
2435

25-
if (!empty($data['filter_category_id'])) {
26-
$implode[] = "'" . (int) $data['filter_category_id'] . "' IN (
27-
SELECT pc.category_id
28-
FROM `".$this->db->getTableName('magefan_blog_post_category')."` pc
29-
WHERE pc.post_id = p.post_id
30-
)";
36+
if ($data['category_id'] != 0) {
37+
$collection->addCategoryFilter($this->_categoryFactory->create()->load($data['category_id']));
3138
}
3239

33-
if (count($implode) > 0) {
34-
$sql .= ' WHERE ' . implode(' AND ', $implode);
40+
if (isset($data['order']) && ($data['order'] == 'DESC')) {
41+
$order = "DESC";
42+
} else {
43+
$order = "ASC";
3544
}
3645

37-
$sql .= " GROUP BY ID";
38-
3946
$sort_data = array(
40-
'ID'
47+
'id' => 'post_id',
48+
'sort_order' => 'position'
4149
);
4250

43-
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
44-
$sql .= " ORDER BY " . $data['sort'];
45-
} else {
46-
$sql .= " ORDER BY ID";
47-
}
48-
49-
if (isset($data['order']) && ($data['order'] == 'DESC')) {
50-
$sql .= " DESC";
51+
if (isset($data['sort']) && in_array($data['sort'], array_keys($sort_data))) {
52+
$sort = $sort_data[$data['sort']];
5153
} else {
52-
$sql .= " ASC";
54+
$sort = "post_id";
5355
}
5456

55-
if (isset($data['start']) || isset($data['limit'])) {
56-
if ($data['start'] < 0) {
57-
$data['start'] = 0;
58-
}
57+
$collection->setOrder($sort, $order);
5958

60-
if ($data['limit'] < 1) {
61-
$data['limit'] = 20;
62-
}
59+
$collection->load();
6360

64-
$sql .= " LIMIT " . (int) $data['start'] . "," . (int) $data['limit'];
65-
}
66-
67-
$results = $this->db->fetchAll($sql);
68-
69-
return $results;
61+
return $collection;
7062
}
7163

72-
public function getTotalPosts($data = array())
64+
public function getNextPost($post_id)
7365
{
74-
$sql = "SELECT count(*) as total
75-
FROM `".$this->db->getTableName('magefan_blog_post')."` p";
66+
/** @var $collection \Magefan\Blog\Model\ResourceModel\Post\Collection */
67+
$collection = $this->_collectionFactory->create();
7668

77-
$implode = array();
69+
$collection->addFieldToFilter('post_id', ['gt' => $post_id]);
7870

79-
if (!empty($data['filter_category_id'])) {
80-
$implode[] = "'" . (int) $data['filter_category_id'] . "' IN (
81-
SELECT pc.category_id
82-
FROM `".$this->db->getTableName('magefan_blog_post_category')."` pc
83-
WHERE pc.post_id = p.post_id
84-
)";
85-
}
86-
87-
if (count($implode) > 0) {
88-
$sql .= ' WHERE ' . implode(' AND ', $implode);
89-
}
71+
$collection->setOrder('publish_time', 'ASC');
9072

91-
$result = $this->db->fetchOne($sql);
73+
$collection->load();
9274

93-
return $result['total'];
75+
return $collection;
9476
}
9577

96-
public function getNextPost($post_id)
78+
public function getPrevPost($post_id)
9779
{
98-
$sql = "SELECT p.*
99-
FROM `".$this->db->getTableName('magefan_blog_post')."` p
100-
WHERE p.post_id > '".$post_id."'
101-
ORDER BY p.publish_time ASC";
102-
103-
$result = $this->db->fetchOne($sql);
80+
/** @var $collection \Magefan\Blog\Model\ResourceModel\Post\Collection */
81+
$collection = $this->_collectionFactory->create();
10482

105-
return $result;
106-
}
83+
$collection->addFieldToFilter('post_id', ['lt' => $post_id]);
10784

108-
public function getPrevPost($post_id)
109-
{
110-
$sql = "SELECT p.*
111-
FROM `".$this->db->getTableName('magefan_blog_post')."` p
112-
WHERE p.post_id < '".$post_id."'
113-
ORDER BY p.post_id DESC";
85+
$collection->setOrder('post_id', 'DESC');
11486

115-
$result = $this->db->fetchOne($sql);
87+
$collection->load();
11688

117-
return $result;
89+
return $collection;
11890
}
11991
}

Vuefront/Vuefront/ApiGraphql/model/blog/review.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)