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