]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MailMerge/Merge.php
Release 6.5.15
[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-2013 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  * Created on Oct 7, 2005
41  *
42  * To change the template for this generated file go to
43  * Window - Preferences - PHPeclipse - PHP - Code Templates
44  */
45 require_once('soap/SoapHelperFunctions.php');
46 require_once('modules/MailMerge/MailMerge.php');
47 require_once('include/upload_file.php');
48
49 global  $beanList, $beanFiles;
50 global $app_strings;
51 global $app_list_strings;
52 global $mod_strings;
53
54 $xtpl = new XTemplate('modules/MailMerge/Merge.html');
55 $xtpl->assign("MAILMERGE_IS_REDIRECT", false);
56
57 $mTime = microtime();
58 $redirectUrl = 'index.php?action=index&step=5&module=MailMerge&mtime=' . $mTime;
59
60 /**
61  * Bug #42275
62  * Just refresh download page to get file which was banned by IE security
63  */
64 if (empty($_SESSION['MAILMERGE_MODULE']) && !empty($_SESSION['mail_merge_file_location']) && !empty($_SESSION['mail_merge_file_name']))
65 {
66     $xtpl->assign("MAILMERGE_REDIRECT", true);
67 }
68 else
69 {
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'] : '');
74
75     $relModule = '';
76     if (!empty($_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO']))
77     {
78         $relModule = $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'];
79     }
80
81     if ($_SESSION['MAILMERGE_MODULE'] == null)
82     {
83         sugar_die("Error during Mail Merge process.  Please try again.");
84     }
85
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;
91     $item_ids = array();
92     parse_str(stripslashes(html_entity_decode($selObjs, ENT_QUOTES)),$item_ids);
93
94     if ($module == 'CampaignProspects')
95     {
96         $module = 'Prospects';
97         if (!empty($_SESSION['MAILMERGE_CAMPAIGN_ID']))
98         {
99             $targets = array_keys($item_ids);
100             require_once('modules/Campaigns/utils.php');
101             campaign_log_mail_merge($_SESSION['MAILMERGE_CAMPAIGN_ID'],$targets);
102         }
103     }
104     $class_name = $beanList[$module];
105     $includedir = $beanFiles[$class_name];
106     require_once($includedir);
107     $seed = new $class_name();
108
109     $fields =  get_field_list($seed);
110
111     $document = new DocumentRevision();//new Document();
112     $document->retrieve($document_id);
113
114     if (!empty($relModule))
115     {
116         $rel_class_name = $beanList[$relModule ];
117         require_once($beanFiles[$rel_class_name]);
118         $rel_seed = new $rel_class_name();
119     }
120
121     global $sugar_config;
122
123     $filter = array();
124     if (array_key_exists('mailmerge_filter', $sugar_config))
125     {
126         //$filter = $sugar_config['mailmerge_filter'];
127     }
128     array_push($filter, 'link');
129
130     $merge_array = array();
131     $merge_array['master_module'] = $module;
132     $merge_array['related_module'] = $relModule;
133     //rrs log merge
134     $ids = array();
135
136     foreach ($item_ids as $key=>$value)
137     {
138         if (!empty($relObjs[$key]))
139         {
140            $ids[$key] = $relObjs[$key];
141         }
142         else
143         {
144            $ids[$key] = '';
145         }
146     }//rof
147     $merge_array['ids'] = $ids;
148
149     $dataDir = getcwd() . '/' . sugar_cached('MergedDocuments/');
150     if (!file_exists($dataDir))
151     {
152         sugar_mkdir($dataDir);
153     }
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';
164
165     $relModule = trim($relModule);
166     $contents = "SUGARCRM_MAIL_MERGE_TOKEN#$templateFile#$dataFile#$module#$relModule";
167
168     $rtfFileName = 'sugartokendoc' . $mTime . '.doc';
169     $fp = sugar_fopen($dataDir . $rtfFileName, 'w');
170     fwrite($fp, $contents);
171     fclose($fp);
172
173     $_SESSION['mail_merge_file_location'] = sugar_cached('MergedDocuments/') . $rtfFileName;
174     $_SESSION['mail_merge_file_name'] = $rtfFileName;
175
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);
181
182     $xtpl->assign("MAILMERGE_REL_MODULE", $relModule);
183 }
184
185 $xtpl->assign("MOD", $mod_strings);
186 $xtpl->assign("APP", $app_strings);
187 $xtpl->assign("MAILMERGE_REDIRECT_URL", $redirectUrl);
188 $xtpl->parse("main");
189 $xtpl->out("main");