2015-04-21 06:47:46 +02:00
|
|
|
<?php
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
namespace SilverStripe\Blog\Admin;
|
|
|
|
|
|
|
|
use SilverStripe\Blog\Admin\GridFieldFormAction;
|
2016-12-16 05:10:38 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-09-14 01:49:27 +02:00
|
|
|
use SilverStripe\Core\Injector\Injectable;
|
2016-12-16 05:10:38 +01:00
|
|
|
use Silverstripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_ActionProvider;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
class GridFieldMergeAction implements GridField_ColumnProvider, GridField_ActionProvider
|
|
|
|
{
|
2017-09-14 01:49:27 +02:00
|
|
|
use Injectable;
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* List of records to show in the MergeAction column.
|
|
|
|
*
|
|
|
|
* @var array|SS_List
|
|
|
|
*/
|
|
|
|
protected $records;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type of parent DataObject (i.e BlogTag, BlogCategory).
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $parentType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Relationship method to reference parent (i.e BlogTags).
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $parentMethod;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Relationship method to reference child (i.e BlogPosts).
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $childMethod;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array|SS_List $records
|
|
|
|
* @param string $parentType
|
|
|
|
* @param string $parentMethod
|
|
|
|
* @param string $childMethod
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
public function __construct($records = [], $parentType, $parentMethod, $childMethod)
|
2015-11-21 07:17:29 +01:00
|
|
|
{
|
|
|
|
$this->records = $records;
|
|
|
|
$this->parentType = $parentType;
|
|
|
|
$this->parentMethod = $parentMethod;
|
|
|
|
$this->childMethod = $childMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function augmentColumns($gridField, &$columns)
|
|
|
|
{
|
|
|
|
if (!in_array('MergeAction', $columns)) {
|
|
|
|
$columns[] = 'MergeAction';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getColumnsHandled($gridField)
|
|
|
|
{
|
2017-09-14 00:59:01 +02:00
|
|
|
return ['MergeAction'];
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getColumnContent($gridField, $record, $columnName)
|
|
|
|
{
|
|
|
|
if ($columnName === 'MergeAction' && $record->{$this->childMethod}()->Count() > 0) {
|
2016-12-16 05:10:38 +01:00
|
|
|
$dropdown = DropdownField::create('Target', 'Target', $this->records->exclude('ID', $record->ID)->map());
|
2015-11-21 07:17:29 +01:00
|
|
|
$dropdown->setAttribute('id', 'Target_'.$record->ID);
|
|
|
|
$prefix = strtolower($this->parentMethod . '-' . $this->childMethod);
|
|
|
|
|
2017-09-14 01:49:27 +02:00
|
|
|
$action = GridFieldFormAction::create(
|
2015-11-21 07:17:29 +01:00
|
|
|
$gridField,
|
|
|
|
'MergeAction' . $record->ID,
|
|
|
|
'Move',
|
|
|
|
'merge',
|
2017-09-14 00:59:01 +02:00
|
|
|
[
|
2015-11-21 07:17:29 +01:00
|
|
|
'record' => $record->ID,
|
|
|
|
'target' => $prefix . '-target-record-' . $record->ID,
|
2017-09-14 00:59:01 +02:00
|
|
|
]
|
2015-11-21 07:17:29 +01:00
|
|
|
);
|
|
|
|
|
2017-09-14 00:59:01 +02:00
|
|
|
$action->setExtraAttributes([
|
2015-11-21 07:17:29 +01:00
|
|
|
'data-target' => $prefix . '-target-record-' . $record->ID
|
2017-09-14 00:59:01 +02:00
|
|
|
]);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
return $dropdown->Field() . $action->Field() . '<a title="Move posts to" class="MergeActionReveal">move posts to</a>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getColumnAttributes($gridField, $record, $columnName)
|
|
|
|
{
|
2017-09-14 00:59:01 +02:00
|
|
|
return ['class' => 'MergeAction'];
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getColumnMetadata($gridField, $columnName)
|
|
|
|
{
|
2017-09-14 00:59:01 +02:00
|
|
|
return ['title' => 'Move posts to'];
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getActions($gridField)
|
|
|
|
{
|
2017-09-14 00:59:01 +02:00
|
|
|
return ['merge'];
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
|
|
|
|
{
|
|
|
|
if ($actionName === 'merge') {
|
|
|
|
$controller = Controller::curr();
|
|
|
|
|
|
|
|
$request = $controller->getRequest();
|
|
|
|
|
|
|
|
$target = $request->requestVar($arguments["target"]);
|
|
|
|
|
|
|
|
$parentType = $this->parentType;
|
|
|
|
|
|
|
|
$fromParent = $parentType::get()->byId($arguments['record']);
|
|
|
|
$toParent = $parentType::get()->byId($target);
|
|
|
|
|
|
|
|
$posts = $fromParent->{$this->childMethod}();
|
|
|
|
|
|
|
|
foreach ($posts as $post) {
|
|
|
|
$relationship = $post->{$this->parentMethod}();
|
|
|
|
|
|
|
|
$relationship->remove($fromParent);
|
|
|
|
$relationship->add($toParent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 00:02:00 +01:00
|
|
|
}
|