]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/EmailTemplates/EmailTemplateFormBase.php
Release 6.5.10
[Github/sugarcrm.git] / modules / EmailTemplates / EmailTemplateFormBase.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description:  Base Form For Notes
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46
47 class EmailTemplateFormBase {
48
49     function __construct()
50     {
51
52     }
53
54         function getFormBody($prefix, $mod='',$formname='', $size='30') {
55
56
57                 global $mod_strings;
58
59                 $temp_strings = $mod_strings;
60
61                 if(!empty($mod)) {
62                         global $current_language;
63                         $mod_strings = return_module_language($current_language, $mod);
64                 }
65                                         global $app_strings;
66                                         global $app_list_strings;
67
68                                 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
69                                 $lbl_subject = $mod_strings['LBL_NOTE_SUBJECT'];
70                                 $lbl_description = $mod_strings['LBL_NOTE'];
71                                 $default_parent_type= $app_list_strings['record_type_default_key'];
72
73 $form = <<<EOF
74                                 <input type="hidden" name="${prefix}record" value="">
75                                 <input type="hidden" name="${prefix}parent_type" value="${default_parent_type}">
76                                 <p>
77                                 <table cellspacing="0" cellpadding="0" border="0">
78                                 <tr>
79                                     <td scope="row">$lbl_subject <span class="required">$lbl_required_symbol</span></td>
80                                 </tr>
81                                 <tr>
82                                     <td ><input name='${prefix}name' size='${size}' maxlength='255' type="text" value=""></td>
83                                 </tr>
84                                 <tr>
85                                     <td scope="row">$lbl_description</td>
86                                 </tr>
87                                 <tr>
88                                     <td ><textarea name='${prefix}description' cols='${size}' rows='4' ></textarea></td>
89                                 </tr>
90                                 </table></p>
91 EOF;
92
93         $javascript = new javascript();
94         $javascript->setFormName($formname);
95         $javascript->setSugarBean(new EmailTemplate());
96         $javascript->addRequiredFields($prefix);
97         $form .=$javascript->getScript();
98         $mod_strings = $temp_strings;
99         return $form;
100         }
101
102         function getForm($prefix, $mod='') {
103                 if(!empty($mod)) {
104                 global $current_language;
105                 $mod_strings = return_module_language($current_language, $mod);
106         }else global $mod_strings;
107                 global $app_strings;
108                 global $app_list_strings;
109
110                 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
111                 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
112                 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
113
114
115                 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
116 $the_form .= <<<EOQ
117
118                                 <form name="${prefix}EmailTemplateSave" onSubmit="return check_form('${prefix}EmailTemplateSave')" method="POST" action="index.php">
119                                         <input type="hidden" name="${prefix}module" value="EmailTemplates">
120                                         <input type="hidden" name="${prefix}action" value="Save">
121 EOQ;
122                 $the_form .= $this->getFormBody($prefix, $mod, "${prefix}EmailTemplateSave", "20");
123 $the_form .= <<<EOQ
124                                 <p><input title="$lbl_save_button_title" accessKey="$lbl_save_button_key" class="button" type="submit" name="button" value="  $lbl_save_button_label  " ></p>
125                                 </form>
126
127 EOQ;
128
129                 $the_form .= get_left_form_footer();
130                 $the_form .= get_validate_record_js();
131
132
133                 return $the_form;
134         }
135
136
137         function handleSave($prefix,$redirect=true, $useRequired=false)
138         {
139                 require_once('include/formbase.php');
140                 require_once('include/upload_file.php');
141                 global $upload_maxsize;
142                 global $mod_strings;
143                 global $sugar_config;
144
145                 $focus = new EmailTemplate();
146                 if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) {
147                         return null;
148                 }
149                 $focus = populateFromPost($prefix, $focus);
150         //process the text only flag
151         if(isset($_POST['text_only']) && ($_POST['text_only'] == '1')){
152             $focus->text_only = 1;
153         }else{
154             $focus->text_only = 0;
155         }
156                 if(!$focus->ACLAccess('Save')) {
157                         ACLController::displayNoAccess(true);
158                         sugar_cleanup(true);
159                 }
160                 if(!isset($_REQUEST['published'])) $focus->published = 'off';
161
162                 $preProcessedImages = array();
163                 $emailTemplateBodyHtml = from_html($focus->body_html);
164                 if(strpos($emailTemplateBodyHtml, '"cache/images/')) {
165                         $matches = array();
166                         preg_match_all('#<img[^>]*[\s]+src[^=]*=[\s]*["\']cache/images/(.+?)["\']#si', $emailTemplateBodyHtml, $matches);
167                         foreach($matches[1] as $match) {
168                                 $filename = urldecode($match);
169                 if($filename != pathinfo($filename, PATHINFO_BASENAME)) {
170                     // don't allow paths there
171                     $emailTemplateBodyHtml = str_replace("cache/images/$match", "", $emailTemplateBodyHtml);
172                     continue;
173                 }
174                                 $file_location = sugar_cached("images/{$filename}");
175                                 $mime_type = pathinfo($filename, PATHINFO_EXTENSION);
176
177                                 if(file_exists($file_location)) {
178                                         $id = create_guid();
179                                         $newFileLocation = "upload://$id";
180                                         if(!copy($file_location, $newFileLocation)) {
181                                                 $GLOBALS['log']->debug("EMAIL Template could not copy attachment to $newFileLocation");
182                                         } else {
183                                                 $secureLink = "index.php?entryPoint=download&type=Notes&id={$id}";
184                                             $emailTemplateBodyHtml = str_replace("cache/images/$match", $secureLink, $emailTemplateBodyHtml);
185                                                 unlink($file_location);
186                                                 $preProcessedImages[$filename] = $id;
187                                         }
188                                 } // if
189                         } // foreach
190                 } // if
191                 if (isset($GLOBALS['check_notify'])) {
192             $check_notify = $GLOBALS['check_notify'];
193         }
194         else {
195             $check_notify = FALSE;
196         }
197         $focus->body_html = $emailTemplateBodyHtml;
198         $return_id = $focus->save($check_notify);
199                 ///////////////////////////////////////////////////////////////////////////////
200                 ////    ATTACHMENT HANDLING
201
202                 ///////////////////////////////////////////////////////////////////////////
203                 ////    ADDING NEW ATTACHMENTS
204
205                 $max_files_upload = count($_FILES);
206
207                 if(!empty($focus->id)) {
208                         $note = new Note();
209                         $where = "notes.parent_id='{$focus->id}'";
210                         if(!empty($_REQUEST['old_id'])) { // to support duplication of email templates
211                                 $where .= " OR notes.parent_id='".$_REQUEST['old_id']."'";
212                         }
213                         $notes_list = $note->get_full_list("", $where, true);
214                 }
215
216                 if(!isset($notes_list)) {
217                         $notes_list = array();
218                 }
219
220                 if(!is_array($focus->attachments)) { // PHP5 does not auto-create arrays(). Need to initialize it here.
221                         $focus->attachments = array();
222                 }
223                 $focus->attachments = array_merge($focus->attachments, $notes_list);
224
225
226
227                 //for($i = 0; $i < $max_files_upload; $i++) {
228
229                 foreach ($_FILES as $key => $file)
230                 {
231                         $note = new Note();
232
233                         //Images are presaved above so we need to prevent duplicate files from being created.
234                         if( isset($preProcessedImages[$file['name']]) )
235                         {
236                             $oldId = $preProcessedImages[$file['name']];
237                             $note->id = $oldId;
238                             $note->new_with_id = TRUE;
239                             $GLOBALS['log']->debug("Image {$file['name']} has already been processed.");
240             }
241
242                         $i=preg_replace("/email_attachment(.+)/",'$1',$key);
243                         $upload_file = new UploadFile($key);
244
245                         if(isset($_FILES[$key]) && $upload_file->confirm_upload() && preg_match("/^email_attachment/",$key)) {
246                                 $note->filename = $upload_file->get_stored_file_name();
247                                 $note->file = $upload_file;
248                                 $note->name = $mod_strings['LBL_EMAIL_ATTACHMENT'].': '.$note->file->original_file_name;
249                                 if(isset($_REQUEST['embedded'.$i]) && !empty($_REQUEST['embedded'.$i])){
250                   if($_REQUEST['embedded'.$i]=='true'){
251                                         $note->embed_flag =true;
252                   }
253                   else{
254                         $note->embed_flag =false;
255                   }
256                                 }
257                                 array_push($focus->attachments, $note);
258                         }
259
260                 }
261
262                 $focus->saved_attachments = array();
263                 foreach($focus->attachments as $note)
264                 {
265                         if( !empty($note->id) && $note->new_with_id === FALSE)
266                         {
267                                 if(empty($_REQUEST['old_id']))
268                                         array_push($focus->saved_attachments, $note); // to support duplication of email templates
269                                 else
270                                 {
271                                         // we're duplicating a template with attachments
272                                         // dupe the file, create a new note, assign the note to the new template
273                                         $newNote = new Note();
274                                         $newNote->retrieve($note->id);
275                                         $newNote->id = create_guid();
276                                         $newNote->parent_id = $focus->id;
277                                         $newNote->new_with_id = true;
278                                         $newNote->date_modified = '';
279                                         $newNote->date_entered = '';
280                                         $newNoteId = $newNote->save();
281
282                                         UploadFile::duplicate_file($note->id, $newNoteId, $note->filename);
283                                 }
284                                 continue;
285                         }
286                         $note->parent_id = $focus->id;
287                         $note->parent_type = 'Emails';
288                         $note->file_mime_type = $note->file->mime_type;
289                         $note_id = $note->save();
290                         array_push($focus->saved_attachments, $note);
291                         $note->id = $note_id;
292
293                         if($note->new_with_id === FALSE)
294                         $note->file->final_move($note->id);
295             else
296                $GLOBALS['log']->debug("Not performing final move for note id {$note->id} as it has already been processed");
297                 }
298
299                 ////    END NEW ATTACHMENTS
300                 ///////////////////////////////////////////////////////////////////////////
301
302         ///////////////////////////////////////////////////////////////////////////
303         ////    ATTACHMENTS FROM DOCUMENTS
304         $count='';
305         //_pp($_REQUEST);
306         //_ppd(count($_REQUEST['document']));
307         if(!empty($_REQUEST['document'])){
308       $count = count($_REQUEST['document']);
309     }
310     else{
311         $count=10;
312     }
313
314         for($i=0; $i<$count; $i++) {
315                 if(isset($_REQUEST['documentId'.$i]) && !empty($_REQUEST['documentId'.$i])) {
316                         $doc = new Document();
317                         $docRev = new DocumentRevision();
318                         $docNote = new Note();
319
320                         $doc->retrieve($_REQUEST['documentId'.$i]);
321                         $docRev->retrieve($doc->document_revision_id);
322
323                         array_push($focus->saved_attachments, $docRev);
324
325                         $docNote->name = $doc->document_name;
326                         $docNote->filename = $docRev->filename;
327                         $docNote->description = $doc->description;
328                         $docNote->parent_id = $focus->id;
329                         $docNote->parent_type = 'Emails';
330                         $docNote->file_mime_type = $docRev->file_mime_type;
331                         $docId = $docNote = $docNote->save();
332
333                         UploadFile::duplicate_file($docRev->id, $docId, $docRev->filename);
334                 }
335
336         }
337
338         ////    END ATTACHMENTS FROM DOCUMENTS
339         ///////////////////////////////////////////////////////////////////////////
340
341                 ///////////////////////////////////////////////////////////////////////////
342                 ////    REMOVE ATTACHMENTS
343
344                 if(isset($_REQUEST['remove_attachment']) && !empty($_REQUEST['remove_attachment'])) {
345                         foreach($_REQUEST['remove_attachment'] as $noteId) {
346                                 $q = 'UPDATE notes SET deleted = 1 WHERE id = \''.$noteId.'\'';
347                                 $focus->db->query($q);
348                         }
349
350                 }
351
352                 ////    END REMOVE ATTACHMENTS
353                 ///////////////////////////////////////////////////////////////////////////
354         ////    END ATTACHMENT HANDLING
355         ///////////////////////////////////////////////////////////////////////////////
356
357         clear_register_value('select_array', $focus->object_name);
358
359                 if($redirect) {
360                 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
361                         handleRedirect($return_id, "EmailTemplates");
362                 }else{
363                         return $focus;
364                 }
365         }
366
367 }
368 ?>