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             
185         });
186         var ef = this.dialog.getLayout().getEl().createChild({tag: 'div'});
187         ef.dom.style.margin = 10;
188          
189         this.form.render(ef.dom);
190
191         var vp = this.dialog.getLayout().add('center', new Ext.ContentPanel(ef, {
192             autoCreate : true,
193             //title: 'Org Details',
194             //toolbar: this.tb,
195             width: 250,
196             maxWidth: 250,
197             fitToFrame:true
198         }));
199           
200
201         
202         
203         this.layout.endUpdate();
204  
205     },
206  
207     
208     
209     loadItemTypes : function() 
210     {
211         var _this = this;
212         this.itemTypes =   {
213             company_id_name_ro : {
214                     name : 'company_id_name',
215                     fieldLabel : "Company",
216                     value : '',
217                     xtype : 'TextField',
218                     readOnly : true,
219                     width : 300
220             },
221             
222             company_id_name : {
223                 
224                 xtype: 'ComboBoxAdder',
225                 fieldLabel: "Company",
226                 name : 'company_id_name',
227                 selectOnFocus:true,
228                 qtip : "Select Company",
229                 allowBlank : false,
230                 width: 300,
231                 
232                 store: {
233                     xtype : 'Store',
234                       // load using HTTP
235                     proxy:{
236                         xtype:  'HttpProxy',
237                         url: baseURL + '/Roo/Companies.html',
238                         method: 'GET'
239                     },
240                     reader: Pman.Readers.Companies,
241                     listeners : {
242                         beforeload : function(st,o)
243                         {
244                         
245                             o.params['!comptype'] = 'OWNER';
246                         },
247                         loadexception : Pman.loadException
248                     
249                     },
250                     sortInfo: {
251                         field: 'name', direction: 'ASC'
252                     }
253                 },
254                 displayField:'name',
255                 valueField : 'id',
256                 hiddenName:  'company_id',
257                 typeAhead: true,
258                 forceSelection: true,
259                 //mode: 'local',
260                 triggerAction: 'all',
261                 tpl: new Ext.Template(
262                     '<div class="x-grid-cell-text x-btn button">',
263                         '<b>{name}</b> {address}',
264                     '</div>'
265                 ),
266                 queryParam: 'query[name]',
267                 loadingText: "Searching...",
268                 listWidth: 400,
269                
270                 minChars: 2,
271                 pageSize:20,
272                 listeners : {
273                     adderclick : function()
274                     {
275                         var cb = this;
276                         Pman.Dialog.CoreCompanies.show( {  id: 0 },  function(data) {
277                             cb.setFromData(data);
278                         }); 
279                     }
280                 }
281                
282                  
283                  
284                  
285             },
286             office_id_name_ro : {
287                     name : 'office_id_name',
288                     fieldLabel : "Office",
289                     value : '',
290                     xtype : 'TextField',
291                     readOnly : true,
292                     width : 300
293             },
294             
295             office_id_name : {
296                 
297                 xtype: 'ComboBoxAdder',
298                 fieldLabel: "Office / Department",
299                 name : 'office_id_name',
300                 selectOnFocus:true,
301                 qtip : "Select Office",
302                 allowBlank : true,
303                 width: 300,
304                 
305                 store:  {
306                     xtype : 'Store',
307                       // load using HTTP
308                     proxy: {
309                         xtype : 'HttpProxy',
310                         url: baseURL + '/Roo/Office.html',
311                         method: 'GET'
312                     },
313                     reader: Pman.Readers.Office,
314                     listeners : {
315                         beforeload : function(st,o)
316                         {
317                             // compnay myst be set..
318                             var coid = _this.form.findField('company_id').getValue();
319                             o.params.company_id = coid;
320                         },
321                         loadexception : Pman.loadException
322                     
323                     },
324                     sortInfo: {
325                         field: 'name', direction: 'ASC'
326                     }
327                 },
328                 listeners : {
329                     adderclick : function()
330                     {
331                         var cb = this;
332                         
333                         // for new - we have
334                         var cfg = false;
335                         var data = false;
336                         if (_this.type == 'new') {
337                             data = _this.form.findField('company_id').lastData;
338                             if (!data.id ) {
339                                 Ext.MessageBox.alert("Error", "Select An Company First");
340                                 return false
341                             }
342                             
343                             cfg = {
344                                 company_id : data.id ,
345                                 company_id_name: data.name,
346                                 address: data.address,
347                                 phone: data.tel,
348                                 fax: data.fax,
349                                 email: data.email
350                             };
351
352                         } else { // all other tyeps have the data in the caller data array.
353                             data  = _this.data;
354                             cfg = {
355                                 company_id : data.company_id,
356                                 company_id_name: data.company_id_name,
357                                 address: data.company_id_address,
358                                 phone: data.company_id_tel,
359                                 fax: data.company_id_fax,
360                                 email: data.company_id_email
361                             }
362                         }
363                         
364                          
365                         
366                         
367                         Pman.Dialog.Office.show(cfg, function(data) {
368                                 cb.setFromData(data);
369                         }); 
370                     },
371                     beforequery : function (qe) {
372                         var coid = _this.form.findField('company_id').getValue();
373                         if (coid < 1 ) {
374                             Ext.MessageBox.alert("Error", "Select An Company First");
375                             return false;
376                         }
377                     }
378                 },
379                 displayField:'name',
380                 valueField : 'id',
381                 hiddenName:  'office_id',
382                 typeAhead: true,
383                 forceSelection: true,
384                 //mode: 'local',
385                 triggerAction: 'all',
386                 tpl: new Ext.Template(
387                     '<div class="x-grid-cell-text x-btn button">',
388                         '<b>{name}</b> {address}',
389                     '</div>'
390                 ),
391                 queryParam: 'query[name]',
392                 loadingText: "Searching...",
393                 listWidth: 400,
394                
395                 minChars: 2,
396                 pageSize:20 
397                  
398                  
399             },
400             name : {
401                 name : 'name',
402                 fieldLabel : "Contact Name",
403                 value : '',
404                 allowBlank : false,
405                 qtip : "Enter name",
406                 xtype : 'TextField',
407                 width : 300
408             },
409             role : {
410                 name : 'role',
411                 fieldLabel : "Role / Position",
412                 value : '',
413                 allowBlank : true,
414                 qtip : "Enter Role / Position",
415                 xtype : 'TextField',
416                 width : 300
417             },
418
419             phone : {
420                 name : 'phone',
421                 fieldLabel : "Phone",
422                 value : '',
423                 allowBlank : true,
424                 qtip : "Enter phone Number",
425                 xtype : 'TextField',
426                 width : 300
427             },
428             fax : {
429               
430                 name : 'fax',
431                 fieldLabel : "Fax",
432                 value : '',
433                 allowBlank : true,
434                 qtip : "Enter fax",
435                 xtype : 'TextField',
436                 width : 300
437             },
438             email : {
439                 name : 'email',
440                 fieldLabel : "Email",
441                 value : '',
442                 allowBlank : true,
443                 qtip : "Enter email",
444                 xtype : 'TextField',
445                 width : 300
446             },
447             bulklist: {
448                 name : 'bulklist',
449                 fieldLabel : "Email address (one per line)",
450                 value : '',
451                 allowBlank : false,
452                 qtip : "Enter email addresse",
453                 xtype : 'TextArea',
454                 width : 300,
455                 height:  200
456             },
457             
458             
459             email_req : {
460                 name : 'email',
461                 fieldLabel : "Email",
462                 value : '',
463                 allowBlank : false,
464                 qtip : "Enter email",
465                 xtype : 'TextField',
466                 width : 300
467             },
468             countries : {
469                 xtype: 'ComboBoxArray',
470                 xns: Roo.form,
471                 fieldLabel : 'Country',
472                 hiddenName : 'countries',
473                 name : 'countries_name',
474                 width : 300,
475                 combo : {
476                     xtype: 'ComboBox',
477                     xns: Roo.form,
478                     allowBlank : true,
479                     alwaysQuery : true,
480                     displayField : 'title',
481                     editable : false,
482                     emptyText : "Select Country",
483                     forceSelection : true,
484                     idField : 'id',
485                     triggerAction : 'all',
486                     typeAhead : true,
487                     valueField : 'code',
488                     width : 280,
489                     store : {
490                         xtype: 'SimpleStore',
491                         xns: Roo.data,
492                         data : (function() {
493                             return Pman.I18n.simpleStoreData('c');
494                         })(),
495                         fields : [  'code', 'title' ]
496                     }
497                 }
498             },
499             passwd1 : {
500                 name : 'passwd1',
501                 fieldLabel : "New Password ",
502                 value : '',
503                 allowBlank : true, // must be filled in as we rely on it for login details..
504                 inputType: 'password', // << if comment out this input type, it should be ok
505                 xtype : 'SecurePass',
506                 width : 220,
507                 imageRoot : rootURL + '/Pman/templates/images'
508             },
509             passwd2 : {
510                 
511                 name : 'passwd2',
512                 fieldLabel : "Password (type again to confirm)",
513                 value : '',
514                 allowBlank : true, // must be filled in as we rely on it for login details..
515                 inputType: 'password', // << if comment out this input type, it should be ok
516                 xtype : 'TextField',
517                 width : 220
518             },
519             project_id_fs : {
520                 xtype : 'FieldSetEx',
521                 name: 'project_id_fs',
522                 value: 0,
523                 labelWidth: 100,
524                 expanded: false,
525                 style: 'width:420px;',
526                 legend : "Always File Messages from this Person in Project",
527                 items : [
528                     Pman.Std.project_id({
529                         width: 300,
530                         fieldLabel : "Project",
531                         allowBlank : true
532                     }),
533                     {
534                       xtype: 'ComboBox',
535                         name : 'action_type_str',
536                         selectOnFocus:true,
537                         qtip : "Action Type",
538                         fieldLabel : "Action Required",
539
540                         allowBlank : true,
541                         width: 50,
542                         
543                         
544                         store: new Ext.data.SimpleStore({
545                               // load using HTTP
546                             fields: [ 'code', 'desc' ],
547                             data:  [[ 'ACTION_REQUIRED', "Yes"] , [ 'NOTIFY', "No"] ]
548                         }),
549                         displayField:'desc',
550                         editable : false,
551                         valueField : 'code',
552                         hiddenName:  'action_type',
553                         value : 'ACTION_REQUIRED',
554                         forceSelection: true,
555                         mode: 'local',
556                         triggerAction: 'all' 
557                        // queryParam: 'query[project]',
558                        // loadingText: "Searching...",
559                         //listWidth: 400
560                        
561                          
562                        
563                     }
564                 ]
565             },
566             
567             id : { name : 'id', value : '', xtype : 'Hidden' },
568             save_send : { name : '_save_send', value : 0, xtype : 'Hidden' },
569             active : { name : 'active', value : 1, xtype : 'Hidden' },
570             company_id : { name : 'company_id', value : '', xtype : 'Hidden' },
571             company_id_email : { name : 'company_id_email', value : '', xtype : 'Hidden' },
572             company_id_address : { name : 'company_id_address', value : '', xtype : 'Hidden' },
573             company_id_tel : { name : 'company_id_tel', value : '', xtype : 'Hidden' },
574             company_id_fax : { name : 'company_id_fax', value : '', xtype : 'Hidden' },
575             project_id_addto : { name : 'project_id_addto', value : '', xtype : 'Hidden' }
576         };
577     
578     }, //end getItemTypes
579     
580     saveSend : function(bt, e)
581     {
582         this.save(bt,e, 1)
583     },
584     sendAfterSave : 0,
585     save : function(bt, e, andsend)
586     {
587         // ensure a company has been selected..
588         this.sendAfterSave  = andsend || 0;
589         
590         if (this.form.findField('bulklist')) {
591             this.saveBulk();
592             return;
593             
594         }
595         if (this.form.findField('company_id') && !this.form.findField('company_id').getValue()) {
596             Ext.MessageBox.alert("Error", "Select a Company");
597             return;
598         }
599         
600         if (this.form.findField('passwd1')) {
601             
602             var p1 = this.form.findField('passwd1').getValue();
603             var p2 = this.form.findField('passwd2').getValue();
604             
605             if (this.sendAfterSave && !p1.length) {
606                 Ext.MessageBox.alert("Error", "You must create a password to send introduction mail");
607                 return;
608             }
609             
610             if (Pman.Login.authUser.id < 0 && !p1.length) {
611                 Ext.MessageBox.alert("Error", "You must create a password for the admin account");
612                 return;
613             }
614             
615             
616             if (p1.length || p2.length) {
617                 if (p1 != p2) {
618                     Ext.MessageBox.alert("Error", "Passwords do not match");
619                     return;
620                 }
621             }
622             
623         
624         }
625         // ensure it's blank!
626         if (this.form.findField('project_id')) {
627             if (!this.form.findbyId('project_id_fs').expanded) {
628                 this.form.findField('project_id').setFromData({
629                     id : 0,
630                     code : ''
631                 });
632             }
633         }
634         this.dialog.el.mask("Sending");
635         this.form.doAction('submit', {
636             url: baseURL + '/Roo/Person.html',
637             method: 'POST',
638             params: {
639                 _id: this._id ,
640                 ts : Math.random()
641             } 
642         });
643     },
644
645     
646      
647     show: function (data, callback)
648     {
649         
650         this.callback = callback;
651         this._id = data.id;
652         this.data = data;
653         this.create();
654         this.form.reset();
655         if ( this._id) {
656             this.dialog.show();
657             this.dialog.el.mask("Loading");
658             this.form.doAction('load', {
659                 url: baseURL + '/Roo/Person.html',
660                 method: 'GET',
661                 params: {
662                     _id: this._id ,
663                     _ts : Math.random()
664                 } 
665             });
666            // this.fireEvent('show');
667             return;
668         }
669         //} else {
670         this.form.setValues(data);
671         //}
672         this.form.fireEvent('actioncomplete', this.form,{
673             type : 'setdata',
674             data: data
675         });
676          
677         this.dialog.show();
678         // no need to load...
679
680     },
681     
682     saveBulk: function() {
683         // similar action to SendIntro
684         // we build a fake list of data..
685         if (!this.form.findField('company_id').getValue()) {
686             Roo.MessageBox.alert("Error", "Select the Company Name");
687             return;
688         }
689         // prompt..
690         var adr = [];
691         var _this = this;
692         
693         Roo.MessageBox.confirm("Send Welcome", "Send Welcome Messages and Generate Passwords?",
694             function(yn) {
695                 var pw = 1;
696                 //console.log(yn);
697                 if (yn != 'yes') {
698                     pw = 0;
699                 }
700                 Roo.each(_this.form.findField('bulklist').getValue().split("\n"), function(v) {
701                     if (!v.length || !v.replace(new RegExp(' ', 'g'), '').length) {
702                         return;
703                     }
704                     adr.push({
705                         id:  0,
706                         email : v,
707                         company_id : _this.form.findField('company_id').getValue(),
708                         office_id  : _this.form.findField('office_id').getValue(),
709                         active : 1,
710                         _create : 1,
711                         _createPasswd : pw
712                         
713                     })
714                 });
715                 if (!adr.length) {
716                     Roo.MessageBox.alert("Error", "No addresses found");
717                     return;
718                 }
719                 _this.dialog.hide();
720                 _this.sendIntro(adr, "Creating Account / Sending Welcome", _this.callback)
721             }
722         );
723         
724         
725         
726         
727       
728       
729     },
730     
731     
732     sendIntro  : function(ar, msg, callback) {
733         // can hanlde multiple items -- will be usefull for later usage
734         // when we do list of multiple users..
735         var i =0;
736         
737         Roo.MessageBox.show({
738            title: "Please wait...",
739            msg: msg,
740            width:350,
741            progress:true,
742            closable:false
743         });
744         
745         //this.sendData = ar; console.log(ar);
746         var _this = this;
747         var wis = function () 
748         {
749             if (i == ar.length) {
750                 Roo.MessageBox.hide();
751                 Roo.MessageBox.alert("Done", "Done - " + msg);
752                 if (callback) {
753                     callback.call(this, false);
754                 }
755                 return;
756             }
757             Roo.MessageBox.updateProgress( 
758                 (i+1)/ar.length,  msg + " : " + ar[i].email
759             );
760             
761              
762             var c = ar[i];
763             i++;
764             new Pman.Request({
765                 url : baseURL+'/Core/SendIntro.html',
766                 method : 'POST',
767                 params: c,
768                 success : function(resp, opts) {
769                     wis();
770                 },
771                 failure: function()
772                 {
773                     Roo.MessageBox.show({
774                        title: "Please wait...",
775                        msg: msg,
776                        width:350,
777                        progress:true,
778                        closable:false
779                     });
780                     // error condition!?!?
781                     wis();
782                 }
783                 
784             });
785             
786         };
787         wis();
788         
789         
790         
791     }
792          
793 };