Pman.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
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/core_person',
192                         method: 'GET'
193                     }),
194                     reader: new Roo.data.JsonReader({}, []),
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/core_person',
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                         // only hide if viewing active members... 
271                         if (!_this.showInActive) {
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         });
285         this.panel  = this.layout.add('center',  new Roo.GridPanel(this.grid , {
286                 fitToframe: true,
287                 fitContainer: true,
288                 //background : false,
289                 id: this.id, 
290                 title: this.title || "Staff", 
291                 controller : this 
292             })
293         );
294         this.grid.render();
295         
296         if (this.hiddenColumns) {
297             var cm = this.grid.getColumnModel();
298             Roo.each(this.hiddenColumns, function(c) {
299                 cm.setHidden(cm.getIndexByDataIndex(c), true);
300             });
301         }
302         
303
304         
305         var gridFoot = this.grid.getView().getFooterPanel(true);
306         this.paging = new Roo.PagingToolbar(gridFoot, this.grid.getDataSource(), {
307             pageSize: 25,
308             displayInfo: true,
309             displayMsg: "Displaying " + (this.itemDisplayName || "Staff") + " {0} - {1} of {2}",
310             emptyMsg: "No " + (this.itemDisplayName || "Staff") + " found"
311         });
312         var grid = this.grid;
313  
314     
315         this.toolbar = new Roo.Toolbar(this.grid.getView().getHeaderPanel(true));
316         
317         var tb = this.toolbar;
318         
319         
320         if (this.parentLayout.getRegion('west') && this.parentLayout.getRegion('west').panels.length) {
321                 
322             this.paging.add( 
323                 '<b><i><font color="red">'+ 
324                     (this.type ?
325                         "Drag person to add or remove from group" :
326                         "Drag person to add or remove from team"
327                     ) +
328                 '</font></i></b>'
329             );
330         }
331         
332         var _this = this;
333         //if (this.permName == 'Core.Staff') {
334                 
335             this.paging.add( '-',
336                 {
337                     text: "Show old staff",
338                     pressed: false,
339                     enableToggle: true,
340                     toggleHandler: function(btn,pressed) {
341                         _this.showInActive = (pressed ? 1 : 0);
342                         btn.setText(pressed ? "Hide old staff": "Show old staff" );
343                         refreshPager();
344                     }
345                 }, 
346                 
347                
348                 '-'
349             );
350         //}
351         
352      
353         this.searchBox = new Roo.form.TextField({
354             name: 'search',
355             width:135,
356             listeners : {
357                 specialkey : function(f,e)
358                 {
359                     
360                     if (e.getKey() == 13) {
361                         
362                         refreshPager();
363                     } 
364                    
365                 
366                 }
367             }
368          
369         });
370         var dg = _this.dialog();
371         tb.add(
372             {
373                 text: "Add",
374                 cls: 'x-btn-text-icon',
375                 icon: Roo.rootURL + 'images/default/dd/drop-add.gif',
376                 hidden :  !dg || (_this.newDefaults() === false) || !Pman.hasPerm(this.permName, 'A'),  
377                 handler : function(){
378                     dg.show(  _this.newDefaults(), refreshPager );  
379                 }
380             }, 
381              { ///... for contacts stuff...
382                 text: "Bulk Add",
383                 cls: 'x-btn-text-icon',
384                 icon: Roo.rootURL + 'images/default/dd/drop-add.gif',
385                 hidden : !this.bulkAdd() || !Pman.hasPerm(this.permName, 'A'),    
386                 handler : function(){
387                     
388                    // Pman.Dialog.PersonBulkAdd.show( {  id : 0 }, refreshPager ); 
389                    _this.bulkAdd().show( {  id : 0 }, refreshPager ); 
390                 }
391             },
392
393             {
394                 text: "Edit",
395                 cls: 'x-btn-text-icon',
396                 icon: Roo.rootURL + 'images/default/tree/leaf.gif',
397                 hidden : !dg || !Pman.hasPerm(this.permName, 'E'),    
398                 handler : function(){
399                     var s = grid.getSelectionModel().getSelections();
400                     if (!s.length || (s.length > 1))  {
401                         Roo.MessageBox.alert("Error", s.length ? "Select only one Row" : "Select a Row");
402                         return;
403                     }
404                     dg.show( s[0].data,refreshPager);
405                  }
406             }, 
407          /*   {
408                 text: "Toogle Active",
409                 cls: 'x-btn-text-icon',
410                 icon:   rootURL + '/Pman/templates/images/trash.gif',
411                 hidden : (this.permName != 'Core.Staff') || !Pman.hasPerm(this.permName, 'E'),   // SPECIFIC TO STAFF!!!!!!
412                 handler : function(){
413                  
414                     var s = grid.getSelectionModel().getSelections();
415                     if (!s.length  )  {
416                         Roo.MessageBox.alert("Error",  "Select People Row");
417                         return;
418                     }
419                     var r = [];
420                     for(var i = 0; i < s.length; i++) {
421                         r.push(s[i].data.id);
422                     }
423                 
424                 
425                 
426                     grid.getView().mainWrap.mask("Sending");
427
428                     
429                     Roo.Ajax.request({
430                         url: baseURL + '/Roo/core_person',
431                         method: 'GET',
432                         params: {
433                             _toggleActive : r.join(',')
434                         },
435                         success: function(resp) {
436                             var res = Pman.processResponse(resp);
437                             grid.getView().mainWrap.unmask();
438                             if (!res.success) {
439                                 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg  : "Error Sending");
440                                 return;
441                             }
442                             refreshPager();
443                             
444                         },
445                         failure: function(act) {
446                             grid.getView().mainWrap.unmask();
447                             Roo.MessageBox.alert("Error", "Error Sending");
448                         }
449                         
450                     });
451                 }
452                 
453             }, 
454             */
455             {
456                 text: "Delete",
457                 cls: 'x-btn-text-icon',
458                 hidden : !Pman.hasPerm(_this.permName, 'D'),    
459                 icon: rootURL + '/Pman/templates/images/trash.gif',
460                 handler : function(){
461                     //Pman.genericDelete(_this, 'Person'); 
462                     
463                     var rec = _this.grid.ds.getAt(_this.grid.selModel.last);
464                     
465                     var rec_id = rec.id;
466                     
467                     if (rec_id * 1 < 1) {
468                         Roo.MessageBox.alert("Error", "Select row to delete");
469                         return;
470                     }
471
472                     Roo.MessageBox.confirm(
473                         "Confirm", 
474                         "Confirm Deletion of selected row (some rows can not be deleted if they are referenced elsewhere", 
475                         function(res) {
476                             if(res != 'yes') {
477                                 return;
478                             }
479                             new Pman.Request({
480                                 method : 'POST',
481                                 url : baseURL + '/Roo/core_person',
482                                 params : {
483                                     _delete  : rec_id
484                                 },
485                                 success : function() {
486                                     _this.paging.onClick('refresh');
487                                     //_this.grid.footer.onClick('refresh');
488                                 }
489                             });
490                         }
491                     );
492
493
494                     
495
496                 }
497             } ,
498
499            
500             '-',
501             'Search: ',
502              
503             this.searchBox,
504         
505             {
506                 
507                
508                 icon: rootURL + '/Pman/templates/images/search.gif', // icons can also be specified inline
509                 cls: 'x-btn-icon',
510                 qtip: "Search",
511                 handler : function () { 
512                     _this.grid.getSelectionModel().clearSelections();
513
514                     refreshPager();
515                 }
516             },   
517              {
518                 
519                
520                 icon: rootURL + '/Pman/templates/images/edit-clear.gif', // icons can also be specified inline
521                 cls: 'x-btn-icon',
522                 qtip: "Reset Search",
523                 handler : function () {
524                     _this.searchBox.setValue('');
525                     _this.grid.getSelectionModel().clearSelections();
526                     
527                     refreshPager();
528                 }
529             },
530             '-',
531              {
532                
533                 xtype : 'Button',
534                 xns : Roo.Toolbar,
535                
536                 text: "Switch to Selected User",
537                 hidden : _this.permName != 'Core.Staff' || !Pman.hasPerm('Core.Staff', 'E'),
538                 listeners : {
539                     click : function () { 
540                         var s = grid.getSelectionModel().getSelections();
541                         if (s.length != 1)  {
542                             Roo.MessageBox.alert("Error",  "Select a Person");
543                             return;
544                         }
545                         new Pman.Request({
546                             url : baseURL+ '/Login.php',
547                             params  :{
548                                 'switch' : s[0].data.id
549                             },
550                             
551                             method : 'GET',
552                             success : function() {
553                                 document.location = baseURL + '?ts=' + Math.random();
554                                 
555                             }, 
556                             failure : function(d) {
557                                 Roo.log(d);
558                                 Roo.MessageBox.alert("Error",  d);
559                             }
560                         });
561                         
562                         
563                     }
564                 }
565             
566                      
567             }
568             
569
570         );
571         
572             
573         //this.toolbar = tb;
574         // add stuff to toolbar?
575         //this.innerLayout.endUpdate();
576          this.layout.endUpdate();
577
578         
579         
580     },
581     /*
582     show: function (parentLayout, region)
583     {
584         this.add(parentLayout, region);
585         this.grid.getDataSource().load({
586             params: {
587                 start:0, 
588                 limit:25
589             }
590         });
591
592     },
593     */
594     
595     c_project_id_code : function(cfg) {
596         cfg = cfg || {};
597         return Roo.apply({               
598             header : "Project",
599             dataIndex : 'project_id_code',
600             sortable : false,
601             width : 70,
602             renderer : function(v,x,r) {
603                 return String.format('<span qtip="{0}">{1}</span>', 
604                     r.data.action_type,
605                     v);
606             }
607         },cfg);
608     },
609
610     
611     
612     
613     c_name : function(cfg) {
614         cfg = cfg || {};
615         return Roo.apply({
616          //   id : (this.id + '-name').toLowerCase(),
617             header : "Name",
618             dataIndex : 'name',
619             sortable : true,
620             renderer : function(v,p,r) { 
621                 if(r.data.active != 1){
622                     return String.format('<div style="text-decoration:line-through">{0}</div>', v); 
623                 }
624                 return String.format('{0}', v); 
625             }
626           //  width : 150  
627         }, cfg);
628     },
629     
630     c_group_membership : function(cfg) {
631         cfg = cfg || {};
632         return Roo.apply({
633          //   id : (this.id + '-name').toLowerCase(),
634             header : "Group Membership",
635             dataIndex : 'member_of',
636             sortable : false,
637             renderer : function(v,p,r) {
638                 if(r.data.active != 1){
639                     return String.format('<div style="text-decoration:line-through">{0}</div>', v).split("\n").join("<br/>"); 
640                 }
641                 return String.format('{0}', v).split("\n").join("<br/>"); 
642             },
643             width : 150  
644         }, cfg);
645     },
646     
647      c_company_id_comptype : function(cfg) {
648         cfg = cfg || {};
649         return Roo.apply({
650             header : "Company Type",
651             dataIndex : 'company_id_comptype',
652             sortable : true,
653             width : 70
654         }, cfg);
655     },
656     
657     c_company_id_name : function(cfg) {
658         cfg = cfg || {};
659         return Roo.apply({
660             header : "Company / Office",
661             dataIndex : 'company_id_name',
662             sortable : true,
663             width : 150,
664             renderer: function(v,x,r) {
665                 return String.format('{0}{1}{2}', 
666                     v,
667                     r.data.office_id ? ' / ' : '',
668                     r.data.office_id_name);
669             }
670
671         }, cfg);
672     },
673     
674     c_office_id_name : function(cfg) {
675         cfg = cfg || {};
676         return Roo.apply({
677             header : "Office / Dept.",
678             dataIndex : 'office_id_name',
679             sortable : true,
680             width : 150  
681         }, cfg);
682         
683     },
684     c_role : function(cfg) {
685         cfg = cfg || {};
686         return Roo.apply({
687             header : "Role / Position",
688             dataIndex : 'role',
689             sortable : true,
690             width : 100
691         }, cfg);
692         
693     },
694     c_phone : function(cfg) {
695         cfg = cfg || {};
696         return Roo.apply({
697             header : "Phone",
698             dataIndex : 'phone',
699             sortable : true,
700             width : 70
701         }, cfg);
702         
703     },
704     c_fax : function(cfg) {
705         cfg = cfg || {};
706         return Roo.apply({
707             header : "Fax",
708             dataIndex : 'fax',
709             sortable : true,
710             width : 70
711         }, cfg);
712         
713     },
714     c_email : function(cfg) {
715         cfg = cfg || {};
716         return Roo.apply({
717             header : "Email",
718             dataIndex : 'email',
719             sortable : true,
720             width : 150,
721             renderer : function (v) {
722                 return (v.length && v.indexOf('@') > 0 ) ? 
723                     String.format('<a href="mailto:{0}">{0}</a>',v) : v;
724             }
725         }, cfg);
726         
727     },
728     c_active : function(cfg) {
729         cfg = cfg || {};
730         return Roo.apply({
731             header : "Active",
732             dataIndex : 'active',
733             sortable : true,
734             width : 50,
735             renderer : function(v) {
736                 // work out what the column is..
737                 
738                 var state = v> 0 ?  '-checked' : '';
739
740                 return '<img class="x-grid-check-icon' + state + '" src="' + Roo.BLANK_IMAGE_URL + '"/>';
741                 
742                 
743             }
744
745         }, cfg);
746         
747     }
748      
749     
750     
751 };
752 // need two version of this 
753 // (one can be used as edit + ProjectDirectory ADD)
754 // - the other one needs selection combos's for company / office
755
756