Version: current

      1 <?php
      2
      3 /**
      4  * A class representing back actions
      5  *
      6  * <code>
      7  * CMSMain::register_batch_action('publishitems', new CMSBatchAction('doPublish',
      8  *  _t('CMSBatchActions.PUBLISHED_PAGES', 'published %d pages')));
      9  * </code>
     10  *
     11  * @package cms
     12  * @subpackage batchaction
     13  */
     14 abstract class CMSBatchAction extends SS_Object {
     15     /**
     16      * The the text to show in the dropdown for this action
     17      */
     18     abstract function getActionTitle();
     19
     20     /**
     21      * Get text to be shown while the action is being processed, of the form
     22      * "publishing pages".
     23      */
     24     abstract function getDoingText();
     25
     26     /**
     27      * Run this action for the given set of pages.
     28      * Return a set of status-updated JavaScript to return to the CMS.
     29      */
     30     abstract function run(DataObjectSet $pages);
     31
     32     /**
     33      * Helper method for processing batch actions.
     34      * Returns a set of status-updating JavaScript to return to the CMS.
     35      *
     36      * @param $pages The DataObjectSet of SiteTree objects to perform this batch action
     37      * on.
     38      * @param $helperMethod The method to call on each of those objects.
     39      */
     40     public function batchaction(DataObjectSet $pages, $helperMethod, $successMessage, $arguments = array()) {
     41         $failures = 0;
     42
     43         foreach($pages as $page) {
     44
     45             // Perform the action
     46             if (call_user_func_array(array($page, $helperMethod), $arguments) === false) {
     47                 $failures++;
     48                 FormResponse::add("\$('sitetree').addNodeClassByIdx('$page->ID', 'failed');");
     49             }
     50
     51
     52             // Now make sure the tree title is appropriately updated
     53             $publishedRecord = DataObject::get_by_id('SiteTree', $page->ID);
     54             if ($publishedRecord) {
     55                 $JS_title = Convert::raw2js($publishedRecord->TreeTitle());
     56                 FormResponse::add("\$('sitetree').setNodeTitle($page->ID, '$JS_title');");
     57             }
     58             $page->destroy();
     59             unset($page);
     60         }
     61
     62         $message = sprintf($successMessage, $pages->Count()-$failures, $failures);
     63
     64         FormResponse::add('statusMessage("'.$message.'","good");');
     65
     66         return FormResponse::respond();
     67     }
     68
     69
     70
     71     /**
     72      * Helper method for applicablePages() methods.  Acts as a skeleton implementation.
     73      *
     74      * @param $ids The IDs passed to applicablePages
     75      * @param $methodName The canXXX() method to call on each page to check if the action is applicable
     76      * @param $checkStagePages Set to true if you want to check stage pages
     77      * @param $checkLivePages Set to true if you want to check live pages (e.g, for deleted-from-draft)
     78      */
     79     function applicablePagesHelper($ids, $methodName, $checkStagePages = true, $checkLivePages = true) {
     80         if(!is_array($ids)) user_error("Bad \$ids passed to applicablePagesHelper()", E_USER_WARNING);
     81         if(!is_string($methodName)) user_error("Bad \$methodName passed to applicablePagesHelper()", E_USER_WARNING);
     82
     83         $applicableIDs = array();
     84
     85         $SQL_ids = implode(', ', array_filter($ids, 'is_numeric'));
     86         if (!$SQL_ids) return array();
     87
     88         $draftPages = DataObject::get("SiteTree", "\"SiteTree\".\"ID\" IN ($SQL_ids)");
     89
     90         $onlyOnLive = array_fill_keys($ids, true);
     91         if($checkStagePages) {
     92             foreach($draftPages as $page) {
     93                 unset($onlyOnLive[$page->ID]);
     94                 if($page->$methodName()) $applicableIDs[] = $page->ID;
     95             }
     96         }
     97
     98         // Get the pages that only exist on live (deleted from stage)
     99         if($checkLivePages && $onlyOnLive) {
    100             $SQL_ids = implode(', ', array_keys($onlyOnLive));
    101             $livePages = ($SQL_ids) ? Versioned::get_by_stage("SiteTree", "Live", "\"SiteTree\".\"ID\" IN ($SQL_ids)") : false;
    102
    103             if($livePages) foreach($livePages as $page) {
    104                 if($page->$methodName()) $applicableIDs[] = $page->ID;
    105             }
    106         }
    107
    108         return $applicableIDs;
    109     }
    110
    111     // if your batchaction has parameters, return a fieldset here
    112     function getParameterFields() {
    113         return false;
    114     }
    115
    116     /**
    117      * If you wish to restrict the batch action to some users, overload this function.
    118      */
    119     function canView() {
    120         return true;
    121     }
    122 }
    123
    124 /**
    125  * Publish items batch action.
    126  *
    127  * @package cms
    128  * @subpackage batchaction
    129  */
    130 class CMSBatchAction_Publish extends CMSBatchAction {
    131     function getActionTitle() {
    132         return _t('CMSBatchActions.PUBLISH_PAGES', 'Publish');
    133     }
    134     function getDoingText() {
    135         return _t('CMSBatchActions.PUBLISHING_PAGES', 'Publishing pages');
    136     }
    137
    138     function run(DataObjectSet $pages) {
    139         return $this->batchaction($pages, 'doPublish',
    140             _t('CMSBatchActions.PUBLISHED_PAGES', 'Published %d pages, %d failures')
    141         );
    142     }
    143
    144     function applicablePages($ids) {
    145         return $this->applicablePagesHelper($ids, 'canPublish', true, false);
    146     }
    147 }
    148
    149 /**
    150  * Delete items batch action.
    151  *
    152  * @package cms
    153  * @subpackage batchaction
    154  */
    155 class CMSBatchAction_Delete extends CMSBatchAction {
    156     function getActionTitle() {
    157         return _t('CMSBatchActions.DELETE_DRAFT_PAGES', 'Delete from draft site');
    158     }
    159     function getDoingText() {
    160         return _t('CMSBatchActions.DELETING_DRAFT_PAGES', 'Deleting selected pages from the draft site');
    161     }
    162
    163     function run(DataObjectSet $pages) {
    164         $failures = 0;
    165
    166         foreach($pages as $page) {
    167             $id = $page->ID;
    168
    169             // Perform the action
    170             if($page->canDelete()) $page->delete();
    171             else $failures++;
    172
    173             // check to see if the record exists on the live site, if it doesn't remove the tree node
    174             $liveRecord = Versioned::get_one_by_stage( 'SiteTree', 'Live', "\"SiteTree\".\"ID\"=$id");
    175             if($liveRecord) {
    176                 $liveRecord->IsDeletedFromStage = true;
    177                 $title = Convert::raw2js($liveRecord->TreeTitle());
    178                 FormResponse::add("$('sitetree').setNodeTitle($id, '$title');");
    179                 FormResponse::add("$('Form_EditForm').reloadIfSetTo($id);");
    180             } else {
    181                 FormResponse::add("var node = $('sitetree').getTreeNodeByIdx('$id');");
    182                 FormResponse::add("if(node && node.parentTreeNode)  node.parentTreeNode.removeTreeNode(node);");
    183                 FormResponse::add("$('Form_EditForm').reloadIfSetTo($id);");
    184             }
    185
    186             $page->destroy();
    187             unset($page);
    188         }
    189
    190         $message = sprintf(_t('CMSBatchActions.DELETED_DRAFT_PAGES', 'Deleted %d pages from the draft site, %d failures'), $pages->Count()-$failures, $failures);
    191         FormResponse::add('statusMessage("'.$message.'","good");');
    192
    193         return FormResponse::respond();
    194     }
    195
    196     function applicablePages($ids) {
    197         return $this->applicablePagesHelper($ids, 'canDelete', true, false);
    198     }
    199 }
    200
    201 /**
    202  * Unpublish (delete from live site) items batch action.
    203  *
    204  * @package cms
    205  * @subpackage batchaction
    206  */
    207 class CMSBatchAction_DeleteFromLive extends CMSBatchAction {
    208     function getActionTitle() {
    209         return _t('CMSBatchActions.DELETE_PAGES', 'Delete from published site');
    210     }
    211     function getDoingText() {
    212         return _t('CMSBatchActions.DELETING_PAGES', 'Deleting selected pages from the published site');
    213     }
    214
    215     function run(DataObjectSet $pages) {
    216         $ids = $pages->column('ID');
    217         $this->batchaction($pages, 'doUnpublish',
    218             _t('CMSBatchActions.DELETED_PAGES', 'Deleted %d pages from the published site, %d failures')
    219         );
    220
    221         foreach($ids as $pageID) {
    222             $id = $pageID;
    223
    224             // check to see if the record exists on the stage site, if it doesn't remove the tree node
    225             $stageRecord = Versioned::get_one_by_stage( 'SiteTree', 'Stage', "\"SiteTree\".\"ID\"=$id");
    226             if($stageRecord) {
    227                 $stageRecord->IsAddedToStage = true;
    228                 $title = Convert::raw2js($stageRecord->TreeTitle());
    229                 FormResponse::add("$('sitetree').setNodeTitle($id, '$title');");
    230                 FormResponse::add("$('Form_EditForm').reloadIfSetTo($id);");
    231             } else {
    232                 FormResponse::add("var node = $('sitetree').getTreeNodeByIdx('$id');");
    233                 FormResponse::add("if(node && node.parentTreeNode)  node.parentTreeNode.removeTreeNode(node);");
    234                 FormResponse::add("$('Form_EditForm').reloadIfSetTo($id);");
    235             }
    236         }
    237
    238         return FormResponse::respond();
    239     }
    240
    241     function applicablePages($ids) {
    242         return $this->applicablePagesHelper($ids, 'canDelete', false, true);
    243     }
    244 }
    245
    246
    247
    248

[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. -