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