init
[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                     
221                 }
222                  
223                  
224         });
225         this.panel  = this.layout.add('center',  new Ext.GridPanel(this.grid , {
226                 fitToframe: true,
227                 fitContainer: true, 
228                 id: this.id, 
229                 title: this.title || "Staff", 
230                 controller : this 
231             })
232         );
233         this.grid.render();
234         
235         if (this.hiddenColumns) {
236             var cm = this.grid.getColumnModel();
237             Roo.each(this.hiddenColumns, function(c) {
238                 cm.setHidden(cm.getIndexByDataIndex(c), true);
239             });
240         }
241         
242
243         
244         var gridFoot = this.grid.getView().getFooterPanel(true);
245         this.paging = new Ext.PagingToolbar(gridFoot, this.grid.getDataSource(), {
246             pageSize: 25,
247             displayInfo: true,
248             displayMsg: "Displaying " + (this.itemDisplayName || "Staff") + " {0} - {1} of {2}",
249             emptyMsg: "No " + (this.itemDisplayName || "Staff") + " found"
250         });
251         var grid = this.grid;
252  
253     
254         this.toolbar = new Ext.Toolbar(this.grid.getView().getHeaderPanel(true));
255         
256         var tb = this.toolbar;
257         
258         
259         if (this.parentLayout.getRegion('west') && this.parentLayout.getRegion('west').panels.length) {
260                 
261             this.paging.add( 
262                 '<b><i><font color="red">'+ 
263                     (this.type ?
264                         "Drag person to add or remove from group" :
265                         "Drag person to add or remove from team"
266                     ) +
267                 '</font></i></b>'
268             );
269         }
270         
271         var _this = this;
272         if (this.permName == 'Core.Staff') {
273                 
274             this.paging.add( '-',
275                 {
276                     text: "Show old staff",
277                     pressed: false,
278                     enableToggle: true,
279                     toggleHandler: function(btn,pressed) {
280                         _this.showInActive = (pressed ? 1 : 0);
281                         btn.setText(pressed ? "Hide old staff": "Show old staff" );
282                         refreshPager();
283                     }
284                 }, 
285                 
286                
287                 '-'
288             );
289         }
290         
291      
292         this.searchBox = new Ext.form.TextField({
293             name: 'search',
294             width:135,
295             listeners : {
296                 specialkey : function(f,e)
297                 {
298                     
299                     if (e.getKey() == 13) {
300                         
301                         refreshPager();
302                     } 
303                    
304                 
305                 }
306             }
307          
308         });
309         var dg = _this.dialog();
310         tb.add(
311             {
312                 text: "Add",
313                 cls: 'x-btn-text-icon',
314                 icon: Ext.rootURL + 'images/default/dd/drop-add.gif',
315                 hidden :  !dg || (_this.newDefaults() === false) || !Pman.hasPerm(this.permName, 'A'),  
316                 handler : function(){
317                     dg.show(  _this.newDefaults(), refreshPager );  
318                 }
319             }, 
320              { ///... for contacts stuff...
321                 text: "Bulk Add",
322                 cls: 'x-btn-text-icon',
323                 icon: Ext.rootURL + 'images/default/dd/drop-add.gif',
324                 hidden : !this.bulkAdd() || !Pman.hasPerm(this.permName, 'A'),    
325                 handler : function(){
326                     
327                    // Pman.Dialog.PersonBulkAdd.show( {  id : 0 }, refreshPager ); 
328                    _this.bulkAdd().show( {  id : 0 }, refreshPager ); 
329                 }
330             },
331
332             {
333                 text: "Edit",
334                 cls: 'x-btn-text-icon',
335                 icon: Ext.rootURL + 'images/default/tree/leaf.gif',
336                 hidden : !dg || !Pman.hasPerm(this.permName, 'E'),    
337                 handler : function(){
338                     var s = grid.getSelectionModel().getSelections();
339                     if (!s.length || (s.length > 1))  {
340                         Ext.MessageBox.alert("Error", s.length ? "Select only one Row" : "Select a Row");
341                         return;
342                     }
343                     dg.show( s[0].data,refreshPager);
344                  }
345             }, 
346             {
347                 text: "Toogle Active",
348                 cls: 'x-btn-text-icon',
349                 icon:   rootURL + '/Pman/templates/images/trash.gif',
350                 hidden : (this.permName != 'Core.Staff') || !Pman.hasPerm(this.permName, 'E'),   // SPECIFIC TO STAFF!!!!!!
351                 handler : function(){
352                  
353                     var s = grid.getSelectionModel().getSelections();
354                     if (!s.length  )  {
355                         Ext.MessageBox.alert("Error",  "Select People Row");
356                         return;
357                     }
358                     var r = [];
359                     for(var i = 0; i < s.length; i++) {
360                         r.push(s[i].data.id);
361                     }
362                 
363                 
364                 
365                     grid.getView().mainWrap.mask("Sending");
366
367                     
368                     Ext.Ajax.request({
369                         url: baseURL + '/Roo/Person.html',
370                         method: 'GET',
371                         params: {
372                             _toggleActive : r.join(',')
373                         },
374                         success: function(resp) {
375                             var res = Pman.processResponse(resp);
376                             grid.getView().mainWrap.unmask();
377                             if (!res.success) {
378                                 Ext.MessageBox.alert("Error", res.errorMsg ? res.errorMsg  : "Error Sending");
379                                 return;
380                             }
381                             refreshPager();
382                             
383                         },
384                         failure: function(act) {
385                             grid.getView().mainWrap.unmask();
386                             Ext.MessageBox.alert("Error", "Error Sending");
387                         }
388                         
389                     });
390                 }
391                 
392             }, 
393             {
394                 text: "Delete",
395                 cls: 'x-btn-text-icon',
396                 hidden : (this.permName == 'Core.Staff') ||  !Pman.hasPerm('Core.Person', 'D') || this.hideDelete,    
397                 icon: rootURL + '/Pman/templates/images/trash.gif',
398                 handler : function(){
399                     Pman.genericDelete(_this, 'Person'); 
400                 }
401             } ,
402
403            
404             '-',
405             'Search: ',
406              
407             this.searchBox,
408         
409             {
410                 
411                
412                 icon: rootURL + '/Pman/templates/images/search.gif', // icons can also be specified inline
413                 cls: 'x-btn-icon',
414                 qtip: "Search",
415                 handler : function () { 
416                     _this.grid.getSelectionModel().clearSelections();
417
418                     refreshPager();
419                 }
420             },   
421              {
422                 
423                
424                 icon: rootURL + '/Pman/templates/images/edit-clear.gif', // icons can also be specified inline
425                 cls: 'x-btn-icon',
426                 qtip: "Reset Search",
427                 handler : function () {
428                     _this.searchBox.setValue('');
429                     _this.grid.getSelectionModel().clearSelections();
430
431                     refreshPager();
432                 }
433             }
434             
435
436         );
437         
438             
439         //this.toolbar = tb;
440         // add stuff to toolbar?
441         //this.innerLayout.endUpdate();
442          this.layout.endUpdate();
443
444         
445         
446     },
447     /*
448     show: function (parentLayout, region)
449     {
450         this.add(parentLayout, region);
451         this.grid.getDataSource().load({
452             params: {
453                 start:0, 
454                 limit:25
455             }
456         });
457
458     },
459     */
460     
461     c_project_id_code : function(cfg) {
462         cfg = cfg || {};
463         return Roo.apply({               
464             header : "Project",
465             dataIndex : 'project_id_code',
466             sortable : false,
467             width : 70,
468             renderer : function(v,x,r) {
469                 return String.format('<span qtip="{0}">{1}</span>', 
470                     r.data.action_type,
471                     v);
472             }
473         },cfg);
474     },
475
476     
477     
478     
479     c_name : function(cfg) {
480         cfg = cfg || {};
481         return Roo.apply({
482             id : this.id + '-name',
483             header : "Name",
484             dataIndex : 'name',
485             sortable : true,
486             width : 150  
487         }, cfg);
488     },
489      c_company_id_comptype : function(cfg) {
490         cfg = cfg || {};
491         return Roo.apply({
492             header : "Company Type",
493             dataIndex : 'company_id_comptype',
494             sortable : true,
495             width : 70
496         }, cfg);
497     },
498     
499     c_company_id_name : function(cfg) {
500         cfg = cfg || {};
501         return Roo.apply({
502             header : "Company / Office",
503             dataIndex : 'company_id_name',
504             sortable : true,
505             width : 150,
506             renderer: function(v,x,r) {
507                 return String.format('{0}{1}{2}', 
508                     v,
509                     r.data.office_id ? ' / ' : '',
510                     r.data.office_id_name);
511             }
512
513         }, cfg);
514     },
515     
516     c_office_id_name : function(cfg) {
517         cfg = cfg || {};
518         return Roo.apply({
519             header : "Office / Dept.",
520             dataIndex : 'office_id_name',
521             sortable : true,
522             width : 150  
523         }, cfg);
524         
525     },
526     c_role : function(cfg) {
527         cfg = cfg || {};
528         return Roo.apply({
529             header : "Role / Position",
530             dataIndex : 'role',
531             sortable : true,
532             width : 100
533         }, cfg);
534         
535     },
536     c_phone : function(cfg) {
537         cfg = cfg || {};
538         return Roo.apply({
539             header : "Phone",
540             dataIndex : 'phone',
541             sortable : true,
542             width : 70
543         }, cfg);
544         
545     },
546     c_fax : function(cfg) {
547         cfg = cfg || {};
548         return Roo.apply({
549             header : "Fax",
550             dataIndex : 'fax',
551             sortable : true,
552             width : 70
553         }, cfg);
554         
555     },
556     c_email : function(cfg) {
557         cfg = cfg || {};
558         return Roo.apply({
559             header : "Email",
560             dataIndex : 'email',
561             sortable : true,
562             width : 150,
563             renderer : function (v) {
564                 return (v.length && v.indexOf('@') > 0 ) ? 
565                     String.format('<a href="mailto:{0}">{0}</a>',v) : v;
566             }
567         }, cfg);
568         
569     },
570     c_active : function(cfg) {
571         cfg = cfg || {};
572         return Roo.apply({
573             header : "Active",
574             dataIndex : 'active',
575             sortable : true,
576             width : 50,
577             renderer : function(v) {
578                 // work out what the column is..
579                 
580                 var state = v> 0 ?  '-checked' : '';
581
582                 return '<img class="x-grid-check-icon' + state + '" src="' + Ext.BLANK_IMAGE_URL + '"/>';
583                 
584                 
585             }
586
587         }, cfg);
588         
589     }
590      
591     
592     
593 };
594 // need two version of this 
595 // (one can be used as edit + ProjectDirectory ADD)
596 // - the other one needs selection combos's for company / office
597
598