Issues (54)

src/Model/RecipeDirection.php (8 issues)

1
<?php
2
3
namespace Dynamic\RecipeBook\Model;
4
5
use Dynamic\RecipeBook\Page\RecipePage;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
8
use SilverStripe\ORM\DataObject;
9
10
/**
11
 * Class RecipeDirection
12
 * @package Dynamic\RecipeBook\Page
13
 *
14
 * @property string $Title
15
 * @property int $Sort
16
 * @property int $RecipeID
17
 * @method RecipePage Recipe()
18
 */
19
class RecipeDirection extends DataObject
20
{
21
    /**
22
     * @var array
23
     */
24
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
25
        'Title' => 'HTMLText',
26
        'Sort' => 'Int',
27
    ];
28
29
    /**
30
     * @var array
31
     */
32
    private static $has_one = [
0 ignored issues
show
The private property $has_one is not used, and could be removed.
Loading history...
33
        'Recipe' => RecipePage::class,
34
    ];
35
36
    /**
37
     * @var string
38
     */
39
    private static $default_sort = 'Sort';
0 ignored issues
show
The private property $default_sort is not used, and could be removed.
Loading history...
40
41
    /**
42
     * @var string
43
     */
44
    private static $table_name = 'RecipeDirection';
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
45
46
    /**
47
     * @return FieldList
48
     */
49
    public function getCMSFields()
50
    {
51
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
52
            $recipe = $fields->dataFieldByName('RecipeID');
53
            $fields->replaceField('RecipeID', $recipe->performReadonlyTransformation());
54
55
            $fields->addFieldToTab(
56
                'Root.Main',
57
                HTMLEditorField::create('Title')
58
                    ->setTitle('Direction Step')
59
                    ->setRows(5)
60
            );
61
62
            $fields->removeByName('Sort');
63
        });
64
65
        return parent::getCMSFields();
66
    }
67
68
    /**
69
     *
70
     */
71
    protected function onBeforeWrite()
72
    {
73
        parent::onBeforeWrite();
74
75
        if (!$this->Sort) {
76
            $this->Sort = static::get()->filter('RecipeID', $this->RecipeID)->max('Sort') + 1;
77
        }
78
    }
79
80
    /**
81
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
82
     * @param array $context
83
     * @return bool
84
     */
85
    public function canCreate($member = null, $context = [])
86
    {
87
        return true;
88
    }
89
90
    /**
91
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
92
     * @return bool
93
     */
94
    public function canEdit($member = null)
95
    {
96
        return true;
97
    }
98
99
    /**
100
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
101
     * @return bool
102
     */
103
    public function canDelete($member = null)
104
    {
105
        return true;
106
    }
107
108
    /**
109
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
110
     * @return bool
111
     */
112
    public function canView($member = null)
113
    {
114
        return true;
115
    }
116
}
117