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