Skip to content

Commit 5c07df4

Browse files
committed
Add support for CMS 6
1 parent 4e6f6dc commit 5c07df4

8 files changed

+255
-288
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This module provides an easy way to build such tree and to add it to a page as a
1111

1212
## Requirements
1313

14-
* SilverStripe 5.x
14+
* SilverStripe 6.x
1515
* (dnadesign/silvertsripe-elemental)[https://github.com/dnadesign/silverstripe-elemental]
1616

1717
## Screenshots

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
"minimum-stability": "dev",
1818
"prefer-stable": true,
1919
"require": {
20-
"dnadesign/silverstripe-elemental": "^5",
21-
"silvershop/silverstripe-hasonefield": "^4",
22-
"unclecheese/display-logic": "^3"
20+
"dnadesign/silverstripe-elemental": "^6",
21+
"silvershop/silverstripe-hasonefield": "^5",
22+
"unclecheese/display-logic": "^4"
2323
},
2424
"config": {
2525
"allow-plugins": {

src/Extensions/ElementDecisionTreeController.php

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@
22

33
namespace DNADesign\SilverStripeElementalDecisionTree\Extensions;
44

5+
use DNADesign\SilverStripeElementalDecisionTree\Model\DecisionTreeAnswer;
6+
use DNADesign\SilverStripeElementalDecisionTree\Model\DecisionTreeStep;
57
use SilverStripe\Control\Controller;
6-
use SilverStripe\View\ArrayData;
78
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;
1011

1112
class ElementDecisionTreeController extends Extension
1213
{
13-
14-
private static $allowed_actions = [
15-
'getNextStepForAnswer'
14+
private static array $allowed_actions = [
15+
'getNextStepForAnswer',
1616
];
1717

1818
/**
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
2726
{
2827
$answerID = $this->owner->getRequest()->postVar('stepanswerid');
2928

@@ -35,7 +34,7 @@ public function getNextStepForAnswer()
3534

3635
if (!$answer || !$answer->exists()) {
3736
return $this->owner->httpError(
38-
404,
37+
404,
3938
$this->renderError('An error has occurred, please reload the page and try again!')
4039
);
4140
}
@@ -44,26 +43,27 @@ public function getNextStepForAnswer()
4443

4544
if (!$nextStep || !$nextStep->exists()) {
4645
return $this->owner->httpError(
47-
404,
46+
404,
4847
$this->renderError('An error has occurred, please reload the page and try again!')
4948
);
5049
}
5150

5251
$html = $this->owner->customise(new ArrayData([
5352
'Step' => $nextStep,
54-
'Controller' => $this->owner
53+
'Controller' => $this->owner,
5554
]))->renderWith('DNADesign\SilverStripeElementalDecisionTree\Model\DecisionTreeStep');
5655

5756
$pathway = $nextStep->getAnswerPathway();
5857

5958
$nextURL = Controller::join_links(
60-
$this->owner->AbsoluteLink(), '?decisionpathway='.implode(',', $pathway)
59+
$this->owner->AbsoluteLink(),
60+
'?decisionpathway=' . implode(',', $pathway)
6161
);
6262

6363
if ($this->owner->getRequest()->isAjax()) {
6464
$data = [
6565
'html' => $html->forTemplate(),
66-
'nexturl' => $nextURL
66+
'nexturl' => $nextURL,
6767
];
6868

6969
return json_encode($data);
@@ -73,12 +73,10 @@ public function getNextStepForAnswer()
7373
}
7474

7575
/**
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
8280
{
8381
$ids = $this->owner->getRequest()->getVar('decisionpathway');
8482

@@ -90,12 +88,12 @@ public function getInitialPathway()
9088
}
9189

9290
/**
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
9997
{
10098
if ($pathway = $this->getInitialPathway()) {
10199
return in_array($answerID, $pathway);
@@ -105,16 +103,16 @@ public function getIsAnswerSelected($answerID)
105103
}
106104

107105
/**
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
114112
{
115113
$step = DecisionTreeStep::get()->byID($stepID);
116114
if ($step->exists()) {
117-
foreach($step->Answers() as $answer) {
115+
foreach ($step->Answers() as $answer) {
118116
if ($this->getIsAnswerSelected($answer->ID)) {
119117
if ($nextStep = $answer->ResultingStep()) {
120118
return $nextStep;
@@ -127,13 +125,9 @@ public function getNextStepFromSelectedAnswer($stepID)
127125
}
128126

129127
/**
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
137131
{
138132
return sprintf(
139133
'<div class="step step--error">
@@ -142,9 +136,8 @@ protected function renderError($message = '')
142136
<span class="step-title">Sorry!</span>
143137
<span class="step-content"><p>%s</p></span>
144138
</div>
145-
</div>',
139+
</div>',
146140
$message
147141
);
148142
}
149-
150143
}

src/Forms/DecisionTreeStepPreview.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22

33
namespace DNADesign\SilverStripeElementalDecisionTree\Forms;
44

5+
use DNADesign\SilverStripeElementalDecisionTree\Model\DecisionTreeStep;
56
use SilverStripe\Forms\FormField;
67

78
/**
89
* This form field is used to display the DecisionTree in the edit form of any
910
* ElementDecisionTree or DecisionTreeStes. It provides a visual way of
1011
* navigating the tree as well as links to edit/add steps.
1112
*/
12-
1313
class DecisionTreeStepPreview extends FormField
1414
{
15-
protected $step = null;
15+
protected ?DecisionTreeStep $step = null;
1616

17-
public function __construct($name, $step = null)
17+
public function __construct($name, ?DecisionTreeStep $step = null)
1818
{
1919
$this->step = $step;
2020
parent::__construct($name);
2121
}
2222

23-
public function getStep()
23+
public function getStep(): ?DecisionTreeStep
2424
{
2525
return $this->step;
2626
}
2727

28-
public function setStep($step)
28+
public function setStep(?DecisionTreeStep $step): static
2929
{
3030
$this->step = $step;
3131

src/Forms/HasOneSelectOrCreateField.php

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,31 @@
22

33
namespace DNADesign\SilverStripeElementalDecisionTree\Forms;
44

5+
use SilverShop\HasOneField\GridFieldHasOneEditButton;
6+
use SilverShop\HasOneField\HasOneButtonField;
57
use SilverStripe\Forms\CompositeField;
68
use SilverStripe\Forms\DropdownField;
79
use SilverStripe\Forms\FormField;
8-
use SilverShop\HasOneField\HasOneButtonField;
9-
use SilverShop\HasOneField\GridFieldHasOneEditButton;
10-
/**
11-
* This a wrapper for a slightly enhanced user experience when using the
12-
* HasOneButtonField class.
13-
* By default, the HasOneButtonField only allow to create or edit the has_one object
14-
* This class adds a dropdown to allow to select another object or remove the relation
15-
* without deleting the object itself.
16-
* Both fields are wrapped in a CompositeField.
17-
*
18-
* @param String | Name of the has_one relationship
19-
* @param String | Title of the drodpwon field
20-
* @param FieldList or Array | Options listed in the dropdown
21-
* @param Object | Current has_one object, if exists
22-
* @param Object | The object calling this field, required by HasOneButtonField
23-
*/
2410

11+
/**
12+
* This a wrapper for a slightly enhanced user experience when using the
13+
* HasOneButtonField class.
14+
* By default, the HasOneButtonField only allow to create or edit the has_one object
15+
* This class adds a dropdown to allow to select another object or remove the relation
16+
* without deleting the object itself.
17+
* Both fields are wrapped in a CompositeField.
18+
*
19+
* @param Name|string of the has_one relationship
20+
* @param string|Title of the drodpwon field
21+
* @param FieldList or Array | Options listed in the dropdown
22+
* @param Current|object has_one object, if exists
23+
* @param object|The object calling this field, required by HasOneButtonField
24+
*/
2525
class HasOneSelectOrCreateField extends CompositeField
2626
{
27+
protected DropdownField $dropdown;
2728

28-
protected $dropdown;
29-
30-
protected $gridfield;
29+
protected HasOneButtonField $gridfield;
3130

3231
public function __construct($record, $name, $title, $options, $current, $parent)
3332
{
@@ -49,21 +48,20 @@ public function __construct($record, $name, $title, $options, $current, $parent)
4948
if ($current && $current->exists()) {
5049
$name = ($current->Title) ? $current->Title : $current->Name;
5150
if ($name) {
52-
$button->setButtonName('Edit '.FormField::name_to_label($name));
51+
$button->setButtonName('Edit ' . FormField::name_to_label($name));
5352
}
5453
} else {
55-
$button->setButtonName('Create '.$label);
54+
$button->setButtonName('Create ' . $label);
5655
}
5756

5857
// Set Empty String on dropdown
59-
$dropdown->setEmptyString('Select '.$label);
58+
$dropdown->setEmptyString('Select ' . $label);
6059

61-
parent::__construct(array($this->dropdown, $this->gridfield));
60+
parent::__construct([$this->dropdown, $this->gridfield]);
6261
}
6362

64-
public function getRelationName()
63+
public function getRelationName(): string
6564
{
66-
return $this->name.'ID';
65+
return $this->name . 'ID';
6766
}
68-
6967
}

0 commit comments

Comments
 (0)