silverstripe-cms/code/BatchActions/CMSBatchAction_Restore.php
Damian Mooyman 041d12129a
Fix regressions from namespacing
Update template locations
Add permission codes to cms sections
Code, phpdoc, and typehint cleanup
Split batch actions into files
2016-08-16 11:56:34 +12:00

63 lines
1.5 KiB
PHP

<?php
namespace SilverStripe\CMS\BatchActions;
use SilverStripe\Admin\CMSBatchAction;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\SS_List;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\Security\Permission;
/**
* Batch restore of pages
* @package cms
* @subpackage batchaction
*/
class CMSBatchAction_Restore extends CMSBatchAction
{
public function getActionTitle()
{
return _t('CMSBatchActions.RESTORE', 'Restore');
}
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',
_t('CMSBatchActions.RESTORED_PAGES', 'Restored %d pages')
);
}
/**
* {@see SiteTree::canEdit()}
*
* @param array $ids
* @return array
*/
public function applicablePages($ids)
{
// Basic permission check based on SiteTree::canEdit
if (!Permission::check(array("ADMIN", "SITETREE_EDIT_ALL"))) {
return array();
}
// Get pages that exist in stage and remove them from the restore-able set
$stageIDs = Versioned::get_by_stage($this->managedClass, 'Stage')->column('ID');
return array_values(array_diff($ids, $stageIDs));
}
}