|
1 | 1 | <?php |
2 | 2 |
|
3 | | -require_once VF_SYSTEM_DIR.'engine/model.php'; |
| 3 | +use Magento\Framework\App\ObjectManager; |
4 | 4 |
|
| 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 | + */ |
5 | 11 | class ModelBlogPost extends Model |
6 | 12 | { |
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; |
12 | 15 |
|
13 | | - $result = $this->db->fetchOne($sql); |
| 16 | + public function __construct($registry) |
| 17 | + { |
| 18 | + parent::__construct($registry); |
14 | 19 |
|
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'); |
16 | 24 | } |
17 | 25 |
|
18 | | - public function getPosts($data = array()) |
| 26 | + public function getPost($post_id) |
19 | 27 | { |
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 | + } |
22 | 30 |
|
23 | | - $implode = array(); |
| 31 | + public function getPosts($data) |
| 32 | + { |
| 33 | + /** @var $collection \Magefan\Blog\Model\ResourceModel\Post\Collection */ |
| 34 | + $collection = $this->_collectionFactory->create(); |
24 | 35 |
|
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'])); |
31 | 38 | } |
32 | 39 |
|
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"; |
35 | 44 | } |
36 | 45 |
|
37 | | - $sql .= " GROUP BY ID"; |
38 | | - |
39 | 46 | $sort_data = array( |
40 | | - 'ID' |
| 47 | + 'id' => 'post_id', |
| 48 | + 'sort_order' => 'position' |
41 | 49 | ); |
42 | 50 |
|
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']]; |
51 | 53 | } else { |
52 | | - $sql .= " ASC"; |
| 54 | + $sort = "post_id"; |
53 | 55 | } |
54 | 56 |
|
55 | | - if (isset($data['start']) || isset($data['limit'])) { |
56 | | - if ($data['start'] < 0) { |
57 | | - $data['start'] = 0; |
58 | | - } |
| 57 | + $collection->setOrder($sort, $order); |
59 | 58 |
|
60 | | - if ($data['limit'] < 1) { |
61 | | - $data['limit'] = 20; |
62 | | - } |
| 59 | + $collection->load(); |
63 | 60 |
|
64 | | - $sql .= " LIMIT " . (int) $data['start'] . "," . (int) $data['limit']; |
65 | | - } |
66 | | - |
67 | | - $results = $this->db->fetchAll($sql); |
68 | | - |
69 | | - return $results; |
| 61 | + return $collection; |
70 | 62 | } |
71 | 63 |
|
72 | | - public function getTotalPosts($data = array()) |
| 64 | + public function getNextPost($post_id) |
73 | 65 | { |
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(); |
76 | 68 |
|
77 | | - $implode = array(); |
| 69 | + $collection->addFieldToFilter('post_id', ['gt' => $post_id]); |
78 | 70 |
|
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'); |
90 | 72 |
|
91 | | - $result = $this->db->fetchOne($sql); |
| 73 | + $collection->load(); |
92 | 74 |
|
93 | | - return $result['total']; |
| 75 | + return $collection; |
94 | 76 | } |
95 | 77 |
|
96 | | - public function getNextPost($post_id) |
| 78 | + public function getPrevPost($post_id) |
97 | 79 | { |
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(); |
104 | 82 |
|
105 | | - return $result; |
106 | | - } |
| 83 | + $collection->addFieldToFilter('post_id', ['lt' => $post_id]); |
107 | 84 |
|
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'); |
114 | 86 |
|
115 | | - $result = $this->db->fetchOne($sql); |
| 87 | + $collection->load(); |
116 | 88 |
|
117 | | - return $result; |
| 89 | + return $collection; |
118 | 90 | } |
119 | 91 | } |
0 commit comments