Pman.Tab.GroupsList.js
[Pman.Core] / Pman.Tab.GroupsList.js
1
2 //<script type="text/javascript">
3
4 // generic groups listing tab
5
6
7 Pman.Tab.GroupsList = function(config) {
8     Ext.apply(this, config);
9 };
10
11 Pman.Tab.GroupsList.prototype = {
12     id : false,
13     grid : false,
14     panel : false,
15     getDialog : false,
16     title : false,
17     type : 0,
18     disabled : false,
19     add : function(parentLayout, region) {
20         
21         var _this = this;
22         
23         var refreshPager = function() {
24             _this.refresh();
25         }
26         
27         if (this.panel) {
28             parentLayout.getRegion(region).showPanel(this.panel);
29             return;
30         }
31           
32         var refreshCenterPanel = function()
33         {
34             var actpan = parentLayout.getRegion('center').getActivePanel();
35             if (actpan && actpan.controller) {
36                 actpan.controller.refresh();
37                 return;
38             }
39             // depreciated..
40             var agid = actpan.id;
41             if (!agid) {
42                 return;
43             }
44             Pman.Tab[agid].refresh();
45         }
46         
47         var frm = parentLayout.getRegion(region).getEl().createChild({tag:'div'});
48         //this.grid = new Ext.grid.EditorGrid(frm,  {
49         this.grid = new Ext.grid.Grid(frm,  {
50                 id: _this.id + '-groups',
51                 
52                 //enableDragDrop:true,
53                 enableDrop:true,
54                 ddGroup: 'groupDD',
55                 
56                 //dropConfig: {
57                 //    appendOnly : true,
58                 //    ddGroup: 'groupDD' 
59                 //},
60                 ds:   new Ext.data.Store({
61                     // load using HTTP
62                     
63                     
64                     proxy: new Ext.data.HttpProxy({
65                         url: baseURL + '/Roo/Groups.html',
66                         method: 'GET'
67                     }),
68                     remoteSort: true,
69                     reader: Pman.Readers.Groups,
70                     sortInfo: {
71                         field: 'name', direction: 'ASC'
72                     },
73                     listeners : {
74                         
75                         beforeload: function(t, o) {
76                             //console.log(o.params);
77                             if (!o.params) {
78                                 o.params = {};
79                             }
80                             o.params.type = _this.type;
81                             
82                             
83                         },
84                         load : function()
85                         {
86                             var sm = _this.grid.getSelectionModel();
87                             if (!sm.getSelections().length) {
88                                 sm.selectFirstRow();
89                             }
90                             refreshCenterPanel();
91                           //  Pman.Tab.Documents_In.delayedCreate();
92                           //  Pman.Tab.Documents_Out.delayedCreate();
93                         },
94                         loadexception : Pman.loadException
95                     
96                     }
97                 }),
98                 sm: new Ext.grid.RowSelectionModel({ singleSelect: true }),
99                 cm: new Ext.grid.ColumnModel(
100                     [{
101                         id : _this.id + '-name',
102                         header : "Name",
103                         dataIndex : 'name',
104                         sortable : true,
105                         width : 100,
106                         renderer : function(v,x,r) {
107                             if (r.data.id == -1) {
108                                 return '<b>' + "Not in a Group" + '</b>';
109                             }
110                             if ((r.data.id == 0) && (_this.type == 0)) {
111                                 return '<b>' + "All Staff (Default Permissions)" + '</b>';
112                             }
113                             if ((r.data.id == 0) && (_this.type == 2)) {
114                                 return '<b>' + "Everybody" + '</b>';
115                             }
116                             if (r.data.id == 0) {
117                                 return '<b>' + "All Staff" + '</b>';
118                             }
119                             if (v == 'Administrators') {
120                                 return '<b>' + "Adminstrators" + '</b>';
121                             }
122                             if (r.data.leader) {
123                                 return v + ' (' + r.data.leader_name + ')';
124                             }
125                             
126                             return v;
127                             /*
128                             switch (v) {
129                                 case 'Default':
130                                     return '<b>' + "All Staff (Default Perms.)" + '</b>';
131                                 case 'Administrators':
132                                     return '<b>' + "Administrators" + '</b>';
133                                 default: 
134                                     return v;
135                             }
136                             */
137                             
138                        }
139                     }]
140                 ),
141                 autoExpandColumn: _this.id + '-name' , // fixme!!!!
142                 clicksToEdit : 1,
143                 
144                 loadMask: true,
145                 listeners : {
146                     rowclick: function(g, ri, e)
147                     {
148                         refreshCenterPanel();
149                     } 
150                 }
151                  
152         });
153         // add selection changed...
154         
155         Roo.log(['Groups', this.panel]);
156         
157         this.panel  = parentLayout.add(region,  new Ext.GridPanel(this.grid ,
158             { fitToframe: true,fitContainer: true, title: _this.title, id : _this.id, background: true})
159         );
160         this.grid.render();
161         
162         
163         new Ext.dd.DropTarget(_this.grid.getView().mainBody, {  
164             ddGroup : 'groupDD',  
165             copy       : true,
166
167             notifyOver : function(dd, e, data){  
168                 var t = Roo.lib.Event.getTarget(e); 
169                 var ri = _this.grid.view.findRowIndex(t);
170                 var rid  = false;
171                 if (ri !== false) {
172                     rid = _this.grid.getDataSource().getAt(ri).data;
173                 }
174                 
175                 var s = _this.grid.getSelectionModel().getSelections();
176                 
177                 var isFromGroup = s.length ? s[0].data.id > 0 : false;
178                 
179                 var isToGroup = rid && rid.id > 0;
180                 
181                 if (isFromGroup && isToGroup) {
182                     return this.dropNotAllowed; 
183                 }
184                 if (!isFromGroup && !isToGroup) {
185                     return this.dropNotAllowed; 
186                 }
187                 if (isFromGroup && !isToGroup) {
188                     return 'x-dd-drop-ok-sub'; 
189                 } 
190                 //if (!isFromGroup && isToGroup) {
191                     return 'x-dd-drop-ok-add'; 
192                 //}
193                 
194                   
195             },  
196             notifyDrop : function(dd, e, data){  
197                 
198                 var t = Roo.lib.Event.getTarget(e); 
199                 var ri = _this.grid.view.findRowIndex(t);
200                 var rid  = false;
201                 if (ri !== false) {
202                     rid = _this.grid.getDataSource().getAt(ri).data;
203                 }
204                 var s = _this.grid.getSelectionModel().getSelections();
205                   
206                 //console.log(data);
207                 var isFromGroup = s.length ? s[0].data.id > 0 : false;
208                 
209                 var isToGroup = rid && rid.id > 0;
210                 
211                 if (isFromGroup && isToGroup) {
212                     return false;
213                 }
214                 if (!isFromGroup && !isToGroup) {
215                     return false;
216                 }
217                 var action = 'add';
218                 if (isFromGroup && !isToGroup) {
219                     action = 'sub';
220                     //return 'x-dd-drop-ok-sub'; 
221                 }
222                 // build a list of selections.
223                 var sels = [];
224                 for (var i=0; i < data.selections.length; i++) {
225                     sels.push(data.selections[i].data.id);
226                 }
227                 
228                 new Pman.Request({
229                     url: baseURL + '/Core/GroupMembers.php',
230                     params: {
231                         action : action,
232                         group_id: action =='add' ? rid.id : s[0].data.id,
233                         type: _this.type,
234                         user_ids : sels.join(',')
235                         
236                     },  
237                     method: 'POST',  
238                     success : function(data) {
239                         refreshPager();
240                     }, 
241                     
242                     failure: function() {
243                         //Ext.get(document.body).unmask();
244                         //if (cb) {
245                         //    cb.call(false);
246                         //}
247                          
248                     }
249                 });
250                 
251                 
252                 
253                 //if (!isFromGroup && isToGroup) {
254                     //return 'x-dd-drop-ok-add'; 
255                 return true;
256                 //}
257                 
258                   
259             }
260         });  
261         
262         /*
263         var gridFoot = this.grid.getView().getFooterPanel(true);
264         
265         this.paging = new Ext.PagingToolbar(gridFoot, this.grid.getDataSource(), {
266             pageSize: 25,
267             displayInfo: true,
268             displayMsg: '',
269             emptyMsg: ''
270         });
271         */
272         var grid = this.grid;
273  
274         var gridHead = this.grid.getView().getHeaderPanel(true);
275         this.toolbar = new Ext.Toolbar(gridHead);
276           
277  
278         var _dialog= this.getDialog();
279         this.toolbar.add({
280             
281             text: "Manage Groups",
282             cls: 'x-btn-text-icon',
283             icon: Ext.rootURL + 'images/default/tree/leaf.gif',
284             menu : {
285                 items : [
286                     
287                     {
288                         text: "Add",
289                          cls: 'x-btn-text-icon',
290                         icon: Ext.rootURL + 'images/default/dd/drop-add.gif',
291                         hidden : !Pman.hasPerm('Core.Groups', 'A'),
292                         handler : function(){
293                             _dialog.show( { id : 0, type: _this.type }, refreshPager ); 
294                         }
295                     }, 
296                     {
297                         text: "Edit",
298                         cls: 'x-btn-text-icon',
299                         icon: Ext.rootURL + 'images/default/tree/leaf.gif',
300                         hidden : !Pman.hasPerm('Core.Groups', 'E'),
301                         handler : function() {
302                             var s = grid.getSelectionModel().getSelections();
303                             if (!s.length || (s.length > 1))  {
304                                 Ext.MessageBox.alert("Error", s.length ? "Select only one Row" : "Select a Row");
305                                 return;
306                             }
307                             if ((s[0].data.name == 'Administrators') ||(s[0].data.name == 'Default')) {
308                                 Ext.MessageBox.alert("Error", "You can not rename that group");
309                                 return;
310                             }
311                             if (s[0].data.id < 1) {
312                                 Ext.MessageBox.alert("Error", "You can not rename that group");
313                                 return;
314                             }
315                             _dialog.show(s[0].data, refreshPager); 
316                         }
317                     },  
318                     
319                     {
320                         text: "Delete",
321                          cls: 'x-btn-text-icon',
322                         icon: rootURL + '/Pman/templates/images/trash.gif',
323                         hidden : !Pman.hasPerm('Core.Groups', 'D'),
324                         handler : function(){
325                             var s = grid.getSelectionModel().getSelections();
326                               
327                             for(var i = 0; i < s.length; i++) {
328                                 
329                                 if ((s[i].data.id < 1) || (s[i].data.name == 'Administrators')) {
330                                     Ext.MessageBox.alert("Error", "You can not delete that group");
331                                     return;
332                                 }
333                             }
334                             
335                             
336                             Pman.genericDelete(_this, 'Groups'); 
337                         } 
338                     } , '-',
339                       {
340                         text: "Reload",
341                          cls: 'x-btn-text-icon',
342                            icon: rootURL + '/Pman/templates/images/view-refresh.gif',
343                         handler : function(){
344                             refreshPager();
345                         }
346                     }
347                 ]
348             }
349                     
350         });
351         this.panel.on('activate', function() {
352            // refreshPager();
353         });
354             
355         //this.toolbar = tb;
356         // add stuff to toolbar?
357         //this.innerLayout.endUpdate();
358         
359         
360         
361     },
362     refresh: function()
363     {
364         this.grid.getDataSource().reload();   
365     } /*,
366    // - is this used anymore? 
367    
368     show: function (parentLayout, region)
369     {
370         this.add(parentLayout, region);
371         this.grid.getDataSource().load({
372             params: {
373                 type: _this.type
374             }
375         });
376
377     }
378     */
379 };