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.
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.
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
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
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.
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.
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 ********************************************************************************/
39 require_once ('include/JSON.php');
40 require_once('modules/MailMerge/modules_array.php');
41 require_once('modules/MailMerge/merge_query.php');
44 global $app_list_strings;
49 global $sugar_version, $sugar_config;
52 $xtpl = new XTemplate('modules/MailMerge/Step3.html');
53 $xtpl->assign("MOD", $mod_strings);
54 $xtpl->assign("APP", $app_strings);
56 if(!isset($_SESSION['MAILMERGE_MODULE']))
58 if(isset($_POST['mailmerge_module']))
60 $_SESSION['MAILMERGE_MODULE'] = $_POST['mailmerge_module'];
64 if(isset($_POST['contains_contact_info'])){
66 $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'] = $_POST['contains_contact_info'];
70 if(!isset($_SESSION["MAILMERGE_DOCUMENT_ID"]))
72 if(!empty($_POST['document_id']))
74 $_SESSION['MAILMERGE_DOCUMENT_ID'] = $_POST['document_id'];
77 $document_id = $_SESSION["MAILMERGE_DOCUMENT_ID"];
78 $document = new Document();
79 $document->retrieve($document_id);
80 $_SESSION["MAILMERGE_TEMPLATE"] = $document->document_name;
82 if(!empty($_POST['selected_objects']))
84 $selObjs = urldecode($_POST['selected_objects']);
85 $_SESSION['SELECTED_OBJECTS_DEF'] = $selObjs;
89 $selObjs = $_SESSION['SELECTED_OBJECTS_DEF'];
92 parse_str(html_entity_decode($selObjs, ENT_QUOTES),$sel_obj);
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',),);
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));
104 $modules = $modules_array;
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, {});' />";
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);
113 $relModule = $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'];
114 $xtpl->assign("STEP3_HEADER", "Set ".get_singular_bean_name($relModule)." Association");
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();
123 if(isset($_SESSION['MAILMERGE_SKIP_REL']) && $_SESSION['MAILMERGE_SKIP_REL'])
125 $disabled = 'disabled';
134 foreach($sel_obj as $key => $value)
136 $value = str_replace("##", "&", $value);
137 $value = stripslashes($value);
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));
142 $fullQuery = get_merge_query($seed, $_SESSION['MAILMERGE_MODULE'], $key);
143 $result = $seed->db->limitQuery($fullQuery, 0, 1, true, "Error performing limit query");
147 if($row = $seed->db->fetchByAssoc($result, 0)) {
148 if($relModule == "Contacts") {
149 $full_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name']);
151 $full_name = $row['name'];
153 $contact_id = $row['id'];
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/>";
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'],
171 $xtpl->assign("MAILMERGE", $items);
175 //todo move to themes
176 $xtpl->assign("ROW_COLOR", 'oddListRow');
177 $xtpl->assign("BG_COLOR", $odd_bg);
181 //todo move to themes
182 $xtpl->assign("ROW_COLOR", 'evenListRow');
183 $xtpl->assign("BG_COLOR", $even_bg);
186 $xtpl->parse("main.items.row");
188 $xtpl->parse("main.items");
191 $xtpl->parse("main");
195 function generateSelect($seed, $relModule){
196 $lowerRelModule = strtolower($relModule);
197 if($seed->load_relationship($lowerRelModule)){
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;