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 ********************************************************************************/
40 * Created on Oct 7, 2005
42 * To change the template for this generated file go to
43 * Window - Preferences - PHPeclipse - PHP - Code Templates
45 require_once('soap/SoapHelperFunctions.php');
46 require_once('modules/MailMerge/MailMerge.php');
47 require_once('include/upload_file.php');
49 global $beanList, $beanFiles;
51 global $app_list_strings;
54 $xtpl = new XTemplate('modules/MailMerge/Merge.html');
55 $xtpl->assign("MAILMERGE_IS_REDIRECT", false);
58 $redirectUrl = 'index.php?action=index&step=5&module=MailMerge&mtime=' . $mTime;
62 * Just refresh download page to get file which was banned by IE security
64 if (empty($_SESSION['MAILMERGE_MODULE']) && !empty($_SESSION['mail_merge_file_location']) && !empty($_SESSION['mail_merge_file_name']))
66 $xtpl->assign("MAILMERGE_REDIRECT", true);
70 $module = $_SESSION['MAILMERGE_MODULE'];
71 $document_id = $_SESSION['MAILMERGE_DOCUMENT_ID'];
72 $selObjs = urldecode($_SESSION['SELECTED_OBJECTS_DEF']);
73 $relObjs = (isset($_SESSION['MAILMERGE_RELATED_CONTACTS']) ? $_SESSION['MAILMERGE_RELATED_CONTACTS'] : '');
76 if (!empty($_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO']))
78 $relModule = $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'];
81 if ($_SESSION['MAILMERGE_MODULE'] == null)
83 sugar_die("Error during Mail Merge process. Please try again.");
86 $_SESSION['MAILMERGE_MODULE'] = null;
87 $_SESSION['MAILMERGE_DOCUMENT_ID'] = null;
88 $_SESSION['SELECTED_OBJECTS_DEF'] = null;
89 $_SESSION['MAILMERGE_SKIP_REL'] = null;
90 $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'] = null;
92 parse_str(stripslashes(html_entity_decode($selObjs, ENT_QUOTES)),$item_ids);
94 if ($module == 'CampaignProspects')
96 $module = 'Prospects';
97 if (!empty($_SESSION['MAILMERGE_CAMPAIGN_ID']))
99 $targets = array_keys($item_ids);
100 require_once('modules/Campaigns/utils.php');
101 campaign_log_mail_merge($_SESSION['MAILMERGE_CAMPAIGN_ID'],$targets);
104 $class_name = $beanList[$module];
105 $includedir = $beanFiles[$class_name];
106 require_once($includedir);
107 $seed = new $class_name();
109 $fields = get_field_list($seed);
111 $document = new DocumentRevision();//new Document();
112 $document->retrieve($document_id);
114 if (!empty($relModule))
116 $rel_class_name = $beanList[$relModule ];
117 require_once($beanFiles[$rel_class_name]);
118 $rel_seed = new $rel_class_name();
121 global $sugar_config;
124 if (array_key_exists('mailmerge_filter', $sugar_config))
126 //$filter = $sugar_config['mailmerge_filter'];
128 array_push($filter, 'link');
130 $merge_array = array();
131 $merge_array['master_module'] = $module;
132 $merge_array['related_module'] = $relModule;
136 foreach ($item_ids as $key=>$value)
138 if (!empty($relObjs[$key]))
140 $ids[$key] = $relObjs[$key];
147 $merge_array['ids'] = $ids;
149 $dataDir = getcwd() . '/' . sugar_cached('MergedDocuments/');
150 if (!file_exists($dataDir))
152 sugar_mkdir($dataDir);
154 srand((double)microtime()*1000000);
155 $dataFileName = 'sugardata' . $mTime . '.php';
156 write_array_to_file('merge_array', $merge_array, $dataDir . $dataFileName);
157 //Save the temp file so we can remove when we are done
158 $_SESSION['MAILMERGE_TEMP_FILE_' . $mTime] = $dataDir . $dataFileName;
159 $site_url = $sugar_config['site_url'];
160 //$templateFile = $site_url . '/' . UploadFile::get_upload_url($document);
161 $templateFile = $site_url . '/' . UploadFile::get_url(from_html($document->filename),$document->id);
162 $dataFile =$dataFileName;
163 $startUrl = 'index.php?action=index&module=MailMerge&reset=true';
165 $relModule = trim($relModule);
166 $contents = "SUGARCRM_MAIL_MERGE_TOKEN#$templateFile#$dataFile#$module#$relModule";
168 $rtfFileName = 'sugartokendoc' . $mTime . '.doc';
169 $fp = sugar_fopen($dataDir . $rtfFileName, 'w');
170 fwrite($fp, $contents);
173 $_SESSION['mail_merge_file_location'] = sugar_cached('MergedDocuments/') . $rtfFileName;
174 $_SESSION['mail_merge_file_name'] = $rtfFileName;
176 $xtpl->assign("MAILMERGE_FIREFOX_URL", $site_url . '/' . $GLOBALS['sugar_config']['cache_dir'] . 'MergedDocuments/' . $rtfFileName);
177 $xtpl->assign("MAILMERGE_START_URL", $startUrl);
178 $xtpl->assign("MAILMERGE_TEMPLATE_FILE", $templateFile);
179 $xtpl->assign("MAILMERGE_DATA_FILE", $dataFile);
180 $xtpl->assign("MAILMERGE_MODULE", $module);
182 $xtpl->assign("MAILMERGE_REL_MODULE", $relModule);
185 $xtpl->assign("MOD", $mod_strings);
186 $xtpl->assign("APP", $app_strings);
187 $xtpl->assign("MAILMERGE_REDIRECT_URL", $redirectUrl);
188 $xtpl->parse("main");