Pman.Tab.AdminContactsGroup.js
[Pman.Admin] / Pman.Tab.AdminContactsGroup.js
1 //<script type="text/javascript">
2
3 // Auto generated file - created by app.Builder.js- do not edit directly (at present!)
4
5
6
7 // register the module first
8 Pman.on('beforeload', function()
9 {
10     Pman.register({
11         modKey : '001-Pman.Tab.AdminContactsGroup',
12         module : Pman.Tab.AdminContactsGroup,
13         region : 'center',
14         parent : Pman.Tab.AdminContactsManager,
15         name : "Pman.Tab.AdminContactsGroup",
16         disabled : false, 
17         permname: '' 
18     });
19 });
20
21 Pman.Tab.AdminContactsGroup = new Roo.util.Observable({
22
23     panel : false,
24     disabled : false,
25     parentLayout:  false,
26
27     add : function(parentLayout, region)
28     {
29
30         var _this = this;
31         this.parentLayout = parentLayout;
32
33         this.panel = parentLayout.addxtype({
34             xtype: 'GridPanel',
35             xns: Roo,
36             listeners : {
37                 activate : function() {
38                     _this.panel = this;
39                     if (_this.grid) {
40                         _this.grid.ds.load({});
41                     }
42                 }
43             },
44             background : true,
45             fitContainer : true,
46             fitToframe : true,
47             region : 'west',
48             tableName : 'Groups',
49             title : "Groups",
50             grid : {
51                 xtype: 'Grid',
52                 xns: Roo.grid,
53                 listeners : {
54                     render : function() 
55                     {
56                         _this.grid = this; 
57                         _this.dialog = Pman.Dialog.Groups;
58                         if (_this.panel.active) {
59                             _this.grid.ds.load({});
60                         }
61                     },
62                     rowdblclick : function (_self, rowIndex, e)
63                     {
64                         if (!_this.dialog) return;
65                         _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {
66                             _this.grid.footer.onClick('first');
67                         }); 
68                     }
69                 },
70                 autoExpandColumn : 'name',
71                 ddGroup : 'groupDD',
72                 enableDrop : true,
73                 loadMask : true,
74                 dataSource : {
75                     xtype: 'Store',
76                     xns: Roo.data,
77                     listeners : {
78                         beforeload : function (_self, o)
79                         {
80                             o.params = o.params || {};
81                             o.params.type =2;
82                         },
83                         load : function (_self, records, options)
84                         {
85                             var sm = _this.grid.getSelectionModel();
86                                 if (!sm.getSelections().length) {
87                                     sm.selectFirstRow();
88                                 }
89                                 Pman.Tab.AdminContacts.grid.footer.onClick('first');
90                         }
91                     },
92                     remoteSort : true,
93                     sortInfo : { field : 'name', direction: 'ASC' },
94                     proxy : {
95                         xtype: 'HttpProxy',
96                         xns: Roo.data,
97                         method : 'GET',
98                         url : baseURL + '/Roo/Groups.php'
99                     },
100                     reader : {
101                         xtype: 'JsonReader',
102                         xns: Roo.data,
103                         totalProperty : 'total',
104                         root : 'data',
105                         id : 'id',
106                         fields : [
107                             {
108                                 'name': 'id',
109                                 'type': 'int'
110                             },
111                             {
112                                 'name': 'name',
113                                 'type': 'string'
114                             },
115                             {
116                                 'name': 'type',
117                                 'type': 'int'
118                             },
119                             {
120                                 'name': 'leader',
121                                 'type': 'int'
122                             },
123                             {
124                                 'name': 'leader_id',
125                                 'type': 'int'
126                             },
127                             {
128                                 'name': 'leader_office_id',
129                                 'type': 'int'
130                             },
131                             {
132                                 'name': 'leader_name',
133                                 'type': 'string'
134                             },
135                             {
136                                 'name': 'leader_phone',
137                                 'type': 'string'
138                             },
139                             {
140                                 'name': 'leader_fax',
141                                 'type': 'string'
142                             },
143                             {
144                                 'name': 'leader_email',
145                                 'type': 'string'
146                             },
147                             {
148                                 'name': 'leader_company_id',
149                                 'type': 'int'
150                             },
151                             {
152                                 'name': 'leader_role',
153                                 'type': 'string'
154                             },
155                             {
156                                 'name': 'leader_active',
157                                 'type': 'int'
158                             },
159                             {
160                                 'name': 'leader_remarks',
161                                 'type': 'string'
162                             },
163                             {
164                                 'name': 'leader_passwd',
165                                 'type': 'string'
166                             },
167                             {
168                                 'name': 'leader_owner_id',
169                                 'type': 'int'
170                             },
171                             {
172                                 'name': 'leader_lang',
173                                 'type': 'string'
174                             },
175                             {
176                                 'name': 'leader_no_reset_sent',
177                                 'type': 'int'
178                             },
179                             {
180                                 'name': 'leader_action_type',
181                                 'type': 'string'
182                             },
183                             {
184                                 'name': 'leader_project_id',
185                                 'type': 'int'
186                             }
187                         ]
188                     }
189                 },
190                 toolbar : {
191                     xtype: 'Toolbar',
192                     xns: Roo,
193                     items : [
194                         {
195                             xtype: 'Button',
196                             xns: Roo.Toolbar,
197                             text : "Manage Groups",
198                             menu : {
199                                 xtype: 'Menu',
200                                 xns: Roo.menu,
201                                 items : [
202                                     {
203                                         xtype: 'Item',
204                                         xns: Roo.menu,
205                                         listeners : {
206                                             click : function()
207                                             {
208                                                 if (!_this.dialog) return;
209                                                 _this.dialog.show( { id : 0,  type: 2 } , function() {
210                                                      _this.grid.ds.load({});
211                                                }); 
212                                             }
213                                         },
214                                         cls : 'x-btn-text-icon',
215                                         text : "Add",
216                                         icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
217                                     },
218                                     {
219                                         xtype: 'Item',
220                                         xns: Roo.menu,
221                                         listeners : {
222                                             click : function()
223                                             {
224                                                 var s = _this.grid.getSelectionModel().getSelections();
225                                                 if (!s.length || (s.length > 1))  {
226                                                     Roo.MessageBox.alert("Error", s.length ? "Select only one Row" : "Select a Row");
227                                                     return;
228                                                 }
229                                                 if (s[0].data.id < 1 ) {
230                                                     return;
231                                                 }
232                                                 if (!_this.dialog) return;
233                                                 _this.dialog.show(s[0].data, function() {
234                                                      _this.grid.ds.load({});
235                                                 }); 
236                                                 
237                                             }
238                                         },
239                                         cls : 'x-btn-text-icon',
240                                         text : "Edit",
241                                         icon : Roo.rootURL + 'images/default/tree/leaf.gif'
242                                     },
243                                     {
244                                         xtype: 'Item',
245                                         xns: Roo.menu,
246                                         listeners : {
247                                             click : function()
248                                             {
249                                                  Pman.genericDelete(_this, 'Groups'); 
250                                             }
251                                         },
252                                         cls : 'x-btn-text-icon',
253                                         text : "Delete",
254                                         icon : rootURL + '/Pman/templates/images/trash.gif'
255                                     },
256                                     {
257                                         xtype: 'Separator',
258                                         xns: Roo.menu
259                                     },
260                                     {
261                                         xtype: 'Item',
262                                         xns: Roo.menu,
263                                         listeners : {
264                                             click : function (_self, e)
265                                             {
266                                               _this.grid.ds.load({});
267                                             }
268                                         },
269                                         text : "Reload"
270                                     }
271                                 ]
272                             }
273                         }
274                     ]
275                 },
276                 colModel : [
277                     {
278                         xtype: 'ColumnModel',
279                         xns: Roo.grid,
280                         dataIndex : 'name',
281                         header : 'Name',
282                         width : 200,
283                         renderer : function(v,x,r) { 
284                             if (r.data.id == -1) {
285                                 return '<b>' + "Not in a Group" + '</b>';
286                             }
287                             if ((r.data.id == 0) && (_this.type == 0)) {
288                                 return '<b>' + "All Staff (Default Permissions)" + '</b>';
289                             }
290                             if ((r.data.id == 0) && (_this.type == 2)) {
291                                 return '<b>' + "Everybody" + '</b>';
292                             }
293                             if (r.data.id == 0) {
294                                 return '<b>' + "All Staff" + '</b>';
295                             }
296                             if (v == 'Administrators') {
297                                 return '<b>' + "Adminstrators" + '</b>';
298                             }
299                             if (r.data.leader) {
300                                 return v + ' (' + r.data.leader_name + ')';
301                             }
302                             
303                             return v;
304                         }
305                     }
306                 ],
307                 sm : {
308                     xtype: 'RowSelectionModel',
309                     xns: Roo.grid,
310                     listeners : {
311                         afterselectionchange : function (_self)
312                         {
313                             Pman.Tab.AdminContacts.grid.footer.onClick('first');
314                         }
315                     },
316                     singleSelect : true
317                 },
318                 dropTarget : {
319                     xtype: 'DropTarget',
320                     xns: Roo.dd,
321                     listeners : {
322                         drop : function (source, e, data)
323                         {
324                             // Roo.log("DROP");
325                             var t = Roo.lib.Event.getTarget(e); 
326                             var ri = _this.grid.view.findRowIndex(t);
327                             var rid  = false;
328                             if (ri !== false) {
329                                 rid = _this.grid.getDataSource().getAt(ri).data;
330                             }
331                             var s = _this.grid.getSelectionModel().getSelections();
332                               
333                             //console.log(data);
334                             var isFromGroup = s.length ? s[0].data.id > 0 : false;
335                         
336                             var isToGroup = rid && rid.id > 0;
337                             this.success = false;
338                             if (isFromGroup && isToGroup) {
339                                 return;
340                             }
341                             if (!isFromGroup && !isToGroup) {
342                                 return;
343                             }
344                             var action = 'add';
345                             if (isFromGroup && !isToGroup) {
346                                 action = 'sub';
347                                 //return 'x-dd-drop-ok-sub'; 
348                             }
349                             // build a list of selections.
350                             var sels = [];
351                             for (var i=0; i < data.selections.length; i++) {
352                                 sels.push(data.selections[i].data.id);
353                             }
354                         
355                             Pman.request({
356                                 url: baseURL + '/Core/GroupMembers.php',
357                                 params: {
358                                     action : action,
359                                     group_id: action =='add' ? rid.id : s[0].data.id,
360                                     type: _this.type,
361                                     user_ids : sels.join(',')
362                                     
363                                 },  
364                                 method: 'POST',  
365                                 success : function(data) {
366                                     //refreshPager();
367                                     // 
368                                     // do we need to do anything??
369                                     if (isFromGroup) {
370                                         Pman.Tab.AdminContacts.grid.footer.onClick('refresh');
371                                     }
372                                 }, 
373                                 
374                                 failure: function() {
375                                     //Ext.get(document.body).unmask();
376                                     //if (cb) {
377                                     //    cb.call(false);
378                                     //}
379                                      
380                                 }
381                             });
382                         
383                             this.success = true;
384                         
385                             //if (!isFromGroup && isToGroup) {
386                                 //return 'x-dd-drop-ok-add'; 
387                             return;
388                             //}
389                         },
390                         over : function (source, e, data)
391                         {
392                             // Roo.log("dragover");
393                              
394                             //Roo.log(e);
395                             var t = Roo.lib.Event.getTarget(e); 
396                                //  Roo.log(t);
397                             var ri = _this.grid.view.findRowIndex(t);
398                               //            Roo.log(ri);
399                             
400                             var rid  = false;
401                             if (ri !== false) {
402                                 rid = _this.grid.getDataSource().getAt(ri).data;
403                             }
404                             
405                             var s = _this.grid.getSelectionModel().getSelections();
406                             
407                             var isFromGroup = s.length ? s[0].data.id > 0 : false;
408                             
409                             var isToGroup = rid && rid.id > 0;
410                             
411                         //    Roo.log("isToGroup:"  + isToGroup + ", isFromGroup" + isFromGroup);
412                              
413                             if (isFromGroup && isToGroup) {
414                                 this.valid = false;
415                         //        Roo.log('not valid');
416                                 return;
417                             }
418                             if (!isFromGroup && !isToGroup) {
419                                 this.valid = false;
420                         //        Roo.log('not valid');
421                                 return  
422                             }
423                             if (isFromGroup && !isToGroup) {
424                                 this.valid = 'ok-sub'; 
425                                 return;
426                             } 
427                             //if (!isFromGroup && isToGroup) {
428                             this.valid = 'ok-add';
429                         //    Roo.log('add'); 
430                             //}
431                         }
432                     },
433                     ddGroup : 'groupDD'
434                 }
435             }
436         });
437         this.layout = this.panel.layout;
438
439     }
440 });