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