]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MailMerge/Step1.php
Release 6.5.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-2012 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     $orderBy = '';
107         $query = $focus->create_export_query($orderBy,$where);
108
109         $result = $db->query($query,true,"Error mail merging {$_SESSION['MAILMERGE_MODULE']}: "."<BR>$query");
110
111         $new_arr = array();
112         while($val = $db->fetchByAssoc($result,false))
113         {
114                 array_push($new_arr, $val['id']);
115         }
116         $_SESSION['MAILMERGE_RECORD'] = $new_arr;
117 }
118 else if(isset($_SESSION['MAILMERGE_RECORDS']))
119 {
120
121         $fromListView = true;
122         $_SESSION['MAILMERGE_RECORD'] = $_SESSION['MAILMERGE_RECORDS'];
123         $_SESSION['MAILMERGE_RECORDS'] = null;
124 }
125 $rModule = '';
126 if(isset($_SESSION['MAILMERGE_RECORD']))
127 {
128         if(!empty($_POST['return_module']) && $_POST['return_module'] != "MailMerge")
129         {
130                 $rModule = $_POST['return_module'];
131         }
132         else if($fromListView)
133         {
134                 $rModule =      $_SESSION['MAILMERGE_MODULE_FROM_LISTVIEW'];
135                 $_SESSION['MAILMERGE_MODULE_FROM_LISTVIEW'] = null;
136         }
137         else
138         {
139                 $rModule = $_SESSION['MAILMERGE_MODULE'];
140         }
141         if($rModule == 'CampaignProspects'){
142         $rModule = 'Campaigns';
143         }
144
145         $_SESSION['MAILMERGE_MODULE'] = $rModule;
146         if(!empty($rModule) && $rModule != "MailMerge")
147         {
148            $class_name = $beanList[$rModule];
149            $includedir = $beanFiles[$class_name];
150            require_once($includedir);
151            $seed = new $class_name();
152
153            $selected_objects = '';
154         foreach($_SESSION['MAILMERGE_RECORD'] as $record_id)
155         {
156                 if($rModule == 'Campaigns'){
157
158                         $prospect = new Prospect();
159                         $prospect_module_list = array('leads', 'contacts', 'prospects', 'users');
160                         foreach($prospect_module_list as $mname){
161                                 $pList = $prospect->retrieveTargetList("campaigns.id = '$record_id' AND related_type = #$mname#", array('id', 'first_name', 'last_name'));
162
163                                 foreach($pList['list'] as $bean){
164                                         $selected_objects .= $bean->id.'='.str_replace("&", "##", $bean->name).'&';
165                                 }
166                         }
167                 }else{
168                         $seed->retrieve($record_id);
169                         $selected_objects .= $record_id.'='.str_replace("&", "##", $seed->name).'&';
170                 }
171         }
172
173
174         if($rModule != 'Contacts'
175            && $rModule != 'Leads' && $rModule != 'Products' && $rModule != 'Campaigns' && $rModule != 'Projects'
176            )
177         {
178                 $_SESSION['MAILMERGE_SKIP_REL'] = false;
179                 $xtpl->assign("STEP", "2");
180                 $xtpl->assign("SELECTED_OBJECTS", $selected_objects);
181                 $_SESSION['SELECTED_OBJECTS_DEF'] = $selected_objects;
182         }
183         else
184         {
185                 $_SESSION['MAILMERGE_SKIP_REL'] = true;
186                 $xtpl->assign("STEP", "2");
187                 $_SESSION['SELECTED_OBJECTS_DEF'] = $selected_objects;
188         }
189     }
190     else
191     {
192         $xtpl->assign("STEP", "2");
193     }
194
195 }
196 else
197 {
198         $xtpl->assign("STEP", "2");
199 }
200 $modules = $module_list;
201 $func = "";
202 if($rModule == 'Campaigns'){
203         $func = "disableModuleDropDown();";
204 }
205 $xtpl->assign("MAILMERGE_DISABLE_DROP_DOWN", $func);
206 $xtpl->assign("MAILMERGE_MODULE_OPTIONS", get_select_options_with_id($modules, $_SESSION['MAILMERGE_MODULE']));
207 $xtpl->assign("MAILMERGE_TEMPLATES", get_select_options_with_id(getDocumentRevisions(), '0'));
208
209 if(isset($_SESSION['MAILMERGE_MODULE'])){
210         $module_select_text = $mod_strings['LBL_MAILMERGE_SELECTED_MODULE'];
211         $xtpl->assign("MAILMERGE_NUM_SELECTED_OBJECTS",count($_SESSION['MAILMERGE_RECORD'])." ".$_SESSION['MAILMERGE_MODULE']." Selected");
212 }
213 else{
214         $module_select_text = $mod_strings['LBL_MAILMERGE_MODULE'];
215 }
216 $xtpl->assign("MODULE_SELECT", $module_select_text);
217 if($_SESSION['MAILMERGE_MODULE'] == 'Campaigns'){
218     $_SESSION['MAILMERGE_MODULE'] = 'CampaignProspects';
219 }
220
221 $admin = new Administration();
222 $admin->retrieveSettings();
223 $user_merge = $current_user->getPreference('mailmerge_on');
224 if ($user_merge != 'on' || !isset($admin->settings['system_mailmerge_on']) || !$admin->settings['system_mailmerge_on']){
225         $xtpl->assign("ADDIN_NOTICE", $mod_strings['LBL_ADDIN_NOTICE']);
226         $xtpl->assign("DISABLE_NEXT_BUTTON", "disabled");
227 }
228
229
230 $xtpl->parse("main");
231 $xtpl->out("main");
232
233 /*function get_user_module_list($user){
234         global $app_list_strings, $current_language;
235         $app_list_strings = return_app_list_strings_language($current_language);
236         $modules = query_module_access_list($user);
237         global $modInvisList;
238
239         return $modules;
240 }*/
241
242 function getDocumentRevisions()
243 {
244         $document = new Document();
245
246         $currentDate = $document->db->now();
247         $empty_date = $document->db->emptyValue("date");
248
249                         $query = "SELECT revision, document_name, document_revisions.id FROM document_revisions
250 LEFT JOIN documents on documents.id = document_revisions.document_id
251 WHERE ((active_date <= $currentDate AND exp_date > $currentDate)
252         OR (active_date is NULL) or (active_date = ".$empty_date.")
253         OR (active_date <= $currentDate AND ((exp_date = $empty_date OR (exp_date is NULL)))))
254 AND is_template = 1 AND template_type = 'mailmerge' AND documents.deleted = 0 ORDER BY document_name";
255
256                         $result = $document->db->query($query,true,"Error retrieving $document->object_name list: ");
257
258                         $list = Array();
259                         $list['None'] = 'None';
260                         while(($row = $document->db->fetchByAssoc($result)) != null)
261                             {
262                                 $revision = null;
263                                 $docName = $row['document_name'];
264                                 $revision = $row['revision'];
265                                 if(!empty($revision));
266                                 {
267                                         $docName .= " (rev. ".$revision.")";
268                                 }
269                                 $list[$row['id']] = $docName;
270                             }
271                         return $list;
272
273 }
274 ?>