/
AbstractList.php
executable file
·259 lines (229 loc) · 6.68 KB
/
AbstractList.php
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
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/
namespace Magefan\Blog\Block\Post\PostList;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Api\SortOrder;
use \Magento\Framework\View\Element\Template;
use \Magento\Framework\DataObject\IdentityInterface;
/**
* Abstract blog post list block
*/
abstract class AbstractList extends Template implements IdentityInterface
{
/**
* @var \Magento\Cms\Model\Template\FilterProvider
*/
protected $_filterProvider;
/**
* @var \Magento\Cms\Model\Page
*/
protected $_post;
/**
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry;
/**
* @var \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory
*/
protected $_postCollectionFactory;
/**
* @var \Magefan\Blog\Model\ResourceModel\Post\Collection
*/
protected $_postCollection;
/**
* @var \Magefan\Blog\Model\Url
*/
protected $_url;
/**
* @var \Magefan\Blog\Model\Config
*/
protected $config;
/**
* @var \Magefan\Blog\Model\TemplatePool
*/
protected $templatePool;
const POSTS_SORT_FIELD_BY_PUBLISH_TIME = 'publish_time';
const POSTS_SORT_FIELD_BY_POSITION = 'position';
const POSTS_SORT_FIELD_BY_TITLE = 'title';
/**
* AbstractList constructor.
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
* @param \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory
* @param \Magefan\Blog\Model\Url $url
* @param array $data
* @param null $config
* @param null $templatePool
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Cms\Model\Template\FilterProvider $filterProvider,
\Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory,
\Magefan\Blog\Model\Url $url,
array $data = [],
$config = null,
$templatePool = null
) {
parent::__construct($context, $data);
$this->_coreRegistry = $coreRegistry;
$this->_filterProvider = $filterProvider;
$this->_postCollectionFactory = $postCollectionFactory;
$this->_url = $url;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->config = $config ?: $objectManager->get(
\Magefan\Blog\Model\Config::class
);
$this->templatePool = $templatePool ?: $objectManager->get(
\Magefan\Blog\Model\TemplatePool::class
);
}
/**
* Prepare posts collection
*
* @return void
*/
protected function _preparePostCollection()
{
$this->_postCollection = $this->_postCollectionFactory->create()
->addActiveFilter()
->addStoreFilter($this->_storeManager->getStore()->getId())
->setOrder($this->getCollectionOrderField(), $this->getCollectionOrderDirection());
if ($this->getPageSize()) {
$this->_postCollection->setPageSize($this->getPageSize());
}
}
/**
* Retrieve collection order field
*
* @return string
*/
public function getCollectionOrderField()
{
return self::POSTS_SORT_FIELD_BY_PUBLISH_TIME;
}
/**
* Retrieve collection order direction
*
* @return string
*/
public function getCollectionOrderDirection()
{
return SortOrder::SORT_DESC;
}
/**
* Prepare posts collection
*
* @return \Magefan\Blog\Model\ResourceModel\Post\Collection
*/
public function getPostCollection()
{
if (null === $this->_postCollection) {
$this->_preparePostCollection();
}
return $this->_postCollection;
}
/**
* Render block HTML
*
* @return string
*/
protected function _toHtml()
{
if (!$this->_scopeConfig->getValue(
\Magefan\Blog\Model\Config::XML_PATH_EXTENSION_ENABLED,
ScopeInterface::SCOPE_STORE
)) {
return '';
}
return parent::_toHtml();
}
/**
* Retrieve identities
*
* @return array
*/
public function getIdentities()
{
$identities = [];
$identities[] = \Magefan\Blog\Model\Post::CACHE_TAG . '_' . 0;
foreach ($this->getPostCollection() as $item) {
$identities = array_merge($identities, $item->getIdentities());
}
return array_unique($identities);
}
/**
* Get cache key informative items
*
* @return array
*/
public function getCacheKeyInfo()
{
return array_merge(
parent::getCacheKeyInfo(),
[$this->getNameInLayout()]
);
}
/**
* Retrieve 1 if display author information is enabled
* @return int
*/
public function authorEnabled()
{
return (int) $this->_scopeConfig->getValue(
'mfblog/author/enabled',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
/**
* Retrieve 1 if author page is enabled
* @return int
*/
public function authorPageEnabled()
{
return (int) $this->_scopeConfig->getValue(
'mfblog/author/page_enabled',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
/**
* Retrieve true if magefan comments are enabled
* @return bool
*/
public function magefanCommentsEnabled()
{
return $this->_scopeConfig->getValue(
'mfblog/post_view/comments/type',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) == \Magefan\Blog\Model\Config\Source\CommetType::MAGEFAN;
}
/**
* @return bool
*/
public function viewsCountEnabled()
{
return (bool)$this->_scopeConfig->getValue(
'mfblog/post_view/views_count/enabled',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
/**
* @return \Magefan\Blog\ViewModel\Style
*/
public function getStyleViewModel()
{
$viewModel = $this->getData('style_view_model');
if (!$viewModel) {
$viewModel = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magefan\Blog\ViewModel\Style::class);
$this->setData('style_view_model', $viewModel);
}
return $viewModel;
}
}