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