Version: current

      1 <?php
      2 /**
      3  * Special request handler for admin/batchaction
      4  *
      5  * @package cms
      6  * @subpackage batchaction
      7  */
      8 class CMSBatchActionHandler extends RequestHandler {
      9     static $batch_actions = array(
     10         'publish' => 'CMSBatchAction_Publish',
     11         'delete' => 'CMSBatchAction_Delete',
     12         'deletefromlive' => 'CMSBatchAction_DeleteFromLive',
     13     );
     14
     15     static $url_handlers = array(
     16         '$BatchAction/applicablepages' => 'handleApplicablePages',
     17         '$BatchAction/confirmation' => 'handleConfirmation',
     18         '$BatchAction' => 'handleAction',
     19     );
     20
     21     protected $parentController;
     22     protected $urlSegment;
     23
     24     /**
     25      * Register a new batch action.  Each batch action needs to be represented by a subclass
     26      * of
     27      *
     28      * @param $urlSegment The URL Segment of the batch action - the URL used to process this
     29      * action will be admin/batchactions/(urlSegment)
     30      * @param $batchActionClass The name of the CMSBatchAction subclass to register
     31      */
     32     static function register($urlSegment, $batchActionClass) {
     33         if(is_subclass_of($batchActionClass, 'CMSBatchAction')) {
     34             self::$batch_actions[$urlSegment] = $batchActionClass;
     35         } else {
     36             user_error("CMSBatchActionHandler::register() - Bad class '$batchActionClass'", E_USER_ERROR);
     37         }
     38     }
     39
     40     function __construct($parentController, $urlSegment) {
     41         $this->parentController = $parentController;
     42         $this->urlSegment = $urlSegment;
     43         parent::__construct();
     44     }
     45
     46     function Link() {
     47         return Controller::join_links($this->parentController->Link(), $this->urlSegment);
     48     }
     49
     50     function handleAction($request) {
     51         // This method can't be called without ajax.
     52         if(!Director::is_ajax()) {
     53             Director::redirectBack();
     54             return;
     55         }
     56
     57         $actions = SS_Object::get_static($this->class, 'batch_actions');
     58         $actionClass = $actions[$request->param('BatchAction')];
     59         $actionHandler = new $actionClass();
     60
     61         // Sanitise ID list and query the database for apges
     62         $ids = preg_split('! *, *!', trim($request->requestVar('csvIDs')));
     63         foreach($ids as $k => $v) if(!is_numeric($v)) unset($ids[$k]);
     64
     65         if($ids) {
     66             $pages = DataObject::get('SiteTree', "\"SiteTree\".\"ID\" IN (" . implode(", ", $ids) . ")");
     67
     68             // If we didn't query all the pages, then find the rest on the live site
     69             if(!$pages || $pages->Count() < sizeof($ids)) {
     70                 foreach($ids as $id) $idsFromLive[$id] = true;
     71                 if($pages) foreach($pages as $page) unset($idsFromLive[$page->ID]);
     72                 $idsFromLive = array_keys($idsFromLive);
     73
     74                 // Debug::message("\"SiteTree\".\"ID\" IN (" . implode(", ", $idsFromLive) . ")");
     75                 $livePages = Versioned::get_by_stage('SiteTree', 'Live', "\"SiteTree\".\"ID\" IN (" . implode(", ", $idsFromLive) . ")");
     76                 if($pages) $pages->merge($livePages);
     77                 else $pages = $livePages;
     78             }
     79         } else {
     80             $pages = new DataObjectSet();
     81         }
     82
     83         return $actionHandler->run($pages);
     84     }
     85
     86     function handleApplicablePages($request) {
     87         // Find the action handler
     88         $actions = SS_Object::get_static($this->class, 'batch_actions');
     89         $actionClass = $actions[$request->param('BatchAction')];
     90         $actionHandler = new $actionClass();
     91
     92         // Sanitise ID list and query the database for apges
     93         $ids = preg_split('! *, *!', trim($request->requestVar('csvIDs')));
     94         foreach($ids as $k => $id) $ids[$k] = (int)$id;
     95         $ids = array_filter($ids);
     96
     97         if($actionHandler->hasMethod('applicablePages')) {
     98             $applicableIDs = $actionHandler->applicablePages($ids);
     99         } else {
    100             $applicableIDs = $ids;
    101         }
    102
    103         $response = new SS_HTTPResponse(json_encode($applicableIDs));
    104         $response->addHeader("Content-type", "application/json");
    105         return $response;
    106     }
    107
    108     function handleConfirmation($request) {
    109         // Find the action handler
    110         $actions = SS_Object::get_static($this->class, 'batch_actions');
    111         $actionClass = $actions[$request->param('BatchAction')];
    112         $actionHandler = new $actionClass();
    113
    114         // Sanitise ID list and query the database for apges
    115         $ids = preg_split('! *, *!', trim($request->requestVar('csvIDs')));
    116         foreach($ids as $k => $id) $ids[$k] = (int)$id;
    117         $ids = array_filter($ids);
    118
    119         if($actionHandler->hasMethod('confirmationDialog')) {
    120             $response = new SS_HTTPResponse(json_encode($actionHandler->confirmationDialog($ids)));
    121         } else {
    122             $response = new SS_HTTPResponse(json_encode(array('alert' => false)));
    123         }
    124
    125         $response->addHeader("Content-type", "application/json");
    126         return $response;
    127     }
    128
    129     /**
    130      * Return a DataObjectSet of ArrayData objects containing the following pieces of info
    131      * about each batch action:
    132      *  - Link
    133      *  - Title
    134      *  - DoingText
    135      */
    136     function batchActionList() {
    137         $actions = SS_Object::get_static($this->class, 'batch_actions');
    138         $actionList = new DataObjectSet();
    139
    140         foreach($actions as $urlSegment => $actionClass) {
    141             $actionObj = new $actionClass();
    142             if($actionObj->canView()) {
    143                 $actionDef = new ArrayData(array(
    144                     "Link" => Controller::join_links($this->Link(), $urlSegment),
    145                     "Title" => $actionObj->getActionTitle(),
    146                     "DoingText" => $actionObj->getDoingText(),
    147                 ));
    148                 $actionList->push($actionDef);
    149             }
    150         }
    151
    152         return $actionList;
    153     }
    154
    155
    156
    157 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -