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.
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 * Created on Oct 4, 2005
41 * To change the template for this generated file go to
42 * Window - Preferences - PHPeclipse - PHP - Code Templates
50 require_once ('include/JSON.php');
51 require_once('modules/MailMerge/modules_array.php');
57 global $app_list_strings;
62 global $sugar_version, $sugar_config;
65 $xtpl = new XTemplate('modules/MailMerge/Step3.html');
66 $xtpl->assign("MOD", $mod_strings);
67 $xtpl->assign("APP", $app_strings);
69 if(!isset($_SESSION['MAILMERGE_MODULE']))
71 if(isset($_POST['mailmerge_module']))
73 $_SESSION['MAILMERGE_MODULE'] = $_POST['mailmerge_module'];
77 if(isset($_POST['contains_contact_info'])){
79 $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'] = $_POST['contains_contact_info'];
83 if(!isset($_SESSION["MAILMERGE_DOCUMENT_ID"]))
85 if(!empty($_POST['document_id']))
87 $_SESSION['MAILMERGE_DOCUMENT_ID'] = $_POST['document_id'];
90 $document_id = $_SESSION["MAILMERGE_DOCUMENT_ID"];
91 $document = new Document();
92 $document->retrieve($document_id);
93 $_SESSION["MAILMERGE_TEMPLATE"] = $document->document_name;
95 if(!empty($_POST['selected_objects']))
97 $selObjs = urldecode($_POST['selected_objects']);
98 $_SESSION['SELECTED_OBJECTS_DEF'] = $selObjs;
102 $selObjs = $_SESSION['SELECTED_OBJECTS_DEF'];
105 parse_str(html_entity_decode($selObjs, ENT_QUOTES),$sel_obj);
107 if(isset($_SESSION['MAILMERGE_RECORD']))
109 $xtpl->assign("PREV_STEP", '2');
111 //$xtpl->assign("RECORD", $_SESSION['MAILMERGE_RECORD']);
115 $xtpl->assign("PREV_STEP", '2');
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();
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));
126 $modules = $modules_array;
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, {});' />";
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);
135 $relModule = $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'];
136 $xtpl->assign("STEP3_HEADER", "Set ".get_singular_bean_name($relModule)." Association");
139 $select = "Select id, name from contacts";
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)',
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)',
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)'),
151 $whereQuery = array('Contacts' => array('Accounts' => 'accounts_contacts.contact_id = contacts.id AND accounts_contacts.account_id = ',
153 'Opportunities' => 'opportunities_contacts.contact_id = contacts.id AND opportunities_contacts.opportunity_id = ',
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 = '),
164 $contact = new Contact();
167 global $beanList, $beanFiles;
168 $class_name = $beanList[$relModule ];
169 require_once($beanFiles[$class_name]);
170 $seed = new $class_name();
172 if(isset($_SESSION['MAILMERGE_SKIP_REL']) && $_SESSION['MAILMERGE_SKIP_REL'])
174 $disabled = 'disabled';
183 foreach($sel_obj as $key => $value)
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));
191 if(empty($selQuery[$relModule][$_SESSION['MAILMERGE_MODULE']])){
192 $select = generateSelect($seed, $relModule);
194 $select = $selQuery[$relModule][$_SESSION['MAILMERGE_MODULE']];
196 if(empty($whereQuery[$relModule][$_SESSION['MAILMERGE_MODULE']])){
197 $where = "{$seed->table_name}.id = ";
199 $where = $whereQuery[$relModule][$_SESSION['MAILMERGE_MODULE']];
202 if($relModule == "Contacts"){
203 $limitSelect = str_replace('contacts.*', 'contacts.first_name, contacts.last_name, contacts.id, contacts.date_entered', $select);
206 $limitSelect = str_replace(strtolower($relModule).'.*', strtolower($relModule).'.name, '.strtolower($relModule).'.date_entered', $select);
208 $fullQuery = $limitSelect." WHERE ".$where."'".$key."' ORDER BY date_entered";
210 $result = $seed->db->limitQuery($fullQuery, 0, 1, true, "Error performing limit query");
213 if($row = $seed->db->fetchByAssoc($result, 0)) {
214 if($relModule == "Contacts"){
215 $full_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name']);
218 $full_name = $row['name'];
220 $contact_id = $row['id'];
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/>";
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'],
234 $xtpl->assign("MAILMERGE", $items);
238 //todo move to themes
239 $xtpl->assign("ROW_COLOR", 'oddListRow');
240 $xtpl->assign("BG_COLOR", $odd_bg);
244 //todo move to themes
245 $xtpl->assign("ROW_COLOR", 'evenListRow');
246 $xtpl->assign("BG_COLOR", $even_bg);
249 $xtpl->parse("main.items.row");
251 $xtpl->parse("main.items");
254 $xtpl->parse("main");
258 function generateSelect($seed, $relModule){
259 $lowerRelModule = strtolower($relModule);
260 if($seed->load_relationship($lowerRelModule)){
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;