-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathTagManagement.php
101 lines (87 loc) · 2.42 KB
/
TagManagement.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
<?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!
*/
declare(strict_types=1);
namespace Magefan\Blog\Model;
/**
* Tag management model
*/
class TagManagement extends AbstractManagement
{
/**
* @var \Magefan\Blog\Model\TagFactory
*/
protected $_itemFactory;
/**
* Initialize dependencies.
*
* @param \Magefan\Blog\Model\TagFactory $tagFactory
*/
public function __construct(
\Magefan\Blog\Model\TagFactory $tagFactory
) {
$this->_itemFactory = $tagFactory;
}
/**
* Retrieve list of tag by page type, term, store, etc
*
* @param string $type
* @param string $term
* @param int $storeId
* @param int $page
* @param int $limit
* @return string
*/
public function getList($type, $term, $storeId, $page, $limit)
{
try {
$collection = $this->_itemFactory->create()->getCollection();
$collection
->addActiveFilter()
->addStoreFilter($storeId)
->setCurPage($page)
->setPageSize($limit);
$type = strtolower($type);
switch ($type) {
case 'search':
$collection->addSearchFilter($term);
break;
}
$tags = [];
foreach ($collection as $item) {
$tags[] = $this->getDynamicData($item);
}
$result = [
'tags' => $tags,
'total_number' => $collection->getSize(),
'current_page' => $collection->getCurPage(),
'last_page' => $collection->getLastPageNumber(),
];
return json_encode($result);
} catch (\Exception $e) {
return $this->getError($e->getMessage());
}
}
/**
* @param $item
* @return array
*/
protected function getDynamicData($item)
{
$data = $item->getData();
$keys = [
'meta_description',
'meta_title',
'tag_url',
];
foreach ($keys as $key) {
$method = 'get' . str_replace('_', '', ucwords($key, '_'));
$data[$key] = $item->$method();
}
return $data;
}
}