|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class EVCSelectPage extends Page |
|
|
|
|
|
|
5
|
|
|
{ |
|
6
|
|
|
} |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class EVCSelectPage_Controller extends Page_Controller |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
12
|
|
|
"SelectForm", |
|
13
|
|
|
"nextstep" |
|
14
|
|
|
); |
|
15
|
|
|
|
|
16
|
|
|
protected $steps = array( |
|
17
|
|
|
"Year", |
|
18
|
|
|
"Make", |
|
19
|
|
|
"Model", |
|
20
|
|
|
"Type", |
|
21
|
|
|
"ODO" |
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
public function init() |
|
25
|
|
|
{ |
|
26
|
|
|
parent::init(); |
|
27
|
|
|
Requirements::themedCSS('ElectricVehicleCalculatorSelectPage', 'electric-vehicle-calculator'); |
|
28
|
|
|
Requirements::javascript("framework/thirdparty/jquery/jquery.js"); |
|
29
|
|
|
Requirements::javascript('electric-vehicle-calculator/javascript/EVCSelectPage.js'); |
|
30
|
|
|
Requirements::customScript("EVCSelectPage.baseLink = '".$this->AbsoluteLink()."';", "EVCSelectPageBasics"); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function SelectForm() |
|
34
|
|
|
{ |
|
35
|
|
|
$fieldList = new FieldList( |
|
36
|
|
|
NumericField::create("Year", "Manufacturing Year"), |
|
37
|
|
|
DropdownField::create("Make", "Make", array(0 => $this->pleaseSelectPhrase())), |
|
38
|
|
|
DropdownField::create("Model", "Model", array(0 => $this->pleaseSelectPhrase())), |
|
39
|
|
|
DropdownField::create("Type", "Type", array(0 => $this->pleaseSelectPhrase())), |
|
40
|
|
|
NumericField::create("ODO", "Current Odometer (overall distance travelled - as shown in your dashboard") |
|
41
|
|
|
); |
|
42
|
|
|
$actions = new FieldList( |
|
43
|
|
|
FormAction::create("doselectform")->setTitle("Start Calculation") |
|
44
|
|
|
); |
|
45
|
|
|
$form = Form::create( |
|
46
|
|
|
$this, |
|
47
|
|
|
"SelectForm", |
|
48
|
|
|
$fieldList, |
|
49
|
|
|
$actions |
|
50
|
|
|
); |
|
51
|
|
|
return $form; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function doselectform($data, $form) |
|
55
|
|
|
{ |
|
56
|
|
|
$params = array(); |
|
57
|
|
|
print_r(EdmundsAPI::get_data("/api/tco/newtruecosttoownbystyleidandzip/".$data["Type"]."/90019", $params)); |
|
58
|
|
|
die("------------"); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* action |
|
64
|
|
|
* set value and returns next list |
|
65
|
|
|
* @param HTTPRequest |
|
66
|
|
|
*/ |
|
67
|
|
|
public function nextstep($request) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
$key = $request->param("ID"); |
|
70
|
|
|
if (!in_array($key, $this->steps)) { |
|
71
|
|
|
return user_error("setting unknown value: $key"); |
|
72
|
|
|
} else { |
|
73
|
|
|
$index = array_search($key, $this->steps); |
|
74
|
|
|
$nextIndex = $index + 1; |
|
75
|
|
|
} |
|
76
|
|
|
$value = $request->param("OtherID"); |
|
77
|
|
|
if ($key == "Year") { |
|
78
|
|
|
$value = intval($value); |
|
79
|
|
|
} |
|
80
|
|
|
Session::set("EVCSelectPage_".$key, $value); |
|
81
|
|
|
$nextKey = $this->steps[$nextIndex]; |
|
82
|
|
|
$method = "get".$nextKey."s"; |
|
83
|
|
|
if ($this->hasMethod($method)) { |
|
84
|
|
|
return json_encode($this->$method()); |
|
85
|
|
|
} |
|
86
|
|
|
return json_encode(array("0" => "sorry an error occured with $method, please reload page ...")); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
public function getValue($key) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$value = Session::get("EVCSelectPage_".$key); |
|
93
|
|
|
if ($key == "Year") { |
|
94
|
|
|
if ($value < 1900 || $value > Date("Year", strtotime("Now"))) { |
|
95
|
|
|
$value = 0; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
return $value; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
protected function getMakes() |
|
103
|
|
|
{ |
|
104
|
|
|
$returnArray = array(0 => $this->pleaseSelectPhrase()); |
|
105
|
|
|
$year = $this->getValue("Year"); |
|
106
|
|
|
if ($year) { |
|
107
|
|
|
$params = array("view" => "full"); |
|
108
|
|
|
$params["year"] = $year; |
|
109
|
|
|
$makes = EdmundsAPI::get_data("/api/vehicle/v2/makes/count", $params); |
|
110
|
|
|
foreach ($makes->makes as $make) { |
|
111
|
|
|
$returnArray[$make->niceName] = $make->niceName; |
|
112
|
|
|
} |
|
113
|
|
|
asort($returnArray); |
|
114
|
|
|
} |
|
115
|
|
|
return $returnArray; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
protected function getModels() |
|
120
|
|
|
{ |
|
121
|
|
|
$returnArray = array(0 => $this->pleaseSelectPhrase()); |
|
122
|
|
|
$makeNiceName = $this->getValue("Make"); |
|
123
|
|
|
$year = $this->getValue("Year"); |
|
124
|
|
|
if ($makeNiceName && $year) { |
|
125
|
|
|
$params = array(); |
|
126
|
|
|
$params["year"] = $year; |
|
127
|
|
|
$models = EdmundsAPI::get_data("/api/vehicle/v2/".$makeNiceName, $params); |
|
128
|
|
|
foreach ($models->models as $model) { |
|
129
|
|
|
$returnArray[$model->niceName] = $model->name; |
|
130
|
|
|
} |
|
131
|
|
|
asort($returnArray); |
|
132
|
|
|
} |
|
133
|
|
|
return $returnArray; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
protected function getTypes() |
|
138
|
|
|
{ |
|
139
|
|
|
$returnArray = array(0 => $this->pleaseSelectPhrase()); |
|
140
|
|
|
$year = $this->getValue("Year"); |
|
141
|
|
|
$makeNiceName = $this->getValue("Make"); |
|
142
|
|
|
$modelNiceName = $this->getValue("Model"); |
|
143
|
|
|
if ($year && $makeNiceName && $modelNiceName) { |
|
144
|
|
|
$params = array("view" => "full"); |
|
145
|
|
|
$params["year"] = $year; |
|
146
|
|
|
$types = EdmundsAPI::get_data("/api/vehicle/v2/".$makeNiceName."/".$modelNiceName, $params); |
|
147
|
|
|
foreach ($types->years as $loopYear) { |
|
148
|
|
|
if (intval($loopYear->year) == intval($year)) { |
|
149
|
|
|
foreach ($loopYear->styles as $style) { |
|
150
|
|
|
$returnArray[$style->id] = $style->name; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
asort($returnArray); |
|
155
|
|
|
} |
|
156
|
|
|
return $returnArray; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function pleaseSelectPhrase() |
|
160
|
|
|
{ |
|
161
|
|
|
return "-- please answer all previous questions --"; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
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.