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