21
21
class Field extends \craft \base \Field
22
22
{
23
23
24
+ const ADD_ANY = 'addAny ' ;
25
+ const ADD_PERMISSION = 'addPermission ' ;
26
+ const ADD_NONE = 'addNone ' ;
27
+ const DELETE_ANY = 'deleteAny ' ;
28
+ const DELETE_PERMISSION = 'deletePermission ' ;
29
+ const DELETE_NONE = 'deleteNone ' ;
30
+
24
31
public static $ table = '{{%notes}} ' ;
25
32
public static $ dateFormat = 'M j, Y g:ia ' ;
26
33
27
- /** @var bool Will allow the deleting of notes if true */
28
- public $ allowDeleting = false ;
34
+ /** @var bool|string Manage who can add notes */
35
+ public $ allowAdding = self ::ADD_PERMISSION ;
36
+
37
+ /** @var bool|string Manage who can delete notes */
38
+ public $ allowDeleting = self ::DELETE_PERMISSION ;
29
39
30
40
public static function displayName (): string
31
41
{
@@ -58,8 +68,16 @@ public function normalizeValue ($value, ElementInterface $element = null)
58
68
59
69
public function getSettingsHtml ()
60
70
{
71
+ // 1.0.2 back-compat
72
+ $ allowDeleting = $ this ->allowDeleting ;
73
+ if ($ allowDeleting === true ) $ allowDeleting = self ::DELETE_ANY ;
74
+ elseif ($ allowDeleting === false ) $ allowDeleting = self ::DELETE_NONE ;
75
+
61
76
return Craft::$ app ->getView ()->renderTemplate ('notes/settings ' , [
62
- 'allowDeleting ' => $ this ->allowDeleting ,
77
+ 'allowAdding ' => $ this ->allowAdding ,
78
+ 'allowDeleting ' => $ allowDeleting ,
79
+ 'addOpts ' => self ::_addOpts (),
80
+ 'deleteOpts ' => self ::_deleteOpts (),
63
81
]);
64
82
}
65
83
@@ -75,8 +93,64 @@ protected function inputHtml ($value, ElementInterface $element = null): string
75
93
'ns ' => $ this ->handle ,
76
94
'element ' => $ element ,
77
95
'notes ' => $ value ,
78
- 'allowDeleting ' => $ this ->allowDeleting ,
96
+ 'allowAdding ' => $ this ->_canAdd (),
97
+ 'allowDeleting ' => $ this ->_canDelete (),
79
98
]);
80
99
}
81
100
101
+ // Helpers
102
+ // =========================================================================
103
+
104
+ private static function _addOpts ()
105
+ {
106
+ return [
107
+ self ::ADD_ANY => Craft::t ('notes ' , 'All users ' ),
108
+ self ::ADD_PERMISSION => Craft::t ('notes ' , 'User permission ' ),
109
+ self ::ADD_NONE => Craft::t ('notes ' , 'Disabled ' ),
110
+ ];
111
+ }
112
+
113
+ private static function _deleteOpts ()
114
+ {
115
+ return [
116
+ self ::DELETE_ANY => Craft::t ('notes ' , 'All users ' ),
117
+ self ::DELETE_PERMISSION => Craft::t ('notes ' , 'User permission ' ),
118
+ self ::DELETE_NONE => Craft::t ('notes ' , 'Disabled ' ),
119
+ ];
120
+ }
121
+
122
+ private function _canAdd ()
123
+ {
124
+ switch ($ this ->allowAdding )
125
+ {
126
+ case self ::ADD_PERMISSION :
127
+ return Craft::$ app ->user ->checkPermission ('addNotes ' );
128
+ case self ::ADD_NONE :
129
+ return false ;
130
+ default :
131
+ case self ::ADD_ANY :
132
+ return true ;
133
+ }
134
+ }
135
+
136
+ private function _canDelete ()
137
+ {
138
+ $ user = Craft::$ app ->getUser ();
139
+
140
+ switch ($ this ->allowDeleting )
141
+ {
142
+ case self ::DELETE_PERMISSION :
143
+ return $ user ->checkPermission ('deleteAllNotes ' )
144
+ ? true
145
+ : $ user ->checkPermission ('deleteOwnNotes ' )
146
+ ? 'own '
147
+ : false ;
148
+ case self ::DELETE_NONE :
149
+ return false ;
150
+ default :
151
+ case self ::DELETE_ANY :
152
+ return true ;
153
+ }
154
+ }
155
+
82
156
}
0 commit comments