2007-07-19 12:40:05 +02:00
|
|
|
<?php
|
2008-01-08 07:41:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package cms
|
|
|
|
* @subpackage newsletter
|
|
|
|
*/
|
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
/**
|
|
|
|
* Create a process in session which is incremented to calls from the client
|
2008-01-10 01:33:49 +01:00
|
|
|
* @package cms
|
|
|
|
* @subpackage newsletter
|
2007-07-19 12:40:05 +02:00
|
|
|
*/
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 )
|
2007-11-23 02:10:19 +01:00
|
|
|
return $this->complete();
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-10 01:33:49 +01:00
|
|
|
/**
|
|
|
|
* Controller for calling the batch processes via Ajax.
|
|
|
|
* @package cms
|
|
|
|
* @subpackage newsletter
|
|
|
|
*/
|
2007-07-19 12:40:05 +02:00
|
|
|
class BatchProcess_Controller extends Controller {
|
|
|
|
|
|
|
|
function next() {
|
|
|
|
|
|
|
|
$processID = $this->urlParams['ID'];
|
|
|
|
|
|
|
|
if( !$processID ) {
|
2007-09-27 22:39:28 +02:00
|
|
|
return "ERROR: Could not continue process";
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$process = unserialize(Session::get('BatchProcesses.' . ($this->urlParams['ID'] - 1)));
|
|
|
|
|
|
|
|
if( !$process ) {
|
|
|
|
return "ERROR:Could not continue process";
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $this->urlParams['Batch'] )
|
|
|
|
return $process->next( $this->urlParams['Batch'] );
|
|
|
|
else
|
|
|
|
return $process->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|