1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Elements\CountDown\Tests; |
4
|
|
|
|
5
|
|
|
use IntlDateFormatter; |
6
|
|
|
use Dynamic\Elements\CountDown\Elements\ElementCountDown; |
7
|
|
|
use SilverStripe\Dev\BuildTask; |
8
|
|
|
use SilverStripe\ORM\Connect\Query; |
9
|
|
|
use SilverStripe\ORM\DataObject; |
10
|
|
|
use SilverStripe\ORM\DataObjectSchema; |
11
|
|
|
use SilverStripe\ORM\DB; |
12
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
13
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
14
|
|
|
use SilverStripe\ORM\Queries\SQLSelect; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class EndSplitTask |
18
|
|
|
* @package Dynamic\Elements\CountDown\Tests |
19
|
|
|
*/ |
20
|
|
|
class EndSplitTask extends BuildTask |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $title = 'Countdown Element End Update'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = "Splits the countdown's End field into EndDate and EndTime"; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $enabled = true; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $request |
39
|
|
|
*/ |
40
|
|
|
public function run($request) |
41
|
|
|
{ |
42
|
|
|
if (!$this->endFieldExists()) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$count = 0; |
47
|
|
|
|
48
|
|
|
/** @var ElementCountDown|\SilverStripe\Versioned\Versioned $element */ |
49
|
|
|
foreach ($this->iterateElements() as $element) { |
50
|
|
|
if ($element->EndDate && $element->EndTime) { |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$published = $element->isPublished(); |
55
|
|
|
$end = $this->getEndField($element->ID); |
56
|
|
|
|
57
|
|
|
// because $end->Date() doesn't return in ISO format, nor does it allow a custom format to be passed |
58
|
|
|
$formatter = $end->getCustomFormatter(null, $end->getISOFormat(), IntlDateFormatter::MEDIUM, IntlDateFormatter::NONE); |
59
|
|
|
$element->EndDate = $formatter->format($end->getTimestamp()); |
60
|
|
|
$element->EndTime = $end->Time(); |
61
|
|
|
|
62
|
|
|
$element->write(); |
63
|
|
|
if ($published) { |
64
|
|
|
$element->publishRecursive(); |
65
|
|
|
} |
66
|
|
|
$count++; |
67
|
|
|
} |
68
|
|
|
echo "Updated {$count} countdown elements."; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return \Generator |
73
|
|
|
*/ |
74
|
|
|
public function iterateElements() |
75
|
|
|
{ |
76
|
|
|
foreach (ElementCountDown::get() as $element) { |
77
|
|
|
yield $element; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getTable() |
85
|
|
|
{ |
86
|
|
|
return ElementCountDown::getSchema()->tableName(ElementCountDown::class); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function endFieldExists() |
93
|
|
|
{ |
94
|
|
|
return array_key_exists('End', DB::field_list($this->getTable())); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param int $id |
99
|
|
|
* |
100
|
|
|
* @return DBDatetime |
101
|
|
|
*/ |
102
|
|
|
public function getEndField($id) |
103
|
|
|
{ |
104
|
|
|
$query = new SQLSelect(); |
105
|
|
|
$query->setFrom($this->getTable()); |
106
|
|
|
$query->addWhere('"ID"='. $id); |
107
|
|
|
$query->setLimit(1); |
108
|
|
|
$results = $query->execute(); |
109
|
|
|
|
110
|
|
|
$record = $results->record(); |
111
|
|
|
return DBField::create_field('DBDatetime', $record['End']); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|