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.
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 ********************************************************************************/
39 * Created on Oct 7, 2005
41 * To change the template for this generated file go to
42 * Window - Preferences - PHPeclipse - PHP - Code Templates
44 require_once('soap/SoapHelperFunctions.php');
45 require_once('modules/MailMerge/MailMerge.php');
49 require_once('include/upload_file.php');
53 global $beanList, $beanFiles;
55 global $app_list_strings;
58 $xtpl = new XTemplate('modules/MailMerge/Merge.html');
60 $module = $_SESSION['MAILMERGE_MODULE'];
61 $document_id = $_SESSION['MAILMERGE_DOCUMENT_ID'];
62 $selObjs = urldecode($_SESSION['SELECTED_OBJECTS_DEF']);
63 $relObjs = (isset($_SESSION['MAILMERGE_RELATED_CONTACTS']) ? $_SESSION['MAILMERGE_RELATED_CONTACTS'] : '');
66 if(!empty($_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'])){
67 $relModule = $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'];
70 if($_SESSION['MAILMERGE_MODULE'] == null)
72 sugar_die("Error during Mail Merge process. Please try again.");
75 $_SESSION['MAILMERGE_MODULE'] = null;
76 $_SESSION['MAILMERGE_DOCUMENT_ID'] = null;
77 $_SESSION['SELECTED_OBJECTS_DEF'] = null;
78 $_SESSION['MAILMERGE_SKIP_REL'] = null;
79 $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'] = null;
81 parse_str(stripslashes(html_entity_decode($selObjs, ENT_QUOTES)),$item_ids);
83 if($module == 'CampaignProspects'){
84 $module = 'Prospects';
85 if(!empty($_SESSION['MAILMERGE_CAMPAIGN_ID'])){
86 $targets = array_keys($item_ids);
87 require_once('modules/Campaigns/utils.php');
88 campaign_log_mail_merge($_SESSION['MAILMERGE_CAMPAIGN_ID'],$targets);
91 $class_name = $beanList[$module];
92 $includedir = $beanFiles[$class_name];
93 require_once($includedir);
94 $seed = new $class_name();
96 $fields = get_field_list($seed);
98 $document = new DocumentRevision();//new Document();
99 $document->retrieve($document_id);
101 if(!empty($relModule)){
102 $rel_class_name = $beanList[$relModule ];
103 require_once($beanFiles[$rel_class_name]);
104 $rel_seed = new $rel_class_name();
107 global $sugar_config;
110 if(array_key_exists('mailmerge_filter', $sugar_config)){
111 // $filter = $sugar_config['mailmerge_filter'];
113 array_push($filter, 'link');
115 $merge_array = array();
116 $merge_array['master_module'] = $module;
117 $merge_array['related_module'] = $relModule;
121 foreach($item_ids as $key=>$value)
123 if(!empty($relObjs[$key])){
124 $ids[$key] = $relObjs[$key];
129 $merge_array['ids'] = $ids;
131 $dataDir = getcwd(). '/' . sugar_cached('MergedDocuments/');
132 if(!file_exists($dataDir))
134 sugar_mkdir($dataDir);
136 srand((double)microtime()*1000000);
137 $mTime = microtime();
138 $dataFileName = 'sugardata'.$mTime.'.php';
139 write_array_to_file('merge_array', $merge_array, $dataDir.$dataFileName);
140 //Save the temp file so we can remove when we are done
141 $_SESSION['MAILMERGE_TEMP_FILE_'.$mTime] = $dataDir.$dataFileName;
142 $site_url = $sugar_config['site_url'];
143 //$templateFile = $site_url . '/' . UploadFile::get_upload_url($document);
144 $templateFile = $site_url.'/'.UploadFile::get_url(from_html($document->filename),$document->id);
145 $dataFile =$dataFileName;
146 $redirectUrl = 'index.php?action=index&step=5&module=MailMerge&mtime='.$mTime;
147 $startUrl = 'index.php?action=index&module=MailMerge&reset=true';
149 $relModule = trim($relModule);
150 $contents = "SUGARCRM_MAIL_MERGE_TOKEN#$templateFile#$dataFile#$module#$relModule";
152 $rtfFileName = 'sugartokendoc'.$mTime.'.doc';
153 $fp = sugar_fopen($dataDir.$rtfFileName, 'w');
154 fwrite($fp, $contents);
157 $_SESSION['mail_merge_file_location'] = sugar_cached('MergedDocuments/').$rtfFileName;
158 $_SESSION['mail_merge_file_name'] = $rtfFileName;
160 $xtpl->assign("MAILMERGE_FIREFOX_URL", $site_url .'/'.$GLOBALS['sugar_config']['cache_dir'].'MergedDocuments/'.$rtfFileName);
161 $xtpl->assign("MAILMERGE_START_URL", $startUrl);
162 $xtpl->assign("MAILMERGE_TEMPLATE_FILE", $templateFile);
163 $xtpl->assign("MAILMERGE_DATA_FILE", $dataFile);
164 $xtpl->assign("MAILMERGE_MODULE", $module);
166 $xtpl->assign("MAILMERGE_REL_MODULE", $relModule);
167 $xtpl->assign("MOD", $mod_strings);
168 $xtpl->assign("APP", $app_strings);
169 $xtpl->assign("MAILMERGE_REDIRECT_URL", $redirectUrl);
170 $xtpl->parse("main");