ENHANCEMENT Making CMSBatchActionHandler specific to record class, in order to have different batch actions for AssetAdmin (removing folders). Added CMSBatchActionHandler->batchActions() and changed internal representation of batch actions

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@92801 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-11-21 03:19:02 +00:00
parent 58cd7beb33
commit e47ce88853
3 changed files with 95 additions and 25 deletions

View File

@ -632,6 +632,26 @@ JS;
} }
} }
/**
* @return Form
*/
function SyncForm() {
$form = new Form(
$this,
'SyncForm',
new FieldSet(),
new FieldSet(
$btn = new FormAction('doSync', _t('FILESYSTEMSYNC','Look for new files'))
)
);
$form->addExtraClass('actionparams');
$form->setFormMethod('GET');
$form->setFormAction('dev/tasks/FilesystemSyncTask');
$btn->describe(_t('AssetAdmin_left.ss.FILESYSTEMSYNC_DESC', 'SilverStripe maintains its own database of the files & images stored in your assets/ folder. Click this button to update that database, if files are added to the assets/ folder from outside SilverStripe, for example, if you have uploaded files via FTP.'));
return $form;
}
public function save($urlParams, $form) { public function save($urlParams, $form) {
// Don't save the root folder - there's no database record // Don't save the root folder - there's no database record
if($_REQUEST['ID'] == 'root') { if($_REQUEST['ID'] == 'root') {

View File

@ -6,38 +6,55 @@
* @subpackage batchaction * @subpackage batchaction
*/ */
class CMSBatchActionHandler extends RequestHandler { class CMSBatchActionHandler extends RequestHandler {
static $batch_actions = array( static $batch_actions = array();
'publish' => 'CMSBatchAction_Publish',
'delete' => 'CMSBatchAction_Delete',
'deletefromlive' => 'CMSBatchAction_DeleteFromLive',
);
static $url_handlers = array( static $url_handlers = array(
'$BatchAction' => 'handleAction' '$BatchAction' => 'handleAction'
); );
protected $parentController; protected $parentController;
/**
* @var String
*/
protected $urlSegment; protected $urlSegment;
/**
* @var String $recordClass The classname that should be affected
* by any batch changes. Needs to be set in the actual {@link CMSBatchAction}
* implementations as well.
*/
protected $recordClass = 'SiteTree';
/** /**
* Register a new batch action. Each batch action needs to be represented by a subclass * Register a new batch action. Each batch action needs to be represented by a subclass
* of * of {@link CMSBatchAction}.
* *
* @param $urlSegment The URL Segment of the batch action - the URL used to process this * @param $urlSegment The URL Segment of the batch action - the URL used to process this
* action will be admin/batchactions/(urlSegment) * action will be admin/batchactions/(urlSegment)
* @param $batchActionClass The name of the CMSBatchAction subclass to register * @param $batchActionClass The name of the CMSBatchAction subclass to register
*/ */
static function register($urlSegment, $batchActionClass) { static function register($urlSegment, $batchActionClass, $recordClass = 'SiteTree') {
if(is_subclass_of($batchActionClass, 'CMSBatchAction')) { if(is_subclass_of($batchActionClass, 'CMSBatchAction')) {
self::$batch_actions[$urlSegment] = $batchActionClass; self::$batch_actions[$urlSegment] = array(
'class' => $batchActionClass,
'recordClass' => $recordClass
);
} else { } else {
user_error("CMSBatchActionHandler::register() - Bad class '$batchActionClass'", E_USER_ERROR); user_error("CMSBatchActionHandler::register() - Bad class '$batchActionClass'", E_USER_ERROR);
} }
} }
function __construct($parentController, $urlSegment) { /**
* @param string $parentController
* @param string $urlSegment
* @param string $recordClass
*/
function __construct($parentController, $urlSegment, $recordClass = null) {
$this->parentController = $parentController; $this->parentController = $parentController;
$this->urlSegment = $urlSegment; $this->urlSegment = $urlSegment;
if($recordClass) $this->recordClass = $recordClass;
parent::__construct(); parent::__construct();
} }
@ -52,8 +69,8 @@ class CMSBatchActionHandler extends RequestHandler {
return; return;
} }
$actions = Object::get_static($this->class, 'batch_actions'); $actions = $this->batchActions();
$actionClass = $actions[$request->param('BatchAction')]; $actionClass = $actions[$request->param('BatchAction')]['class'];
$actionHandler = new $actionClass(); $actionHandler = new $actionClass();
// Sanitise ID list and query the database for apges // Sanitise ID list and query the database for apges
@ -61,19 +78,33 @@ class CMSBatchActionHandler extends RequestHandler {
foreach($ids as $k => $v) if(!is_numeric($v)) unset($ids[$k]); foreach($ids as $k => $v) if(!is_numeric($v)) unset($ids[$k]);
if($ids) { if($ids) {
$pages = DataObject::get('SiteTree', "\"SiteTree\".\"ID\" IN (" . implode(", ", $ids) . ")"); $pages = DataObject::get(
$this->recordClass,
sprintf(
'"%s"."ID" IN (%s)',
ClassInfo::baseDataClass($this->recordClass),
implode(", ", $ids)
)
);
if(Object::has_extension($this->recordClass, 'Versioned')) {
// If we didn't query all the pages, then find the rest on the live site // If we didn't query all the pages, then find the rest on the live site
if(!$pages || $pages->Count() < sizeof($ids)) { if(!$pages || $pages->Count() < sizeof($ids)) {
foreach($ids as $id) $idsFromLive[$id] = true; foreach($ids as $id) $idsFromLive[$id] = true;
if($pages) foreach($pages as $page) unset($idsFromLive[$page->ID]); if($pages) foreach($pages as $page) unset($idsFromLive[$page->ID]);
$idsFromLive = array_keys($idsFromLive); $idsFromLive = array_keys($idsFromLive);
Debug::message("\"SiteTree\".\"ID\" IN (" . implode(", ", $idsFromLive) . ")"); $sql = sprintf(
$livePages = Versioned::get_by_stage('SiteTree', 'Live', "\"SiteTree\".\"ID\" IN (" . implode(", ", $idsFromLive) . ")"); '"%s"."ID" IN (%s)',
$this->recordClass,
implode(", ", $idsFromLive)
);
Debug::message($sql);
$livePages = Versioned::get_by_stage($this->recordClass, 'Live', $sql);
if($pages) $pages->merge($livePages); if($pages) $pages->merge($livePages);
else $pages = $livePages; else $pages = $livePages;
} }
}
} else { } else {
$pages = new DataObjectSet(); $pages = new DataObjectSet();
} }
@ -89,10 +120,11 @@ class CMSBatchActionHandler extends RequestHandler {
* - DoingText * - DoingText
*/ */
function batchActionList() { function batchActionList() {
$actions = Object::get_static($this->class, 'batch_actions'); $actions = $this->batchActions();
$actionList = new DataObjectSet(); $actionList = new DataObjectSet();
foreach($actions as $urlSegment => $actionClass) { foreach($actions as $urlSegment => $action) {
$actionClass = $action['class'];
$actionObj = new $actionClass(); $actionObj = new $actionClass();
$actionDef = new ArrayData(array( $actionDef = new ArrayData(array(
"Link" => Controller::join_links($this->Link(), $urlSegment), "Link" => Controller::join_links($this->Link(), $urlSegment),
@ -104,6 +136,19 @@ class CMSBatchActionHandler extends RequestHandler {
return $actionList; return $actionList;
} }
/**
* Get all registered actions through the static defaults set by {@link register()}.
* Filters for the currently set {@link recordClass}.
*
* @return array See {@link register()} for the returned format.
*/
function batchActions() {
$actions = Object::get_static($this->class, 'batch_actions');
if($actions) foreach($actions as $action) {
if($action['recordClass'] != $this->recordClass) unset($action);
}
return $actions;
}
} }

View File

@ -85,6 +85,11 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
Requirements::javascript(CMS_DIR . '/javascript/CMSMain.Translatable.js'); Requirements::javascript(CMS_DIR . '/javascript/CMSMain.Translatable.js');
Requirements::css(CMS_DIR . '/css/CMSMain.css'); Requirements::css(CMS_DIR . '/css/CMSMain.css');
CMSBatchActionHandler::register('publish', 'CMSBatchAction_Publish');
CMSBatchActionHandler::register('unpublish', 'CMSBatchAction_Unpublish');
CMSBatchActionHandler::register('delete', 'CMSBatchAction_Delete');
CMSBatchActionHandler::register('deletefromlive', 'CMSBatchAction_DeleteFromLive');
} }
/** /**