]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MailMerge/Step3.php
Release 6.5.1
[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-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 require_once ('include/JSON.php');
40 require_once('modules/MailMerge/modules_array.php');
41 require_once('modules/MailMerge/merge_query.php');
42
43 global $app_strings;
44 global $app_list_strings;
45 global $mod_strings;
46 global $current_user;
47 global $odd_bg;
48 global $even_bg;
49 global $sugar_version, $sugar_config;
50 global $locale;
51
52 $xtpl = new XTemplate('modules/MailMerge/Step3.html');
53 $xtpl->assign("MOD", $mod_strings);
54 $xtpl->assign("APP", $app_strings);
55
56 if(!isset($_SESSION['MAILMERGE_MODULE']))
57 {
58         if(isset($_POST['mailmerge_module']))
59         {
60                 $_SESSION['MAILMERGE_MODULE'] = $_POST['mailmerge_module'];
61         }
62 }
63
64 if(isset($_POST['contains_contact_info'])){
65
66         $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'] = $_POST['contains_contact_info'];
67
68 }
69
70 if(!isset($_SESSION["MAILMERGE_DOCUMENT_ID"]))
71 {
72         if(!empty($_POST['document_id']))
73         {
74                 $_SESSION['MAILMERGE_DOCUMENT_ID'] = $_POST['document_id'];
75         }
76 }
77 $document_id = $_SESSION["MAILMERGE_DOCUMENT_ID"];
78 $document = new Document();
79 $document->retrieve($document_id);
80 $_SESSION["MAILMERGE_TEMPLATE"] = $document->document_name;
81
82 if(!empty($_POST['selected_objects']))
83 {
84         $selObjs = urldecode($_POST['selected_objects']);
85         $_SESSION['SELECTED_OBJECTS_DEF'] = $selObjs;
86 }
87 else
88 {
89         $selObjs = $_SESSION['SELECTED_OBJECTS_DEF'];
90 }
91 $sel_obj = array();
92 parse_str(html_entity_decode($selObjs, ENT_QUOTES),$sel_obj);
93 $step_num = 3;
94 $xtpl->assign("PREV_STEP", '2');
95 $xtpl->assign("STEP_NUM", "Step 3:");
96 $popup_request_data = array ('call_back_function' => 'set_return', 'form_name' => 'EditView', 'field_to_name_array' => array ('id' => 'rel_id', 'name' => 'rel_name',),);
97         $json = getJSONobj();
98
99         // must urlencode to put into the filter request string
100         // because IE gets an out of memory error when it is passed
101         // as the usual object literal
102 $encoded_popup_request_data = urlencode($json->encode($popup_request_data));
103
104 $modules = $modules_array;
105
106
107 $xtpl->assign("MAILMERGE_MODULE_OPTIONS", get_select_options_with_id($modules, '0'));
108 $change_parent_button = "<input title='".$app_strings['LBL_SELECT_BUTTON_TITLE']."' tabindex='2' 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, {});' />";
109
110 $change_parent_button = "<input title='".$app_strings['LBL_SELECT_BUTTON_TITLE']."' tabindex='2' 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, {});' />";
111 $xtpl->assign("CHANGE_PARENT_BUTTON", $change_parent_button);
112
113 $relModule = $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'];
114 $xtpl->assign("STEP3_HEADER", "Set ".get_singular_bean_name($relModule)." Association");
115
116
117 $select = "Select id, name from contacts";
118 global $beanList, $beanFiles;
119 $class_name = $beanList[$relModule];
120 require_once($beanFiles[$class_name]);
121 $seed = new $class_name();
122
123 if(isset($_SESSION['MAILMERGE_SKIP_REL']) && $_SESSION['MAILMERGE_SKIP_REL'])
124 {
125         $disabled = 'disabled';
126 }
127 else
128 {
129         $disabled = '';
130 }
131 $oddRow = true;
132
133
134 foreach($sel_obj as $key => $value)
135 {
136         $value = str_replace("##", "&", $value);
137         $value = stripslashes($value);
138         $code = md5($key);
139         $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,),);
140         $encoded_popup_request_data = urlencode($json->encode($popup_request_data));
141
142         $fullQuery = get_merge_query($seed, $_SESSION['MAILMERGE_MODULE'], $key);
143         $result = $seed->db->limitQuery($fullQuery, 0, 1, true, "Error performing limit query");
144
145         $full_name = '';
146         $contact_id = '';
147         if($row = $seed->db->fetchByAssoc($result, 0)) {
148                         if($relModule == "Contacts") {
149                             $full_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name']);
150                         } else {
151                                 $full_name = $row['name'];
152                         }
153                         $contact_id = $row['id'];
154         }
155         $umodule =urlencode($_SESSION['MAILMERGE_MODULE']);
156         $ukey = urlencode($key);
157         $change_parent_button = "<input title='{$app_strings['LBL_SELECT_BUTTON_TITLE']}' tabindex='2'  type='button' class='button' value='{$app_strings['LBL_SELECT_BUTTON_LABEL']}'
158                 name='button' onclick='open_popup(document.EditView.rel_type_{$code}.value, 600, 400,
159                         \"&html=mail_merge&rel_module=$umodule&id=$ukey&request_data=$encoded_popup_request_data\", true, false, {});' $disabled/>";
160         $items = array(
161         'ID' => $key,
162         'NAME' => $value,
163         'CODE' => $code,
164         'TYPE_OPTIONS' => get_select_options_with_id($modules, '0'),
165         'CHANGE_RELATIONSHIP' => $change_parent_button,
166         'CONTACT_ID' => $contact_id,
167         'CONTACT_NAME' => $full_name,
168         'REL_MODULE' => $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'],
169         );
170
171         $xtpl->assign("MAILMERGE", $items);
172
173         if($oddRow)
174         {
175         //todo move to themes
176                 $xtpl->assign("ROW_COLOR", 'oddListRow');
177                 $xtpl->assign("BG_COLOR", $odd_bg);
178     }
179     else
180     {
181         //todo move to themes
182                 $xtpl->assign("ROW_COLOR", 'evenListRow');
183                 $xtpl->assign("BG_COLOR", $even_bg);
184     }
185         $oddRow = !$oddRow;
186         $xtpl->parse("main.items.row");
187 }
188 $xtpl->parse("main.items");
189
190
191 $xtpl->parse("main");
192 $xtpl->out("main");
193
194
195 function generateSelect($seed, $relModule){
196         $lowerRelModule = strtolower($relModule);
197         if($seed->load_relationship($lowerRelModule)){
198                 $params = array();
199                 $params['join_table_alias'] = 'r1';
200                 $params['join_table_link_alias'] = 'r2';
201                 $params['join_type'] = 'LEFT JOIN';
202                 $join = $seed->$lowerRelModule->getJoin($params);
203                 $select = "SELECT {$seed->table_name}.* FROM {$seed->table_name} ".$join;
204                 return $select;
205         }
206         return "";
207 }
208
209 ?>