1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\ShoppingCart\Tasks; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\BuildTask; |
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverCommerce\ShoppingCart\Model\ShoppingCart as ShoppingCart; |
8
|
|
|
use SilverCommerce\OrdersAdmin\Model\Estimate; |
9
|
|
|
use DateTime; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Simple task that removes estimates that have passed their end date |
13
|
|
|
* and are not assigned to a customer. |
14
|
|
|
* |
15
|
|
|
* @author ilateral (http://www.ilateral.co.uk) |
16
|
|
|
* @package shoppingcart |
17
|
|
|
*/ |
18
|
|
|
class CleanExpiredEstimatesTask extends BuildTask |
19
|
|
|
{ |
20
|
|
|
protected $title = 'Clean expired estimates'; |
21
|
|
|
|
22
|
|
|
protected $description = 'Clean all estimates that are past their expiration date and have no users assifgned'; |
23
|
|
|
|
24
|
|
|
protected $enabled = true; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Should this task output commands |
28
|
|
|
* |
29
|
|
|
* @var boolean |
30
|
|
|
*/ |
31
|
|
|
protected $silent = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return boolean |
35
|
|
|
*/ |
36
|
|
|
public function getSilent() |
37
|
|
|
{ |
38
|
|
|
return $this->silent; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* set the silent parameter |
43
|
|
|
* |
44
|
|
|
* @param boolean $set |
45
|
|
|
* @return CleanExpiredEstimatesTask |
46
|
|
|
*/ |
47
|
|
|
public function setSilent($set) |
48
|
|
|
{ |
49
|
|
|
$this->silent = $set; |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
function run($request) { |
|
|
|
|
54
|
|
|
$now = new DateTime(); |
55
|
|
|
$days = Estimate::config()->default_end; |
56
|
|
|
$past = $now->modify("-{$days} days"); |
57
|
|
|
|
58
|
|
|
$all = ShoppingCart::get()->filter([ |
59
|
|
|
"StartDate:LessThan" => $past->format('Y-m-d H:i:s') |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
$i = 0; |
63
|
|
|
foreach ($all as $cart) { |
64
|
|
|
// Is the cart currentyl assigned to a member? |
65
|
|
|
$curr = Member::get()->find("CartID", $cart->ID); |
|
|
|
|
66
|
|
|
|
67
|
|
|
if (empty($curr)) { |
68
|
|
|
$cart->delete(); |
69
|
|
|
$i++; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->log('removed '.$i.' expired estimates.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function log($message) |
77
|
|
|
{ |
78
|
|
|
if (!$this->silent) { |
79
|
|
|
if(Director::is_cli()) { |
80
|
|
|
echo $message . "\n"; |
81
|
|
|
} else { |
82
|
|
|
echo $message . "<br/>"; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.