i-lateral /
silverstripe-notifications
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ilateral\SilverStripe\Notifier\Model; |
||
| 4 | |||
| 5 | use SilverStripe\ORM\DataObject; |
||
| 6 | use SilverStripe\Forms\FieldList; |
||
| 7 | use ilateral\SilverStripe\Notifier\Model\Notification; |
||
| 8 | use SilverStripe\Forms\DropdownField; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @property string FieldName |
||
| 12 | * @property string Value |
||
| 13 | * @property bool WasChanged |
||
| 14 | * @property string Summary |
||
| 15 | * |
||
| 16 | * @method Notification Notification |
||
| 17 | */ |
||
| 18 | class NotificationRule extends DataObject |
||
| 19 | { |
||
| 20 | private static $table_name = 'Notifications_NotificationRule'; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 21 | |||
| 22 | private static $db = [ |
||
|
0 ignored issues
–
show
|
|||
| 23 | 'FieldName' => 'Varchar', |
||
| 24 | 'Value' => 'Varchar', |
||
| 25 | 'WasChanged' => 'Boolean' |
||
| 26 | ]; |
||
| 27 | |||
| 28 | private static $has_one = [ |
||
|
0 ignored issues
–
show
|
|||
| 29 | 'Notification' => Notification::class |
||
| 30 | ]; |
||
| 31 | |||
| 32 | private static $casting = [ |
||
|
0 ignored issues
–
show
|
|||
| 33 | 'Summary' => 'Varchar' |
||
| 34 | ]; |
||
| 35 | |||
| 36 | private static $summary_fields = [ |
||
|
0 ignored issues
–
show
|
|||
| 37 | 'RuleName', |
||
| 38 | 'FieldName', |
||
| 39 | 'Value', |
||
| 40 | 'WasChanged' |
||
| 41 | ]; |
||
| 42 | |||
| 43 | private static $field_labels = [ |
||
|
0 ignored issues
–
show
|
|||
| 44 | 'RuleName' => 'Name', |
||
| 45 | 'FieldName' => 'Field Name', |
||
| 46 | 'WasChanged' => 'Was the field changed at all', |
||
| 47 | 'Value' => 'Value is equal to' |
||
| 48 | ]; |
||
| 49 | |||
| 50 | public function getRuleName(): string |
||
| 51 | { |
||
| 52 | return $this->i18n_singular_name(); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Attempt to generate a summary of this rule |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function getSummary(): string |
||
| 61 | { |
||
| 62 | $results = [$this->FieldName]; |
||
| 63 | |||
| 64 | if ($this->WasChanged === true) { |
||
| 65 | $results[] = 'Changed'; |
||
| 66 | } else { |
||
| 67 | $results[] = $this->Value; |
||
| 68 | } |
||
| 69 | |||
| 70 | return implode(': ', $results); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get a list of valid field names and their labels |
||
| 75 | * |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | public function getValidFields(): array |
||
| 79 | { |
||
| 80 | $fields = []; |
||
| 81 | $class = $this->Notification()->BaseClassName; |
||
| 82 | |||
| 83 | if (!empty($class) && class_exists($class)) { |
||
| 84 | $obj = singleton($class); |
||
| 85 | |||
| 86 | foreach ($obj::config()->get('db') as $field => $type) { |
||
| 87 | $fields[$field] = $obj->fieldLabel($field); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | return $fields; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function getCMSFields() |
||
| 95 | { |
||
| 96 | $self = $this; |
||
| 97 | $this->beforeUpdateCMSFields( |
||
| 98 | function (FieldList $fields) use ($self) { |
||
|
0 ignored issues
–
show
|
|||
| 99 | $fields->replaceField( |
||
| 100 | 'FieldName', |
||
| 101 | DropdownField::create( |
||
| 102 | 'FieldName', |
||
| 103 | $this->fieldLabel('FieldName'), |
||
| 104 | $this->getValidFields() |
||
| 105 | ) |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | ); |
||
| 109 | |||
| 110 | return parent::getCMSFields(); |
||
| 111 | } |
||
| 112 | } |
||
| 113 |