Pman.Dialog.CoreEmail.bjs
[Pman.Core] / Pman.Dialog.CoreEmail.js
1 //<script type="text/javascript">
2
3 // Auto generated file - created by app.Builder.js- do not edit directly (at present!)
4
5 Pman.Dialog.CoreEmail = {
6
7     dialog : false,
8     callback:  false,
9
10     show : function(data, cb)
11     {
12         if (!this.dialog) {
13             this.create();
14         }
15
16         this.callback = cb;
17         this.data = data;
18         this.dialog.show(this.data._el);
19         if (this.form) {
20            this.form.reset();
21            this.form.setValues(data);
22            this.form.fireEvent('actioncomplete', this.form,  { type: 'setdata', data: data });
23         }
24
25     },
26
27     create : function()
28     {
29         var _this = this;
30         this.dialog = Roo.factory({
31             xtype: 'LayoutDialog',
32             xns: Roo,
33             listeners : {
34                 show : function (_self)
35                 {
36                     
37                     _self.layout.getRegion('center').showPanel(0);
38                     var w = Roo.lib.Dom.getViewWidth();\r
39                     var h = Roo.lib.Dom.getViewHeight();    \r    this.resizeTo(w-50, h-50);\r
40                     this.center();\r    
41                     var ew = Math.max(250, w-320);\r
42                     var eh = Math.max(250, h-350) ;\r
43                     var e = _this.dialog.layout.getRegion('east');
44                     if (e.visible) {
45                         e.hide();
46                     }
47                     
48                     var el = _self.getEl();
49                     var elw = el.dom.clientWidth;
50                     
51                     var bdtext = _this.form.findField('bodytext');
52                     var ptext = _this.form.findField('plaintext');
53                     if(bdtext.resizeEl){
54                         bdtext.width = elw-100;
55                         bdtext.resizeEl.resizeTo.defer(110, bdtext.resizeEl,[ bdtext.width, bdtext.height  ] );
56                         ptext.setSize(bdtext.width , bdtext.height);
57                     }
58                     
59                 }
60             },
61             closable : true,
62             collapsible : false,
63             height : 500,
64             modal : true,
65             resizable : true,
66             title : "Edit / Create Message",
67             width : 800,
68             items : [
69                 {
70                     xtype: 'NestedLayoutPanel',
71                     xns: Roo,
72                     autoScroll : false,
73                     fitContainer : true,
74                     fitToFrame : true,
75                     region : 'center',
76                     toolbar : {
77                         xtype: 'Toolbar',
78                         xns: Roo,
79                         items : [
80                             {
81                                 xtype: 'Button',
82                                 xns: Roo.Toolbar,
83                                 text : "Import",
84                                 menu : {
85                                     xtype: 'Menu',
86                                     xns: Roo.menu,
87                                     items : [
88                                         {
89                                             xtype: 'Item',
90                                             xns: Roo.menu,
91                                             listeners : {
92                                                 click : function (_self, e)
93                                                 {
94                                                     Pman.Dialog.CoreImportUrl.show({
95                                                         target : '/Core/ImportMailMessage.php'
96                                                     }, function(data) {
97                                                         if  (data) {
98                                                           //  Roo.log(data);
99                                                             _this.form.findField('bodytext').setValue(data);
100                                                         }
101                                                     });
102                                                 }
103                                             },
104                                             text : "URL"
105                                         },
106                                         {
107                                             xtype: 'Item',
108                                             xns: Roo.menu,
109                                             listeners : {
110                                                 click : function (_self, e)
111                                                 {
112                                                     Pman.Dialog.Image.show({
113                                                         _url : baseURL + '/Core/ImportMailMessage.php'
114                                                     }, function(data) {
115                                                         if  (data) {
116                                                             _this.form.findField('bodytext').setValue(data);
117                                                         }
118                                                     });
119                                                 }
120                                             },
121                                             text : "Html File"
122                                         }
123                                     ]
124                                 }
125                             },
126                             {
127                                 xtype: 'Button',
128                                 xns: Roo.Toolbar,
129                                 text : "Use template",
130                                 menu : {
131                                     xtype: 'Menu',
132                                     xns: Roo.menu,
133                                     items : [
134                                         {
135                                             xtype: 'Item',
136                                             xns: Roo.menu,
137                                             listeners : {
138                                                 click : function (_self, e)
139                                                 {
140                                                 
141                                                     var l = document.location;
142                                                     new Pman.Request({
143                                                 
144                                                         url : baseURL + '/Core/ImportMailMessage.php',
145                                                 
146                                                         method: 'POST',
147                                                         mask : "Loading",
148                                                         params : {
149                                                               importUrl : l.protocol +'//' + l.host +   rootURL + '/Pman/Crm/mail_templates/responsive1.html',
150                                                        },
151                                                         success : function (res) {
152                                                 
153                                                          _this.form.findField('bodytext').setValue(res.data);
154                                                         }
155                                                   
156                                                     });
157                                                 }
158                                             },
159                                             text : "Responsive Email (1)"
160                                         }
161                                     ]
162                                 }
163                             },
164                             {
165                                 xtype: 'ComboBox',
166                                 xns: Roo.form,
167                                 listeners : {
168                                     select : function (combo, record, index)
169                                     {
170                                        
171                                     /*
172                                         (function() { 
173                                             combo.setValue('');
174                                         }).defer(100);
175                                     */    
176                                         if(!record){
177                                             return;
178                                         }
179                                         _this.form.findField('bodytext').setValue(record.data.content);
180                                     
181                                     }
182                                 },
183                                 allowBlank : true,
184                                 alwaysQuery : true,
185                                 displayField : 'file',
186                                 editable : false,
187                                 emptyText : "Select Template",
188                                 fieldLabel : 'Template',
189                                 forceSelection : true,
190                                 hiddenName : 'template',
191                                 listWidth : 400,
192                                 loadingText : "Searching...",
193                                 minChars : 2,
194                                 name : 'template',
195                                 pageSize : 20,
196                                 qtip : "Select Template",
197                                 selectOnFocus : true,
198                                 tpl : '<div class="x-grid-cell-text x-btn button"><b>{file}</b> </div>',
199                                 triggerAction : 'all',
200                                 typeAhead : true,
201                                 valueField : 'file',
202                                 width : 200,
203                                 store : {
204                                     xtype: 'Store',
205                                     xns: Roo.data,
206                                     listeners : {
207                                         beforeload : function (_self, o){
208                                             o.params = o.params || {};
209                                             // set more here
210                                            
211                                         }
212                                     },
213                                     remoteSort : true,
214                                     sortInfo : { direction : 'DESC', field: 'file' },
215                                     proxy : {
216                                         xtype: 'HttpProxy',
217                                         xns: Roo.data,
218                                         method : 'GET',
219                                         url : baseURL + '/Core/MailTemplateList.php'
220                                     },
221                                     reader : {
222                                         xtype: 'JsonReader',
223                                         xns: Roo.data,
224                                         id : 'name',
225                                         root : 'data',
226                                         totalProperty : 'total',
227                                         fields : [{"name":"file","type":"string"},{"name":"content","type":"string"}]
228                                     }
229                                 }
230                             },
231                             {
232                                 xtype: 'Fill',
233                                 xns: Roo.Toolbar
234                             },
235                             {
236                                 xtype: 'Button',
237                                 xns: Roo.Toolbar,
238                                 listeners : {
239                                     click : function (_self, e)
240                                     {
241                                         var el = _this.dialog.layout.getRegion('east');
242                                         if (el.visible) {
243                                             el.hide();
244                                         } else {
245                                             el.show();
246                                             el.showPanel(0);
247                                         }
248                                         
249                                     }
250                                 },
251                                 text : "Images / Attachments >>"
252                             }
253                         ]
254                     },
255                     layout : {
256                         xtype: 'BorderLayout',
257                         xns: Roo,
258                         items : [
259                             {
260                                 xtype: 'ContentPanel',
261                                 xns: Roo,
262                                 listeners : {
263                                     render : function (_self, width, height)
264                                     {
265                                         
266                                           Roo.log("RESIZE, " + width + ',' + height);
267                                         
268                                         var ew = Math.max(250, width-50);
269                                         var eh = Math.max(250,height-50) ;
270                                         
271                                        
272                                     
273                                     },
274                                     resize : function (_self, width, height)\r
275                                     {\r
276                                        var ew = Math.max(250, width-50);\r
277                                         var eh = Math.max(250,height-50) ;
278                                         
279                                         if (!_this.form) {\r
280                                             return;\r
281                                         }\r
282                                         var bdtext = _this.form.findField('bodytext');\r
283                                         var ptext = _this.form.findField('plaintext');\r
284                                         if(bdtext.resizeEl){\r
285                                             bdtext.width = ew-50;\r
286                                             bdtext.resizeEl.resizeTo.defer(110, bdtext.resizeEl,[ bdtext.width, bdtext.height  ] );\r
287                                             ptext.setSize(bdtext.width , bdtext.height);\r
288                                         }\r\r
289                                     \r
290                                     }
291                                 },
292                                 autoScroll : false,
293                                 background : false,
294                                 fitContainer : true,
295                                 fitToFrame : true,
296                                 region : 'center',
297                                 title : "Message",
298                                 items : [
299                                     {
300                                         xtype: 'Form',
301                                         xns: Roo.form,
302                                         listeners : {
303                                             actioncomplete : function(_self,action)
304                                             {
305                                                
306                                                 if (action.type == 'setdata') {
307                                                 
308                                                     _this.data.module = _this.data.module || 'crm_mailing_list_message';
309                                                     
310                                                     _this.form.url = baseURL + '/Roo/' + _this.data.module;
311                                                     
312                                                     _this.html_preview.hide();
313                                                     _this.preview_btn.hide();
314                                                         
315                                                     if(_this.data.id*1 > 0){
316                                                         _this.dialog.el.mask("Loading");
317                                                         this.load({ method: 'GET', params: { '_id' : _this.data.id }});
318                                                         _this.html_preview.show();
319                                                         _this.preview_btn.show();
320                                                         
321                                                     } else {
322                                                         _this.form.setValues({
323                                                             'from_name' : Pman.Login.authUser.name,
324                                                             'from_email' : Pman.Login.authUser.email
325                                                         });
326                                                     }
327                                                    return;
328                                                 }
329                                                 if (action.type == 'load') {
330                                                     _this.dialog.el.unmask();
331                                                     return;
332                                                 }
333                                                 if (action.type =='submit') {
334                                                 
335                                                     _this.dialog.el.unmask();
336                                                     _this.dialog.hide();
337                                                 
338                                                      if (_this.callback) {
339                                                         _this.callback.call(_this, action.result.data);
340                                                      }
341                                                      _this.form.reset();
342                                                      return;
343                                                 }
344                                             },
345                                             rendered : function (form)
346                                             {
347                                                 _this.form= form;
348                                             }
349                                         },
350                                         labelAlign : 'right',
351                                         labelWidth : 120,
352                                         method : 'POST',
353                                         style : 'margin:10px',
354                                         preValidate : function(done_callback) {
355                                             
356                                             Roo.MessageBox.progress("Uploading Images", "Uploading");
357                                             var html = _this.form.findField('bodytext').getValue();
358                                             
359                                             var s = Roo.get(_this.form.findField('bodytext').editorcore.doc.documentElement);
360                                             
361                                             var ontable = (_this.data.module) ? _this.data.module : 'crm_mailing_list_message';
362                                             
363                                             var nodes = [];
364                                             s.select('img[src]').each(function(i) {
365                                                 nodes.push(i.dom);
366                                             });
367                                             var total = nodes.length;
368                                             var mkimg = function() {
369                                             
370                                                 if (!nodes.length) {
371                                                       Roo.MessageBox.hide();
372                                                       _this.form.findField('bodytext').syncValue();
373                                                       done_callback(true);
374                                                    //    _this.form.doAction("submit");
375                                                       return;
376                                                 }
377                                                 var i = nodes.pop(); 
378                                                 
379                                                 var n = i.getAttribute('src').match(/(baseURL|server_baseurl)/);
380                                                 
381                                                 if(n){
382                                                     mkimg();
383                                                     return;
384                                                 }
385                                                 
386                                                 n = i.getAttribute('src').match(/^http(.*)/)
387                                                
388                                                 if(!n ){
389                                                     mkimg();
390                                                     return;
391                                                 }
392                                                 
393                                                 new Pman.Request({
394                                                     url : baseURL + '/Roo/Images.php',
395                                                     method : 'POST',
396                                                     params : {
397                                                         onid : _this.form.findField('id').getValue(),
398                                                         ontable : ontable ,
399                                                         _remote_upload : i.src
400                                                     },
401                                                     success : function(res){
402                                                         if(res.success == true){      
403                                                             i.setAttribute('src', res.data);
404                                                             Roo.MessageBox.updateProgress( (total - nodes.length) / total , "Done " + (total - nodes.length) + '/' + total);
405                                                         }
406                                                         mkimg();
407                                                     }
408                                                 });
409                                                
410                                             }
411                                             mkimg();
412                                         },
413                                         url : baseURL + '/Roo/crm_mailing_list_message.php',
414                                         items : [
415                                             {
416                                                 xtype: 'Row',
417                                                 xns: Roo.form,
418                                                 items : [
419                                                     {
420                                                         xtype: 'TextField',
421                                                         xns: Roo.form,
422                                                         allowBlank : false,
423                                                         fieldLabel : 'Mailout Name',
424                                                         name : 'name',
425                                                         width : 400
426                                                     }
427                                                 ]
428                                             },
429                                             {
430                                                 xtype: 'Row',
431                                                 xns: Roo.form,
432                                                 items : [
433                                                     {
434                                                         xtype: 'TextField',
435                                                         xns: Roo.form,
436                                                         allowBlank : false,
437                                                         fieldLabel : 'From',
438                                                         name : 'from_name',
439                                                         width : 300
440                                                     },
441                                                     {
442                                                         xtype: 'TextField',
443                                                         xns: Roo.form,
444                                                         allowBlank : false,
445                                                         fieldLabel : 'Email address',
446                                                         name : 'from_email',
447                                                         width : 300
448                                                     }
449                                                 ]
450                                             },
451                                             {
452                                                 xtype: 'TextField',
453                                                 xns: Roo.form,
454                                                 allowBlank : false,
455                                                 fieldLabel : 'Subject',
456                                                 name : 'subject',
457                                                 width : 600
458                                             },
459                                             {
460                                                 xtype: 'Row',
461                                                 xns: Roo.form,
462                                                 hideLabels : true,
463                                                 items : [
464                                                     {
465                                                         xtype: 'FieldSet',
466                                                         xns: Roo.form,
467                                                         hideLabels : true,
468                                                         legend : "Html Editor",
469                                                         style : 'text-align:center;',
470                                                         items : [
471                                                             {
472                                                                 xtype: 'HtmlEditor',
473                                                                 xns: Roo.form,
474                                                                 clearUp : false,
475                                                                 height : 250,
476                                                                 name : 'bodytext',
477                                                                 resizable : 's',
478                                                                 cwhite : [ 
479                                                                     'margin',
480                                                                     'padding',
481                                                                     'text-align',
482                                                                     'background',
483                                                                     'height',
484                                                                     'width',
485                                                                     'background-color',
486                                                                     'font-size',
487                                                                     'line-height',
488                                                                     'color',
489                                                                     'outline',
490                                                                     'text-decoration',
491                                                                     'position',
492                                                                     'clear',
493                                                                     'overflow',
494                                                                     'margin-top',
495                                                                     'border-bottom',
496                                                                     'top',
497                                                                     'list-style',
498                                                                     'margin-left',
499                                                                     'border',
500                                                                     'float' ,
501                                                                     'margin-right',
502                                                                     'padding-top',
503                                                                     'min-height',
504                                                                     'left',
505                                                                     'padding-left',
506                                                                     'font-weight',
507                                                                     'font-family',
508                                                                     'display',
509                                                                     'margin-bottom',
510                                                                     'padding-bottom',
511                                                                     'vertical-align',
512                                                                     'cursor',
513                                                                     'z-index',
514                                                                     'right',
515                                                                  ],
516                                                                 toolbars : [
517                                                                     {
518                                                                         xtype: 'ToolbarContext',
519                                                                         xns: Roo.form.HtmlEditor
520                                                                     },
521                                                                     {
522                                                                         xtype: 'ToolbarStandard',
523                                                                         xns: Roo.form.HtmlEditor,
524                                                                         btns : [
525                                                                             {
526                                                                                 xtype: 'ComboBox',
527                                                                                 xns: Roo.form,
528                                                                                 listeners : {
529                                                                                     render : function (_self)
530                                                                                     {
531                                                                                         _this.extendimgselect = _self;
532                                                                                     },
533                                                                                     select : function (combo, record, index)
534                                                                                     {
535                                                                                         Roo.log(record);
536                                                                                         (function() { 
537                                                                                             combo.setValue('');
538                                                                                         }).defer(100);
539                                                                                         var editor = _this.form.findField('bodytext');
540                                                                                         editor.insertAtCursor(
541                                                                                                 String.format('<img src="{0}/Images/{1}/{2}#image-{1}">',
542                                                                                                 baseURL,  record.data.id, record.data.filename
543                                                                                                 )
544                                                                                          );
545                                                                                     
546                                                                                         
547                                                                                      },
548                                                                                     beforequery : function (combo, query, forceAll, cancel, e)
549                                                                                     {
550                                                                                         var id = _this.form.findField('id').getValue() * 1;    
551                                                                                         if (!id) {
552                                                                                             Roo.MessageBox.alert("Error", "Save message first");
553                                                                                             return false;
554                                                                                         }
555                                                                                     }
556                                                                                 },
557                                                                                 alwaysQuery : true,
558                                                                                 displayField : 'name',
559                                                                                 editable : false,
560                                                                                 emptyText : "Add Image",
561                                                                                 fieldLabel : 'Images',
562                                                                                 forceSelection : true,
563                                                                                 listWidth : 400,
564                                                                                 loadingText : "Searching...",
565                                                                                 minChars : 2,
566                                                                                 pageSize : 20,
567                                                                                 qtip : "Select Images",
568                                                                                 selectOnFocus : true,
569                                                                                 tpl : '<div class="x-grid-cell-text x-btn button"><img src="{public_baseURL}/Core/Images/Thumb/150x150/{id}.jpg" height="150" width="150"><b>{filename}</b> </div>',
570                                                                                 triggerAction : 'all',
571                                                                                 typeAhead : true,
572                                                                                 valueField : 'id',
573                                                                                 width : 100,
574                                                                                 store : {
575                                                                                     xtype: 'Store',
576                                                                                     xns: Roo.data,
577                                                                                     listeners : {
578                                                                                         beforeload : function (_self, o){
579                                                                                             o.params = o.params || {};
580                                                                                         
581                                                                                             var id = _this.form.findField('id').getValue() * 1;    
582                                                                                             if (!id) {
583                                                                                                 Roo.MessageBox.alert("Error", "Save email template first");
584                                                                                                 return false;
585                                                                                             }
586                                                                                             o.params.onid = id
587                                                                                             o.params.ontable = (_this.data.module) ? _this.data.module : 'crm_mailing_list_message';
588                                                                                             
589                                                                                            // o.params.imgtype = 'PressRelease';
590                                                                                             //o.params['query[imagesize]'] = '150x150';
591                                                                                             // set more here
592                                                                                         }
593                                                                                     },
594                                                                                     remoteSort : true,
595                                                                                     sortInfo : { direction : 'ASC', field: 'id' },
596                                                                                     proxy : {
597                                                                                         xtype: 'HttpProxy',
598                                                                                         xns: Roo.data,
599                                                                                         method : 'GET',
600                                                                                         url : baseURL + '/Roo/Images.php'
601                                                                                     },
602                                                                                     reader : {
603                                                                                         xtype: 'JsonReader',
604                                                                                         xns: Roo.data,
605                                                                                         id : 'id',
606                                                                                         root : 'data',
607                                                                                         totalProperty : 'total',
608                                                                                         fields : [{"name":"id","type":"int"},{"name":"filename","type":"string"},{"name":"url_thumb","type":"string"}]
609                                                                                     }
610                                                                                 }
611                                                                             },
612                                                                             {
613                                                                                 xtype: 'ComboBox',
614                                                                                 xns: Roo.form,
615                                                                                 listeners : {
616                                                                                     render : function (_self)
617                                                                                     {
618                                                                                         _this.unsubscribeselect = _self;
619                                                                                     },
620                                                                                     select : function (combo, record, index)
621                                                                                     {
622                                                                                         Roo.log(record);
623                                                                                         (function() { 
624                                                                                             combo.setValue('');
625                                                                                         }).defer(100);
626                                                                                         var editor = _this.form.findField('bodytext');
627                                                                                         
628                                                                                         if(record.data.name == 'Unsubscribe'){
629                                                                                             editor.insertAtCursor(
630                                                                                                 String.format('<a href="{0}">{1}</a>',
631                                                                                                     record.data.type,  record.data.name
632                                                                                                 )
633                                                                                             );
634                                                                                             return;     
635                                                                                         }
636                                                                                         
637                                                                                         editor.insertAtCursor(
638                                                                                             String.format('{0}',
639                                                                                                 record.data.type
640                                                                                             )
641                                                                                         );
642                                                                                         
643                                                                                      }
644                                                                                 },
645                                                                                 alwaysQuery : true,
646                                                                                 displayField : 'name',
647                                                                                 editable : false,
648                                                                                 emptyText : "Insert Field",
649                                                                                 fieldLabel : 'Field',
650                                                                                 forceSelection : true,
651                                                                                 listWidth : 400,
652                                                                                 loadingText : "Searching...",
653                                                                                 minChars : 2,
654                                                                                 pageSize : 20,
655                                                                                 qtip : "Insert Field",
656                                                                                 selectOnFocus : true,
657                                                                                 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>',
658                                                                                 triggerAction : 'all',
659                                                                                 typeAhead : true,
660                                                                                 valueField : 'type',
661                                                                                 width : 100,
662                                                                                 store : {
663                                                                                     xtype: 'SimpleStore',
664                                                                                     xns: Roo.data,
665                                                                                     data : [ 
666                                                                                         [ '{person.firstname}', "First Name"],
667                                                                                         [ '{person.lastname}' , "Last Name"],
668                                                                                         [ '{person.name}', "Full Name"],
669                                                                                         [ '#unsubscribe', "Unsubscribe"]
670                                                                                     ],
671                                                                                     fields : [  'type', 'name']
672                                                                                 }
673                                                                             }
674                                                                         ]
675                                                                     }
676                                                                 ]
677                                                             }
678                                                         ]
679                                                     }
680                                                 ]
681                                             },
682                                             {
683                                                 xtype: 'Row',
684                                                 xns: Roo.form,
685                                                 hideLabels : true,
686                                                 items : [
687                                                     {
688                                                         xtype: 'Button',
689                                                         xns: Roo,
690                                                         listeners : {
691                                                             click : function (_self, e)
692                                                             {
693                                                                 var h = _this.form.findField('bodytext').getValue();
694                                                                 var p = _this.form.findField('plaintext');
695                                                                 
696                                                                 new Pman.Request({\r
697                                                                     url : baseURL + '/Core/ImportMailMessage.php',\r
698                                                                     method : 'POST',\r
699                                                                     params : {
700                                                                       bodytext : h,\r
701                                                                       _convertToPlain : true,
702                                                                       _check_unsubscribe : true\r
703                                                                     }, \r
704                                                                     success : function(res) {\r
705                                                                         if(res.success == true){
706                                                                            p.setValue(res.data);
707                                                                         }\r
708                                                                     }\r
709                                                                 });  
710                                                                 
711                                                             }
712                                                         },
713                                                         text : "Convert Html to Text"
714                                                     }
715                                                 ]
716                                             },
717                                             {
718                                                 xtype: 'Row',
719                                                 xns: Roo.form,
720                                                 hideLabels : true,
721                                                 items : [
722                                                     {
723                                                         xtype: 'FieldSet',
724                                                         xns: Roo.form,
725                                                         hideLabels : true,
726                                                         legend : "Plain Text",
727                                                         style : 'text-align:center;',
728                                                         items : [
729                                                             {
730                                                                 xtype: 'TextArea',
731                                                                 xns: Roo.form,
732                                                                 height : 50,
733                                                                 name : 'plaintext'
734                                                             }
735                                                         ]
736                                                     }
737                                                 ]
738                                             },
739                                             {
740                                                 xtype: 'Hidden',
741                                                 xns: Roo.form,
742                                                 name : 'id'
743                                             }
744                                         ]
745                                     }
746                                 ]
747                             }
748                         ],
749                         center : {
750                             xtype: 'LayoutRegion',
751                             xns: Roo,
752                             autoScroll : true
753                         }
754                     }
755                 },
756                 {
757                     xtype: 'GridPanel',
758                     xns: Roo,
759                     listeners : {
760                         activate : function() {
761                             _this.ipanel = this;
762                             if (_this.igrid) {
763                                _this.igrid.ds.load({});
764                             }
765                         }
766                     },
767                     autoScroll : false,
768                     background : false,
769                     fitContainer : true,
770                     fitToframe : true,
771                     region : 'east',
772                     tableName : 'Images',
773                     title : "Images / Attachments",
774                     grid : {
775                         xtype: 'Grid',
776                         xns: Roo.grid,
777                         listeners : {
778                             render : function() 
779                             {
780                                 _this.igrid = this; 
781                                 //_this.dialog = Pman.Dialog.FILL_IN
782                                 if (_this.ipanel.active) {
783                                //    _this.igrid.ds.load({});
784                                 }
785                             }
786                         },
787                         autoExpandColumn : 'filename',
788                         loadMask : true,
789                         dataSource : {
790                             xtype: 'Store',
791                             xns: Roo.data,
792                             listeners : {
793                                 beforeload : function (_self, options)
794                                 {
795                                     options.params = options.params || {};
796                                     if (typeof(_this.data) == 'undefined') {
797                                         return false;
798                                     }
799                                     if(_this.data.id * 1 >= 0)
800                                     {
801                                         options.params.onid = _this.data.id;
802                                 
803                                         options.params.ontable = (_this.data.module) ? _this.data.module : 'crm_mailing_list_message';
804                                     }
805                                 }
806                             },
807                             remoteSort : true,
808                             sortInfo : { field : 'filename', direction: 'ASC' },
809                             proxy : {
810                                 xtype: 'HttpProxy',
811                                 xns: Roo.data,
812                                 method : 'GET',
813                                 url : baseURL + '/Roo/Images.php'
814                             },
815                             reader : {
816                                 xtype: 'JsonReader',
817                                 xns: Roo.data,
818                                 id : 'id',
819                                 root : 'data',
820                                 totalProperty : 'total',
821                                 fields : [
822                                     {
823                                         'name': 'id',
824                                         'type': 'int'
825                                     },
826                                     {
827                                         'name': 'filename',
828                                         'type': 'string'
829                                     },
830                                     {
831                                         'name': 'ontable',
832                                         'type': 'string'
833                                     },
834                                     {
835                                         'name': 'onid',
836                                         'type': 'int'
837                                     },
838                                     {
839                                         'name': 'mimetype',
840                                         'type': 'string'
841                                     },
842                                     {
843                                         'name': 'width',
844                                         'type': 'int'
845                                     },
846                                     {
847                                         'name': 'height',
848                                         'type': 'int'
849                                     },
850                                     {
851                                         'name': 'filesize',
852                                         'type': 'int'
853                                     },
854                                     {
855                                         'name': 'displayorder',
856                                         'type': 'int'
857                                     },
858                                     {
859                                         'name': 'language',
860                                         'type': 'string'
861                                     },
862                                     {
863                                         'name': 'parent_image_id',
864                                         'type': 'int'
865                                     },
866                                     {
867                                         'name': 'created',
868                                         'type': 'date',
869                                         'dateFormat': 'Y-m-d'
870                                     },
871                                     {
872                                         'name': 'imgtype',
873                                         'type': 'string'
874                                     },
875                                     {
876                                         'name': 'linkurl',
877                                         'type': 'string'
878                                     },
879                                     {
880                                         'name': 'descript',
881                                         'type': 'string'
882                                     },
883                                     {
884                                         'name': 'title',
885                                         'type': 'string'
886                                     },
887                                     {
888                                         'name': 'parent_image_id_id',
889                                         'type': 'int'
890                                     },
891                                     {
892                                         'name': 'parent_image_id_filename',
893                                         'type': 'string'
894                                     },
895                                     {
896                                         'name': 'parent_image_id_ontable',
897                                         'type': 'string'
898                                     },
899                                     {
900                                         'name': 'parent_image_id_onid',
901                                         'type': 'int'
902                                     },
903                                     {
904                                         'name': 'parent_image_id_mimetype',
905                                         'type': 'string'
906                                     },
907                                     {
908                                         'name': 'parent_image_id_width',
909                                         'type': 'int'
910                                     },
911                                     {
912                                         'name': 'parent_image_id_height',
913                                         'type': 'int'
914                                     },
915                                     {
916                                         'name': 'parent_image_id_filesize',
917                                         'type': 'int'
918                                     },
919                                     {
920                                         'name': 'parent_image_id_displayorder',
921                                         'type': 'int'
922                                     },
923                                     {
924                                         'name': 'parent_image_id_language',
925                                         'type': 'string'
926                                     },
927                                     {
928                                         'name': 'parent_image_id_parent_image_id',
929                                         'type': 'int'
930                                     },
931                                     {
932                                         'name': 'parent_image_id_created',
933                                         'type': 'date'
934                                     },
935                                     {
936                                         'name': 'parent_image_id_imgtype',
937                                         'type': 'string'
938                                     },
939                                     {
940                                         'name': 'parent_image_id_linkurl',
941                                         'type': 'string'
942                                     },
943                                     {
944                                         'name': 'parent_image_id_descript',
945                                         'type': 'string'
946                                     },
947                                     {
948                                         'name': 'parent_image_id_title',
949                                         'type': 'string'
950                                     }
951                                 ]
952                             }
953                         },
954                         toolbar : {
955                             xtype: 'Toolbar',
956                             xns: Roo,
957                             items : [
958                                 {
959                                     xtype: 'Button',
960                                     xns: Roo.Toolbar,
961                                     listeners : {
962                                         click : function()
963                                         {
964                                             var id = _this.form.findField('id').getValue();
965                                             
966                                             if(id*1 < 1){
967                                                 Roo.MessageBox.alert('Error', 'Please save the email template first');
968                                                 return;
969                                             }
970                                             
971                                             var ontable = (_this.data.module) ? _this.data.module : 'crm_mailing_list_message';
972                                             
973                                             Pman.Dialog.Image.show( { id : 0, onid: id, ontable: ontable }, function() {
974                                                 _this.igrid.getDataSource().load({});
975                                             }); 
976                                         }
977                                     },
978                                     cls : 'x-btn-text-icon',
979                                     text : "Add",
980                                     icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
981                                 },
982                                 {
983                                     xtype: 'Button',
984                                     xns: Roo.Toolbar,
985                                     listeners : {
986                                         click : function()
987                                         {
988                                             var s = _this.igrid.getSelectionModel().getSelected();
989                                             if (!s || isNaN(s.id *1)) {
990                                                 Roo.MessageBox.alert("Error", "Select a image"); 
991                                                 return;
992                                             }
993                                             Roo.MessageBox.confirm("Confirm", "Are sure you want to delete this image?", function (v){
994                                                 if (v != 'yes') {
995                                                     return;
996                                                 }
997                                                 
998                                                 new Pman.Request({
999                                                     url : baseURL + '/Roo/Images.php',
1000                                                     method: 'POST',
1001                                                     params : {
1002                                                         _delete : s.id
1003                                                     },
1004                                                     success : function()
1005                                                     {
1006                                                         Roo.log('Got Success!!');
1007                                                        _this.igrid.ds.load({});
1008                                                     }
1009                                                 });
1010                                             });
1011                                         }
1012                                     },
1013                                     cls : 'x-btn-text-icon',
1014                                     text : "Delete",
1015                                     icon : rootURL + '/Pman/templates/images/trash.gif'
1016                                 }
1017                             ]
1018                         },
1019                         colModel : [
1020                             {
1021                                 xtype: 'ColumnModel',
1022                                 xns: Roo.grid,
1023                                 dataIndex : 'filename',
1024                                 header : 'Filename',
1025                                 width : 300,
1026                                 renderer : function(v,x,r)
1027                                 {
1028                                    return '<img src="' + baseURL + '/Images/' + r.data.id + '/' + r.data.filename + '" width="' + r.data.width + '" height="' + r.data.height + '" />';
1029                                 }
1030                             },
1031                             {
1032                                 xtype: 'ColumnModel',
1033                                 xns: Roo.grid,
1034                                 header : 'Displayorder',
1035                                 width : 75,
1036                                 dataIndex : 'displayorder',
1037                                 renderer : function(v) { return String.format('{0}', v); }
1038                             },
1039                             {
1040                                 xtype: 'ColumnModel',
1041                                 xns: Roo.grid,
1042                                 dataIndex : 'title',
1043                                 header : 'Title',
1044                                 width : 75,
1045                                 renderer : function(v) { return String.format('{0}', v); }
1046                             }
1047                         ]
1048                     }
1049                 }
1050             ],
1051             center : {
1052                 xtype: 'LayoutRegion',
1053                 xns: Roo,
1054                 tabPosition : 'top'
1055             },
1056             east : {
1057                 xtype: 'LayoutRegion',
1058                 xns: Roo,
1059                 hidden : true,
1060                 split : true,
1061                 title : "Images / Attachments",
1062                 titlebar : true,
1063                 width : 500
1064             },
1065             buttons : [
1066                 {
1067                     xtype: 'Button',
1068                     xns: Roo,
1069                     listeners : {
1070                         click : function (_self, e)
1071                         {
1072                             //_this.dialog.hide();
1073                             Roo.log(_this.data.module);
1074                             Pman.Dialog.CoreEmailPreview.show({ id : _this.form.findField('id').getValue(), module : _this.data.module });
1075                         },
1076                         render : function (_self)
1077                         {
1078                             _this.preview_btn = _self;
1079                         }
1080                     },
1081                     text : "Preview"
1082                 },
1083                 {
1084                     xtype: 'Button',
1085                     xns: Roo,
1086                     listeners : {
1087                         click : function (_self, e)
1088                         {
1089                             //_this.dialog.hide();
1090                             
1091                             var id = _this.form.findField('id').getValue();
1092                             
1093                             if(id*1 < 1){
1094                                 Roo.MessageBox.alert('Error', 'Please save the message frist!');
1095                                 return;
1096                             }
1097                            
1098                             new Pman.Request({
1099                                 url : baseURL + '/Core/MessagePreview',
1100                                 method : 'POST',
1101                                 mask: 'Sending',
1102                                 params : {
1103                                     _id : id,
1104                                     _table : _this.data.module
1105                                 }, 
1106                                 success : function(res) { 
1107                                     if(res.data == 'SUCCESS'){
1108                                         Roo.MessageBox.alert("Email Sent", 'The report was sent to your email (HTML format).');
1109                                     }
1110                                 }
1111                             });
1112                         },
1113                         render : function (_self)
1114                         {
1115                             _this.html_preview = _self;
1116                         }
1117                     },
1118                     text : "Send me a test copy"
1119                 },
1120                 {
1121                     xtype: 'Button',
1122                     xns: Roo,
1123                     listeners : {
1124                         click : function (_self, e)
1125                         {
1126                             _this.dialog.hide();
1127                         }
1128                     },
1129                     text : "Cancel"
1130                 },
1131                 {
1132                     xtype: 'Button',
1133                     xns: Roo,
1134                     listeners : {
1135                         click : function (_self, e)
1136                         {
1137                         
1138                             // do some checks?
1139                             _this.form.preValidate(function(res) {
1140                                 if (!res) {
1141                                     return; //failed.
1142                                 }
1143                                  _this.form.doAction("submit");
1144                             });
1145                         
1146                         }
1147                     },
1148                     text : "Save"
1149                 }
1150             ]
1151         });
1152     }
1153 };