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