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 : 'passwd2',
522                 fieldLabel : "Password (type again to confirm)",
523                 value : '',
524                 allowBlank : true, // must be filled in as we rely on it for login details..
525                 inputType: 'password', // << if comment out this input type, it should be ok
526                 xtype : 'TextField',
527                 width : 220
528             },
529             project_id_fs : {
530                 xtype : 'FieldSetEx',
531                 name: 'project_id_fs',
532                 value: 0,
533                 labelWidth: 100,
534                 expanded: false,
535                 style: 'width:420px;',
536                 legend : "Always File Messages from this Person in Project",
537                 items : [
538                     Pman.Std.project_id({
539                         width: 300,
540                         fieldLabel : "Project",
541                         allowBlank : true
542                     }),
543                     {
544                       xtype: 'ComboBox',
545                         name : 'action_type_str',
546                         selectOnFocus:true,
547                         qtip : "Action Type",
548                         fieldLabel : "Action Required",
549
550                         allowBlank : true,
551                         width: 50,
552                         
553                         
554                         store: new Ext.data.SimpleStore({
555                               // load using HTTP
556                             fields: [ 'code', 'desc' ],
557                             data:  [[ 'ACTION_REQUIRED', "Yes"] , [ 'NOTIFY', "No"] ]
558                         }),
559                         displayField:'desc',
560                         editable : false,
561                         valueField : 'code',
562                         hiddenName:  'action_type',
563                         value : 'ACTION_REQUIRED',
564                         forceSelection: true,
565                         mode: 'local',
566                         triggerAction: 'all' 
567                        // queryParam: 'query[project]',
568                        // loadingText: "Searching...",
569                         //listWidth: 400
570                        
571                          
572                        
573                     }
574                 ]
575             },
576             
577             id : { name : 'id', value : '', xtype : 'Hidden' },
578             save_send : { name : '_save_send', value : 0, xtype : 'Hidden' },
579             active : { name : 'active', value : 1, xtype : 'Hidden' },
580             company_id : { name : 'company_id', value : '', xtype : 'Hidden' },
581             company_id_email : { name : 'company_id_email', value : '', xtype : 'Hidden' },
582             company_id_address : { name : 'company_id_address', value : '', xtype : 'Hidden' },
583             company_id_tel : { name : 'company_id_tel', value : '', xtype : 'Hidden' },
584             company_id_fax : { name : 'company_id_fax', value : '', xtype : 'Hidden' },
585             project_id_addto : { name : 'project_id_addto', value : '', xtype : 'Hidden' }
586         };
587     
588     }, //end getItemTypes
589     
590     saveSend : function(bt, e)
591     {
592         this.save(bt,e, 1)
593     },
594     sendAfterSave : 0,
595     save : function(bt, e, andsend)
596     {
597         // ensure a company has been selected..
598         this.sendAfterSave  = andsend || 0;
599         
600         if (this.form.findField('bulklist')) {
601             this.saveBulk();
602             return;
603             
604         }
605         if (this.form.findField('company_id') && !this.form.findField('company_id').getValue()) {
606             Ext.MessageBox.alert("Error", "Select a Company");
607             return;
608         }
609         
610         if (this.form.findField('passwd1')) {
611             
612             var p1 = this.form.findField('passwd1').getValue();
613             var p2 = this.form.findField('passwd2').getValue();
614             
615             if (this.sendAfterSave && !p1.length) {
616                 Ext.MessageBox.alert("Error", "You must create a password to send introduction mail");
617                 return;
618             }
619             
620             if (Pman.Login.authUser.id < 0 && !p1.length) {
621                 Ext.MessageBox.alert("Error", "You must create a password for the admin account");
622                 return;
623             }
624             
625             
626             if (p1.length || p2.length) {
627                 if (p1 != p2) {
628                     Ext.MessageBox.alert("Error", "Passwords do not match");
629                     return;
630                 }
631             }
632             
633         
634         }
635         // ensure it's blank!
636         if (this.form.findField('project_id')) {
637             if (!this.form.findbyId('project_id_fs').expanded) {
638                 this.form.findField('project_id').setFromData({
639                     id : 0,
640                     code : ''
641                 });
642             }
643         }
644         this.dialog.el.mask("Sending");
645         this.form.doAction('submit', {
646             url: baseURL + '/Roo/Person.html',
647             method: 'POST',
648             params: {
649                 _id: this._id ,
650                 ts : Math.random()
651             } 
652         });
653     },
654
655     
656      
657     show: function (data, callback)
658     {
659         
660         this.callback = callback;
661         this._id = data.id;
662         this.data = data;
663         this.create();
664         this.form.reset();
665         if ( this._id) {
666             this.dialog.show();
667             this.dialog.el.mask("Loading");
668             this.form.doAction('load', {
669                 url: baseURL + '/Roo/Person.html',
670                 method: 'GET',
671                 params: {
672                     _id: this._id ,
673                     _ts : Math.random()
674                 } 
675             });
676            // this.fireEvent('show');
677             return;
678         }
679         //} else {
680         this.form.setValues(data);
681         //}
682         this.form.fireEvent('actioncomplete', this.form,{
683             type : 'setdata',
684             data: data
685         });
686          
687         this.dialog.show();
688         // no need to load...
689
690     },
691     
692     saveBulk: function() {
693         // similar action to SendIntro
694         // we build a fake list of data..
695         if (!this.form.findField('company_id').getValue()) {
696             Roo.MessageBox.alert("Error", "Select the Company Name");
697             return;
698         }
699         // prompt..
700         var adr = [];
701         var _this = this;
702         
703         Roo.MessageBox.confirm("Send Welcome", "Send Welcome Messages and Generate Passwords?",
704             function(yn) {
705                 var pw = 1;
706                 //console.log(yn);
707                 if (yn != 'yes') {
708                     pw = 0;
709                 }
710                 Roo.each(_this.form.findField('bulklist').getValue().split("\n"), function(v) {
711                     if (!v.length || !v.replace(new RegExp(' ', 'g'), '').length) {
712                         return;
713                     }
714                     adr.push({
715                         id:  0,
716                         email : v,
717                         company_id : _this.form.findField('company_id').getValue(),
718                         office_id  : _this.form.findField('office_id').getValue(),
719                         active : 1,
720                         _create : 1,
721                         _createPasswd : pw
722                         
723                     })
724                 });
725                 if (!adr.length) {
726                     Roo.MessageBox.alert("Error", "No addresses found");
727                     return;
728                 }
729                 _this.dialog.hide();
730                 _this.sendIntro(adr, "Creating Account / Sending Welcome", _this.callback)
731             }
732         );
733         
734         
735         
736         
737       
738       
739     },
740     
741     
742     sendIntro  : function(ar, msg, callback) {
743         // can hanlde multiple items -- will be usefull for later usage
744         // when we do list of multiple users..
745         var i =0;
746         
747         Roo.MessageBox.show({
748            title: "Please wait...",
749            msg: msg,
750            width:350,
751            progress:true,
752            closable:false
753         });
754         
755         //this.sendData = ar; console.log(ar);
756         var _this = this;
757         var wis = function () 
758         {
759             if (i == ar.length) {
760                 Roo.MessageBox.hide();
761                 Roo.MessageBox.alert("Done", "Done - " + msg);
762                 if (callback) {
763                     callback.call(this, false);
764                 }
765                 return;
766             }
767             Roo.MessageBox.updateProgress( 
768                 (i+1)/ar.length,  msg + " : " + ar[i].email
769             );
770             
771              
772             var c = ar[i];
773             i++;
774             new Pman.Request({
775                 url : baseURL+'/Core/SendIntro.html',
776                 method : 'POST',
777                 params: c,
778                 success : function(resp, opts) {
779                     wis();
780                 },
781                 failure: function()
782                 {
783                     Roo.MessageBox.show({
784                        title: "Please wait...",
785                        msg: msg,
786                        width:350,
787                        progress:true,
788                        closable:false
789                     });
790                     // error condition!?!?
791                     wis();
792                 }
793                 
794             });
795             
796         };
797         wis();
798         
799         
800         
801     }
802          
803 };