|
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( |
|
59
|
|
|
null, |
|
60
|
|
|
$end->getISOFormat(), |
|
61
|
|
|
IntlDateFormatter::MEDIUM, |
|
62
|
|
|
IntlDateFormatter::NONE |
|
63
|
|
|
); |
|
64
|
|
|
$element->EndDate = $formatter->format($end->getTimestamp()); |
|
65
|
|
|
$element->EndTime = $end->Time(); |
|
66
|
|
|
|
|
67
|
|
|
$element->write(); |
|
68
|
|
|
if ($published) { |
|
69
|
|
|
$element->publishRecursive(); |
|
70
|
|
|
} |
|
71
|
|
|
$count++; |
|
72
|
|
|
} |
|
73
|
|
|
echo "Updated {$count} countdown elements."; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return \Generator |
|
78
|
|
|
*/ |
|
79
|
|
|
public function iterateElements() |
|
80
|
|
|
{ |
|
81
|
|
|
foreach (ElementCountDown::get() as $element) { |
|
82
|
|
|
yield $element; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getTable() |
|
90
|
|
|
{ |
|
91
|
|
|
return ElementCountDown::getSchema()->tableName(ElementCountDown::class); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public function endFieldExists() |
|
98
|
|
|
{ |
|
99
|
|
|
return array_key_exists('End', DB::field_list($this->getTable())); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param int $id |
|
104
|
|
|
* |
|
105
|
|
|
* @return DBDatetime |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getEndField($id) |
|
108
|
|
|
{ |
|
109
|
|
|
$query = new SQLSelect(); |
|
110
|
|
|
$query->setFrom($this->getTable()); |
|
111
|
|
|
$query->addWhere('"ID"='. $id); |
|
112
|
|
|
$query->setLimit(1); |
|
113
|
|
|
$results = $query->execute(); |
|
114
|
|
|
|
|
115
|
|
|
$record = $results->record(); |
|
116
|
|
|
return DBField::create_field('DBDatetime', $record['End']); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|