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