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         this.panel  = parentLayout.add(region,  new Ext.GridPanel(this.grid ,
156             { fitToframe: true,fitContainer: true, title: _this.title, id : _this.id, background: true})
157         );
158
159         Roo.log(['Groups', parentLayout, this.panel]);
160         
161         this.grid.render();
162         
163         
164         new Ext.dd.DropTarget(_this.grid.getView().mainBody, {  
165             ddGroup : 'groupDD',  
166             copy       : true,
167
168             notifyOver : function(dd, e, data){  
169                 var t = Roo.lib.Event.getTarget(e); 
170                 var ri = _this.grid.view.findRowIndex(t);
171                 var rid  = false;
172                 if (ri !== false) {
173                     rid = _this.grid.getDataSource().getAt(ri).data;
174                 }
175                 
176                 var s = _this.grid.getSelectionModel().getSelections();
177                 
178                 var isFromGroup = s.length ? s[0].data.id > 0 : false;
179                 
180                 var isToGroup = rid && rid.id > 0;
181                 
182                 if (isFromGroup && isToGroup) {
183                     return this.dropNotAllowed; 
184                 }
185                 if (!isFromGroup && !isToGroup) {
186                     return this.dropNotAllowed; 
187                 }
188                 if (isFromGroup && !isToGroup) {
189                     return 'x-dd-drop-ok-sub'; 
190                 } 
191                 //if (!isFromGroup && isToGroup) {
192                     return 'x-dd-drop-ok-add'; 
193                 //}
194                 
195                   
196             },  
197             notifyDrop : function(dd, e, data){  
198                 
199                 var t = Roo.lib.Event.getTarget(e); 
200                 var ri = _this.grid.view.findRowIndex(t);
201                 var rid  = false;
202                 if (ri !== false) {
203                     rid = _this.grid.getDataSource().getAt(ri).data;
204                 }
205                 var s = _this.grid.getSelectionModel().getSelections();
206                   
207                 //console.log(data);
208                 var isFromGroup = s.length ? s[0].data.id > 0 : false;
209                 
210                 var isToGroup = rid && rid.id > 0;
211                 
212                 if (isFromGroup && isToGroup) {
213                     return false;
214                 }
215                 if (!isFromGroup && !isToGroup) {
216                     return false;
217                 }
218                 var action = 'add';
219                 if (isFromGroup && !isToGroup) {
220                     action = 'sub';
221                     //return 'x-dd-drop-ok-sub'; 
222                 }
223                 // build a list of selections.
224                 var sels = [];
225                 for (var i=0; i < data.selections.length; i++) {
226                     sels.push(data.selections[i].data.id);
227                 }
228                 
229                 new Pman.Request({
230                     url: baseURL + '/Core/GroupMembers.php',
231                     params: {
232                         action : action,
233                         group_id: action =='add' ? rid.id : s[0].data.id,
234                         type: _this.type,
235                         user_ids : sels.join(',')
236                         
237                     },  
238                     method: 'POST',  
239                     success : function(data) {
240                         refreshPager();
241                     }, 
242                     
243                     failure: function() {
244                         //Ext.get(document.body).unmask();
245                         //if (cb) {
246                         //    cb.call(false);
247                         //}
248                          
249                     }
250                 });
251                 
252                 
253                 
254                 //if (!isFromGroup && isToGroup) {
255                     //return 'x-dd-drop-ok-add'; 
256                 return true;
257                 //}
258                 
259                   
260             }
261         });  
262         
263         /*
264         var gridFoot = this.grid.getView().getFooterPanel(true);
265         
266         this.paging = new Ext.PagingToolbar(gridFoot, this.grid.getDataSource(), {
267             pageSize: 25,
268             displayInfo: true,
269             displayMsg: '',
270             emptyMsg: ''
271         });
272         */
273         var grid = this.grid;
274  
275         var gridHead = this.grid.getView().getHeaderPanel(true);
276         this.toolbar = new Ext.Toolbar(gridHead);
277           
278  
279         var _dialog= this.getDialog();
280         this.toolbar.add({
281             
282             text: "Manage Groups",
283             cls: 'x-btn-text-icon',
284             icon: Ext.rootURL + 'images/default/tree/leaf.gif',
285             menu : {
286                 items : [
287                     
288                     {
289                         text: "Add",
290                          cls: 'x-btn-text-icon',
291                         icon: Ext.rootURL + 'images/default/dd/drop-add.gif',
292                         hidden : !Pman.hasPerm('Core.Groups', 'A'),
293                         handler : function(){
294                             _dialog.show( { id : 0, type: _this.type }, refreshPager ); 
295                         }
296                     }, 
297                     {
298                         text: "Edit",
299                         cls: 'x-btn-text-icon',
300                         icon: Ext.rootURL + 'images/default/tree/leaf.gif',
301                         hidden : !Pman.hasPerm('Core.Groups', 'E'),
302                         handler : function() {
303                             var s = grid.getSelectionModel().getSelections();
304                             if (!s.length || (s.length > 1))  {
305                                 Ext.MessageBox.alert("Error", s.length ? "Select only one Row" : "Select a Row");
306                                 return;
307                             }
308                             if ((s[0].data.name == 'Administrators') ||(s[0].data.name == 'Default')) {
309                                 Ext.MessageBox.alert("Error", "You can not rename that group");
310                                 return;
311                             }
312                             if (s[0].data.id < 1) {
313                                 Ext.MessageBox.alert("Error", "You can not rename that group");
314                                 return;
315                             }
316                             _dialog.show(s[0].data, refreshPager); 
317                         }
318                     },  
319                     
320                     {
321                         text: "Delete",
322                          cls: 'x-btn-text-icon',
323                         icon: rootURL + '/Pman/templates/images/trash.gif',
324                         hidden : !Pman.hasPerm('Core.Groups', 'D'),
325                         handler : function(){
326                             var s = grid.getSelectionModel().getSelections();
327                               
328                             for(var i = 0; i < s.length; i++) {
329                                 
330                                 if ((s[i].data.id < 1) || (s[i].data.name == 'Administrators')) {
331                                     Ext.MessageBox.alert("Error", "You can not delete that group");
332                                     return;
333                                 }
334                             }
335                             
336                             
337                             Pman.genericDelete(_this, 'Groups'); 
338                         } 
339                     } , '-',
340                       {
341                         text: "Reload",
342                          cls: 'x-btn-text-icon',
343                            icon: rootURL + '/Pman/templates/images/view-refresh.gif',
344                         handler : function(){
345                             refreshPager();
346                         }
347                     }
348                 ]
349             }
350                     
351         });
352         this.panel.on('activate', function() {
353            // refreshPager();
354         });
355             
356         //this.toolbar = tb;
357         // add stuff to toolbar?
358         //this.innerLayout.endUpdate();
359         
360         
361         
362     },
363     refresh: function()
364     {
365         this.grid.getDataSource().reload();   
366     } /*,
367    // - is this used anymore? 
368    
369     show: function (parentLayout, region)
370     {
371         this.add(parentLayout, region);
372         this.grid.getDataSource().load({
373             params: {
374                 type: _this.type
375             }
376         });
377
378     }
379     */
380 };