2
2
3
3
namespace DNADesign \SilverStripeElementalDecisionTree \Extensions ;
4
4
5
+ use DNADesign \SilverStripeElementalDecisionTree \Model \DecisionTreeAnswer ;
6
+ use DNADesign \SilverStripeElementalDecisionTree \Model \DecisionTreeStep ;
5
7
use SilverStripe \Control \Controller ;
6
- use SilverStripe \View \ArrayData ;
7
8
use SilverStripe \Core \Extension ;
8
- use DNADesign \ SilverStripeElementalDecisionTree \ Model \DecisionTreeStep ;
9
- use DNADesign \ SilverStripeElementalDecisionTree \ Model \ DecisionTreeAnswer ;
9
+ use SilverStripe \ Model \ArrayData ;
10
+ use SilverStripe \ ORM \ FieldType \ DBHTMLText ;
10
11
11
12
class ElementDecisionTreeController extends Extension
12
13
{
13
-
14
- private static $ allowed_actions = [
15
- 'getNextStepForAnswer '
14
+ private static array $ allowed_actions = [
15
+ 'getNextStepForAnswer ' ,
16
16
];
17
17
18
18
/**
19
- * Return the HTMl for the next step to be displayed
20
- * as well as the updated URL which includes the ids of the answers
21
- * leading to this next step to be returned
22
- *
23
- * @param stepanswerid (POST)
24
- * @return json
25
- */
26
- public function getNextStepForAnswer ()
19
+ * Return the HTMl for the next step to be displayed
20
+ * as well as the updated URL which includes the ids of the answers
21
+ * leading to this next step to be returned.
22
+ *
23
+ * @param stepanswerid (POST)
24
+ */
25
+ public function getNextStepForAnswer (): null |bool |string |DBHTMLText
27
26
{
28
27
$ answerID = $ this ->owner ->getRequest ()->postVar ('stepanswerid ' );
29
28
@@ -35,7 +34,7 @@ public function getNextStepForAnswer()
35
34
36
35
if (!$ answer || !$ answer ->exists ()) {
37
36
return $ this ->owner ->httpError (
38
- 404 ,
37
+ 404 ,
39
38
$ this ->renderError ('An error has occurred, please reload the page and try again! ' )
40
39
);
41
40
}
@@ -44,26 +43,27 @@ public function getNextStepForAnswer()
44
43
45
44
if (!$ nextStep || !$ nextStep ->exists ()) {
46
45
return $ this ->owner ->httpError (
47
- 404 ,
46
+ 404 ,
48
47
$ this ->renderError ('An error has occurred, please reload the page and try again! ' )
49
48
);
50
49
}
51
50
52
51
$ html = $ this ->owner ->customise (new ArrayData ([
53
52
'Step ' => $ nextStep ,
54
- 'Controller ' => $ this ->owner
53
+ 'Controller ' => $ this ->owner ,
55
54
]))->renderWith ('DNADesign\SilverStripeElementalDecisionTree\Model\DecisionTreeStep ' );
56
55
57
56
$ pathway = $ nextStep ->getAnswerPathway ();
58
57
59
58
$ nextURL = Controller::join_links (
60
- $ this ->owner ->AbsoluteLink (), '?decisionpathway= ' .implode (', ' , $ pathway )
59
+ $ this ->owner ->AbsoluteLink (),
60
+ '?decisionpathway= ' . implode (', ' , $ pathway )
61
61
);
62
62
63
63
if ($ this ->owner ->getRequest ()->isAjax ()) {
64
64
$ data = [
65
65
'html ' => $ html ->forTemplate (),
66
- 'nexturl ' => $ nextURL
66
+ 'nexturl ' => $ nextURL,
67
67
];
68
68
69
69
return json_encode ($ data );
@@ -73,12 +73,10 @@ public function getNextStepForAnswer()
73
73
}
74
74
75
75
/**
76
- * Returns an array of DecisionStepID from the URL param
77
- * in order to display the same question when we reload the page
78
- *
79
- * @return Array
80
- */
81
- public function getInitialPathway ()
76
+ * Returns an array of DecisionStepID from the URL param
77
+ * in order to display the same question when we reload the page.
78
+ */
79
+ public function getInitialPathway (): ?array
82
80
{
83
81
$ ids = $ this ->owner ->getRequest ()->getVar ('decisionpathway ' );
84
82
@@ -90,12 +88,12 @@ public function getInitialPathway()
90
88
}
91
89
92
90
/**
93
- * Check if an answer should be selected by default
94
- * ie. The question depending on it is displayed
95
- *
96
- * @return Boolean
97
- */
98
- public function getIsAnswerSelected ($ answerID )
91
+ * Check if an answer should be selected by default
92
+ * ie. The question depending on it is displayed.
93
+ *
94
+ * @param mixed $answerID
95
+ */
96
+ public function getIsAnswerSelected ($ answerID ): bool
99
97
{
100
98
if ($ pathway = $ this ->getInitialPathway ()) {
101
99
return in_array ($ answerID , $ pathway );
@@ -105,16 +103,16 @@ public function getIsAnswerSelected($answerID)
105
103
}
106
104
107
105
/**
108
- * Gets the next step to be displayed in regards to the selected answer.
109
- * Used by template to display all the relevant steps from the URL
110
- *
111
- * @return DecisionTreeStep
112
- */
113
- public function getNextStepFromSelectedAnswer ($ stepID )
106
+ * Gets the next step to be displayed in regards to the selected answer.
107
+ * Used by template to display all the relevant steps from the URL.
108
+ *
109
+ * @param mixed $stepID
110
+ */
111
+ public function getNextStepFromSelectedAnswer ($ stepID ): ? DecisionTreeStep
114
112
{
115
113
$ step = DecisionTreeStep::get ()->byID ($ stepID );
116
114
if ($ step ->exists ()) {
117
- foreach ($ step ->Answers () as $ answer ) {
115
+ foreach ($ step ->Answers () as $ answer ) {
118
116
if ($ this ->getIsAnswerSelected ($ answer ->ID )) {
119
117
if ($ nextStep = $ answer ->ResultingStep ()) {
120
118
return $ nextStep ;
@@ -127,13 +125,9 @@ public function getNextStepFromSelectedAnswer($stepID)
127
125
}
128
126
129
127
/**
130
- * Template returned via ajax in case of an error occuring.
131
- *
132
- * @param string
133
- *
134
- * @return string
135
- */
136
- protected function renderError ($ message = '' )
128
+ * Template returned via ajax in case of an error occuring.
129
+ */
130
+ protected function renderError (string $ message = '' ): string
137
131
{
138
132
return sprintf (
139
133
'<div class="step step--error">
@@ -142,9 +136,8 @@ protected function renderError($message = '')
142
136
<span class="step-title">Sorry!</span>
143
137
<span class="step-content"><p>%s</p></span>
144
138
</div>
145
- </div> ' ,
139
+ </div> ' ,
146
140
$ message
147
141
);
148
142
}
149
-
150
143
}
0 commit comments