-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathTag.php
executable file
·292 lines (256 loc) · 6.9 KB
/
Tag.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
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
<?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\Model;
use Magefan\Blog\Model\Url;
use Magefan\Blog\Api\ShortContentExtractorInterface;
/**
* Tag model
*
* @method \Magefan\Blog\Model\ResourceModel\Tag _getResource()
* @method \Magefan\Blog\Model\ResourceModel\Tag getResource()
* @method string getTitle()
* @method $this setTitle(string $value)
* @method $this setIdentifier(string $value)
*/
class Tag extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
{
/**
* Tag Status
*/
const STATUS_ENABLED = 1;
/**
* blog cache tag
*/
const CACHE_TAG = 'mfb_t';
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'magefan_blog_tag';
/**
* Parameter name in event
*
* In observe method you can use $observer->getEvent()->getObject() in this case
*
* @var string
*/
protected $_eventObject = 'blog_tag';
/**
* @var \Magento\Framework\UrlInterface
*/
protected $_url;
/**
* @var string
*/
protected $controllerName;
/**
* @var ShortContentExtractorInterface
*/
protected $shortContentExtractor;
/**
* Initialize dependencies.
*
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magefan\Blog\Model\Url $url
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
Url $url,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->_url = $url;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init(\Magefan\Blog\Model\ResourceModel\Tag::class);
$this->controllerName = URL::CONTROLLER_TAG;
}
/**
* Retrieve true if tag is active
* @return boolean
*/
public function isActive()
{
return ($this->getIsActive() == self::STATUS_ENABLED);
}
/**
* Retrieve if is visible on store
* @return bool
*/
public function isVisibleOnStore($storeId)
{
return $this->getIsActive()
&& (null === $storeId || array_intersect([0, $storeId], $this->getStoreIds()));
}
/**
* Retrieve model title
* @param boolean $plural
* @return string
*/
public function getOwnTitle($plural = false)
{
return $plural ? 'Tags' : 'Tag';
}
/**
* Check if tag identifier exist for specific store
* return tag id if tag exists
*
* @param string $identifier
* @param int $storeId
* @return int
*/
public function checkIdentifier($identifier, $storeId)
{
return $this->_getResource()->checkIdentifier($identifier, $storeId);
}
/**
* Retrieve catgegory url route path
* @return string
*/
public function getUrl()
{
return $this->_url->getUrlPath($this, URL::CONTROLLER_TAG);
}
/**
* Retrieve tag url
* @return string
*/
public function getTagUrl()
{
$url = $this->getData('tag_url');
if (!$url) {
$url = $this->_url->getUrl($this, URL::CONTROLLER_TAG);
$this->setData('tag_url', $url);
}
return $url;
}
/**
* Retrieve meta title
* @return string
*/
public function getMetaTitle()
{
$title = $this->getData('meta_title');
if (!$title) {
$title = $this->getData('title');
}
return trim($title ?: '');
}
/**
* Retrieve meta description
* @return string
*/
public function getMetaDescription()
{
$desc = $this->getData('meta_description');
if (!$desc) {
$desc = $this->getShortContentExtractor()->execute($this->getData('content'), 500);
}
$stylePattern = "~<style\b[^>]*>.*?</style>~is";
$desc = preg_replace($stylePattern, '', $desc);
$desc = trim(strip_tags((string)$desc));
$desc = str_replace(["\r\n", "\n\r", "\r", "\n"], ' ', $desc);
if (mb_strlen($desc) > 160) {
$desc = mb_substr($desc, 0, 160);
$lastSpace = mb_strrpos($desc, ' ');
if ($lastSpace !== false) {
$desc = mb_substr($desc, 0, $lastSpace) . '...';
}
}
return trim($desc);
}
/**
* Retrieve identities
*
* @return array
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}
/**
* Retrieve block identifier
*
* @return string
*/
public function getIdentifier()
{
return (string)$this->getData('identifier');
}
/**
* Retrieve controller name
* @return string
*/
public function getControllerName()
{
return $this->controllerName;
}
/**
* @deprecated use getDynamicData method in graphQL data provider
* Return all additional data
* @return array
*/
public function getDynamicData()
{
$data = $this->getData();
$keys = [
'meta_description',
'meta_title',
'tag_url',
];
foreach ($keys as $key) {
$method = 'get' . str_replace(
'_',
'',
ucwords($key, '_')
);
$data[$key] = $this->$method();
}
return $data;
}
/**
* @return ShortContentExtractorInterface
*/
public function getShortContentExtractor()
{
if (null === $this->shortContentExtractor) {
$this->shortContentExtractor = \Magento\Framework\App\ObjectManager::getInstance()
->get(ShortContentExtractorInterface::class);
}
return $this->shortContentExtractor;
}
/**
* @return array|mixed|null
*/
public function getTagImage()
{
if (!$this->hasData('tag_image')) {
if ($file = $this->getData('tag_img')) {
$image = $this->_url->getMediaUrl($file);
} else {
$image = false;
}
$this->setData('tag_image', $image);
}
return $this->getData('tag_image');
}
}