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