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/);
380                                                 
381                                                 if(n){
382                                                     mkimg();
383                                                     return;
384                                                 }
385                                                        
386                                                 var n = i.getAttribute('src').match(/^http(.*)/)
387                                                 if(!n ){
388                                                     mkimg();
389                                                     return;
390                                                 }
391                                                 
392                                                 new Pman.Request({
393                                                     url : baseURL + '/Roo/Images.php',
394                                                     method : 'POST',
395                                                     params : {
396                                                         onid : _this.form.findField('id').getValue(),
397                                                         ontable : ontable ,
398                                                         _remote_upload : i.src
399                                                     },
400                                                     success : function(res){
401                                                         if(res.success == true){      
402                                                             i.setAttribute('src', res.data);
403                                                             Roo.MessageBox.updateProgress( (total - nodes.length) / total , "Done " + (total - nodes.length) + '/' + total);
404                                                         }
405                                                         mkimg();
406                                                     }
407                                                 });
408                                                
409                                             }
410                                             mkimg();
411                                         },
412                                         url : baseURL + '/Roo/crm_mailing_list_message.php',
413                                         items : [
414                                             {
415                                                 xtype: 'Row',
416                                                 xns: Roo.form,
417                                                 items : [
418                                                     {
419                                                         xtype: 'TextField',
420                                                         xns: Roo.form,
421                                                         allowBlank : false,
422                                                         fieldLabel : 'Mailout Name',
423                                                         name : 'name',
424                                                         width : 400
425                                                     }
426                                                 ]
427                                             },
428                                             {
429                                                 xtype: 'Row',
430                                                 xns: Roo.form,
431                                                 items : [
432                                                     {
433                                                         xtype: 'TextField',
434                                                         xns: Roo.form,
435                                                         allowBlank : false,
436                                                         fieldLabel : 'From',
437                                                         name : 'from_name',
438                                                         width : 300
439                                                     },
440                                                     {
441                                                         xtype: 'TextField',
442                                                         xns: Roo.form,
443                                                         allowBlank : false,
444                                                         fieldLabel : 'Email address',
445                                                         name : 'from_email',
446                                                         width : 300
447                                                     }
448                                                 ]
449                                             },
450                                             {
451                                                 xtype: 'TextField',
452                                                 xns: Roo.form,
453                                                 allowBlank : false,
454                                                 fieldLabel : 'Subject',
455                                                 name : 'subject',
456                                                 width : 600
457                                             },
458                                             {
459                                                 xtype: 'Row',
460                                                 xns: Roo.form,
461                                                 hideLabels : true,
462                                                 items : [
463                                                     {
464                                                         xtype: 'FieldSet',
465                                                         xns: Roo.form,
466                                                         hideLabels : true,
467                                                         legend : "Html Editor",
468                                                         style : 'text-align:center;',
469                                                         items : [
470                                                             {
471                                                                 xtype: 'HtmlEditor',
472                                                                 xns: Roo.form,
473                                                                 clearUp : false,
474                                                                 height : 250,
475                                                                 name : 'bodytext',
476                                                                 resizable : 's',
477                                                                 cwhite : [ 
478                                                                     'margin',
479                                                                     'padding',
480                                                                     'text-align',
481                                                                     'background',
482                                                                     'height',
483                                                                     'width',
484                                                                     'background-color',
485                                                                     'font-size',
486                                                                     'line-height',
487                                                                     'color',
488                                                                     'outline',
489                                                                     'text-decoration',
490                                                                     'position',
491                                                                     'clear',
492                                                                     'overflow',
493                                                                     'margin-top',
494                                                                     'border-bottom',
495                                                                     'top',
496                                                                     'list-style',
497                                                                     'margin-left',
498                                                                     'border',
499                                                                     'float' ,
500                                                                     'margin-right',
501                                                                     'padding-top',
502                                                                     'min-height',
503                                                                     'left',
504                                                                     'padding-left',
505                                                                     'font-weight',
506                                                                     'font-family',
507                                                                     'display',
508                                                                     'margin-bottom',
509                                                                     'padding-bottom',
510                                                                     'vertical-align',
511                                                                     'cursor',
512                                                                     'z-index',
513                                                                     'right',
514                                                                  ],
515                                                                 toolbars : [
516                                                                     {
517                                                                         xtype: 'ToolbarContext',
518                                                                         xns: Roo.form.HtmlEditor
519                                                                     },
520                                                                     {
521                                                                         xtype: 'ToolbarStandard',
522                                                                         xns: Roo.form.HtmlEditor,
523                                                                         btns : [
524                                                                             {
525                                                                                 xtype: 'ComboBox',
526                                                                                 xns: Roo.form,
527                                                                                 listeners : {
528                                                                                     render : function (_self)
529                                                                                     {
530                                                                                         _this.extendimgselect = _self;
531                                                                                     },
532                                                                                     select : function (combo, record, index)
533                                                                                     {
534                                                                                         Roo.log(record);
535                                                                                         (function() { 
536                                                                                             combo.setValue('');
537                                                                                         }).defer(100);
538                                                                                         var editor = _this.form.findField('bodytext');
539                                                                                         editor.insertAtCursor(
540                                                                                                 String.format('<img src="{0}/Images/{1}/{2}#image-{1}">',
541                                                                                                 baseURL,  record.data.id, record.data.filename
542                                                                                                 )
543                                                                                          );
544                                                                                     
545                                                                                         
546                                                                                      },
547                                                                                     beforequery : function (combo, query, forceAll, cancel, e)
548                                                                                     {
549                                                                                         var id = _this.form.findField('id').getValue() * 1;    
550                                                                                         if (!id) {
551                                                                                             Roo.MessageBox.alert("Error", "Save message first");
552                                                                                             return false;
553                                                                                         }
554                                                                                     }
555                                                                                 },
556                                                                                 alwaysQuery : true,
557                                                                                 displayField : 'name',
558                                                                                 editable : false,
559                                                                                 emptyText : "Add Image",
560                                                                                 fieldLabel : 'Images',
561                                                                                 forceSelection : true,
562                                                                                 listWidth : 400,
563                                                                                 loadingText : "Searching...",
564                                                                                 minChars : 2,
565                                                                                 pageSize : 20,
566                                                                                 qtip : "Select Images",
567                                                                                 selectOnFocus : true,
568                                                                                 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>',
569                                                                                 triggerAction : 'all',
570                                                                                 typeAhead : true,
571                                                                                 valueField : 'id',
572                                                                                 width : 100,
573                                                                                 store : {
574                                                                                     xtype: 'Store',
575                                                                                     xns: Roo.data,
576                                                                                     listeners : {
577                                                                                         beforeload : function (_self, o){
578                                                                                             o.params = o.params || {};
579                                                                                         
580                                                                                             var id = _this.form.findField('id').getValue() * 1;    
581                                                                                             if (!id) {
582                                                                                                 Roo.MessageBox.alert("Error", "Save email template first");
583                                                                                                 return false;
584                                                                                             }
585                                                                                             o.params.onid = id
586                                                                                             o.params.ontable = (_this.data.module) ? _this.data.module : 'crm_mailing_list_message';
587                                                                                             
588                                                                                            // o.params.imgtype = 'PressRelease';
589                                                                                             //o.params['query[imagesize]'] = '150x150';
590                                                                                             // set more here
591                                                                                         }
592                                                                                     },
593                                                                                     remoteSort : true,
594                                                                                     sortInfo : { direction : 'ASC', field: 'id' },
595                                                                                     proxy : {
596                                                                                         xtype: 'HttpProxy',
597                                                                                         xns: Roo.data,
598                                                                                         method : 'GET',
599                                                                                         url : baseURL + '/Roo/Images.php'
600                                                                                     },
601                                                                                     reader : {
602                                                                                         xtype: 'JsonReader',
603                                                                                         xns: Roo.data,
604                                                                                         id : 'id',
605                                                                                         root : 'data',
606                                                                                         totalProperty : 'total',
607                                                                                         fields : [{"name":"id","type":"int"},{"name":"filename","type":"string"},{"name":"url_thumb","type":"string"}]
608                                                                                     }
609                                                                                 }
610                                                                             },
611                                                                             {
612                                                                                 xtype: 'ComboBox',
613                                                                                 xns: Roo.form,
614                                                                                 listeners : {
615                                                                                     render : function (_self)
616                                                                                     {
617                                                                                         _this.unsubscribeselect = _self;
618                                                                                     },
619                                                                                     select : function (combo, record, index)
620                                                                                     {
621                                                                                         Roo.log(record);
622                                                                                         (function() { 
623                                                                                             combo.setValue('');
624                                                                                         }).defer(100);
625                                                                                         var editor = _this.form.findField('bodytext');
626                                                                                         
627                                                                                         if(record.data.name == 'Unsubscribe'){
628                                                                                             editor.insertAtCursor(
629                                                                                                 String.format('<a href="{0}">{1}</a>',
630                                                                                                     record.data.type,  record.data.name
631                                                                                                 )
632                                                                                             );
633                                                                                             return;     
634                                                                                         }
635                                                                                         
636                                                                                         editor.insertAtCursor(
637                                                                                             String.format('{0}',
638                                                                                                 record.data.type
639                                                                                             )
640                                                                                         );
641                                                                                         
642                                                                                      }
643                                                                                 },
644                                                                                 alwaysQuery : true,
645                                                                                 displayField : 'name',
646                                                                                 editable : false,
647                                                                                 emptyText : "Insert Field",
648                                                                                 fieldLabel : 'Field',
649                                                                                 forceSelection : true,
650                                                                                 listWidth : 400,
651                                                                                 loadingText : "Searching...",
652                                                                                 minChars : 2,
653                                                                                 pageSize : 20,
654                                                                                 qtip : "Insert Field",
655                                                                                 selectOnFocus : true,
656                                                                                 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>',
657                                                                                 triggerAction : 'all',
658                                                                                 typeAhead : true,
659                                                                                 valueField : 'type',
660                                                                                 width : 100,
661                                                                                 store : {
662                                                                                     xtype: 'SimpleStore',
663                                                                                     xns: Roo.data,
664                                                                                     data : [ 
665                                                                                         [ '{person.firstname}', "First Name"],
666                                                                                         [ '{person.lastname}' , "Last Name"],
667                                                                                         [ '{person.name}', "Full Name"],
668                                                                                         [ '#unsubscribe', "Unsubscribe"]
669                                                                                     ],
670                                                                                     fields : [  'type', 'name']
671                                                                                 }
672                                                                             }
673                                                                         ]
674                                                                     }
675                                                                 ]
676                                                             }
677                                                         ]
678                                                     }
679                                                 ]
680                                             },
681                                             {
682                                                 xtype: 'Row',
683                                                 xns: Roo.form,
684                                                 hideLabels : true,
685                                                 items : [
686                                                     {
687                                                         xtype: 'Button',
688                                                         xns: Roo,
689                                                         listeners : {
690                                                             click : function (_self, e)
691                                                             {
692                                                                 var h = _this.form.findField('bodytext').getValue();
693                                                                 var p = _this.form.findField('plaintext');
694                                                                 
695                                                                 new Pman.Request({\r
696                                                                     url : baseURL + '/Core/ImportMailMessage.php',\r
697                                                                     method : 'POST',\r
698                                                                     params : {
699                                                                       bodytext : h,\r
700                                                                       _convertToPlain : true,
701                                                                       _check_unsubscribe : true\r
702                                                                     }, \r
703                                                                     success : function(res) {\r
704                                                                         if(res.success == true){
705                                                                            p.setValue(res.data);
706                                                                         }\r
707                                                                     }\r
708                                                                 });  
709                                                                 
710                                                             }
711                                                         },
712                                                         text : "Convert Html to Text"
713                                                     }
714                                                 ]
715                                             },
716                                             {
717                                                 xtype: 'Row',
718                                                 xns: Roo.form,
719                                                 hideLabels : true,
720                                                 items : [
721                                                     {
722                                                         xtype: 'FieldSet',
723                                                         xns: Roo.form,
724                                                         hideLabels : true,
725                                                         legend : "Plain Text",
726                                                         style : 'text-align:center;',
727                                                         items : [
728                                                             {
729                                                                 xtype: 'TextArea',
730                                                                 xns: Roo.form,
731                                                                 height : 50,
732                                                                 name : 'plaintext'
733                                                             }
734                                                         ]
735                                                     }
736                                                 ]
737                                             },
738                                             {
739                                                 xtype: 'Hidden',
740                                                 xns: Roo.form,
741                                                 name : 'id'
742                                             }
743                                         ]
744                                     }
745                                 ]
746                             }
747                         ],
748                         center : {
749                             xtype: 'LayoutRegion',
750                             xns: Roo,
751                             autoScroll : true
752                         }
753                     }
754                 },
755                 {
756                     xtype: 'GridPanel',
757                     xns: Roo,
758                     listeners : {
759                         activate : function() {
760                             _this.ipanel = this;
761                             if (_this.igrid) {
762                                _this.igrid.ds.load({});
763                             }
764                         }
765                     },
766                     autoScroll : false,
767                     background : false,
768                     fitContainer : true,
769                     fitToframe : true,
770                     region : 'east',
771                     tableName : 'Images',
772                     title : "Images / Attachments",
773                     grid : {
774                         xtype: 'Grid',
775                         xns: Roo.grid,
776                         listeners : {
777                             render : function() 
778                             {
779                                 _this.igrid = this; 
780                                 //_this.dialog = Pman.Dialog.FILL_IN
781                                 if (_this.ipanel.active) {
782                                //    _this.igrid.ds.load({});
783                                 }
784                             }
785                         },
786                         autoExpandColumn : 'filename',
787                         loadMask : true,
788                         dataSource : {
789                             xtype: 'Store',
790                             xns: Roo.data,
791                             listeners : {
792                                 beforeload : function (_self, options)
793                                 {
794                                     options.params = options.params || {};
795                                     if (typeof(_this.data) == 'undefined') {
796                                         return false;
797                                     }
798                                     if(_this.data.id * 1 >= 0)
799                                     {
800                                         options.params.onid = _this.data.id;
801                                 
802                                         options.params.ontable = (_this.data.module) ? _this.data.module : 'crm_mailing_list_message';
803                                     }
804                                 }
805                             },
806                             remoteSort : true,
807                             sortInfo : { field : 'filename', direction: 'ASC' },
808                             proxy : {
809                                 xtype: 'HttpProxy',
810                                 xns: Roo.data,
811                                 method : 'GET',
812                                 url : baseURL + '/Roo/Images.php'
813                             },
814                             reader : {
815                                 xtype: 'JsonReader',
816                                 xns: Roo.data,
817                                 id : 'id',
818                                 root : 'data',
819                                 totalProperty : 'total',
820                                 fields : [
821                                     {
822                                         'name': 'id',
823                                         'type': 'int'
824                                     },
825                                     {
826                                         'name': 'filename',
827                                         'type': 'string'
828                                     },
829                                     {
830                                         'name': 'ontable',
831                                         'type': 'string'
832                                     },
833                                     {
834                                         'name': 'onid',
835                                         'type': 'int'
836                                     },
837                                     {
838                                         'name': 'mimetype',
839                                         'type': 'string'
840                                     },
841                                     {
842                                         'name': 'width',
843                                         'type': 'int'
844                                     },
845                                     {
846                                         'name': 'height',
847                                         'type': 'int'
848                                     },
849                                     {
850                                         'name': 'filesize',
851                                         'type': 'int'
852                                     },
853                                     {
854                                         'name': 'displayorder',
855                                         'type': 'int'
856                                     },
857                                     {
858                                         'name': 'language',
859                                         'type': 'string'
860                                     },
861                                     {
862                                         'name': 'parent_image_id',
863                                         'type': 'int'
864                                     },
865                                     {
866                                         'name': 'created',
867                                         'type': 'date',
868                                         'dateFormat': 'Y-m-d'
869                                     },
870                                     {
871                                         'name': 'imgtype',
872                                         'type': 'string'
873                                     },
874                                     {
875                                         'name': 'linkurl',
876                                         'type': 'string'
877                                     },
878                                     {
879                                         'name': 'descript',
880                                         'type': 'string'
881                                     },
882                                     {
883                                         'name': 'title',
884                                         'type': 'string'
885                                     },
886                                     {
887                                         'name': 'parent_image_id_id',
888                                         'type': 'int'
889                                     },
890                                     {
891                                         'name': 'parent_image_id_filename',
892                                         'type': 'string'
893                                     },
894                                     {
895                                         'name': 'parent_image_id_ontable',
896                                         'type': 'string'
897                                     },
898                                     {
899                                         'name': 'parent_image_id_onid',
900                                         'type': 'int'
901                                     },
902                                     {
903                                         'name': 'parent_image_id_mimetype',
904                                         'type': 'string'
905                                     },
906                                     {
907                                         'name': 'parent_image_id_width',
908                                         'type': 'int'
909                                     },
910                                     {
911                                         'name': 'parent_image_id_height',
912                                         'type': 'int'
913                                     },
914                                     {
915                                         'name': 'parent_image_id_filesize',
916                                         'type': 'int'
917                                     },
918                                     {
919                                         'name': 'parent_image_id_displayorder',
920                                         'type': 'int'
921                                     },
922                                     {
923                                         'name': 'parent_image_id_language',
924                                         'type': 'string'
925                                     },
926                                     {
927                                         'name': 'parent_image_id_parent_image_id',
928                                         'type': 'int'
929                                     },
930                                     {
931                                         'name': 'parent_image_id_created',
932                                         'type': 'date'
933                                     },
934                                     {
935                                         'name': 'parent_image_id_imgtype',
936                                         'type': 'string'
937                                     },
938                                     {
939                                         'name': 'parent_image_id_linkurl',
940                                         'type': 'string'
941                                     },
942                                     {
943                                         'name': 'parent_image_id_descript',
944                                         'type': 'string'
945                                     },
946                                     {
947                                         'name': 'parent_image_id_title',
948                                         'type': 'string'
949                                     }
950                                 ]
951                             }
952                         },
953                         toolbar : {
954                             xtype: 'Toolbar',
955                             xns: Roo,
956                             items : [
957                                 {
958                                     xtype: 'Button',
959                                     xns: Roo.Toolbar,
960                                     listeners : {
961                                         click : function()
962                                         {
963                                             var id = _this.form.findField('id').getValue();
964                                             
965                                             if(id*1 < 1){
966                                                 Roo.MessageBox.alert('Error', 'Please save the email template first');
967                                                 return;
968                                             }
969                                             
970                                             var ontable = (_this.data.module) ? _this.data.module : 'crm_mailing_list_message';
971                                             
972                                             Pman.Dialog.Image.show( { id : 0, onid: id, ontable: ontable }, function() {
973                                                 _this.igrid.getDataSource().load({});
974                                             }); 
975                                         }
976                                     },
977                                     cls : 'x-btn-text-icon',
978                                     text : "Add",
979                                     icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
980                                 },
981                                 {
982                                     xtype: 'Button',
983                                     xns: Roo.Toolbar,
984                                     listeners : {
985                                         click : function()
986                                         {
987                                             var s = _this.igrid.getSelectionModel().getSelected();
988                                             if (!s || isNaN(s.id *1)) {
989                                                 Roo.MessageBox.alert("Error", "Select a image"); 
990                                                 return;
991                                             }
992                                             Roo.MessageBox.confirm("Confirm", "Are sure you want to delete this image?", function (v){
993                                                 if (v != 'yes') {
994                                                     return;
995                                                 }
996                                                 
997                                                 new Pman.Request({
998                                                     url : baseURL + '/Roo/Images.php',
999                                                     method: 'POST',
1000                                                     params : {
1001                                                         _delete : s.id
1002                                                     },
1003                                                     success : function()
1004                                                     {
1005                                                         Roo.log('Got Success!!');
1006                                                        _this.igrid.ds.load({});
1007                                                     }
1008                                                 });
1009                                             });
1010                                         }
1011                                     },
1012                                     cls : 'x-btn-text-icon',
1013                                     text : "Delete",
1014                                     icon : rootURL + '/Pman/templates/images/trash.gif'
1015                                 }
1016                             ]
1017                         },
1018                         colModel : [
1019                             {
1020                                 xtype: 'ColumnModel',
1021                                 xns: Roo.grid,
1022                                 dataIndex : 'filename',
1023                                 header : 'Filename',
1024                                 width : 300,
1025                                 renderer : function(v,x,r)
1026                                 {
1027                                    return '<img src="' + baseURL + '/Images/' + r.data.id + '/' + r.data.filename + '" width="' + r.data.width + '" height="' + r.data.height + '" />';
1028                                 }
1029                             },
1030                             {
1031                                 xtype: 'ColumnModel',
1032                                 xns: Roo.grid,
1033                                 header : 'Displayorder',
1034                                 width : 75,
1035                                 dataIndex : 'displayorder',
1036                                 renderer : function(v) { return String.format('{0}', v); }
1037                             },
1038                             {
1039                                 xtype: 'ColumnModel',
1040                                 xns: Roo.grid,
1041                                 dataIndex : 'title',
1042                                 header : 'Title',
1043                                 width : 75,
1044                                 renderer : function(v) { return String.format('{0}', v); }
1045                             }
1046                         ]
1047                     }
1048                 }
1049             ],
1050             center : {
1051                 xtype: 'LayoutRegion',
1052                 xns: Roo,
1053                 tabPosition : 'top'
1054             },
1055             east : {
1056                 xtype: 'LayoutRegion',
1057                 xns: Roo,
1058                 hidden : true,
1059                 split : true,
1060                 title : "Images / Attachments",
1061                 titlebar : true,
1062                 width : 500
1063             },
1064             buttons : [
1065                 {
1066                     xtype: 'Button',
1067                     xns: Roo,
1068                     listeners : {
1069                         click : function (_self, e)
1070                         {
1071                             //_this.dialog.hide();
1072                             Roo.log(_this.data.module);
1073                             Pman.Dialog.CoreEmailPreview.show({ id : _this.form.findField('id').getValue(), module : _this.data.module });
1074                         },
1075                         render : function (_self)
1076                         {
1077                             _this.preview_btn = _self;
1078                         }
1079                     },
1080                     text : "Preview"
1081                 },
1082                 {
1083                     xtype: 'Button',
1084                     xns: Roo,
1085                     listeners : {
1086                         click : function (_self, e)
1087                         {
1088                             //_this.dialog.hide();
1089                             
1090                             var id = _this.form.findField('id').getValue();
1091                             
1092                             if(id*1 < 1){
1093                                 Roo.MessageBox.alert('Error', 'Please save the message frist!');
1094                                 return;
1095                             }
1096                            
1097                             new Pman.Request({
1098                                 url : baseURL + '/Core/MessagePreview',
1099                                 method : 'POST',
1100                                 mask: 'Sending',
1101                                 params : {
1102                                     _id : id,
1103                                     _table : _this.data.module
1104                                 }, 
1105                                 success : function(res) { 
1106                                     if(res.data == 'SUCCESS'){
1107                                         Roo.MessageBox.alert("Email Sent", 'The report was sent to your email (HTML format).');
1108                                     }
1109                                 }
1110                             });
1111                         },
1112                         render : function (_self)
1113                         {
1114                             _this.html_preview = _self;
1115                         }
1116                     },
1117                     text : "Send me a test copy"
1118                 },
1119                 {
1120                     xtype: 'Button',
1121                     xns: Roo,
1122                     listeners : {
1123                         click : function (_self, e)
1124                         {
1125                             _this.dialog.hide();
1126                         }
1127                     },
1128                     text : "Cancel"
1129                 },
1130                 {
1131                     xtype: 'Button',
1132                     xns: Roo,
1133                     listeners : {
1134                         click : function (_self, e)
1135                         {
1136                         
1137                             // do some checks?
1138                             _this.form.preValidate(function(res) {
1139                                 if (!res) {
1140                                     return; //failed.
1141                                 }
1142                                  _this.form.doAction("submit");
1143                             });
1144                         
1145                         }
1146                     },
1147                     text : "Save"
1148                 }
1149             ]
1150         });
1151     }
1152 };