1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class EVCPage extends Page |
|
|
|
|
5
|
|
|
{ |
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class EVCPage_Controller extends Page_Controller |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
private static $allowed_actions = array( |
|
|
|
|
12
|
|
|
"show" => true, |
13
|
|
|
"save" => true, |
14
|
|
|
"retrieve" => true, |
15
|
|
|
"reset" => true, |
16
|
|
|
"previous" => true, |
17
|
|
|
"lock" => true |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
public function init() |
21
|
|
|
{ |
22
|
|
|
parent::init(); |
23
|
|
|
Requirements::javascript("framework/thirdparty/jquery/jquery.js"); |
24
|
|
|
if(Director::isLive()) { |
25
|
|
|
Requirements::themedCSS('ElectricVehicleCalculator.min', 'electric-vehicle-calculator'); |
26
|
|
|
// Requirements::javascript("https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js"); |
|
|
|
|
27
|
|
|
Requirements::javascript("electric-vehicle-calculator/thirdparty/chartjs/Chart.min.js"); |
28
|
|
|
Requirements::javascript("electric-vehicle-calculator/javascript/ElectricVehicleCalculator.min.js"); |
29
|
|
|
} else { |
30
|
|
|
Requirements::themedCSS('ElectricVehicleCalculator', 'electric-vehicle-calculator'); |
31
|
|
|
Requirements::javascript("electric-vehicle-calculator/thirdparty/chartjs/Chart.min.js"); |
32
|
|
|
// Requirements::javascript("https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js"); |
|
|
|
|
33
|
|
|
Requirements::javascript("electric-vehicle-calculator/javascript/ElectricVehicleCalculator.js"); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected $evcDataSet = null; |
38
|
|
|
|
39
|
|
|
public function index() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$code = Session::get("EVCLastCode"); |
42
|
|
|
$this->evcDataSet = EVCDataSet::find_or_create($code, true); |
43
|
|
|
if (!$code) { |
44
|
|
|
$code = $this->evcDataSet->Code; |
|
|
|
|
45
|
|
|
Session::set("EVCLastCode", $code); |
46
|
|
|
} |
47
|
|
|
return $this->redirect($this->evcDataSet->MyLink($this, "show")); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
function BackgroundImage() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
if(rand(0,1) == 1) { |
53
|
|
|
return '/electric-vehicle-calculator/images/nz-road1.jpg'; |
54
|
|
|
} else { |
55
|
|
|
return '/electric-vehicle-calculator/images/nz-road2.jpg'; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function HasCustomTitle() |
60
|
|
|
{ |
61
|
|
|
if ($this->evcDataSet && $this->evcDataSet->Title) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function Title() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if ($this->HasCustomTitle()) { |
69
|
|
|
return Convert::raw2xml(urldecode($this->evcDataSet->Title)); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
return Convert::raw2xml($this->Title); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public function MetaTitle() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
if ($this->HasCustomTitle()) { |
77
|
|
|
return Convert::raw2xml(urldecode($this->evcDataSet->Title)); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
return Convert::raw2xml($this->MetaTitle); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function show($request) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$code = $request->param("ID"); |
85
|
|
|
$this->evcDataSet = EVCDataSet::find_or_create($code, false); |
86
|
|
|
if ($this->evcDataSet && $this->evcDataSet->exists()) { |
87
|
|
|
Requirements::javascript('electric-vehicle-calculator/javascript/ElectricVehicleCalculator.js'); |
88
|
|
|
$link = $this->Link(); |
89
|
|
|
if($link === '/') { |
90
|
|
|
$link = '/home/'; |
91
|
|
|
} |
92
|
|
|
//Requirements::javascript("assets/evc/translations.js"); |
|
|
|
|
93
|
|
|
Requirements::customScript(" |
94
|
|
|
EVC.serverInteraction.baseLink = '".$link."'; |
95
|
|
|
EVC.serverInteraction.serverKey = '".$this->evcDataSet->Code."'; |
|
|
|
|
96
|
|
|
", "EVCSetBasics"); |
97
|
|
|
Requirements::customScript($this->evcDataSet->returnValuesAsJS(), "EVCreturnValuesAsJS"); |
98
|
|
|
return array(); |
99
|
|
|
} else { |
100
|
|
|
return $this->httpError(404); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* returns empty string on error... |
106
|
|
|
* ajax method ... |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function save($request) |
110
|
|
|
{ |
111
|
|
|
$code = $request->param("ID"); |
112
|
|
|
$this->evcDataSet = EVCDataSet::find_or_create($code, true); |
113
|
|
|
//save it |
114
|
|
|
$key = Convert::raw2sql($request->getVar("key")); |
115
|
|
|
$value = Convert::raw2sql($request->getVar("value")); |
116
|
|
|
if ($newCode = $this->evcDataSet->setValue($key, $value)) { |
|
|
|
|
117
|
|
|
Session::set("EVCLastCode", $newCode); |
118
|
|
|
Session::save(); |
119
|
|
|
return $newCode; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function retrieve($request) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$code = $request->param("ID"); |
126
|
|
|
$this->evcDataSet = EVCDataSet::find_or_create($code, false); |
127
|
|
|
if ($this->evcDataSet && $this->evcDataSet->exists() && $this->evcDataSet->Data) { |
|
|
|
|
128
|
|
|
Session::set("EVCLastCode", $this->evcDataSet->Code); |
|
|
|
|
129
|
|
|
Session::save(); |
130
|
|
|
return $this->redirect($this->evcDataSet->MyLink($this, "show")); |
131
|
|
|
} else { |
132
|
|
|
return $this->httpError(404); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function reset($request) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
Session::set("EVCLastCode", ""); |
139
|
|
|
Session::clear("EVCLastCode"); |
140
|
|
|
Session::save(); |
141
|
|
|
return $this->redirect($this->Link()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* ajax method ... |
146
|
|
|
* |
147
|
|
|
*/ |
148
|
|
|
public function lock($request) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$code = $request->param("ID"); |
151
|
|
|
$this->evcDataSet = EVCDataSet::find_or_create($code, false); |
152
|
|
|
$title = $request->getVar("title"); |
153
|
|
|
if ($title && $this->evcDataSet && $this->evcDataSet->exists()) { |
154
|
|
|
if ($title == "ignore") { |
|
|
|
|
155
|
|
|
//no need to do anything |
156
|
|
|
} else { |
157
|
|
|
$this->evcDataSet = $this->evcDataSet->getCopyIfNeeded(); |
158
|
|
|
$this->evcDataSet->Locked = true; |
|
|
|
|
159
|
|
|
$this->evcDataSet->Title = Convert::raw2sql(urldecode($title)); |
|
|
|
|
160
|
|
|
$this->evcDataSet->write(); |
161
|
|
|
} |
162
|
|
|
return $this->evcDataSet->MyLink($this, "retrieve"); |
163
|
|
|
} |
164
|
|
|
return "ERROR!"; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function EVCDataSet() |
168
|
|
|
{ |
169
|
|
|
return $this->evcDataSet; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function IsLocked() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
return $this->evcDataSet->Locked; |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
protected $previousCalculations = null; |
179
|
|
|
|
180
|
|
|
public function previous($request) |
181
|
|
|
{ |
182
|
|
|
$this->previousCalculations = PaginatedList::create(EVCDataSet::get()->filter(array("Locked" => 1))->where("Title IS NOT NULL AND Title <> ''")); |
183
|
|
|
$this->previousCalculations->setPageLength(100); |
184
|
|
|
return array(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function PreviousCalculations() |
188
|
|
|
{ |
189
|
|
|
return $this->previousCalculations; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.