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