2121class Field extends \craft \base \Field
2222{
2323
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+
2431 public static $ table = '{{%notes}} ' ;
2532 public static $ dateFormat = 'M j, Y g:ia ' ;
2633
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 ;
2939
3040 public static function displayName (): string
3141 {
@@ -58,8 +68,16 @@ public function normalizeValue ($value, ElementInterface $element = null)
5868
5969 public function getSettingsHtml ()
6070 {
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+
6176 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 (),
6381 ]);
6482 }
6583
@@ -75,8 +93,64 @@ protected function inputHtml ($value, ElementInterface $element = null): string
7593 'ns ' => $ this ->handle ,
7694 'element ' => $ element ,
7795 'notes ' => $ value ,
78- 'allowDeleting ' => $ this ->allowDeleting ,
96+ 'allowAdding ' => $ this ->_canAdd (),
97+ 'allowDeleting ' => $ this ->_canDelete (),
7998 ]);
8099 }
81100
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+
82156}
0 commit comments