]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MailMerge/Step3.php
Release 6.4.0
[Github/sugarcrm.git] / modules / MailMerge / Step3.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
49
50 require_once ('include/JSON.php');
51 require_once('modules/MailMerge/modules_array.php');
52
53
54
55
56 global $app_strings;
57 global $app_list_strings;
58 global $mod_strings;
59 global $current_user;
60 global $odd_bg;
61 global $even_bg;
62 global $sugar_version, $sugar_config;
63 global $locale;
64
65 $xtpl = new XTemplate('modules/MailMerge/Step3.html');
66 $xtpl->assign("MOD", $mod_strings);
67 $xtpl->assign("APP", $app_strings);
68
69 if(!isset($_SESSION['MAILMERGE_MODULE']))
70 {
71         if(isset($_POST['mailmerge_module']))
72         {
73                 $_SESSION['MAILMERGE_MODULE'] = $_POST['mailmerge_module'];
74         }       
75 }
76
77 if(isset($_POST['contains_contact_info'])){
78
79         $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'] = $_POST['contains_contact_info']; 
80
81 }
82
83 if(!isset($_SESSION["MAILMERGE_DOCUMENT_ID"]))
84 {
85         if(!empty($_POST['document_id']))
86         {
87                 $_SESSION['MAILMERGE_DOCUMENT_ID'] = $_POST['document_id'];
88         }
89 }
90 $document_id = $_SESSION["MAILMERGE_DOCUMENT_ID"];
91 $document = new Document();
92 $document->retrieve($document_id);
93 $_SESSION["MAILMERGE_TEMPLATE"] = $document->document_name;     
94
95 if(!empty($_POST['selected_objects']))
96 {
97         $selObjs = urldecode($_POST['selected_objects']);
98         $_SESSION['SELECTED_OBJECTS_DEF'] = $selObjs;
99 }
100 else
101 {
102         $selObjs = $_SESSION['SELECTED_OBJECTS_DEF'];
103 }
104 $sel_obj = array();
105 parse_str(html_entity_decode($selObjs, ENT_QUOTES),$sel_obj);
106 $step_num = 3;
107 if(isset($_SESSION['MAILMERGE_RECORD']))
108 {
109         $xtpl->assign("PREV_STEP", '2');
110         $step_num = 3;
111         //$xtpl->assign("RECORD", $_SESSION['MAILMERGE_RECORD']);       
112 }
113 else
114 {
115         $xtpl->assign("PREV_STEP", '2');
116 }
117 $xtpl->assign("STEP_NUM", "Step ".$step_num.":");
118 $popup_request_data = array ('call_back_function' => 'set_return', 'form_name' => 'EditView', 'field_to_name_array' => array ('id' => 'rel_id', 'name' => 'rel_name',),);
119         $json = getJSONobj();
120
121         // must urlencode to put into the filter request string
122         // because IE gets an out of memory error when it is passed
123         // as the usual object literal
124 $encoded_popup_request_data = urlencode($json->encode($popup_request_data));
125
126 $modules = $modules_array;
127
128
129 $xtpl->assign("MAILMERGE_MODULE_OPTIONS", get_select_options_with_id($modules, '0'));
130 $change_parent_button = "<input title='".$app_strings['LBL_SELECT_BUTTON_TITLE']."' tabindex='2' accessKey='".$app_strings['LBL_SELECT_BUTTON_KEY']."' type='button' class='button' value='".$app_strings['LBL_SELECT_BUTTON_LABEL']."' name='button' onclick='open_popup(document.EditView.rel_type.value, 600, 400, \"&request_data=$encoded_popup_request_data\", true, false, {});' />";
131
132 $change_parent_button = "<input title='".$app_strings['LBL_SELECT_BUTTON_TITLE']."' tabindex='2' accessKey='".$app_strings['LBL_SELECT_BUTTON_KEY']."' type='button' class='button' value='".$app_strings['LBL_SELECT_BUTTON_LABEL']."' name='button' onclick='open_popup(document.EditView.parent_type.value, 600, 400, \"&request_data=$encoded_popup_request_data\", true, false, {});' />";
133 $xtpl->assign("CHANGE_PARENT_BUTTON", $change_parent_button);
134
135 $relModule = $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'];
136 $xtpl->assign("STEP3_HEADER", "Set ".get_singular_bean_name($relModule)." Association");
137
138
139 $select = "Select id, name from contacts";
140
141 $selQuery = array ('Contacts'=>array('Accounts' => 'SELECT contacts.* FROM contacts LEFT JOIN accounts_contacts ON contacts.id=accounts_contacts.contact_id AND (accounts_contacts.deleted is NULL or accounts_contacts.deleted=0)',
142 'Contacts' => '',
143 'Opportunities' => 'SELECT contacts.* FROM contacts LEFT JOIN opportunities_contacts ON contacts.id=opportunities_contacts.contact_id AND (opportunities_contacts.deleted is NULL or opportunities_contacts.deleted=0)',
144 'Leads' => '',
145 'Cases' => 'SELECT contacts.* FROM contacts LEFT JOIN contacts_cases ON contacts.id=contacts_cases.contact_id AND (contacts_cases.deleted is NULL or contacts_cases.deleted=0)',
146 'Bugs' => 'SELECT contacts.* FROM contacts LEFT JOIN contacts_bugs ON contacts.id=contacts_bugs.contact_id AND (contacts_bugs.deleted is NULL or contacts_bugs.deleted=0)',
147 'Quotes' => 'SELECT contacts.* FROM contacts LEFT JOIN quotes_contacts ON contacts.id=quotes_contacts.contact_id AND (quotes_contacts.deleted is NULL or quotes_contacts.deleted=0)'),
148 'Opportunities'=>array("Accounts"=>'SELECT opportunities.id, opportunities.name FROM opportunities LEFT JOIN accounts_opportunities ON opportunities.id = accounts_opportunities.opportunity_id AND (accounts_opportunities.deleted is NULL or accounts_opportunities.deleted=0)'),
149 'Accounts'=>array("Opportunities"=>'SELECT accounts.id, accounts.name FROM accounts LEFT JOIN accounts_opportunities ON accounts.id = accounts_opportunities.account_id AND (accounts_opportunities.deleted is NULL or accounts_opportunities.deleted=0)'),
150 );
151 $whereQuery = array('Contacts' => array('Accounts' => 'accounts_contacts.contact_id = contacts.id AND accounts_contacts.account_id = ',
152 'Contacts' => '',
153 'Opportunities' => 'opportunities_contacts.contact_id = contacts.id AND opportunities_contacts.opportunity_id = ',
154 'Leads' => '',
155 'Cases' => 'contacts_cases.contact_id = contacts.id AND contacts_cases.case_id = ',
156 'Bugs' => 'contacts_bugs.contact_id = contacts.id AND contacts_bugs.bug_id = ',
157 'Quotes' => 'quotes_contacts.contact_id = contacts.id AND quotes_contacts.quote_id = '),
158 'Opportunities'=>array('Accounts'=>'accounts_opportunities.opportunity_id = opportunities.id AND accounts_opportunities.account_id = '),
159 'Accounts'=>array('Opportunities'=>'accounts_opportunities.account_id = accounts.id  AND accounts_opportunities.opportunity_id = '),
160 );
161
162
163
164 $contact = new Contact();
165
166
167 global $beanList, $beanFiles;
168 $class_name = $beanList[$relModule ];
169 require_once($beanFiles[$class_name]);
170 $seed = new $class_name();
171
172 if(isset($_SESSION['MAILMERGE_SKIP_REL']) && $_SESSION['MAILMERGE_SKIP_REL'])
173 {
174         $disabled = 'disabled';
175 }
176 else
177 {
178         $disabled = '';
179 }
180 $oddRow = true;
181
182
183 foreach($sel_obj as $key => $value)
184 {
185         $value = str_replace("##", "&", $value);
186         $value = stripslashes($value);
187         $code = str_replace('-', '', $key);
188         $popup_request_data = array ('call_back_function' => 'set_return', 'form_name' => 'EditView', 'field_to_name_array' => array ('id' => 'rel_id_'.$code, 'name' => 'rel_name_'.$code,),);
189         $encoded_popup_request_data = urlencode($json->encode($popup_request_data));
190
191         if(empty($selQuery[$relModule][$_SESSION['MAILMERGE_MODULE']])){
192                 $select = generateSelect($seed, $relModule);
193         }else{
194                 $select = $selQuery[$relModule][$_SESSION['MAILMERGE_MODULE']];
195         }
196         if(empty($whereQuery[$relModule][$_SESSION['MAILMERGE_MODULE']])){
197                 $where = "{$seed->table_name}.id = ";
198         }else{
199                 $where = $whereQuery[$relModule][$_SESSION['MAILMERGE_MODULE']];
200         }
201         
202         if($relModule == "Contacts"){
203         $limitSelect = str_replace('contacts.*', 'contacts.first_name, contacts.last_name, contacts.id, contacts.date_entered', $select);
204         }
205         else{
206                 $limitSelect = str_replace(strtolower($relModule).'.*', strtolower($relModule).'.name, '.strtolower($relModule).'.date_entered', $select);
207         }
208         $fullQuery = $limitSelect." WHERE ".$where."'".$key."' ORDER BY date_entered";
209         
210         $result = $seed->db->limitQuery($fullQuery, 0, 1, true, "Error performing limit query");
211         $full_name = '';
212         $contact_id = '';
213         if($row = $seed->db->fetchByAssoc($result, 0)) {
214                         if($relModule == "Contacts"){
215                         $full_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name']);
216                         }
217                         else{
218                                 $full_name = $row['name'];
219                         }
220                         $contact_id = $row['id'];
221         }
222         $change_parent_button = "<input title='".$app_strings['LBL_SELECT_BUTTON_TITLE']."' tabindex='2' accessKey='".$app_strings['LBL_SELECT_BUTTON_KEY']."' type='button' class='button' value='".$app_strings['LBL_SELECT_BUTTON_LABEL']."' name='button' onclick='open_popup(document.EditView.rel_type_".$code.".value, 600, 400, \"&html=mail_merge&select=$select&where=$where&id=$key&request_data=$encoded_popup_request_data\", true, false, {});' $disabled/>";
223         $items = array(
224         'ID' => $key,
225         'NAME' => $value,
226         'CODE' => $code,
227         'TYPE_OPTIONS' => get_select_options_with_id($modules, '0'),
228         'CHANGE_RELATIONSHIP' => $change_parent_button,
229         'CONTACT_ID' => $contact_id,
230         'CONTACT_NAME' => $full_name,
231         'REL_MODULE' => $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'],
232         );
233
234         $xtpl->assign("MAILMERGE", $items);
235         
236         if($oddRow)
237         {
238         //todo move to themes
239                 $xtpl->assign("ROW_COLOR", 'oddListRow');
240                 $xtpl->assign("BG_COLOR", $odd_bg);
241     }
242     else
243     {
244         //todo move to themes
245                 $xtpl->assign("ROW_COLOR", 'evenListRow');
246                 $xtpl->assign("BG_COLOR", $even_bg);
247     }
248         $oddRow = !$oddRow;
249         $xtpl->parse("main.items.row");
250 }
251 $xtpl->parse("main.items");
252
253
254 $xtpl->parse("main");
255 $xtpl->out("main");
256
257
258 function generateSelect($seed, $relModule){
259         $lowerRelModule = strtolower($relModule);
260         if($seed->load_relationship($lowerRelModule)){
261                 $params = array();
262                 $params['join_table_alias'] = 'r1';
263                 $params['join_table_link_alias'] = 'r2';
264                 $params['join_type'] = 'LEFT JOIN';
265                 $join = $seed->$lowerRelModule->getJoin($params);
266                 $select = "SELECT {$seed->table_name}.* FROM {$seed->table_name} ".$join;
267                 return $select;
268         }
269         return "";
270 }
271
272 ?>