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