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