2016-08-11 04:10:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\CMS\BatchActions;
|
|
|
|
|
|
|
|
use SilverStripe\Admin\CMSBatchAction;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\SS_List;
|
2017-03-21 05:26:46 +01:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-08-11 04:10:51 +02:00
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Batch restore of pages
|
|
|
|
*/
|
|
|
|
class CMSBatchAction_Restore extends CMSBatchAction
|
|
|
|
{
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
public function getActionTitle()
|
|
|
|
{
|
2017-05-19 02:11:46 +02:00
|
|
|
return _t(__CLASS__ . '.RESTORE', 'Restore');
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function run(SS_List $pages)
|
|
|
|
{
|
|
|
|
// Sort pages by depth
|
|
|
|
$pageArray = $pages->toArray();
|
|
|
|
// because of https://bugs.php.net/bug.php?id=50688
|
|
|
|
/** @var SiteTree $page */
|
|
|
|
foreach ($pageArray as $page) {
|
|
|
|
$page->getPageLevel();
|
|
|
|
}
|
|
|
|
usort($pageArray, function (SiteTree $a, SiteTree $b) {
|
|
|
|
return $a->getPageLevel() - $b->getPageLevel();
|
|
|
|
});
|
|
|
|
$pages = new ArrayList($pageArray);
|
|
|
|
|
|
|
|
// Restore
|
|
|
|
return $this->batchaction(
|
|
|
|
$pages,
|
|
|
|
'doRestoreToStage',
|
2017-05-19 02:11:46 +02:00
|
|
|
_t(__CLASS__ . '.RESTORED_PAGES', 'Restored %d pages')
|
2017-01-25 21:59:25 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@see SiteTree::canEdit()}
|
|
|
|
*
|
|
|
|
* @param array $ids
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function applicablePages($ids)
|
|
|
|
{
|
|
|
|
// Basic permission check based on SiteTree::canEdit
|
2020-04-19 06:18:01 +02:00
|
|
|
if (!Permission::check(["ADMIN", "SITETREE_EDIT_ALL"])) {
|
|
|
|
return [];
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get pages that exist in stage and remove them from the restore-able set
|
2018-03-14 04:34:46 +01:00
|
|
|
$stageIDs = Versioned::get_by_stage($this->managedClass, Versioned::DRAFT)->column('ID');
|
2017-01-25 21:59:25 +01:00
|
|
|
return array_values(array_diff($ids, $stageIDs));
|
|
|
|
}
|
2016-08-11 04:10:51 +02:00
|
|
|
}
|