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.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
40 * Description: Base Form For Notes
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
47 class EmailTemplateFormBase {
49 function getFormBody($prefix, $mod='',$formname='', $size='30') {
54 $temp_strings = $mod_strings;
57 global $current_language;
58 $mod_strings = return_module_language($current_language, $mod);
61 global $app_list_strings;
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'];
69 <input type="hidden" name="${prefix}record" value="">
70 <input type="hidden" name="${prefix}parent_type" value="${default_parent_type}">
72 <table cellspacing="0" cellpadding="0" border="0">
74 <td scope="row">$lbl_subject <span class="required">$lbl_required_symbol</span></td>
77 <td ><input name='${prefix}name' size='${size}' maxlength='255' type="text" value=""></td>
80 <td scope="row">$lbl_description</td>
83 <td ><textarea name='${prefix}description' cols='${size}' rows='4' ></textarea></td>
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;
97 function getForm($prefix, $mod='') {
99 global $current_language;
100 $mod_strings = return_module_language($current_language, $mod);
101 }else global $mod_strings;
103 global $app_list_strings;
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'];
110 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
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">
117 $the_form .= $this->getFormBody($prefix, $mod, "${prefix}EmailTemplateSave", "20");
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>
124 $the_form .= get_left_form_footer();
125 $the_form .= get_validate_record_js();
132 function handleSave($prefix,$redirect=true, $useRequired=false)
134 require_once('include/formbase.php');
135 require_once('include/upload_file.php');
136 global $upload_maxsize;
138 global $sugar_config;
140 $focus = new EmailTemplate();
141 if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) {
144 $focus = populateFromPost($prefix, $focus);
145 //process the text only flag
146 if(isset($_POST['text_only']) && ($_POST['text_only'] == '1')){
147 $focus->text_only = 1;
149 $focus->text_only = 0;
151 if(!$focus->ACLAccess('Save')) {
152 ACLController::displayNoAccess(true);
155 if(!isset($_REQUEST['published'])) $focus->published = 'off';
157 $preProcessedImages = array();
158 $emailTemplateBodyHtml = from_html($focus->body_html);
159 if(strpos($emailTemplateBodyHtml, '"cache/images/')) {
161 preg_match_all('#<img[^>]*[\s]+src[^=]*=[\s]*["\']cache/images/(.+?)["\']#si', $emailTemplateBodyHtml, $matches);
162 foreach($matches[1] as $match) {
163 $filename = urldecode($match);
165 $file_location = sugar_cached("images/{$filename}");
166 $mime_type = pathinfo($filename, PATHINFO_EXTENSION);
168 if(file_exists($file_location)) {
170 $newFileLocation = "upload://$id";
171 if(!copy($file_location, $newFileLocation)) {
172 $GLOBALS['log']->debug("EMAIL Template could not copy attachment to $newFileLocation");
174 $secureLink = "index.php?entryPoint=download&type=Notes&id={$id}";
175 $emailTemplateBodyHtml = str_replace("cache/images/$match", $secureLink, $emailTemplateBodyHtml);
176 unlink($file_location);
177 $preProcessedImages[$filename] = $id;
182 if (isset($GLOBALS['check_notify'])) {
183 $check_notify = $GLOBALS['check_notify'];
186 $check_notify = FALSE;
188 $focus->body_html = $emailTemplateBodyHtml;
189 $return_id = $focus->save($check_notify);
190 ///////////////////////////////////////////////////////////////////////////////
191 //// ATTACHMENT HANDLING
193 ///////////////////////////////////////////////////////////////////////////
194 //// ADDING NEW ATTACHMENTS
196 $max_files_upload = count($_FILES);
198 if(!empty($focus->id)) {
200 $where = "notes.parent_id='{$focus->id}'";
201 if(!empty($_REQUEST['old_id'])) { // to support duplication of email templates
202 $where .= " OR notes.parent_id='".$_REQUEST['old_id']."'";
204 $notes_list = $note->get_full_list("", $where, true);
207 if(!isset($notes_list)) {
208 $notes_list = array();
211 if(!is_array($focus->attachments)) { // PHP5 does not auto-create arrays(). Need to initialize it here.
212 $focus->attachments = array();
214 $focus->attachments = array_merge($focus->attachments, $notes_list);
218 //for($i = 0; $i < $max_files_upload; $i++) {
220 foreach ($_FILES as $key => $file)
224 //Images are presaved above so we need to prevent duplicate files from being created.
225 if( isset($preProcessedImages[$file['name']]) )
227 $oldId = $preProcessedImages[$file['name']];
229 $note->new_with_id = TRUE;
230 $GLOBALS['log']->debug("Image {$file['name']} has already been processed.");
233 $i=preg_replace("/email_attachment(.+)/",'$1',$key);
234 $upload_file = new UploadFile($key);
236 if(isset($_FILES[$key]) && $upload_file->confirm_upload() && preg_match("/^email_attachment/",$key)) {
237 $note->filename = $upload_file->get_stored_file_name();
238 $note->file = $upload_file;
239 $note->name = $mod_strings['LBL_EMAIL_ATTACHMENT'].': '.$note->file->original_file_name;
240 if(isset($_REQUEST['embedded'.$i]) && !empty($_REQUEST['embedded'.$i])){
241 if($_REQUEST['embedded'.$i]=='true'){
242 $note->embed_flag =true;
245 $note->embed_flag =false;
248 array_push($focus->attachments, $note);
253 $focus->saved_attachments = array();
254 foreach($focus->attachments as $note)
256 if( !empty($note->id) && $note->new_with_id === FALSE)
258 if(empty($_REQUEST['old_id']))
259 array_push($focus->saved_attachments, $note); // to support duplication of email templates
262 // we're duplicating a template with attachments
263 // dupe the file, create a new note, assign the note to the new template
264 $newNote = new Note();
265 $newNote->retrieve($note->id);
266 $newNote->id = create_guid();
267 $newNote->parent_id = $focus->id;
268 $newNote->new_with_id = true;
269 $newNote->date_modified = '';
270 $newNote->date_entered = '';
271 $newNoteId = $newNote->save();
273 UploadFile::duplicate_file($note->id, $newNoteId, $note->filename);
277 $note->parent_id = $focus->id;
278 $note->parent_type = 'Emails';
279 $note->file_mime_type = $note->file->mime_type;
280 $note_id = $note->save();
281 array_push($focus->saved_attachments, $note);
282 $note->id = $note_id;
284 if($note->new_with_id === FALSE)
285 $note->file->final_move($note->id);
287 $GLOBALS['log']->debug("Not performing final move for note id {$note->id} as it has already been processed");
290 //// END NEW ATTACHMENTS
291 ///////////////////////////////////////////////////////////////////////////
293 ///////////////////////////////////////////////////////////////////////////
294 //// ATTACHMENTS FROM DOCUMENTS
297 //_ppd(count($_REQUEST['document']));
298 if(!empty($_REQUEST['document'])){
299 $count = count($_REQUEST['document']);
305 for($i=0; $i<$count; $i++) {
306 if(isset($_REQUEST['documentId'.$i]) && !empty($_REQUEST['documentId'.$i])) {
307 $doc = new Document();
308 $docRev = new DocumentRevision();
309 $docNote = new Note();
311 $doc->retrieve($_REQUEST['documentId'.$i]);
312 $docRev->retrieve($doc->document_revision_id);
314 array_push($focus->saved_attachments, $docRev);
316 $docNote->name = $doc->document_name;
317 $docNote->filename = $docRev->filename;
318 $docNote->description = $doc->description;
319 $docNote->parent_id = $focus->id;
320 $docNote->parent_type = 'Emails';
321 $docNote->file_mime_type = $docRev->file_mime_type;
322 $docId = $docNote = $docNote->save();
324 UploadFile::duplicate_file($docRev->id, $docId, $docRev->filename);
329 //// END ATTACHMENTS FROM DOCUMENTS
330 ///////////////////////////////////////////////////////////////////////////
332 ///////////////////////////////////////////////////////////////////////////
333 //// REMOVE ATTACHMENTS
335 if(isset($_REQUEST['remove_attachment']) && !empty($_REQUEST['remove_attachment'])) {
336 foreach($_REQUEST['remove_attachment'] as $noteId) {
337 $q = 'UPDATE notes SET deleted = 1 WHERE id = \''.$noteId.'\'';
338 $focus->db->query($q);
343 //// END REMOVE ATTACHMENTS
344 ///////////////////////////////////////////////////////////////////////////
345 //// END ATTACHMENT HANDLING
346 ///////////////////////////////////////////////////////////////////////////////
348 clear_register_value('select_array', $focus->object_name);
351 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
352 handleRedirect($return_id, "EmailTemplates");