Skip to content

Commit 692f965

Browse files
authored
Allow Save As Copy Article In The Frontend (#35378)
1 parent 894d743 commit 692f965

File tree

6 files changed

+64
-18
lines changed

6 files changed

+64
-18
lines changed

administrator/components/com_content/src/Model/ArticleModel.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Joomla\CMS\Event\AbstractEvent;
1515
use Joomla\CMS\Factory;
16+
use Joomla\CMS\Filter\InputFilter;
17+
use Joomla\CMS\Filter\OutputFilter;
1618
use Joomla\CMS\Form\Form;
1719
use Joomla\CMS\Form\FormFactoryInterface;
1820
use Joomla\CMS\Helper\TagsHelper;
@@ -684,10 +686,9 @@ public function validate($form, $data, $group = null)
684686
*/
685687
public function save($data)
686688
{
687-
$input = Factory::getApplication()->input;
688-
$filter = \JFilterInput::getInstance();
689-
$db = $this->getDbo();
690-
$user = Factory::getUser();
689+
$app = Factory::getApplication();
690+
$input = $app->input;
691+
$filter = InputFilter::getInstance();
691692

692693
if (isset($data['metadata']) && isset($data['metadata']['author']))
693694
{
@@ -780,21 +781,39 @@ public function save($data)
780781
// Alter the title for save as copy
781782
if ($input->get('task') == 'save2copy')
782783
{
783-
$origTable = clone $this->getTable();
784-
$origTable->load($input->getInt('id'));
784+
$origTable = $this->getTable();
785+
786+
if ($app->isClient('site'))
787+
{
788+
$origTable->load($input->getInt('a_id'));
789+
790+
if ($origTable->title === $data['title'])
791+
{
792+
/**
793+
* If title of article is not changed, set alias to original article alias so that Joomla! will generate
794+
* new Title and Alias for the copied article
795+
*/
796+
$data['alias'] = $origTable->alias;
797+
}
798+
else
799+
{
800+
$data['alias'] = '';
801+
}
802+
}
803+
else
804+
{
805+
$origTable->load($input->getInt('id'));
806+
}
785807

786808
if ($data['title'] == $origTable->title)
787809
{
788810
list($title, $alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['title']);
789811
$data['title'] = $title;
790812
$data['alias'] = $alias;
791813
}
792-
else
814+
elseif ($data['alias'] == $origTable->alias)
793815
{
794-
if ($data['alias'] == $origTable->alias)
795-
{
796-
$data['alias'] = '';
797-
}
816+
$data['alias'] = '';
798817
}
799818
}
800819

@@ -803,16 +822,16 @@ public function save($data)
803822
{
804823
if ($data['alias'] == null)
805824
{
806-
if (Factory::getApplication()->get('unicodeslugs') == 1)
825+
if ($app->get('unicodeslugs') == 1)
807826
{
808-
$data['alias'] = \JFilterOutput::stringUrlUnicodeSlug($data['title']);
827+
$data['alias'] = OutputFilter::stringUrlUnicodeSlug($data['title']);
809828
}
810829
else
811830
{
812-
$data['alias'] = \JFilterOutput::stringURLSafe($data['title']);
831+
$data['alias'] = OutputFilter::stringURLSafe($data['title']);
813832
}
814833

815-
$table = Table::getInstance('Content', 'JTable');
834+
$table = $this->getTable();
816835

817836
if ($table->load(array('alias' => $data['alias'], 'catid' => $data['catid'])))
818837
{
@@ -824,7 +843,7 @@ public function save($data)
824843

825844
if (isset($msg))
826845
{
827-
Factory::getApplication()->enqueueMessage($msg, 'warning');
846+
$app->enqueueMessage($msg, 'warning');
828847
}
829848
}
830849
}

components/com_content/src/Controller/ArticleController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ public function save($key = null, $urlVar = 'a_id')
391391
$this->setRedirect(Route::_('index.php?Itemid=' . $menuitem . $lang, false));
392392
}
393393
}
394+
elseif ($this->getTask() === 'save2copy')
395+
{
396+
// Redirect to the article page, use the redirect url set from parent controller
397+
}
394398
else
395399
{
396400
// If ok, redirect to the return page.

components/com_content/src/View/Form/HtmlView.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ class HtmlView extends BaseHtmlView
8686
*/
8787
protected $captchaEnabled = false;
8888

89+
/**
90+
* Should we show Save As Copy button?
91+
*
92+
* @var boolean
93+
* @since __DEPLOY_VERSION__
94+
*/
95+
protected $showSaveAsCopy = false;
96+
8997
/**
9098
* Execute and display a template script.
9199
*
@@ -95,8 +103,8 @@ class HtmlView extends BaseHtmlView
95103
*/
96104
public function display($tpl = null)
97105
{
98-
$user = Factory::getUser();
99106
$app = Factory::getApplication();
107+
$user = $app->getIdentity();
100108

101109
// Get model data.
102110
$this->state = $this->get('State');
@@ -181,7 +189,15 @@ public function display($tpl = null)
181189
}
182190
}
183191

192+
// If the article is being edited and the current user has permission to create article
193+
if ($this->item->id
194+
&& ($user->authorise('core.create', 'com_content') || \count($user->getAuthorisedCategories('com_content', 'core.create'))))
195+
{
196+
$this->showSaveAsCopy = true;
197+
}
198+
184199
$this->_prepareDocument();
200+
185201
parent::display($tpl);
186202
}
187203

components/com_content/tmpl/form/edit.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@
161161
<span class="icon-check" aria-hidden="true"></span>
162162
<?php echo Text::_('JSAVE'); ?>
163163
</button>
164+
<?php if ($this->showSaveAsCopy) : ?>
165+
<button type="button" class="btn btn-primary" data-submit-task="article.save2copy">
166+
<span class="icon-copy" aria-hidden="true"></span>
167+
<?php echo Text::_('JSAVEASCOPY'); ?>
168+
</button>
169+
<?php endif; ?>
164170
<button type="button" class="btn btn-danger" data-submit-task="article.cancel">
165171
<span class="icon-times" aria-hidden="true"></span>
166172
<?php echo Text::_('JCANCEL'); ?>

language/en-GB/joomla.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ JREGISTER="Register"
109109
JREQUIRED="Required"
110110
JRESET="Reset"
111111
JSAVE="Save"
112+
JSAVEASCOPY="Save As Copy"
112113
JSELECT="Select"
113114
JSHOW="Show"
114115
JSHOWPASSWORD="Show Password"

libraries/src/MVC/Controller/FormController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ public function save($key = null, $urlVar = null)
749749
{
750750
case 'apply':
751751
// Set the record data in the session.
752-
$recordId = $model->getState($this->context . '.id');
752+
$recordId = $model->getState($model->getName() . '.id');
753753
$this->holdEditId($context, $recordId);
754754
$app->setUserState($context . '.data', null);
755755
$model->checkout($recordId);

0 commit comments

Comments
 (0)