Skip to content

Commit e7b0df5

Browse files
author
Yuriy
committed
12780-blog-api-expension
1 parent 2b7daa3 commit e7b0df5

12 files changed

+587
-3
lines changed

Diff for: Api/CommentManagementInterface.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Magefan\Blog\Api;
12+
13+
interface CommentManagementInterface extends ManagementInterface
14+
{
15+
16+
}

Diff for: Api/TagManagementInterface.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Magefan\Blog\Api;
12+
13+
interface TagManagementInterface extends ManagementInterface
14+
{
15+
16+
}

Diff for: Api/VersionInterface.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
declare(strict_types=1);
9+
10+
namespace Magefan\Blog\Api;
11+
12+
interface VersionInterface
13+
{
14+
/**
15+
* get blog version and edition
16+
*
17+
* @api
18+
* @return string
19+
*/
20+
public function getVersion(): string;
21+
}

Diff for: Model/AbstractManagement.php

+76-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,67 @@
1616
abstract class AbstractManagement implements ManagementInterface
1717
{
1818
/**
19-
* @var Magento\Framework\Model\AbstractModel
19+
* @var \Magento\Framework\Model\AbstractModel
2020
*/
2121
protected $_itemFactory;
2222

23+
protected $_imagePath = 'magefan_blog/';
24+
25+
protected $_imagesMap = [
26+
'featured_img',
27+
'featured_list_img',
28+
'category_img',
29+
'tag_img'
30+
];
31+
32+
/**
33+
* Saves a file with unique name if necessary.
34+
*
35+
* @param string $fileName Desired file name (without path)
36+
* @param string $fileContent Content of the file
37+
* @return string Saved file name with short path
38+
*/
39+
public function saveFile($fileName, $fileContent)
40+
{
41+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
42+
43+
$file = $objectManager->get(\Magento\Framework\Filesystem\Driver\File::class);
44+
$directoryList = $objectManager->get(\Magento\Framework\Filesystem\DirectoryList::class);
45+
46+
$targetDirectory = $directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA) . '/' . $this->_imagePath;
47+
$file->createDirectory($targetDirectory);
48+
49+
$finalFileName = $this->getUniqueFileName($targetDirectory, $fileName, $file);
50+
$finalFilePath = $targetDirectory . $finalFileName;
51+
52+
$file->filePutContents($finalFilePath, base64_decode($fileContent));
53+
54+
return $this->_imagePath . $finalFileName;
55+
}
56+
57+
/**
58+
* Generates a unique file name if the file already exists.
59+
*
60+
* @param $directory
61+
* @param $fileName
62+
* @param $fileDriver
63+
* @return string
64+
*/
65+
protected function getUniqueFileName($directory, $fileName, $fileDriver)
66+
{
67+
$name = pathinfo($fileName, PATHINFO_FILENAME);
68+
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
69+
$counter = 1;
70+
71+
$newFileName = $fileName;
72+
while ($fileDriver->isExists($directory . $newFileName)) {
73+
$newFileName = $name . '_' . $counter . '.' . $extension;
74+
$counter++;
75+
}
76+
77+
return $newFileName;
78+
}
79+
2380
/**
2481
* Create new item using data
2582
*
@@ -30,6 +87,15 @@ public function create($data)
3087
{
3188
try {
3289
$data = json_decode($data, true);
90+
foreach ($this->_imagesMap as $key) {
91+
if (empty($data[$key . '_name']) || empty($data[$key . '_content'])) {
92+
unset($data[$key . '_name']);
93+
unset($data[$key . '_content']);
94+
continue;
95+
}
96+
$data[$key] = $this->saveFile($data[$key . '_name'], $data[$key . '_content']);
97+
}
98+
3399
$item = $this->_itemFactory->create();
34100
$item->setData($data)->save();
35101
return json_encode($item->getData());
@@ -55,6 +121,15 @@ public function update($id, $data)
55121
return false;
56122
}
57123
$data = json_decode($data, true);
124+
foreach ($this->_imagesMap as $key) {
125+
if (empty($data[$key . '_name']) || empty($data[$key . '_content'])) {
126+
unset($data[$key . '_name']);
127+
unset($data[$key . '_content']);
128+
continue;
129+
}
130+
$data[$key] = $this->saveFile($data[$key . '_name'], $data[$key . '_content']);
131+
}
132+
58133
$item->addData($data)->save();
59134
return json_encode($item->getData());
60135
} catch (\Exception $e) {

Diff for: Model/Comment.php

+10
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ public function isActive()
148148
return ($this->getStatus() == \Magefan\Blog\Model\Config\Source\CommentStatus::APPROVED);
149149
}
150150

151+
/**
152+
* Retrieve if is visible on store
153+
* @return bool
154+
*/
155+
public function isVisibleOnStore($storeId)
156+
{
157+
return $this->isActive()
158+
&& (null === $storeId || array_intersect([0, $storeId], [$this->getStoreId()]));
159+
}
160+
151161
/**
152162
* Retrieve post
153163
* @return \Magefan\Blog\Model\Post | false

Diff for: Model/CommentManagement.php

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Magefan\Blog\Model;
12+
13+
/**
14+
* Comment management model
15+
*/
16+
class CommentManagement extends AbstractManagement
17+
{
18+
/**
19+
* @var \Magefan\Blog\Model\CommentFactory
20+
*/
21+
protected $_itemFactory;
22+
23+
/**
24+
* Initialize dependencies.
25+
*
26+
* @param \Magefan\Blog\Model\CommentFactory $commentFactory
27+
*/
28+
public function __construct(
29+
\Magefan\Blog\Model\CommentFactory $commentFactory
30+
) {
31+
$this->_itemFactory = $commentFactory;
32+
}
33+
34+
/**
35+
* Retrieve list of tag by page type, term, store, etc
36+
*
37+
* @param string $type
38+
* @param string $postId
39+
* @param int $storeId
40+
* @param int $page
41+
* @param int $limit
42+
* @return string
43+
*/
44+
public function getList($type, $term, $storeId, $page, $limit)
45+
{
46+
try {
47+
$collection = $this->_itemFactory->create()->getCollection();
48+
$collection
49+
->addActiveFilter()
50+
->addStoreFilter($storeId)
51+
->setCurPage($page)
52+
->setPageSize($limit);
53+
54+
$type = strtolower($type);
55+
56+
switch ($type) {
57+
case 'list':
58+
$collection->addPostFilter($term);
59+
break;
60+
}
61+
62+
$comments = [];
63+
foreach ($collection as $item) {
64+
$comments[] = $this->getDynamicData($item);
65+
}
66+
67+
$result = [
68+
'comments' => $comments,
69+
'total_number' => $collection->getSize(),
70+
'current_page' => $collection->getCurPage(),
71+
'last_page' => $collection->getLastPageNumber(),
72+
];
73+
74+
return json_encode($result);
75+
} catch (\Exception $e) {
76+
return false;
77+
}
78+
}
79+
80+
public function getDynamicData($item)
81+
{
82+
$data = $item->getData();
83+
$fields = ['replies' => 1];
84+
if (is_array($fields) && array_key_exists('replies', $fields)) {
85+
$replies = [];
86+
foreach ($item->getRepliesCollection() as $reply) {
87+
$replier = $reply->getDynamicData(
88+
isset($fields['replies']) ? $fields['replies'] : null
89+
);
90+
if ($replier && !empty(debug_backtrace()[1]['function']) && (debug_backtrace()[1]['function'] == 'view' || debug_backtrace()[1]['function'] == 'getList')) {
91+
unset($replier['author_email']);
92+
}
93+
$replies[] = $replier;
94+
}
95+
$data['replies'] = $replies;
96+
}
97+
if (!empty(debug_backtrace()[1]['function']) && (debug_backtrace()[1]['function'] == 'view' || debug_backtrace()[1]['function'] == 'getList')) {
98+
unset($data['author_email']);
99+
}
100+
101+
return $data;
102+
}
103+
}

