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