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