Version: current

      1 <?php
      2
      3 /**
      4  * Create a process in session which is incremented to calls from the client
      5  *
      6  * @package newsletter
      7  */
      8 class BatchProcess extends Object {
      9
     10     protected $objects;
     11     protected $current;
     12     protected $id;
     13     protected $scriptOutput = true;
     14
     15     function __construct( $collection ) {
     16
     17         $this->current = 0;
     18
     19         if( $collection ) {
     20           if( is_array( $collection ) )
     21               $this->objects = $collection;
     22           elseif( is_a( $collection, 'DataObjectSet' ) ) {
     23               $this->objects = $collection->toArray();
     24
     25           } else
     26               $this->objects = array( $collection );
     27         }
     28
     29        parent::__construct();
     30   }
     31
     32     function __wakeup() {
     33         parent::__construct();
     34     }
     35
     36     function runToCompletion() {
     37         $this->scriptOutput = false;
     38         $this->current = 0;
     39         $ignore = $this->next( count( $this->objects ) );
     40
     41         $this->complete();
     42     }
     43
     44     function getID() {
     45         return $this->id;
     46     }
     47
     48     function next() {
     49         self::addProcess( $this );
     50         return $this->id.':'.$this->current.'/'.count( $this->objects );
     51     }
     52
     53     function start() {
     54         $this->current = 0;
     55         $this->id = self::generateID();
     56
     57     if( !$this->objects || count( $this->objects ) === 0 )
     58         return $this->complete();
     59
     60         return $this->next();
     61     }
     62
     63     function complete() {
     64         self::removeProcess( $this );
     65     }
     66
     67     static function generateID() {
     68         return count(Session::get('BatchProcesses')) + 1;
     69     }
     70
     71     static function addProcess( $process ) {
     72         Session::set('BatchProcesses.' . ($process->getID() - 1), serialize($process));
     73     }
     74
     75     static function removeProcess( $process ) {
     76         Session::clear('BatchProcesses.' . ($process->getID() - 1));
     77     }
     78 }
     79
     80 /**
     81  * Controller for calling the batch processes via Ajax.
     82  *
     83  * @package newsletter
     84  */
     85 class BatchProcess_Controller extends Controller {
     86
     87     function next() {
     88
     89         $processID = $this->urlParams['ID'];
     90
     91         if( !$processID ) {
     92                 return _t('BatchProcess_Controller.ERROR', 'ERROR: Could not continue process');
     93         }
     94
     95         $process = unserialize(Session::get('BatchProcesses.' . ($this->urlParams['ID'] - 1)));
     96
     97         if( !$process ) {
     98                 return _t('BatchProcess_Controller.ERROR');
     99         }
    100
    101         if( $this->urlParams['Batch'] )
    102             return $process->next( $this->urlParams['Batch'] );
    103         else
    104             return $process->next();
    105     }
    106 }
    107 ?>
    108

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