Pman.Tab.PersonList.js
[Pman.Core] / Pman.Tab.PersonList.js
1 //<script type="text/javascript">
2
3 /**
4  * 
5  * generic person list - used by perms. and staff lists.
6  * 
7  */
8
9
10
11 Pman.Tab.PersonList = function(config)
12 {
13     Ext.apply(this, config);
14 }
15
16 Pman.Tab.PersonList.prototype = {
17     
18     //--- things that should be set!!!!
19     id : '',  // should be set to something!
20     type : 0, // means!! = 0 = Groups (perms) 1= teams - loose grouping..
21     title : false,
22     hiddenColumns: false,  // lsit of cols to hide..
23     itemDisplayName : false, /// eg "Staff Employees / Contacts etc."
24     permName : 'Core.Person', // or 'Core.Staff'
25     getLeftSelections : function() { return []; },
26     hideDelete : false,
27     
28     // beforeload handler... -- override on extended versions..
29     beforeload: function(t, o) {
30         //console.log(o.params);
31         // teams!?!
32         alert('person list not configured');
33         return false;
34         var tms = _this.getLeftSelections();
35         
36         if (tms.length) {
37             o.params['query[in_group]'] = tms[0].data.id;
38         }
39         o.params['query[name]'] = this.searchBox.getValue();
40         o.params['query[type]'] = this.type; // group type..
41         o.params['query[person_internal_only_all]'] = 1;
42         o.params['query[person_inactive]'] = this.showInActive ? 0  : 1;
43         
44     },
45     
46     columns : function()
47     {
48         alert('person list not configured');
49         return false;
50         return [
51             this.c_name(),
52             this.c_office_id_name(),
53             this.c_role(),
54             this.c_phone(),
55             this.c_fax(),
56             this.c_email(),
57             this.c_active()
58         ]
59     },
60     
61     dialog: function () {
62         alert('person list not configured');
63         return false;
64         return Pman.Dialog.PersonStaff;
65     },
66     bulkAdd : function() {
67         //return Pman.Dialog.PersonBulkAdd
68         return false;
69     },
70     newDefaults : function() {
71         alert('person list not configured');
72         return false;
73         return {
74             
75             id : 0,
76             company_id : Pman.Login.authUser.company_id,
77             company_id_name : Pman.Login.authUser.company_id_name,
78             company_id_address : Pman.Login.authUser.company_id_address,
79             company_id_tel : Pman.Login.authUser.company_id_tel,
80             company_id_fax : Pman.Login.authUser.company_id_fax
81         };
82     },
83          
84     
85     
86     /// --- end extendable bits...
87     
88     
89     parentLayout : false,
90     showInActive : 0,  // toggle var for hiding and showing active staff..
91     grid : false,
92     panel : false,
93     toolbar : false,
94     paging:  false,
95     tab: false,
96     
97     
98     refreshWestPanel : function() /// used wher???
99     {
100         var actpan = this.parentLayout.getRegion('west').getActivePanel();
101         if (actpan && actpan.controller) {
102             actpan.controller.paging.onClick('refresh');
103             return;
104         }
105         // depreciated..    
106     
107         if (!actpan || !actpan.id) {
108             return;
109         }
110         Pman.Tab[actpan.id].refresh();
111     },
112     
113     refresh: function(){
114         if (!this.paging) {
115             this.delayedCreate();
116         }
117         this.paging.onClick('refresh');
118     },
119     
120     loadFirst: function(){
121         if (!this.paging) {
122             this.delayedCreate();
123         }
124         this.paging.onClick('first');
125     },  
126     
127     
128     
129     add : function(parentLayout, region) {
130         
131         var _this = this;
132         if (this.tab) {
133             parentLayout.getRegion(region).showPanel(this.panel);
134             return;
135         }
136         this.parentLayout = parentLayout;
137         
138         this.layout = new Ext.BorderLayout(
139             parentLayout.getEl().createChild({tag:'div'}),
140             {
141                
142                 center: {
143                     autoScroll:true,
144                     hideTabs: true
145                 }
146             }
147         );
148
149
150
151         this.tab = parentLayout.add(region,  new Ext.NestedLayoutPanel(
152             this.layout, {title: this.title, background: true, controller : this}));
153
154         this.tab.on('activate', function() {
155             _this.delayedCreate();
156            // _this.paging.onClick('refresh');
157         });
158     },
159     delayedCreate : function () 
160      
161     {
162         var _this = this;
163         if (this.grid) {
164             return;
165         }
166         
167         var refreshPager = function() {
168             _this.refresh();
169         }
170         this.layout.beginUpdate();
171         
172         var frm = this.layout.getRegion('center').getEl().createChild({tag:'div'});
173         //this.grid = new Ext.grid.EditorGrid(frm,  {
174         this.grid = new Ext.grid.Grid(frm,  {
175                 ddGroup: 'groupDD',
176                 //enableDrag: true,
177                 enableDrag: true,
178                 id: this.id + '-grid',
179                 ds:   new Ext.data.Store({
180                     // load using HTTP
181                     proxy: new Ext.data.HttpProxy({
182                         url: baseURL + '/Roo/Person.html',
183                         method: 'GET'
184                     }),
185                     reader: Pman.Readers.Person,
186                     remoteSort: true,
187                     listeners : {
188                         
189                         beforeload: function(t, o) {
190                             //console.log(o.params);
191                             // teams!?!
192                             return _this.beforeload(t,o);
193                              
194                             
195                         },
196                         loadexception : Pman.loadException
197                     
198                     },
199                     sortInfo: {
200                         field: 'name', direction: 'ASC'
201                     }
202                 }),
203                 cm: new Ext.grid.ColumnModel(
204                     this.columns()
205                 ),
206                 autoExpandColumn: _this.id + '-name' , // fixme!!!!
207                 clicksToEdit : 1,
208                 
209                 loadMask: true,
210
211                 listeners : {
212                     rowdblclick : function(g, ri, e) {
213                         var s = g.getDataSource().getAt(ri).data;
214                         if (_this.dialog() && Pman.hasPerm(_this.permName, 'E')) {
215                             _this.dialog().show(s,refreshPager);
216                         }
217                         
218                         
219                     },
220                     cellclick : function (_self, rowIndex, columnIndex, e)
221                     {
222                         var id = this.colModel.getDataIndex(colIndex);
223                         if (columnIndex > 0 ) {
224                             return;
225                         }
226                         var sel = _this.panel.tree.getSelectionModel().getSelectedNode();
227                         if (!sel || isNaN(parseInt(sel.id))) {
228                              
229                             return  ;
230                         }
231                         
232                         var rec = _this.grid.ds.getAt(rowIndex);
233                         if (rec.data.has_perm) {
234                             if (rec.data.has_perm != sel.id) {
235                                 Roo.MessageBox.alert("Error", "This is an inherited permission, Remove permission for parent category");
236                                 return;
237                             }
238                             Roo.log('setting perm to 0');
239                             rec.set('has_perm', 0);
240                             rec.commit();
241                             // remove it only if it's the same..
242                             return;
243                         }
244                         rec.set('has_perm', sel.id);
245                         rec.commit();
246                         
247                         
248                     }
249                     
250                 }
251                  
252                  
253         });
254         this.panel  = this.layout.add('center',  new Ext.GridPanel(this.grid , {
255                 fitToframe: true,
256                 fitContainer: true, 
257                 id: this.id, 
258                 title: this.title || "Staff", 
259                 controller : this 
260             })
261         );
262         this.grid.render();
263         
264         if (this.hiddenColumns) {
265             var cm = this.grid.getColumnModel();
266             Roo.each(this.hiddenColumns, function(c) {
267                 cm.setHidden(cm.getIndexByDataIndex(c), true);
268             });
269         }
270         
271
272         
273         var gridFoot = this.grid.getView().getFooterPanel(true);
274         this.paging = new Ext.PagingToolbar(gridFoot, this.grid.getDataSource(), {
275             pageSize: 25,
276             displayInfo: true,
277             displayMsg: "Displaying " + (this.itemDisplayName || "Staff") + " {0} - {1} of {2}",
278             emptyMsg: "No " + (this.itemDisplayName || "Staff") + " found"
279         });
280         var grid = this.grid;
281  
282     
283         this.toolbar = new Ext.Toolbar(this.grid.getView().getHeaderPanel(true));
284         
285         var tb = this.toolbar;
286         
287         
288         if (this.parentLayout.getRegion('west') && this.parentLayout.getRegion('west').panels.length) {
289                 
290             this.paging.add( 
291                 '<b><i><font color="red">'+ 
292                     (this.type ?
293                         "Drag person to add or remove from group" :
294                         "Drag person to add or remove from team"
295                     ) +
296                 '</font></i></b>'
297             );
298         }
299         
300         var _this = this;
301         if (this.permName == 'Core.Staff') {
302                 
303             this.paging.add( '-',
304                 {
305                     text: "Show old staff",
306                     pressed: false,
307                     enableToggle: true,
308                     toggleHandler: function(btn,pressed) {
309                         _this.showInActive = (pressed ? 1 : 0);
310                         btn.setText(pressed ? "Hide old staff": "Show old staff" );
311                         refreshPager();
312                     }
313                 }, 
314                 
315                
316                 '-'
317             );
318         }
319         
320      
321         this.searchBox = new Ext.form.TextField({
322             name: 'search',
323             width:135,
324             listeners : {
325                 specialkey : function(f,e)
326                 {
327                     
328                     if (e.getKey() == 13) {
329                         
330                         refreshPager();
331                     } 
332                    
333                 
334                 }
335             }
336          
337         });
338         var dg = _this.dialog();
339         tb.add(
340             {
341                 text: "Add",
342                 cls: 'x-btn-text-icon',
343                 icon: Ext.rootURL + 'images/default/dd/drop-add.gif',
344                 hidden :  !dg || (_this.newDefaults() === false) || !Pman.hasPerm(this.permName, 'A'),  
345                 handler : function(){
346                     dg.show(  _this.newDefaults(), refreshPager );  
347                 }
348             }, 
349              { ///... for contacts stuff...
350                 text: "Bulk Add",
351                 cls: 'x-btn-text-icon',
352                 icon: Ext.rootURL + 'images/default/dd/drop-add.gif',
353                 hidden : !this.bulkAdd() || !Pman.hasPerm(this.permName, 'A'),    
354                 handler : function(){
355                     
356                    // Pman.Dialog.PersonBulkAdd.show( {  id : 0 }, refreshPager ); 
357                    _this.bulkAdd().show( {  id : 0 }, refreshPager ); 
358                 }
359             },
360
361             {
362                 text: "Edit",
363                 cls: 'x-btn-text-icon',
364                 icon: Ext.rootURL + 'images/default/tree/leaf.gif',
365                 hidden : !dg || !Pman.hasPerm(this.permName, 'E'),    
366                 handler : function(){
367                     var s = grid.getSelectionModel().getSelections();
368                     if (!s.length || (s.length > 1))  {
369                         Ext.MessageBox.alert("Error", s.length ? "Select only one Row" : "Select a Row");
370                         return;
371                     }
372                     dg.show( s[0].data,refreshPager);
373                  }
374             }, 
375             {
376                 text: "Toogle Active",
377                 cls: 'x-btn-text-icon',
378                 icon:   rootURL + '/Pman/templates/images/trash.gif',
379                 hidden : (this.permName != 'Core.Staff') || !Pman.hasPerm(this.permName, 'E'),   // SPECIFIC TO STAFF!!!!!!
380                 handler : function(){
381                  
382                     var s = grid.getSelectionModel().getSelections();
383                     if (!s.length  )  {
384                         Ext.MessageBox.alert("Error",  "Select People Row");
385                         return;
386                     }
387                     var r = [];
388                     for(var i = 0; i < s.length; i++) {
389                         r.push(s[i].data.id);
390                     }
391                 
392                 
393                 
394                     grid.getView().mainWrap.mask("Sending");
395
396                     
397                     Ext.Ajax.request({
398                         url: baseURL + '/Roo/Person.html',
399                         method: 'GET',
400                         params: {
401                             _toggleActive : r.join(',')
402                         },
403                         success: function(resp) {
404                             var res = Pman.processResponse(resp);
405                             grid.getView().mainWrap.unmask();
406                             if (!res.success) {
407                                 Ext.MessageBox.alert("Error", res.errorMsg ? res.errorMsg  : "Error Sending");
408                                 return;
409                             }
410                             refreshPager();
411                             
412                         },
413                         failure: function(act) {
414                             grid.getView().mainWrap.unmask();
415                             Ext.MessageBox.alert("Error", "Error Sending");
416                         }
417                         
418                     });
419                 }
420                 
421             }, 
422             {
423                 text: "Delete",
424                 cls: 'x-btn-text-icon',
425                 hidden : (this.permName == 'Core.Staff') ||  !Pman.hasPerm('Core.Person', 'D') || this.hideDelete,    
426                 icon: rootURL + '/Pman/templates/images/trash.gif',
427                 handler : function(){
428                     Pman.genericDelete(_this, 'Person'); 
429                 }
430             } ,
431
432            
433             '-',
434             'Search: ',
435              
436             this.searchBox,
437         
438             {
439                 
440                
441                 icon: rootURL + '/Pman/templates/images/search.gif', // icons can also be specified inline
442                 cls: 'x-btn-icon',
443                 qtip: "Search",
444                 handler : function () { 
445                     _this.grid.getSelectionModel().clearSelections();
446
447                     refreshPager();
448                 }
449             },   
450              {
451                 
452                
453                 icon: rootURL + '/Pman/templates/images/edit-clear.gif', // icons can also be specified inline
454                 cls: 'x-btn-icon',
455                 qtip: "Reset Search",
456                 handler : function () {
457                     _this.searchBox.setValue('');
458                     _this.grid.getSelectionModel().clearSelections();
459
460                     refreshPager();
461                 }
462             }
463             
464
465         );
466         
467             
468         //this.toolbar = tb;
469         // add stuff to toolbar?
470         //this.innerLayout.endUpdate();
471          this.layout.endUpdate();
472
473         
474         
475     },
476     /*
477     show: function (parentLayout, region)
478     {
479         this.add(parentLayout, region);
480         this.grid.getDataSource().load({
481             params: {
482                 start:0, 
483                 limit:25
484             }
485         });
486
487     },
488     */
489     
490     c_project_id_code : function(cfg) {
491         cfg = cfg || {};
492         return Roo.apply({               
493             header : "Project",
494             dataIndex : 'project_id_code',
495             sortable : false,
496             width : 70,
497             renderer : function(v,x,r) {
498                 return String.format('<span qtip="{0}">{1}</span>', 
499                     r.data.action_type,
500                     v);
501             }
502         },cfg);
503     },
504
505     
506     
507     
508     c_name : function(cfg) {
509         cfg = cfg || {};
510         return Roo.apply({
511             id : this.id + '-name',
512             header : "Name",
513             dataIndex : 'name',
514             sortable : true,
515             width : 150  
516         }, cfg);
517     },
518      c_company_id_comptype : function(cfg) {
519         cfg = cfg || {};
520         return Roo.apply({
521             header : "Company Type",
522             dataIndex : 'company_id_comptype',
523             sortable : true,
524             width : 70
525         }, cfg);
526     },
527     
528     c_company_id_name : function(cfg) {
529         cfg = cfg || {};
530         return Roo.apply({
531             header : "Company / Office",
532             dataIndex : 'company_id_name',
533             sortable : true,
534             width : 150,
535             renderer: function(v,x,r) {
536                 return String.format('{0}{1}{2}', 
537                     v,
538                     r.data.office_id ? ' / ' : '',
539                     r.data.office_id_name);
540             }
541
542         }, cfg);
543     },
544     
545     c_office_id_name : function(cfg) {
546         cfg = cfg || {};
547         return Roo.apply({
548             header : "Office / Dept.",
549             dataIndex : 'office_id_name',
550             sortable : true,
551             width : 150  
552         }, cfg);
553         
554     },
555     c_role : function(cfg) {
556         cfg = cfg || {};
557         return Roo.apply({
558             header : "Role / Position",
559             dataIndex : 'role',
560             sortable : true,
561             width : 100
562         }, cfg);
563         
564     },
565     c_phone : function(cfg) {
566         cfg = cfg || {};
567         return Roo.apply({
568             header : "Phone",
569             dataIndex : 'phone',
570             sortable : true,
571             width : 70
572         }, cfg);
573         
574     },
575     c_fax : function(cfg) {
576         cfg = cfg || {};
577         return Roo.apply({
578             header : "Fax",
579             dataIndex : 'fax',
580             sortable : true,
581             width : 70
582         }, cfg);
583         
584     },
585     c_email : function(cfg) {
586         cfg = cfg || {};
587         return Roo.apply({
588             header : "Email",
589             dataIndex : 'email',
590             sortable : true,
591             width : 150,
592             renderer : function (v) {
593                 return (v.length && v.indexOf('@') > 0 ) ? 
594                     String.format('<a href="mailto:{0}">{0}</a>',v) : v;
595             }
596         }, cfg);
597         
598     },
599     c_active : function(cfg) {
600         cfg = cfg || {};
601         return Roo.apply({
602             header : "Active",
603             dataIndex : 'active',
604             sortable : true,
605             width : 50,
606             renderer : function(v) {
607                 // work out what the column is..
608                 
609                 var state = v> 0 ?  '-checked' : '';
610
611                 return '<img class="x-grid-check-icon' + state + '" src="' + Ext.BLANK_IMAGE_URL + '"/>';
612                 
613                 
614             }
615
616         }, cfg);
617         
618     }
619      
620     
621     
622 };
623 // need two version of this 
624 // (one can be used as edit + ProjectDirectory ADD)
625 // - the other one needs selection combos's for company / office
626
627