Skip to content

Commit d3cb8d5

Browse files
committed
Blog Post & Category Duplicate functionality
1 parent 1571561 commit d3cb8d5

File tree

8 files changed

+151
-5
lines changed

8 files changed

+151
-5
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © 2015-2017 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
namespace Magefan\Blog\Block\Adminhtml\Edit;
9+
10+
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
11+
12+
/**
13+
* Class DuplicateButton
14+
*/
15+
class DuplicateButton extends GenericButton implements ButtonProviderInterface
16+
{
17+
/**
18+
* @return array
19+
*/
20+
public function getButtonData()
21+
{
22+
$data = [];
23+
if ($this->getPostId()) {
24+
$data = [
25+
'label' => __('Duplicate'),
26+
'class' => 'duplicate',
27+
'on_click' => 'window.location=\'' . $this->getDuplicateUrl() . '\'',
28+
'sort_order' => 40,
29+
];
30+
}
31+
return $data;
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function getDuplicateUrl()
38+
{
39+
return $this->getUrl('*/*/duplicate', ['id' => $this->getPostId()]);
40+
}
41+
}

Controller/Adminhtml/Actions.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
3+
* Copyright © 2015-2017 Ihor Vansach ([email protected]). All rights reserved.
44
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
55
*
66
* Glory to Ukraine! Glory to the heroes!
@@ -80,7 +80,7 @@ abstract class Actions extends \Magento\Backend\App\Action
8080
*/
8181
public function execute()
8282
{
83-
$_preparedActions = ['index', 'grid', 'new', 'edit', 'save', 'delete', 'config', 'massStatus'];
83+
$_preparedActions = ['index', 'grid', 'new', 'edit', 'save', 'duplicate', 'delete', 'config', 'massStatus'];
8484
$_action = $this->getRequest()->getActionName();
8585
if (in_array($_action, $_preparedActions)) {
8686
$method = '_'.$_action.'Action';
@@ -246,6 +246,34 @@ public function _saveAction()
246246

247247
}
248248

249+
/**
250+
* Duplicat action
251+
* @return void
252+
*/
253+
protected function _duplicateAction()
254+
{
255+
try {
256+
$originModel = $this->_getModel();
257+
if (!$originModel->getId()) {
258+
throw new \Exception("Item is not longer exist.", 1);
259+
}
260+
261+
$model = $originModel->duplicate();
262+
263+
$this->messageManager->addSuccess(__('%1 has been duplicated.', $model->getOwnTitle()));
264+
$this->_redirect('*/*/edit', [$this->_idKey => $model->getId()]);
265+
} catch (Exception $e) {
266+
$this->messageManager->addException(
267+
$e,
268+
__('Something went wrong while saving this %1. %2',
269+
strtolower($model->getOwnTitle()),
270+
$e->getMessage()
271+
)
272+
);
273+
$this->_redirect('*/*/edit', [$this->_idKey => $originModel->getId()]);
274+
}
275+
}
276+
249277
/**
250278
* Before model Save action
251279
* @return void
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © 2015-2017 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Controller\Adminhtml\Category;
10+
11+
/**
12+
* Blog category duplicate controller
13+
*/
14+
class Duplicate extends \Magefan\Blog\Controller\Adminhtml\Category
15+
{
16+
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © 2015-2017 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Controller\Adminhtml\Post;
10+
11+
/**
12+
* Blog post duplicate controller
13+
*/
14+
class Duplicate extends \Magefan\Blog\Controller\Adminhtml\Post
15+
{
16+
17+
}

Model/Category.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,19 @@ public function initDinamicData()
361361
return $this;
362362
}
363363

364+
/**
365+
* Duplicate category and return new object
366+
* @return self
367+
*/
368+
public function duplicate()
369+
{
370+
$object = clone $this;
371+
$object
372+
->unsetData('category_id')
373+
->setTitle($object->getTitle() . ' (' . __('Duplicated') . ')')
374+
->setData('is_active', 0);
375+
376+
return $object->save();
377+
}
378+
364379
}

Model/Post.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
3+
* Copyright © 2015-2017 Ihor Vansach ([email protected]). All rights reserved.
44
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
55
*
66
* Glory to Ukraine! Glory to the heroes!
@@ -671,4 +671,30 @@ public function initDinamicData()
671671
return $this;
672672
}
673673

674+
/**
675+
* Duplicate post and return new object
676+
* @return self
677+
*/
678+
public function duplicate()
679+
{
680+
$object = clone $this;
681+
$object
682+
->unsetData('post_id')
683+
->setTitle($object->getTitle() . ' (' . __('Duplicated') . ')')
684+
->setData('is_active', 0);
685+
686+
$relatedProductIds = $this->getRelatedProducts()->getAllIds();
687+
$relatedPpostIds = $this->getRelatedPosts()->getAllIds();
688+
689+
$object->setData(
690+
'links',
691+
[
692+
'product' => array_combine($relatedProductIds, $relatedProductIds),
693+
'post' => array_combine($relatedPpostIds, $relatedPpostIds),
694+
]
695+
);
696+
697+
return $object->save();
698+
}
699+
674700
}

view/adminhtml/ui_component/blog_category_form.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
/**
4-
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* Copyright © 2015-2017 Ihor Vansach ([email protected]). All rights reserved.
55
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
66
*
77
* Glory to Ukraine! Glory to the heroes!
@@ -24,6 +24,7 @@
2424
<item name="back" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\BackButton</item>
2525
<item name="delete" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\DeleteButton</item>
2626
<item name="reset" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\ResetButton</item>
27+
<item name="duplicate" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\DuplicateButton</item>
2728
<item name="save" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\SaveButton</item>
2829
<item name="save_and_continue" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\SaveAndContinueButton</item>
2930
</item>

view/adminhtml/ui_component/blog_post_form.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
/**
4-
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* Copyright © 2015-2017 Ihor Vansach ([email protected]). All rights reserved.
55
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
66
*
77
* Glory to Ukraine! Glory to the heroes!
@@ -24,6 +24,7 @@
2424
<item name="back" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\BackButton</item>
2525
<item name="delete" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\DeleteButton</item>
2626
<item name="reset" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\ResetButton</item>
27+
<item name="duplicate" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\DuplicateButton</item>
2728
<item name="save" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\SaveButton</item>
2829
<item name="save_and_continue" xsi:type="string">Magefan\Blog\Block\Adminhtml\Edit\SaveAndContinueButton</item>
2930
</item>

0 commit comments

Comments
 (0)