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