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