Pman.Dialog.PersonEditor.js
[Pman.Core] / Pman.Dialog.PersonEditor.js
1 //<script type="text/javascript">
2
3 Pman.Dialog.PersonEditor = function(config)
4 {
5    
6     Roo.apply(this, config);
7     
8 };
9
10 Pman.Dialog.PersonEditor.prototype = {
11     
12     itemList : false, // list of itemTypes used on form.
13     dialogConfig : false, // 
14     type : '',
15     
16     
17     itemTypes : false, // set in contructor
18     
19     dialog : false,
20     form : false,
21     layout : false,
22     
23     callback: false, 
24     _id : false,
25     data : false,
26     
27     
28     create : function()
29     {
30         if (this.dialog) {
31             return;
32         }
33         var _this = this;
34         this.dialog = new Ext.LayoutDialog(Ext.get(document.body).createChild({tag:'div'}),  
35             Roo.apply({ 
36                 autoCreated: true,
37                 title: 'Edit Contact Details',
38                 modal: true,
39                 width:  530,
40                 height: 400,
41                 shadow:true,
42                 minWidth:200,
43                 minHeight:180,
44                 //proxyDrag: true,
45                 closable: false,
46                 draggable: false,
47                 center: {
48                     autoScroll:false,
49                     titlebar: false,
50                    // tabPosition: 'top',
51                     hideTabs: true,
52                     closeOnTab: true,
53                     alwaysShowTabs: false
54                 }
55             },this.dialogConfig)
56         );
57         
58         this.dialog.addKeyListener(27, this.dialog.hide, this.dialog);
59         if (this.itemList.indexOf('save_send') > -1 ) {
60             this.dialog.addButton("Send Introduction Mail", this.saveSend, this);
61         }
62         
63         this.dialog.addButton("Cancel", this.dialog.hide, this.dialog);
64         
65         
66         
67         this.dialog.addButton("Save", this.save, this);
68         this.layout = this.dialog.getLayout();
69         this.layout.beginUpdate();
70         
71          
72         this.form = new Ext.form.Form({
73             labelWidth: 120,
74             listeners : {
75                 actionfailed : function(f, act) {
76                     _this.dialog.el.unmask();
77                     // error msg???
78                     Pman.standardActionFailed(f,act);
79                               
80                 },
81                 actioncomplete: function(f, act) {
82                     _this.dialog.el.unmask();
83                     if (act.type == 'load') {
84                         
85                         _this.data = act.result.data;
86                     }
87                     
88                                 
89                     if  ((act.type == 'load') || (act.type == 'setdata')) {
90                         var data = _this.data;
91                         // we dont have  a form where company name is sent in - and is editable..
92                         //this.form.findField('office_id')
93                         if(!data.countries && _this.form.findField('countries')){
94                             _this.form.findField('countries').setValue();// set empty array by default...
95                         }
96                         
97                         if (_this.form.findField('company_id') && _this.form.findField('company_id').setFromData) {
98                             _this.form.findField('company_id').setFromData( data.company_id ? {
99                                 id : data.company_id,
100                                 name : data.company_id_name,
101                                 remarks:  data.company_id_remarks,
102                                 address:  data.company_id_address,
103                                 tel:  data.company_id_tel,
104                                 fax:  data.company_id_fax,
105                                 email:  data.company_id_email
106                             } : { id: 0, name : ''  });
107                         }
108                         
109                         
110                         if (_this.form.findField('office_id') && _this.form.findField('office_id').setFromData) {
111                             // set up the office details.. new, edit, staff
112                             
113                             _this.form.findField('office_id').setFromData(data.office_id ? { 
114                                 id: data.office_id,
115                                 name:  data.office_id_name,
116                                 remarks:  data.office_id_remarks,
117                                 address:  data.office_id_address,
118                                 tel:  data.office_id_tel,
119                                 fax:  data.office_id_fax,
120                                 email:  data.office_id_email
121                                 // should we add in company_name etc. ????
122                                 
123                             } :  {  id: 0, name:  ''  });
124                             
125                         }
126                         if (_this.form.findField('project_id')) {
127                             _this.form.findbyId('project_id_fs').setExpand(data.project_id ? true: false);
128                             
129                             
130                             _this.form.findField('project_id').setFromData(data.project_id ?{
131                                id : data.project_id,
132                                code : data.project_id_code
133                               } : { id: 0, code :'' } );
134                        }
135                         
136                         
137                         if (this.type == 'staff') {
138                             _this.form.findField('passwd1').allowBlank = false;
139                             _this.form.findField('passwd2').allowBlank = false;
140                             if (data.id > 0) {
141                                 _this.form.findField('passwd1').allowBlank = true;
142                                 _this.form.findField('passwd2').allowBlank = true;
143                             }
144
145                         }
146                         
147                         
148                         return;
149                     } 
150                     
151                     if (act.type == 'submit') { // only submitted here if we are 
152                         _this.dialog.hide();
153                         
154                         
155                         
156                         if (_this.callback) {
157                             _this.callback.call(this, act.result.data);
158                         }
159                         if (_this.sendAfterSave) {
160                             act.result.data.rawPasswd = _this.form.findField('passwd1').getValue();
161                             _this.sendIntro([ act.result.data ], "Sending Welcome Message");
162                         }
163                         
164                         return; 
165                     }
166                     // unmask?? 
167                 }
168             }
169          
170              
171         });
172         this.loadItemTypes();
173         Roo.each(this.itemList, function(il) {
174             if (typeof(il) != 'object') {
175                 // no permission for Core offices.. - can not show department...
176                 if (il == 'office_id_name' && !Pman.hasPerm('Core.Offices','S')) {
177                     return true;
178                 }
179                 
180                 _this.form.addxtype(_this.itemTypes[il]);
181                 return true;
182             }
183             _this.form.addxtype(Roo.apply(il, _this.itemTypes[il.name]));
184             return true;
185             
186         });
187         var ef = this.dialog.getLayout().getEl().createChild({tag: 'div'});
188         ef.dom.style.margin = 10;
189          
190         this.form.render(ef.dom);
191
192         var vp = this.dialog.getLayout().add('center', new Ext.ContentPanel(ef, {
193             autoCreate : true,
194             //title: 'Org Details',
195             //toolbar: this.tb,
196             width: 250,
197             maxWidth: 250,
198             fitToFrame:true
199         }));
200           
201
202         
203         
204         this.layout.endUpdate();
205  
206     },
207  
208     
209     
210     loadItemTypes : function() 
211     {
212         var _this = this;
213         this.itemTypes =   {
214             company_id_name_ro : {
215                     name : 'company_id_name',
216                     fieldLabel : "Company",
217                     value : '',
218                     xtype : 'TextField',
219                     readOnly : true,
220                     width : 300
221             },
222             
223             company_id_name : {
224                 
225                 xtype: 'ComboBoxAdder',
226                 fieldLabel: "Company",
227                 name : 'company_id_name',
228                 selectOnFocus:true,
229                 qtip : "Select Company",
230                 allowBlank : false,
231                 width: 300,
232                 
233                 store: {
234                     xtype : 'Store',
235                       // load using HTTP
236                     proxy:{
237                         xtype:  'HttpProxy',
238                         url: baseURL + '/Roo/Companies.html',
239                         method: 'GET'
240                     },
241                     reader: Pman.Readers.Companies,
242                     listeners : {
243                         beforeload : function(st,o)
244                         {
245                         
246                             o.params['!comptype'] = 'OWNER';
247                         },
248                         loadexception : Pman.loadException
249                     
250                     },
251                     sortInfo: {
252                         field: 'name', direction: 'ASC'
253                     }
254                 },
255                 displayField:'name',
256                 valueField : 'id',
257                 hiddenName:  'company_id',
258                 typeAhead: true,
259                 forceSelection: true,
260                 //mode: 'local',
261                 triggerAction: 'all',
262                 tpl: new Ext.Template(
263                     '<div class="x-grid-cell-text x-btn button">',
264                         '<b>{name}</b> {address}',
265                     '</div>'
266                 ),
267                 queryParam: 'query[name]',
268                 loadingText: "Searching...",
269                 listWidth: 400,
270                
271                 minChars: 2,
272                 pageSize:20,
273                 listeners : {
274                     adderclick : function()
275                     {
276                         var cb = this;
277                         Pman.Dialog.CoreCompanies.show( {  id: 0 },  function(data) {
278                             cb.setFromData(data);
279                         }); 
280                     }
281                 }
282                
283                  
284                  
285                  
286             },
287             office_id_name_ro : {
288                     name : 'office_id_name',
289                     fieldLabel : "Office",
290                     value : '',
291                     xtype : 'TextField',
292                     readOnly : true,
293                     width : 300
294             },
295             
296             office_id_name : {
297                 
298                 xtype: 'ComboBoxAdder',
299                 fieldLabel: "Office / Department",
300                 name : 'office_id_name',
301                 selectOnFocus:true,
302                 qtip : "Select Office",
303                 allowBlank : true,
304                 width: 300,
305                 
306                 store:  {
307                     xtype : 'Store',
308                       // load using HTTP
309                     proxy: {
310                         xtype : 'HttpProxy',
311                         url: baseURL + '/Roo/Office.html',
312                         method: 'GET'
313                     },
314                     reader: Pman.Readers.Office,
315                     listeners : {
316                         beforeload : function(st,o)
317                         {
318                             // compnay myst be set..
319                             var coid = _this.form.findField('company_id').getValue();
320                             o.params.company_id = coid;
321                         },
322                         loadexception : Pman.loadException
323                     
324                     },
325                     sortInfo: {
326                         field: 'name', direction: 'ASC'
327                     }
328                 },
329                 listeners : {
330                     adderclick : function()
331                     {
332                         var cb = this;
333                         
334                         // for new - we have
335                         var cfg = false;
336                         var data = false;
337                         if (_this.type == 'new') {
338                             data = _this.form.findField('company_id').lastData;
339                             if (!data.id ) {
340                                 Ext.MessageBox.alert("Error", "Select An Company First");
341                                 return false
342                             }
343                             
344                             cfg = {
345                                 company_id : data.id ,
346                                 company_id_name: data.name,
347                                 address: data.address,
348                                 phone: data.tel,
349                                 fax: data.fax,
350                                 email: data.email
351                             };
352
353                         } else { // all other tyeps have the data in the caller data array.
354                             data  = _this.data;
355                             cfg = {
356                                 company_id : data.company_id,
357                                 company_id_name: data.company_id_name,
358                                 address: data.company_id_address,
359                                 phone: data.company_id_tel,
360                                 fax: data.company_id_fax,
361                                 email: data.company_id_email
362                             }
363                         }
364                         
365                          
366                         
367                         
368                         Pman.Dialog.Office.show(cfg, function(data) {
369                                 cb.setFromData(data);
370                         }); 
371                     },
372                     beforequery : function (qe) {
373                         var coid = _this.form.findField('company_id').getValue();
374                         if (coid < 1 ) {
375                             Ext.MessageBox.alert("Error", "Select An Company First");
376                             return false;
377                         }
378                     }
379                 },
380                 displayField:'name',
381                 valueField : 'id',
382                 hiddenName:  'office_id',
383                 typeAhead: true,
384                 forceSelection: true,
385                 //mode: 'local',
386                 triggerAction: 'all',
387                 tpl: new Ext.Template(
388                     '<div class="x-grid-cell-text x-btn button">',
389                         '<b>{name}</b> {address}',
390                     '</div>'
391                 ),
392                 queryParam: 'query[name]',
393                 loadingText: "Searching...",
394                 listWidth: 400,
395                
396                 minChars: 2,
397                 pageSize:20 
398                  
399                  
400             },
401             name : {
402                 name : 'name',
403                 fieldLabel : "Contact Name",
404                 value : '',
405                 allowBlank : false,
406                 qtip : "Enter name",
407                 xtype : 'TextField',
408                 width : 300
409             },
410             role : {
411                 name : 'role',
412                 fieldLabel : "Role / Position",
413                 value : '',
414                 allowBlank : true,
415                 qtip : "Enter Role / Position",
416                 xtype : 'TextField',
417                 width : 300
418             },
419
420             phone : {
421                 name : 'phone',
422                 fieldLabel : "Phone",
423                 value : '',
424                 allowBlank : true,
425                 qtip : "Enter phone Number",
426                 xtype : 'TextField',
427                 width : 300
428             },
429             fax : {
430               
431                 name : 'fax',
432                 fieldLabel : "Fax",
433                 value : '',
434                 allowBlank : true,
435                 qtip : "Enter fax",
436                 xtype : 'TextField',
437                 width : 300
438             },
439             email : {
440                 name : 'email',
441                 fieldLabel : "Email",
442                 value : '',
443                 allowBlank : true,
444                 qtip : "Enter email",
445                 xtype : 'TextField',
446                 width : 300
447             },
448             bulklist: {
449                 name : 'bulklist',
450                 fieldLabel : "Email address (one per line)",
451                 value : '',
452                 allowBlank : false,
453                 qtip : "Enter email addresse",
454                 xtype : 'TextArea',
455                 width : 300,
456                 height:  200
457             },
458             
459             
460             email_req : {
461                 name : 'email',
462                 fieldLabel : "Email",
463                 value : '',
464                 allowBlank : false,
465                 qtip : "Enter email",
466                 xtype : 'TextField',
467                 width : 300
468             },
469             countries : {
470                 xtype: 'ComboBoxArray',
471                 xns: Roo.form,
472                 fieldLabel : 'Country',
473                 hiddenName : 'countries',
474                 name : 'countries_name',
475                 width : 300,
476                 combo : {
477                     xtype: 'ComboBox',
478                     xns: Roo.form,
479                     allowBlank : true,
480                     alwaysQuery : true,
481                     displayField : 'title',
482                     editable : false,
483                     emptyText : "Select Country",
484                     forceSelection : true,
485                     idField : 'id',
486                     triggerAction : 'all',
487                     typeAhead : true,
488                     valueField : 'code',
489                     width : 280,
490                     store : {
491                         xtype: 'SimpleStore',
492                         xns: Roo.data,
493                         data : (function() {
494                             return Pman.I18n.simpleStoreData('c');
495                         })(),
496                         fields : [  'code', 'title' ]
497                     }
498                 }
499             },
500             passwd1 : {
501                 name : 'passwd1',
502                 fieldLabel : "New Password ",
503                 value : '',
504                 allowBlank : true, // must be filled in as we rely on it for login details..
505                 inputType: 'password', // << if comment out this input type, it should be ok
506                 xtype : 'SecurePass',
507                 width : 220,
508                 imageRoot : rootURL + '/Pman/templates/images'
509             },
510             passwd2 : {
511                 
512                 name : 'passwd2',
513                 fieldLabel : "Password (type again to confirm)",
514                 value : '',
515                 allowBlank : true, // must be filled in as we rely on it for login details..
516                 inputType: 'password', // << if comment out this input type, it should be ok
517                 xtype : 'TextField',
518                 width : 220
519             },
520             secure_passowrd : {
521                 name : 'secure_passowrd',
522                 fieldLabel : "Secure passwords",
523                 inputValue : 1,
524                 valueOff : 0,
525                 checked : true,
526                 xtype : 'Checkbox',
527                 xns : Roo.form,
528                 width : 220,
529                 listeners : {
530                     check : function (_self, checked) {
531                         Roo.log(this.form);
532                         if(checked){
533                             
534                         }
535                     }
536                 }
537             },
538             project_id_fs : {
539                 xtype : 'FieldSetEx',
540                 name: 'project_id_fs',
541                 value: 0,
542                 labelWidth: 100,
543                 expanded: false,
544                 style: 'width:420px;',
545                 legend : "Always File Messages from this Person in Project",
546                 items : [
547                     Pman.Std.project_id({
548                         width: 300,
549                         fieldLabel : "Project",
550                         allowBlank : true
551                     }),
552                     {
553                       xtype: 'ComboBox',
554                         name : 'action_type_str',
555                         selectOnFocus:true,
556                         qtip : "Action Type",
557                         fieldLabel : "Action Required",
558
559                         allowBlank : true,
560                         width: 50,
561                         
562                         
563                         store: new Ext.data.SimpleStore({
564                               // load using HTTP
565                             fields: [ 'code', 'desc' ],
566                             data:  [[ 'ACTION_REQUIRED', "Yes"] , [ 'NOTIFY', "No"] ]
567                         }),
568                         displayField:'desc',
569                         editable : false,
570                         valueField : 'code',
571                         hiddenName:  'action_type',
572                         value : 'ACTION_REQUIRED',
573                         forceSelection: true,
574                         mode: 'local',
575                         triggerAction: 'all' 
576                        // queryParam: 'query[project]',
577                        // loadingText: "Searching...",
578                         //listWidth: 400
579                        
580                          
581                        
582                     }
583                 ]
584             },
585             
586             id : { name : 'id', value : '', xtype : 'Hidden' },
587             save_send : { name : '_save_send', value : 0, xtype : 'Hidden' },
588             active : { name : 'active', value : 1, xtype : 'Hidden' },
589             company_id : { name : 'company_id', value : '', xtype : 'Hidden' },
590             company_id_email : { name : 'company_id_email', value : '', xtype : 'Hidden' },
591             company_id_address : { name : 'company_id_address', value : '', xtype : 'Hidden' },
592             company_id_tel : { name : 'company_id_tel', value : '', xtype : 'Hidden' },
593             company_id_fax : { name : 'company_id_fax', value : '', xtype : 'Hidden' },
594             project_id_addto : { name : 'project_id_addto', value : '', xtype : 'Hidden' }
595         };
596     
597     }, //end getItemTypes
598     
599     saveSend : function(bt, e)
600     {
601         this.save(bt,e, 1)
602     },
603     sendAfterSave : 0,
604     save : function(bt, e, andsend)
605     {
606         // ensure a company has been selected..
607         this.sendAfterSave  = andsend || 0;
608         
609         if (this.form.findField('bulklist')) {
610             this.saveBulk();
611             return;
612             
613         }
614         if (this.form.findField('company_id') && !this.form.findField('company_id').getValue()) {
615             Ext.MessageBox.alert("Error", "Select a Company");
616             return;
617         }
618         
619         if (this.form.findField('passwd1')) {
620             
621             var p1 = this.form.findField('passwd1').getValue();
622             var p2 = this.form.findField('passwd2').getValue();
623             
624             if (this.sendAfterSave && !p1.length) {
625                 Ext.MessageBox.alert("Error", "You must create a password to send introduction mail");
626                 return;
627             }
628             
629             if (Pman.Login.authUser.id < 0 && !p1.length) {
630                 Ext.MessageBox.alert("Error", "You must create a password for the admin account");
631                 return;
632             }
633             
634             
635             if (p1.length || p2.length) {
636                 if (p1 != p2) {
637                     Ext.MessageBox.alert("Error", "Passwords do not match");
638                     return;
639                 }
640             }
641             
642         
643         }
644         // ensure it's blank!
645         if (this.form.findField('project_id')) {
646             if (!this.form.findbyId('project_id_fs').expanded) {
647                 this.form.findField('project_id').setFromData({
648                     id : 0,
649                     code : ''
650                 });
651             }
652         }
653         this.dialog.el.mask("Sending");
654         this.form.doAction('submit', {
655             url: baseURL + '/Roo/Person.html',
656             method: 'POST',
657             params: {
658                 _id: this._id ,
659                 ts : Math.random()
660             } 
661         });
662     },
663
664     
665      
666     show: function (data, callback)
667     {
668         
669         this.callback = callback;
670         this._id = data.id;
671         this.data = data;
672         this.create();
673         this.form.reset();
674         if ( this._id) {
675             this.dialog.show();
676             this.dialog.el.mask("Loading");
677             this.form.doAction('load', {
678                 url: baseURL + '/Roo/Person.html',
679                 method: 'GET',
680                 params: {
681                     _id: this._id ,
682                     _ts : Math.random()
683                 } 
684             });
685            // this.fireEvent('show');
686             return;
687         }
688         //} else {
689         this.form.setValues(data);
690         //}
691         this.form.fireEvent('actioncomplete', this.form,{
692             type : 'setdata',
693             data: data
694         });
695          
696         this.dialog.show();
697         // no need to load...
698
699     },
700     
701     saveBulk: function() {
702         // similar action to SendIntro
703         // we build a fake list of data..
704         if (!this.form.findField('company_id').getValue()) {
705             Roo.MessageBox.alert("Error", "Select the Company Name");
706             return;
707         }
708         // prompt..
709         var adr = [];
710         var _this = this;
711         
712         Roo.MessageBox.confirm("Send Welcome", "Send Welcome Messages and Generate Passwords?",
713             function(yn) {
714                 var pw = 1;
715                 //console.log(yn);
716                 if (yn != 'yes') {
717                     pw = 0;
718                 }
719                 Roo.each(_this.form.findField('bulklist').getValue().split("\n"), function(v) {
720                     if (!v.length || !v.replace(new RegExp(' ', 'g'), '').length) {
721                         return;
722                     }
723                     adr.push({
724                         id:  0,
725                         email : v,
726                         company_id : _this.form.findField('company_id').getValue(),
727                         office_id  : _this.form.findField('office_id').getValue(),
728                         active : 1,
729                         _create : 1,
730                         _createPasswd : pw
731                         
732                     })
733                 });
734                 if (!adr.length) {
735                     Roo.MessageBox.alert("Error", "No addresses found");
736                     return;
737                 }
738                 _this.dialog.hide();
739                 _this.sendIntro(adr, "Creating Account / Sending Welcome", _this.callback)
740             }
741         );
742         
743         
744         
745         
746       
747       
748     },
749     
750     
751     sendIntro  : function(ar, msg, callback) {
752         // can hanlde multiple items -- will be usefull for later usage
753         // when we do list of multiple users..
754         var i =0;
755         
756         Roo.MessageBox.show({
757            title: "Please wait...",
758            msg: msg,
759            width:350,
760            progress:true,
761            closable:false
762         });
763         
764         //this.sendData = ar; console.log(ar);
765         var _this = this;
766         var wis = function () 
767         {
768             if (i == ar.length) {
769                 Roo.MessageBox.hide();
770                 Roo.MessageBox.alert("Done", "Done - " + msg);
771                 if (callback) {
772                     callback.call(this, false);
773                 }
774                 return;
775             }
776             Roo.MessageBox.updateProgress( 
777                 (i+1)/ar.length,  msg + " : " + ar[i].email
778             );
779             
780              
781             var c = ar[i];
782             i++;
783             new Pman.Request({
784                 url : baseURL+'/Core/SendIntro.html',
785                 method : 'POST',
786                 params: c,
787                 success : function(resp, opts) {
788                     wis();
789                 },
790                 failure: function()
791                 {
792                     Roo.MessageBox.show({
793                        title: "Please wait...",
794                        msg: msg,
795                        width:350,
796                        progress:true,
797                        closable:false
798                     });
799                     // error condition!?!?
800                     wis();
801                 }
802                 
803             });
804             
805         };
806         wis();
807         
808         
809         
810     }
811          
812 };