Skip to content

Commit c2d2b87

Browse files
dev4dev4
dev4
authored and
dev4
committed
2197-new-release 1 [Implemented]
1 parent 3a12d09 commit c2d2b87

File tree

6 files changed

+60
-43
lines changed

6 files changed

+60
-43
lines changed

Api/AuthorRepositoryInterface.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@
88

99
namespace Magefan\Blog\Api;
1010

11-
use Magefan\Blog\Model\Author;
12-
1311
/**
1412
* Interface AuthorRepositoryInterface
1513
* @package Magefan\Blog\Api
1614
*/
1715
interface AuthorRepositoryInterface
1816
{
1917
/**
20-
* @param Author $author
18+
* @param AuthorInterface $author
2119
* @return mixed
2220
*/
23-
public function save(Author $author);
21+
public function save(AuthorInterface $author);
2422

2523
/**
2624
* @param $authorId
@@ -39,10 +37,10 @@ public function getById($authorId);
3937
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
4038

4139
/**
42-
* @param Author $author
40+
* @param AuthorInterface $author
4341
* @return mixed
4442
*/
45-
public function delete(Author $author);
43+
public function delete(AuthorInterface $author);
4644

4745
/**
4846
* Delete Author by ID.

Block/Adminhtml/Grid/Column/Filter/Author.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@
1414
class Author extends \Magento\Backend\Block\Widget\Grid\Column\Filter\Select
1515
{
1616
/**
17-
* @var \Magefan\Blog\Model\ResourceModel\Author\CollectionFactory
17+
* @var \Magefan\Blog\Api\AuthorCollectionInterfaceFactory
1818
*/
1919
protected $collectionFactory;
2020

2121
/**
22+
* Author constructor.
2223
* @param \Magento\Backend\Block\Context $context
2324
* @param \Magento\Framework\DB\Helper $resourceHelper
24-
* @param \Magefan\Blog\Model\ResourceModel\Author\CollectionFactory $collectionFactory
25+
* @param \Magefan\Blog\Api\AuthorCollectionInterfaceFactory $collectionFactory
2526
* @param array $data
2627
*/
2728
public function __construct(
2829
\Magento\Backend\Block\Context $context,
2930
\Magento\Framework\DB\Helper $resourceHelper,
30-
\Magefan\Blog\Model\ResourceModel\Author\CollectionFactory $collectionFactory,
31+
\Magefan\Blog\Api\AuthorCollectionInterfaceFactory $collectionFactory,
3132
array $data = []
3233
) {
3334
$this->collectionFactory = $collectionFactory;

Block/Adminhtml/Grid/Column/Render/Author.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@
1414
class Author extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
1515
{
1616
/**
17-
* @var \Magefan\Blog\Model\AuthorFactory
17+
* @var \Magefan\Blog\Api\AuthorInterfaceFactory
1818
*/
19-
protected $authoryFactory;
19+
protected $authorFactory;
2020

2121
/**
2222
* @var array
2323
*/
2424
static protected $authors = [];
2525

2626
/**
27+
* Author constructor.
2728
* @param \Magento\Backend\Block\Context $context
28-
* @param \Magefan\Blog\Model\AuthorFactory $localeLists
29+
* @param \Magefan\Blog\Api\AuthorInterfaceFactory $authorFactory
2930
* @param array $data
3031
*/
3132
public function __construct(
3233
\Magento\Backend\Block\Context $context,
33-
\Magefan\Blog\Model\AuthorFactory $authorFactory,
34+
\Magefan\Blog\Api\AuthorInterfaceFactory $authorFactory,
3435
array $data = []
3536
) {
3637
parent::__construct($context, $data);

Model/AuthorRepository.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
namespace Magefan\Blog\Model;
77

88
use Magefan\Blog\Api\AuthorRepositoryInterface;
9-
use Magefan\Blog\Model\AuthorFactory;
9+
use Magefan\Blog\Api\AuthorInterface;
1010
use Magefan\Blog\Model\ResourceModel\Author as AuthorResourceModel;
11-
use Magefan\Blog\Model\ResourceModel\Author\CollectionFactory;
11+
use Magefan\Blog\Api\AuthorCollectionInterfaceFactory;
1212
use Magento\Framework\Api\SearchResultsFactory;
1313
use Magento\Framework\Api\SearchCriteriaInterface;
1414
use Magento\Framework\DB\Adapter\ConnectionException;
@@ -25,15 +25,15 @@
2525
class AuthorRepository implements AuthorRepositoryInterface
2626
{
2727
/**
28-
* @var AuthorFactory
28+
* @var AuthorInterface
2929
*/
3030
private $authorFactory;
3131
/**
3232
* @var AuthorResourceModel
3333
*/
3434
private $authorResourceModel;
3535
/**
36-
* @var CollectionFactory
36+
* @var AuthorCollectionInterfaceFactory
3737
*/
3838
private $collectionFactory;
3939
/**
@@ -43,15 +43,15 @@ class AuthorRepository implements AuthorRepositoryInterface
4343

4444
/**
4545
* AuthorRepository constructor.
46-
* @param \Magefan\Blog\Model\AuthorFactory $authorFactory
46+
* @param AuthorInterface $authorFactory
4747
* @param AuthorResourceModel $authorResourceModel
48-
* @param CollectionFactory $collectionFactory
48+
* @param AuthorCollectionInterfaceFactory $collectionFactory
4949
* @param SearchResultsFactory $searchResultsFactory
5050
*/
5151
public function __construct(
52-
AuthorFactory $authorFactory,
52+
AuthorInterface $authorFactory,
5353
AuthorResourceModel $authorResourceModel,
54-
CollectionFactory $collectionFactory,
54+
AuthorCollectionInterfaceFactory $collectionFactory,
5555
SearchResultsFactory $searchResultsFactory
5656
) {
5757
$this->authorFactory = $authorFactory;
@@ -61,13 +61,13 @@ public function __construct(
6161
}
6262

6363
/**
64-
* @param Author $author
65-
* @return bool|mixed
64+
* @param AuthorInterface $author
65+
* @return bool|Author|mixed
6666
* @throws CouldNotSaveException
6767
* @throws NoSuchEntityException
6868
* @throws \Magento\Framework\Exception\AlreadyExistsException
6969
*/
70-
public function save(Author $author)
70+
public function save(AuthorInterface $author)
7171
{
7272
if ($author) {
7373
try {
@@ -107,12 +107,12 @@ public function getById($authorId, $editMode = false, $storeId = null, $forceRel
107107
}
108108

109109
/**
110-
* @param Author $author
110+
* @param AuthorInterface $author
111111
* @return bool|mixed
112112
* @throws CouldNotDeleteException
113113
* @throws StateException
114114
*/
115-
public function delete(Author $author)
115+
public function delete(AuthorInterface $author)
116116
{
117117
try {
118118
$this->authorResourceModel->delete($author);

Model/Comment.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
class Comment extends AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
4242
{
4343
/**
44-
* @var \Magefan\Blog\Model\AuthorFactory
44+
* @var PostFactory
4545
*/
4646
protected $postFactory;
4747

@@ -51,7 +51,7 @@ class Comment extends AbstractModel implements \Magento\Framework\DataObject\Ide
5151
protected $customerFactory;
5252

5353
/**
54-
* @var \Magento\User\Model\UserFactory
54+
* @var \Magefan\Blog\Api\AuthorInterfaceFactory
5555
*/
5656
protected $userFactory;
5757

@@ -81,7 +81,7 @@ class Comment extends AbstractModel implements \Magento\Framework\DataObject\Ide
8181
* @param \Magento\Framework\Registry $registry
8282
* @param \Magefan\Blog\Model\PostFactory $postFactory
8383
* @param \Magento\Customer\Model\CustomerFactory $customerFactory,
84-
* @param \Magento\User\Model\Useractory $userFactory,
84+
* @param \Magefan\Blog\Api\AuthorInterfaceFactory $userFactory,
8585
* @param \Magefan\Blog\Model\ResourceModel\Comment\CollectionFactory $commentCollectionFactory
8686
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
8787
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
@@ -92,7 +92,7 @@ public function __construct(
9292
\Magento\Framework\Registry $registry,
9393
\Magefan\Blog\Model\PostFactory $postFactory,
9494
\Magento\Customer\Model\CustomerFactory $customerFactory,
95-
\Magento\User\Model\UserFactory $userFactory,
95+
\Magefan\Blog\Api\AuthorInterfaceFactory $userFactory,
9696
\Magefan\Blog\Model\ResourceModel\Comment\CollectionFactory $commentCollectionFactory,
9797
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
9898
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
@@ -200,7 +200,7 @@ public function getAuthor()
200200
$admin = $this->userFactory->create();
201201
$admin->load($this->getAdminId());
202202
$this->author->setData([
203-
'nickname' => $customer->getName(),
203+
'nickname' => $admin->getName(),
204204
'email' => $this->getEmail(),
205205
'admin' => $admin,
206206
]);

Model/ResourceModel/Post.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,6 @@ protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
173173
}
174174
}
175175

176-
/* Save tags links */
177-
$newIds = (array)$object->getTags();
178-
foreach ($newIds as $key => $id) {
179-
if (!$id) { // e.g.: zero
180-
unset($newIds[$key]);
181-
}
182-
}
183-
if (is_array($newIds)) {
184-
$oldIds = $this->lookupTagIds($object->getId());
185-
$this->_updateLinks($object, $newIds, $oldIds, 'magefan_blog_post_tag', 'tag_id');
186-
}
187-
188176
/* Save related post & product links */
189177
if ($links = $object->getData('links')) {
190178
if (is_array($links)) {
@@ -229,6 +217,13 @@ protected function _updateLinks(
229217
) {
230218
$table = $this->getTable($tableName);
231219

220+
if ($object->getId() && empty($rowData)) {
221+
$currentData = $this->_lookupAll($object->getId(), $tableName, '*');
222+
foreach ($currentData as $item) {
223+
$rowData[$item[$field]] = $item;
224+
}
225+
}
226+
232227
$insert = $newRelatedIds;
233228
$delete = $oldRelatedIds;
234229

@@ -437,4 +432,26 @@ protected function _lookupIds($postId, $tableName, $field)
437432

438433
return $adapter->fetchCol($select);
439434
}
435+
436+
/**
437+
* Get ids to which specified item is assigned
438+
* @param int $postId
439+
* @param string $tableName
440+
* @param string $field
441+
* @return array
442+
*/
443+
protected function _lookupAll($postId, $tableName, $field)
444+
{
445+
$adapter = $this->getConnection();
446+
447+
$select = $adapter->select()->from(
448+
$this->getTable($tableName),
449+
$field
450+
)->where(
451+
'post_id = ?',
452+
(int)$postId
453+
);
454+
455+
return $adapter->fetchAll($select);
456+
}
440457
}

0 commit comments

Comments
 (0)