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