| 1 | <?php |
||
| 2 | |||
| 3 | namespace Dynamic\RecipeBook\Model; |
||
| 4 | |||
| 5 | use Dynamic\RecipeBook\Page\RecipePage; |
||
| 6 | use SilverStripe\Forms\FieldList; |
||
| 7 | use SilverStripe\Forms\TextField; |
||
| 8 | use SilverStripe\ORM\DataObject; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Class RecipeIngredient |
||
| 12 | * @package Dynamic\Nucu\Model |
||
| 13 | * |
||
| 14 | * @property string $Title |
||
| 15 | * @property int $Sort |
||
| 16 | * @property int $RecipeID |
||
| 17 | * @method RecipePage Recipe() |
||
| 18 | */ |
||
| 19 | class RecipeIngredient extends DataObject |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private static $singular_name = 'Recipe Ingredient'; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private static $plural_name = 'Recipe Ingredients'; |
||
|
0 ignored issues
–
show
|
|||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private static $description = 'An ingredient used in a Recipe'; |
||
|
0 ignored issues
–
show
|
|||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private static $db = [ |
||
|
0 ignored issues
–
show
|
|||
| 40 | 'Title' => 'Varchar(255)', |
||
| 41 | 'Sort' => 'Int', |
||
| 42 | ]; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | private static $has_one = [ |
||
|
0 ignored issues
–
show
|
|||
| 48 | 'Recipe' => RecipePage::class, |
||
| 49 | ]; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private static $default_sort = 'Sort'; |
||
|
0 ignored issues
–
show
|
|||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private static $table_name = 'RecipeIngredient'; |
||
|
0 ignored issues
–
show
|
|||
| 60 | |||
| 61 | /** |
||
| 62 | * @return FieldList |
||
| 63 | */ |
||
| 64 | public function getCMSFields() |
||
| 65 | { |
||
| 66 | $this->beforeUpdateCMSFields(function (FieldList $fields) { |
||
| 67 | $recipe = $fields->dataFieldByName('RecipeID'); |
||
| 68 | $fields->replaceField('RecipeID', $recipe->performReadonlyTransformation()); |
||
| 69 | |||
| 70 | $fields->addFieldToTab('Root.Main', TextField::create('Title')->setTitle('Ingredient and measurement')); |
||
| 71 | $fields->removeByName('Sort'); |
||
| 72 | }); |
||
| 73 | |||
| 74 | return parent::getCMSFields(); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * |
||
| 79 | */ |
||
| 80 | protected function onBeforeWrite() |
||
| 81 | { |
||
| 82 | parent::onBeforeWrite(); |
||
| 83 | |||
| 84 | if (!$this->Sort) { |
||
| 85 | $this->Sort = static::get()->filter('RecipeID', $this->RecipeID)->max('Sort') + 1; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param null $member |
||
|
0 ignored issues
–
show
|
|||
| 91 | * @param array $context |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | public function canCreate($member = null, $context = []) |
||
| 95 | { |
||
| 96 | return true; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param null $member |
||
|
0 ignored issues
–
show
|
|||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | public function canEdit($member = null) |
||
| 104 | { |
||
| 105 | return true; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param null $member |
||
|
0 ignored issues
–
show
|
|||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function canDelete($member = null) |
||
| 113 | { |
||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param null $member |
||
|
0 ignored issues
–
show
|
|||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | public function canView($member = null) |
||
| 122 | { |
||
| 123 | return true; |
||
| 124 | } |
||
| 125 | } |
||
| 126 |