]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/EmailTemplates/EmailTemplateFormBase.php
Release 6.2.0
[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-2011 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 getFormBody($prefix, $mod='',$formname='', $size='30') {
50                 
51                 
52                 global $mod_strings;
53
54                 $temp_strings = $mod_strings;
55
56                 if(!empty($mod)) {
57                         global $current_language;
58                         $mod_strings = return_module_language($current_language, $mod);
59                 }
60                                         global $app_strings;
61                                         global $app_list_strings;
62
63                                 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
64                                 $lbl_subject = $mod_strings['LBL_NOTE_SUBJECT'];
65                                 $lbl_description = $mod_strings['LBL_NOTE'];
66                                 $default_parent_type= $app_list_strings['record_type_default_key'];
67
68 $form = <<<EOF
69                                 <input type="hidden" name="${prefix}record" value="">
70                                 <input type="hidden" name="${prefix}parent_type" value="${default_parent_type}">
71                                 <p>
72                                 <table cellspacing="0" cellpadding="0" border="0">
73                                 <tr>
74                                     <td scope="row">$lbl_subject <span class="required">$lbl_required_symbol</span></td>
75                                 </tr>
76                                 <tr>
77                                     <td ><input name='${prefix}name' size='${size}' maxlength='255' type="text" value=""></td>
78                                 </tr>
79                                 <tr>
80                                     <td scope="row">$lbl_description</td>
81                                 </tr>
82                                 <tr>
83                                     <td ><textarea name='${prefix}description' cols='${size}' rows='4' ></textarea></td>
84                                 </tr>
85                                 </table></p>
86 EOF;
87
88         $javascript = new javascript();
89         $javascript->setFormName($formname);
90         $javascript->setSugarBean(new EmailTemplate());
91         $javascript->addRequiredFields($prefix);
92         $form .=$javascript->getScript();
93         $mod_strings = $temp_strings;
94         return $form;
95         }
96
97         function getForm($prefix, $mod='') {
98                 if(!empty($mod)) {
99                 global $current_language;
100                 $mod_strings = return_module_language($current_language, $mod);
101         }else global $mod_strings;
102                 global $app_strings;
103                 global $app_list_strings;
104
105                 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
106                 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
107                 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
108
109
110                 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
111 $the_form .= <<<EOQ
112
113                                 <form name="${prefix}EmailTemplateSave" onSubmit="return check_form('${prefix}EmailTemplateSave')" method="POST" action="index.php">
114                                         <input type="hidden" name="${prefix}module" value="EmailTemplates">
115                                         <input type="hidden" name="${prefix}action" value="Save">
116 EOQ;
117                 $the_form .= $this->getFormBody($prefix, $mod, "${prefix}EmailTemplateSave", "20");
118 $the_form .= <<<EOQ
119                                 <p><input title="$lbl_save_button_title" accessKey="$lbl_save_button_key" class="button" type="submit" name="button" value="  $lbl_save_button_label  " ></p>
120                                 </form>
121
122 EOQ;
123
124                 $the_form .= get_left_form_footer();
125                 $the_form .= get_validate_record_js();
126
127
128                 return $the_form;
129         }
130
131
132         function handleSave($prefix,$redirect=true, $useRequired=false) {
133                 
134                 
135                 
136                 
137                 require_once('include/formbase.php');
138                 require_once('include/upload_file.php');
139                 global $upload_maxsize, $upload_dir;
140                 global $mod_strings;
141                 global $sugar_config;
142
143                 $focus = new EmailTemplate();
144                 if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) {
145                         return null;
146                 }
147                 $focus = populateFromPost($prefix, $focus);
148         //process the text only flag
149         if(isset($_POST['text_only']) && ($_POST['text_only'] == '1')){
150             $focus->text_only = 1;
151         }else{
152             $focus->text_only = 0;
153         }
154                 if(!$focus->ACLAccess('Save')) {
155                         ACLController::displayNoAccess(true);
156                         sugar_cleanup(true);
157                 }
158                 if(!isset($_REQUEST['published'])) $focus->published = 'off';
159
160                 $preProcessedImages = array();
161                 $emailTemplateBodyHtml = from_html($focus->body_html);
162                 $fileBasePath = "{$sugar_config['cache_dir']}images/";
163                 $filePatternSearch = "{$sugar_config['cache_dir']}";
164                 $filePatternSearch = str_replace("/", "\/", $filePatternSearch);
165                 $filePatternSearch = $filePatternSearch . "images\/";
166                 $fileBasePath1 = "\"" .$fileBasePath;
167                 if(strpos($emailTemplateBodyHtml, "\"{$fileBasePath}")) {
168                         $matches = array();
169                         preg_match_all("/{$filePatternSearch}.+?\"/i", $emailTemplateBodyHtml, $matches);
170                         foreach($matches[0] as $match) {
171                                 $filenameUndecoded = str_replace($fileBasePath, '', $match);
172                                 $filename = urldecode(substr($filenameUndecoded, 0, -1));
173                                 $filenameUndecoded = str_replace("\"", '', $filenameUndecoded);
174                                 $cid = $filename;
175                                 $file_location = clean_path(getcwd()."/{$sugar_config['cache_dir']}images/{$filename}");
176                                 $mime_type = strtolower(substr($filename, strrpos($filename, ".")+1, strlen($filename)));
177
178                                 if(file_exists($file_location)) {
179                                         $id = create_guid();
180                                         $newFileLocation = "{$sugar_config['upload_dir']}{$id}";                                        
181                                         if(!copy($file_location, $newFileLocation)) {
182                                                 $GLOBALS['log']->debug("EMAIL Template could not copy attachment to {$sugar_config['upload_dir']} [ {$newFileLocation} ]");
183                                         } else {
184                                                 $secureLink = 'index.php?entryPoint=download&id='.$id.'&type=Notes';
185                                             $emailTemplateBodyHtml = str_replace("{$sugar_config['cache_dir']}images/{$filenameUndecoded}", $secureLink, $emailTemplateBodyHtml);
186                                                 unlink($file_location);
187                                                 $preProcessedImages[$filename] = $id;
188                                         }
189                                 } // if
190                         } // foreach
191                 } // if
192                 if (isset($GLOBALS['check_notify'])) {
193             $check_notify = $GLOBALS['check_notify'];
194         }
195         else {
196             $check_notify = FALSE;
197         }
198         $focus->body_html = $emailTemplateBodyHtml;
199         $return_id = $focus->save($check_notify);
200                 ///////////////////////////////////////////////////////////////////////////////
201                 ////    ATTACHMENT HANDLING
202
203                 ///////////////////////////////////////////////////////////////////////////
204                 ////    ADDING NEW ATTACHMENTS
205
206                 $max_files_upload = count($_FILES);
207                                 
208                 if(!empty($focus->id)) {
209                         $note = new Note();
210                         $where = "notes.parent_id='{$focus->id}'";
211                         if(!empty($_REQUEST['old_id'])) { // to support duplication of email templates
212                                 $where .= " OR notes.parent_id='".$_REQUEST['old_id']."'";
213                         }
214                         $notes_list = $note->get_full_list("", $where, true);
215                 }
216
217                 if(!isset($notes_list)) {
218                         $notes_list = array();
219                 }
220
221                 if(!is_array($focus->attachments)) { // PHP5 does not auto-create arrays(). Need to initialize it here.
222                         $focus->attachments = array();
223                 }
224                 $focus->attachments = array_merge($focus->attachments, $notes_list);
225
226
227
228                 //for($i = 0; $i < $max_files_upload; $i++) {
229
230                 foreach ($_FILES as $key => $file)
231                 {
232                         $note = new Note();
233
234                         //Images are presaved above so we need to prevent duplicate files from being created.
235                         if( isset($preProcessedImages[$file['name']]) )
236                         {
237                             $oldId = $preProcessedImages[$file['name']];
238                             $note->id = $oldId;
239                             $note->new_with_id = TRUE;
240                             $GLOBALS['log']->debug("Image {$file['name']} has already been processed.");
241             }
242             
243                         $i=preg_replace("/email_attachment(.+)/",'$1',$key);
244                         $upload_file = new UploadFile($key);
245
246                         
247                         if(isset($_FILES[$key]) && $upload_file->confirm_upload() && preg_match("/^email_attachment/",$key)) {
248                                 $note->filename = $upload_file->get_stored_file_name();
249                                 $note->file = $upload_file;
250                                 $note->name = $mod_strings['LBL_EMAIL_ATTACHMENT'].': '.$note->file->original_file_name;
251                                 if(isset($_REQUEST['embedded'.$i]) && !empty($_REQUEST['embedded'.$i])){
252                   if($_REQUEST['embedded'.$i]=='true'){
253                                         $note->embed_flag =true;
254                   }
255                   else{
256                         $note->embed_flag =false;
257                   }
258                                 }
259                                 array_push($focus->attachments, $note);
260                         }
261
262                 }
263
264                 $focus->saved_attachments = array();
265                 foreach($focus->attachments as $note) 
266                 {
267                         if( !empty($note->id) && $note->new_with_id === FALSE) 
268                         {
269                                 if(empty($_REQUEST['old_id'])) 
270                                         array_push($focus->saved_attachments, $note); // to support duplication of email templates
271                                 else 
272                                 {
273                                         // we're duplicating a template with attachments
274                                         // dupe the file, create a new note, assign the note to the new template
275                                         $newNote = new Note();
276                                         $newNote->retrieve($note->id);
277                                         $newNote->id = create_guid();
278                                         $newNote->parent_id = $focus->id;
279                                         $newNote->new_with_id = true;
280                                         $newNote->date_modified = '';
281                                         $newNote->date_entered = '';                                    
282                                         $newNoteId = $newNote->save();
283
284                                         $dupeFile = new UploadFile('duplicate');
285                                         $dupeFile->duplicate_file($note->id, $newNoteId, $note->filename);
286                                 }
287                                 continue;
288                         }
289                         $note->parent_id = $focus->id;
290                         $note->parent_type = 'Emails';
291                         $note->file_mime_type = $note->file->mime_type;
292                         $note_id = $note->save();
293                         array_push($focus->saved_attachments, $note);
294                         $note->id = $note_id;
295                         
296                         if($note->new_with_id === FALSE)
297                         $note->file->final_move($note->id);
298             else 
299                $GLOBALS['log']->debug("Not performing final move for note id {$note->id} as it has already been processed");
300                 }
301
302                 ////    END NEW ATTACHMENTS
303                 ///////////////////////////////////////////////////////////////////////////
304
305         ///////////////////////////////////////////////////////////////////////////
306         ////    ATTACHMENTS FROM DOCUMENTS
307         $count='';
308         //_pp($_REQUEST);
309         //_ppd(count($_REQUEST['document']));
310         if(!empty($_REQUEST['document'])){
311       $count = count($_REQUEST['document']);
312     }
313     else{
314         $count=10;
315     }
316
317         for($i=0; $i<$count; $i++) {
318                 if(isset($_REQUEST['documentId'.$i]) && !empty($_REQUEST['documentId'.$i])) {
319                         $doc = new Document();
320                         $docRev = new DocumentRevision();
321                         $docNote = new Note();
322                         $noteFile = new UploadFile('none');
323
324                         $doc->retrieve($_REQUEST['documentId'.$i]);
325                         $docRev->retrieve($doc->document_revision_id);
326
327                         array_push($focus->saved_attachments, $docRev);
328
329                         $docNote->name = $doc->document_name;
330                         $docNote->filename = $docRev->filename;
331                         $docNote->description = $doc->description;
332                         $docNote->parent_id = $focus->id;
333                         $docNote->parent_type = 'Emails';
334                         $docNote->file_mime_type = $docRev->file_mime_type;
335                         $docId = $docNote = $docNote->save();
336
337                         $noteFile->duplicate_file($docRev->id, $docId, $docRev->filename);
338                 }
339
340         }
341
342         ////    END ATTACHMENTS FROM DOCUMENTS
343         ///////////////////////////////////////////////////////////////////////////
344
345                 ///////////////////////////////////////////////////////////////////////////
346                 ////    REMOVE ATTACHMENTS
347
348                 if(isset($_REQUEST['remove_attachment']) && !empty($_REQUEST['remove_attachment'])) {
349                         foreach($_REQUEST['remove_attachment'] as $noteId) {
350                                 $q = 'UPDATE notes SET deleted = 1 WHERE id = \''.$noteId.'\'';
351                                 $focus->db->query($q);
352                         }
353
354                 }
355
356                 ////    END REMOVE ATTACHMENTS
357                 ///////////////////////////////////////////////////////////////////////////
358         ////    END ATTACHMENT HANDLING
359         ///////////////////////////////////////////////////////////////////////////////
360
361                 if($redirect) {
362                 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
363                         handleRedirect($return_id, "EmailTemplates");
364                 }else{
365                         return $focus;
366                 }
367         }
368
369 }
370 ?>