ok I got it... not so difficult indeed ;-)
[here it is a reference:]https://octobercms.com/docs/backend/controllers-ajax
first of all I added a button in the _list_toolbar.htm (under controllers) of this kind, where the most important point is the data-request param:
<button
class="btn btn-default oc-icon-clone"
disabled="disabled"
onclick="$(this).data('request-data', {
checked: $('.control-list').listWidget('getChecked')
})"
data-request="onDuplicate"
data-request-confirm="Do you wanna duplicate this event?"
data-trigger-action="enable"
data-trigger=".control-list input[type=checkbox]"
data-trigger-condition="checked"
data-request-success="$(this).prop('disabled', true)"
data-stripe-load-indicator>
Duplicate selected
</button>
then in the controller php file I define the function assigned to this button with something like this:
public function onDuplicate() {
$checked_items_ids = input('checked');
foreach ($checked_items_ids as $id) {
$original = Event::where("id", $id)->first();
$clone = $original->replicate();
$clone->title = "CPY ".$clone->title;
$clone->slug = now()->timestamp."_".$clone->slug;
$clone->published = 0;
$clone->highlight = 0;
$fake = new \System\Models\File;
$imgs = $original->gallery;
$fake->fromUrl($imgs[0]->getPath(), now()->timestamp.'.jpg');
$clone->id = Event::withTrashed()->max('id') + 1;
$clone->gallery()->add($fake);
$clone->save();
}
\Flash::success('Event cloned');
return $this->listRefresh();
}
note that the gallery is required in my project, so I have to create a clone also of the first image at least... another point is about the 'withTrashed" because I'm using softdelete.
And finally the listRefresh redraws the list in the view and I can see, open and edit the duplicated event.
Cool isn't it? :smirk:
Thanks Octobercms, I'm really enjoying using it!