]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MailMerge/Step1.php
Release 6.4.0
[Github/sugarcrm.git] / modules / MailMerge / Step1.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 4, 2005
40  *
41  * To change the template for this generated file go to
42  * Window - Preferences - PHPeclipse - PHP - Code Templates
43  */
44
45
46
47
48 require_once('modules/MailMerge/modules_array.php');
49
50
51 require_once('include/json_config.php');
52 $json_config = new json_config();
53
54 global $app_strings;
55 global $app_list_strings;
56 global $mod_strings;
57 global $current_user;
58 global  $beanList, $beanFiles;
59 global $sugar_version, $sugar_config, $db;
60
61 $xtpl = new XTemplate('modules/MailMerge/Step1.html');
62 $xtpl->assign("MOD", $mod_strings);
63 $xtpl->assign("APP", $app_strings);
64 $xtpl->assign('JSON_CONFIG_JAVASCRIPT', $json_config->get_static_json_server(false, true));
65
66 if($_SESSION['MAILMERGE_MODULE'] == 'Campaigns' || $_SESSION['MAILMERGE_MODULE'] == 'CampaignProspects'){
67         $modules_array['Campaigns'] = 'Campaigns';
68 }
69 $module_list = $modules_array;
70
71 if(isset($_REQUEST['reset']) && $_REQUEST['reset'])
72 {
73         $_SESSION['MAILMERGE_MODULE'] = null;
74         $_SESSION['MAILMERGE_DOCUMENT_ID'] = null;
75         $_SESSION['SELECTED_OBJECTS_DEF'] = null;
76         $_SESSION['MAILMERGE_SKIP_REL'] = null;
77         $_SESSION['MAILMERGE_RECORD'] = null;
78         $_SESSION['MAILMERGE_RECORDS'] = null;
79         $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'] = null;
80 }
81 $fromListView = false;
82 if(!empty($_REQUEST['record']))
83 {
84         $_SESSION['MAILMERGE_RECORD'] = $_REQUEST['record'];
85 }
86 else if(isset($_REQUEST['uid'])) {
87         $_SESSION['MAILMERGE_RECORD'] = explode(',', $_REQUEST['uid']);
88
89 }
90 else if(isset($_REQUEST['entire']) && $_REQUEST['entire'] == 'true') {
91         // do entire list
92         $focus = 0;
93
94         $bean = $beanList[ $_SESSION['MAILMERGE_MODULE']];
95         require_once($beanFiles[$bean]);
96         $focus = new $bean;
97
98         if(isset($_SESSION['export_where']) && !empty($_SESSION['export_where'])) { // bug 4679
99                 $where = $_SESSION['export_where'];
100         } else {
101                 $where = '';
102         }
103     $beginWhere = substr(trim($where), 0, 5);
104     if ($beginWhere == "where")
105         $where = substr(trim($where), 5, strlen($where));
106         $query = $focus->create_export_query('',$where);
107
108         $result = $db->query($query,true,"Error mail merging {$_SESSION['MAILMERGE_MODULE']}: "."<BR>$query");
109
110         $new_arr = array();
111         while($val = $db->fetchByAssoc($result,false))
112         {
113                 array_push($new_arr, $val['id']);
114         }
115         $_SESSION['MAILMERGE_RECORD'] = $new_arr;
116 }
117 else if(isset($_SESSION['MAILMERGE_RECORDS']))
118 {
119
120         $fromListView = true;
121         $_SESSION['MAILMERGE_RECORD'] = $_SESSION['MAILMERGE_RECORDS'];
122         $_SESSION['MAILMERGE_RECORDS'] = null;
123 }
124 $rModule = '';
125 if(isset($_SESSION['MAILMERGE_RECORD']))
126 {
127         if(!empty($_POST['return_module']) && $_POST['return_module'] != "MailMerge")
128         {
129                 $rModule = $_POST['return_module'];
130         }
131         else if($fromListView)
132         {
133                 $rModule =      $_SESSION['MAILMERGE_MODULE_FROM_LISTVIEW'];
134                 $_SESSION['MAILMERGE_MODULE_FROM_LISTVIEW'] = null;
135         }
136         else
137         {
138                 $rModule = $_SESSION['MAILMERGE_MODULE'];
139         }
140         if($rModule == 'CampaignProspects'){
141         $rModule = 'Campaigns';
142         }
143
144         $_SESSION['MAILMERGE_MODULE'] = $rModule;
145         if(!empty($rModule) && $rModule != "MailMerge")
146         {
147            $class_name = $beanList[$rModule];
148            $includedir = $beanFiles[$class_name];
149            require_once($includedir);
150            $seed = new $class_name();
151
152            $selected_objects = '';
153         foreach($_SESSION['MAILMERGE_RECORD'] as $record_id)
154         {
155                 if($rModule == 'Campaigns'){
156
157                         $prospect = new Prospect();
158                         $prospect_module_list = array('leads', 'contacts', 'prospects', 'users');
159                         foreach($prospect_module_list as $mname){
160                                 $pList = $prospect->retrieveTargetList("campaigns.id = '$record_id' AND related_type = #$mname#", array('id', 'first_name', 'last_name'));
161
162                                 foreach($pList['list'] as $bean){
163                                         $selected_objects .= $bean->id.'='.str_replace("&", "##", $bean->name).'&';
164                                 }
165                         }
166                 }else{
167                         $seed->retrieve($record_id);
168                         $selected_objects .= $record_id.'='.str_replace("&", "##", $seed->name).'&';
169                 }
170         }
171
172
173         if($rModule != 'Contacts'
174            && $rModule != 'Leads' && $rModule != 'Products' && $rModule != 'Campaigns' && $rModule != 'Projects'
175            )
176         {
177                 $_SESSION['MAILMERGE_SKIP_REL'] = false;
178                 $xtpl->assign("STEP", "2");
179                 $xtpl->assign("SELECTED_OBJECTS", $selected_objects);
180                 $_SESSION['SELECTED_OBJECTS_DEF'] = $selected_objects;
181         }
182         else
183         {
184                 $_SESSION['MAILMERGE_SKIP_REL'] = true;
185                 $xtpl->assign("STEP", "2");
186                 $_SESSION['SELECTED_OBJECTS_DEF'] = $selected_objects;
187         }
188     }
189     else
190     {
191         $xtpl->assign("STEP", "2");
192     }
193
194 }
195 else
196 {
197         $xtpl->assign("STEP", "2");
198 }
199 $modules = $module_list;
200 $func = "";
201 if($rModule == 'Campaigns'){
202         $func = "disableModuleDropDown();";
203 }
204 $xtpl->assign("MAILMERGE_DISABLE_DROP_DOWN", $func);
205 $xtpl->assign("MAILMERGE_MODULE_OPTIONS", get_select_options_with_id($modules, $_SESSION['MAILMERGE_MODULE']));
206 $xtpl->assign("MAILMERGE_TEMPLATES", get_select_options_with_id(getDocumentRevisions(), '0'));
207
208 if(isset($_SESSION['MAILMERGE_MODULE'])){
209         $module_select_text = $mod_strings['LBL_MAILMERGE_SELECTED_MODULE'];
210         $xtpl->assign("MAILMERGE_NUM_SELECTED_OBJECTS",count($_SESSION['MAILMERGE_RECORD'])." ".$_SESSION['MAILMERGE_MODULE']." Selected");
211 }
212 else{
213         $module_select_text = $mod_strings['LBL_MAILMERGE_MODULE'];
214 }
215 $xtpl->assign("MODULE_SELECT", $module_select_text);
216 if($_SESSION['MAILMERGE_MODULE'] == 'Campaigns'){
217     $_SESSION['MAILMERGE_MODULE'] = 'CampaignProspects';
218 }
219
220 $admin = new Administration();
221 $admin->retrieveSettings();
222 $user_merge = $current_user->getPreference('mailmerge_on');
223 if ($user_merge != 'on' || !isset($admin->settings['system_mailmerge_on']) || !$admin->settings['system_mailmerge_on']){
224         $xtpl->assign("ADDIN_NOTICE", $mod_strings['LBL_ADDIN_NOTICE']);
225         $xtpl->assign("DISABLE_NEXT_BUTTON", "disabled");
226 }
227
228
229 $xtpl->parse("main");
230 $xtpl->out("main");
231
232 /*function get_user_module_list($user){
233         global $app_list_strings, $current_language;
234         $app_list_strings = return_app_list_strings_language($current_language);
235         $modules = query_module_access_list($user);
236         global $modInvisList;
237
238         return $modules;
239 }*/
240
241 function getDocumentRevisions()
242 {
243         $document = new Document();
244
245         $currentDate = $document->db->now();
246         $empty_date = $document->db->emptyValue("date");
247
248                         $query = "SELECT revision, document_name, document_revisions.id FROM document_revisions
249 LEFT JOIN documents on documents.id = document_revisions.document_id
250 WHERE ((active_date <= $currentDate AND exp_date > $currentDate)
251         OR (active_date is NULL) or (active_date = ".$empty_date.")
252         OR (active_date <= $currentDate AND ((exp_date = $empty_date OR (exp_date is NULL)))))
253 AND is_template = 1 AND template_type = 'mailmerge' AND documents.deleted = 0 ORDER BY document_name";
254
255                         $result = $document->db->query($query,true,"Error retrieving $document->object_name list: ");
256
257                         $list = Array();
258                         $list['None'] = 'None';
259                         while(($row = $document->db->fetchByAssoc($result)) != null)
260                             {
261                                 $revision = null;
262                                 $docName = $row['document_name'];
263                                 $revision = $row['revision'];
264                                 if(!empty($revision));
265                                 {
266                                         $docName .= " (rev. ".$revision.")";
267                                 }
268                                 $list[$row['id']] = $docName;
269                             }
270                         return $list;
271
272 }
273 ?>