Diff for: Model/ResourceModel/Comment/Collection.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class Collection extends AbstractCollection
1818
*/
1919
protected $_idFieldName = 'comment_id';
2020

21+
/**
22+
* @var int
23+
*/
24+
protected $_storeId;
25+
2126
/**
2227
* Constructor
2328
* Configures collection
@@ -155,7 +160,7 @@ public function addPostFilter($post)
155160
}
156161
}
157162

158-
$this->addFilter('post_id', ['in' => $post], 'public');
163+
$this->addFilter('main_table.post_id', ['in' => $post], 'public');
159164
$this->setFlag('post_filter_added', 1);
160165
}
161166
return $this;

Diff for: Model/ResourceModel/Tag/Collection.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,24 @@ public function addFieldToFilter($field, $condition = null)
5454
$condition = isset($condition[0]) ? $condition[0] : $condition;
5555
}
5656
}
57-
57+
5858
if ($field === 'store_id' || $field === 'store_ids') {
5959
return $this->addStoreFilter($condition);
6060
}
6161

6262
return parent::addFieldToFilter($field, $condition);
6363
}
6464

65+
/**
66+
* Add search filter to collection
67+
* @param string $term
68+
* @return $this
69+
*/
70+
public function addSearchFilter(string $term)
71+
{
72+
return $this->addFieldToFilter('title', ['like' => '%' . $term . '%']);
73+
}
74+
6575
/**
6676
* Add store filter to collection
6777
* @param array|int|\Magento\Store\Model\Store $store

0 commit comments

Comments
 (0)