Permalink
Browse files

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
  • Loading branch information...
1 parent 58cd7be commit e47ce88853866c13bb31903eec25208ce4d6e408 @chillu chillu committed Nov 21, 2009
Showing with 95 additions and 25 deletions.
  1. +20 −0 code/AssetAdmin.php
  2. +70 −25 code/CMSBatchActionHandler.php
  3. +5 −0 code/CMSMain.php
View
@@ -632,6 +632,26 @@ public function removefile(){
}
}
+ /**
+ * @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) {
// Don't save the root folder - there's no database record
if($_REQUEST['ID'] == 'root') {
@@ -6,38 +6,55 @@
* @subpackage batchaction
*/
class CMSBatchActionHandler extends RequestHandler {
- static $batch_actions = array(
- 'publish' => 'CMSBatchAction_Publish',
- 'delete' => 'CMSBatchAction_Delete',
- 'deletefromlive' => 'CMSBatchAction_DeleteFromLive',
- );
+ static $batch_actions = array();
static $url_handlers = array(
'$BatchAction' => 'handleAction'
);
protected $parentController;
+
+ /**
+ * @var String
+ */
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
- * of
+ * of {@link CMSBatchAction}.
*
* @param $urlSegment The URL Segment of the batch action - the URL used to process this
* action will be admin/batchactions/(urlSegment)
* @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')) {
- self::$batch_actions[$urlSegment] = $batchActionClass;
+ self::$batch_actions[$urlSegment] = array(
+ 'class' => $batchActionClass,
+ 'recordClass' => $recordClass
+ );
} else {
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->urlSegment = $urlSegment;
+ if($recordClass) $this->recordClass = $recordClass;
+
parent::__construct();
}
@@ -52,27 +69,41 @@ function handleAction($request) {
return;
}
- $actions = Object::get_static($this->class, 'batch_actions');
- $actionClass = $actions[$request->param('BatchAction')];
+ $actions = $this->batchActions();
+ $actionClass = $actions[$request->param('BatchAction')]['class'];
$actionHandler = new $actionClass();
// Sanitise ID list and query the database for apges
$ids = split(' *, *', trim($request->requestVar('csvIDs')));
foreach($ids as $k => $v) if(!is_numeric($v)) unset($ids[$k]);
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 we didn't query all the pages, then find the rest on the live site
- if(!$pages || $pages->Count() < sizeof($ids)) {
- foreach($ids as $id) $idsFromLive[$id] = true;
- if($pages) foreach($pages as $page) unset($idsFromLive[$page->ID]);
- $idsFromLive = array_keys($idsFromLive);
-
- Debug::message("\"SiteTree\".\"ID\" IN (" . implode(", ", $idsFromLive) . ")");
- $livePages = Versioned::get_by_stage('SiteTree', 'Live', "\"SiteTree\".\"ID\" IN (" . implode(", ", $idsFromLive) . ")");
- if($pages) $pages->merge($livePages);
- else $pages = $livePages;
+ if(Object::has_extension($this->recordClass, 'Versioned')) {
+ // If we didn't query all the pages, then find the rest on the live site
+ if(!$pages || $pages->Count() < sizeof($ids)) {
+ foreach($ids as $id) $idsFromLive[$id] = true;
+ if($pages) foreach($pages as $page) unset($idsFromLive[$page->ID]);
+ $idsFromLive = array_keys($idsFromLive);
+
+ $sql = sprintf(
+ '"%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);
+ else $pages = $livePages;
+ }
}
} else {
$pages = new DataObjectSet();
@@ -89,10 +120,11 @@ function handleAction($request) {
* - DoingText
*/
function batchActionList() {
- $actions = Object::get_static($this->class, 'batch_actions');
+ $actions = $this->batchActions();
$actionList = new DataObjectSet();
- foreach($actions as $urlSegment => $actionClass) {
+ foreach($actions as $urlSegment => $action) {
+ $actionClass = $action['class'];
$actionObj = new $actionClass();
$actionDef = new ArrayData(array(
"Link" => Controller::join_links($this->Link(), $urlSegment),
@@ -104,6 +136,19 @@ function batchActionList() {
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
@@ -85,6 +85,11 @@ public function init() {
Requirements::javascript(CMS_DIR . '/javascript/CMSMain.Translatable.js');
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');
}
/**

0 comments on commit e47ce88

Please sign in to comment.