]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MailMerge/Merge.php
Release 6.4.0
[Github/sugarcrm.git] / modules / MailMerge / Merge.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  * Created on Oct 7, 2005
40  *
41  * To change the template for this generated file go to
42  * Window - Preferences - PHPeclipse - PHP - Code Templates
43  */
44 require_once('soap/SoapHelperFunctions.php');
45 require_once('modules/MailMerge/MailMerge.php'); 
46  
47  
48  
49 require_once('include/upload_file.php');
50
51
52
53 global  $beanList, $beanFiles;
54 global $app_strings;
55 global $app_list_strings;
56 global $mod_strings;
57
58 $xtpl = new XTemplate('modules/MailMerge/Merge.html');
59
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'] : '');
64
65 $relModule = '';
66 if(!empty($_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'])){
67         $relModule = $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'];
68 }
69
70 if($_SESSION['MAILMERGE_MODULE'] == null)
71 {
72         sugar_die("Error during Mail Merge process.  Please try again.");
73 }
74
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;
80 $item_ids = array();
81 parse_str(stripslashes(html_entity_decode($selObjs, ENT_QUOTES)),$item_ids);
82
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);
89     }
90 }
91 $class_name = $beanList[$module];
92 $includedir = $beanFiles[$class_name];
93 require_once($includedir);
94 $seed = new $class_name(); 
95
96 $fields =  get_field_list($seed);
97
98 $document = new DocumentRevision();//new Document();
99 $document->retrieve($document_id);
100
101 if(!empty($relModule)){
102 $rel_class_name = $beanList[$relModule ];
103 require_once($beanFiles[$rel_class_name]);
104 $rel_seed = new $rel_class_name();
105 }
106
107 global $sugar_config;
108
109 $filter = array();
110 if(array_key_exists('mailmerge_filter', $sugar_config)){
111  //   $filter = $sugar_config['mailmerge_filter']; 
112 }
113 array_push($filter, 'link');
114
115 $merge_array = array();
116 $merge_array['master_module'] = $module;
117 $merge_array['related_module'] = $relModule;
118 //rrs log merge
119 $ids = array();
120
121 foreach($item_ids as $key=>$value)
122 {
123         if(!empty($relObjs[$key])){
124            $ids[$key] = $relObjs[$key];
125         }else{
126            $ids[$key] = '';
127         }
128 }//rof
129 $merge_array['ids'] = $ids;
130
131 $dataDir = getcwd(). '/' . sugar_cached('MergedDocuments/');
132 if(!file_exists($dataDir))
133 {
134         sugar_mkdir($dataDir);
135 }
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';
148
149 $relModule = trim($relModule);
150 $contents = "SUGARCRM_MAIL_MERGE_TOKEN#$templateFile#$dataFile#$module#$relModule";
151
152 $rtfFileName = 'sugartokendoc'.$mTime.'.doc';
153 $fp = sugar_fopen($dataDir.$rtfFileName, 'w');
154 fwrite($fp, $contents);
155 fclose($fp);
156
157 $_SESSION['mail_merge_file_location'] = sugar_cached('MergedDocuments/').$rtfFileName;
158 $_SESSION['mail_merge_file_name'] = $rtfFileName;
159
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);
165
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");
171 $xtpl->out("main");
172 ?>