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-2013 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 ********************************************************************************/
38 /*********************************************************************************
40 * Description: base form for account
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
46 class AccountFormBase{
49 function checkForDuplicates($prefix){
50 require_once('include/formbase.php');
52 $focus = new Account();
54 $baseQuery = 'select id, name, website, billing_address_city from accounts where deleted!=1 and ';
55 if(!empty($_POST[$prefix.'name'])){
56 $query = $baseQuery ." name like '".$_POST[$prefix.'name']."%'";
59 if(!empty($_POST[$prefix.'billing_address_city']) || !empty($_POST[$prefix.'shipping_address_city'])){
62 if(!empty($_POST[$prefix.'billing_address_city'])){
63 if(empty($temp_query)){
64 $temp_query = " billing_address_city like '".$_POST[$prefix.'billing_address_city']."%'";
66 $temp_query .= "or billing_address_city like '".$_POST[$prefix.'billing_address_city']."%'";
69 if(!empty($_POST[$prefix.'shipping_address_city'])){
70 if(empty($temp_query)){
71 $temp_query = " shipping_address_city like '".$_POST[$prefix.'shipping_address_city']."%'";
73 $temp_query .= "or shipping_address_city like '".$_POST[$prefix.'shipping_address_city']."%'";
81 $query .= ' ('. $temp_query . ' ) ';
88 $result = $db->query($query);
90 while(($row=$db->fetchByAssoc($result)) != null) {
94 if ($i==-1) return null;
102 function buildTableForm($rows, $mod='Accounts'){
103 if(!ACLController::checkAccess('Accounts', 'edit', true)){
108 global $current_language;
109 $mod_strings = return_module_language($current_language, $mod);
110 }else global $mod_strings;
112 $cols = sizeof($rows[0]) * 2 + 1;
113 if ($action != 'ShowDuplicates')
115 $form = "<form action='index.php' method='post' id='dupAccounts' name='dupAccounts'><input type='hidden' name='selectedAccount' value=''>";
116 $form .= '<table width="100%"><tr><td>'.$mod_strings['MSG_DUPLICATE']. '</td></tr><tr><td height="20"></td></tr></table>';
117 unset($_POST['selectedAccount']);
121 $form = '<table width="100%"><tr><td>'.$mod_strings['MSG_SHOW_DUPLICATES']. '</td></tr><tr><td height="20"></td></tr></table>';
124 if(isset($_POST['return_action']) && $_POST['return_action'] == 'SubPanelViewer') {
125 $_POST['return_action'] = 'DetailView';
128 if(isset($_POST['return_action']) && $_POST['return_action'] == 'DetailView' && empty($_REQUEST['return_id'])) {
129 unset($_POST['return_action']);
132 $form .= "<table width='100%' cellpadding='0' cellspacing='0' class='list view' border='0'><tr class='pagination'><td colspan='$cols'><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td>";
134 if ($action == 'ShowDuplicates') {
135 $return_action = 'ListView'; // cn: bug 6658 - hardcoded return action break popup -> create -> duplicate -> cancel
136 $return_action = (isset($_REQUEST['return_action']) && !empty($_REQUEST['return_action'])) ? $_REQUEST['return_action'] : $return_action;
137 $form .= "<input type='hidden' name='selectedAccount' id='selectedAccount' value=''><input title='${app_strings['LBL_SAVE_BUTTON_TITLE']}' accessKey='${app_strings['LBL_SAVE_BUTTON_KEY']}' class='button' onclick=\"this.form.action.value='Save';\" type='submit' name='button' value=' ${app_strings['LBL_SAVE_BUTTON_LABEL']} '>\n";
139 if (!empty($_REQUEST['return_module']) && !empty($_REQUEST['return_action']) && !empty($_REQUEST['return_id']))
140 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' accessKey='${app_strings['LBL_CANCEL_BUTTON_KEY']}' class='button' onclick=\"this.form.module.value='".$_REQUEST['return_module']."';this.form.action.value='".$_REQUEST['return_action']."';this.form.record.value='".$_REQUEST['return_id']."'\" type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
141 else if (!empty($_POST['return_module']) && !empty($_POST['return_action']))
142 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' accessKey='${app_strings['LBL_CANCEL_BUTTON_KEY']}' class='button' onclick=\"this.form.module.value='".$_POST['return_module']."';this.form.action.value='". $_POST['return_action']."';\" type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
144 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' accessKey='${app_strings['LBL_CANCEL_BUTTON_KEY']}' class='button' onclick=\"this.form.action.value='ListView';\" type='submit' type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
146 $form .= "<input type='submit' class='button' name='ContinueAccount' value='${mod_strings['LNK_NEW_ACCOUNT']}'>\n";
148 $form .= "</td></tr></table></td></tr><tr>";
149 if ($action != 'ShowDuplicates')
151 $form .= "<th> </th>";
153 require_once('include/formbase.php');
155 $form .= getPostToForm();
157 foreach ($rows[0] as $key=>$value){
160 $form .= "<th>". $mod_strings[$mod_strings['db_'.$key]]. "</th>";
166 $rowColor = 'oddListRowS1';
167 foreach($rows as $row){
169 $form .= "<tr class='$rowColor'>";
170 if ($action != 'ShowDuplicates')
172 $form .= "<td width='1%' nowrap><a href='javascript:void(0)' onclick='document.dupAccounts.selectedAccount.value=\"${row['id']}\"; document.dupAccounts.submit(); '>[${app_strings['LBL_SELECT_BUTTON_LABEL']}]</a> </td>\n";
174 foreach ($row as $key=>$value){
176 if(isset($_POST['popup']) && $_POST['popup']==true){
177 $form .= "<td scope='row'><a href='javascript:void(0)' onclick=\"window.opener.location='index.php?module=Accounts&action=DetailView&record=${row['id']}'\">$value</a></td>\n";
180 $form .= "<td><a target='_blank' href='index.php?module=Accounts&action=DetailView&record=${row['id']}'>$value</a></td>\n";
184 if($rowColor == 'evenListRowS1'){
185 $rowColor = 'oddListRowS1';
187 $rowColor = 'evenListRowS1';
191 $form .= "<tr class='pagination'><td colspan='$cols'><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td>";
194 if ($action == 'ShowDuplicates') {
195 $return_action = 'ListView'; // cn: bug 6658 - hardcoded return action break popup -> create -> duplicate -> cancel
196 $return_action = (isset($_REQUEST['return_action']) && !empty($_REQUEST['return_action'])) ? $_REQUEST['return_action'] : $return_action;
197 $form .= "<input type='hidden' name='selectedAccount' id='selectedAccount' value=''><input title='${app_strings['LBL_SAVE_BUTTON_TITLE']}' class='button' onclick=\"this.form.action.value='Save';\" type='submit' name='button' value=' ${app_strings['LBL_SAVE_BUTTON_LABEL']} '>\n";
199 if (!empty($_REQUEST['return_module']) && !empty($_REQUEST['return_action']) && !empty($_REQUEST['return_id']))
200 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' class='button' onclick=\"this.form.module.value='".$_REQUEST['return_module']."';this.form.action.value='".$_REQUEST['return_action']."';this.form.record.value='".$_REQUEST['return_id']."'\" type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
201 else if (!empty($_POST['return_module']) && !empty($_POST['return_action']))
202 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' class='button' onclick=\"this.form.module.value='".$_POST['return_module']."';this.form.action.value='". $_POST['return_action']."';\" type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
204 $form .= "<input title='${app_strings['LBL_CANCEL_BUTTON_TITLE']}' class='button' onclick=\"this.form.action.value='ListView';\" type='submit' name='button' value=' ${app_strings['LBL_CANCEL_BUTTON_LABEL']} '>";
206 $form .= "<input type='submit' class='button' name='ContinueAccount' value='${mod_strings['LNK_NEW_ACCOUNT']}'></form>\n";
208 $form .= "</td></tr></table></td></tr></table>";
215 function getForm($prefix, $mod='', $form=''){
216 if(!ACLController::checkAccess('Accounts', 'edit', true)){
220 global $current_language;
221 $mod_strings = return_module_language($current_language, $mod);
222 }else global $mod_strings;
224 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
225 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
226 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
229 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
231 <form name="${prefix}AccountSave" onSubmit="return check_form('${prefix}AccountSave');" method="POST" action="index.php">
232 <input type="hidden" name="${prefix}module" value="Accounts">
233 <input type="hidden" name="${prefix}action" value="Save">
235 $the_form .= $this->getFormBody($prefix, $mod, $prefix."AccountSave");
237 <p><input title="$lbl_save_button_title" accessKey="$lbl_save_button_key" class="button" type="submit" name="button" value=" $lbl_save_button_label " ></p>
241 $the_form .= get_left_form_footer();
242 $the_form .= get_validate_record_js();
248 function getFormBody($prefix,$mod='', $formname=''){
249 if(!ACLController::checkAccess('Accounts', 'edit', true)){
253 $temp_strings = $mod_strings;
255 global $current_language;
256 $mod_strings = return_module_language($current_language, $mod);
259 global $current_user;
261 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
262 $lbl_account_name = $mod_strings['LBL_ACCOUNT_NAME'];
263 $lbl_phone = $mod_strings['LBL_PHONE'];
264 $lbl_website = $mod_strings['LBL_WEBSITE'];
265 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
266 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
267 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
268 $user_id = $current_user->id;
271 <p><input type="hidden" name="record" value="">
272 <input type="hidden" name="email1" value="">
273 <input type="hidden" name="email2" value="">
274 <input type="hidden" name="assigned_user_id" value='${user_id}'>
275 <input type="hidden" name="action" value="Save">
277 $form .= "$lbl_account_name <span class='required'>$lbl_required_symbol</span><br><input name='name' type='text' value=''><br>";
278 $form .= "$lbl_phone<br><input name='phone_office' type='text' value=''><br>";
279 $form .= "$lbl_website<br><input name='website' type='text' value='http://'><br>";
284 $javascript = new javascript();
285 $javascript->setFormName($formname);
286 $javascript->setSugarBean(new Account());
287 $javascript->addRequiredFields($prefix);
288 $form .=$javascript->getScript();
289 $mod_strings = $temp_strings;
295 function getWideFormBody($prefix, $mod='',$formname='', $contact=''){
296 if(!ACLController::checkAccess('Accounts', 'edit', true)){
301 $contact = new Contact();
304 $temp_strings = $mod_strings;
306 global $current_language;
307 $mod_strings = return_module_language($current_language, $mod);
310 global $current_user;
311 $account = new Account();
313 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
314 $lbl_account_name = $mod_strings['LBL_ACCOUNT_NAME'];
315 $lbl_phone = $mod_strings['LBL_PHONE'];
316 $lbl_website = $mod_strings['LBL_WEBSITE'];
317 if (isset($contact->assigned_user_id)) {
318 $user_id=$contact->assigned_user_id;
320 $user_id = $current_user->id;
323 //Retrieve Email address and set email1, email2
324 $sugarEmailAddress = new SugarEmailAddress();
325 $sugarEmailAddress->handleLegacyRetrieve($contact);
326 if(!isset($contact->email1)){
327 $contact->email1 = '';
329 if(!isset($contact->email2)){
330 $contact->email2 = '';
332 if(!isset($contact->email_opt_out)){
333 $contact->email_opt_out = '';
337 if (!empty($contact->description)) {
338 $default_desc=$contact->description;
341 <input type="hidden" name="${prefix}record" value="">
342 <input type="hidden" name="${prefix}phone_fax" value="{$contact->phone_fax}">
343 <input type="hidden" name="${prefix}phone_other" value="{$contact->phone_other}">
344 <input type="hidden" name="${prefix}email1" value="{$contact->email1}">
345 <input type="hidden" name="${prefix}email2" value="{$contact->email2}">
346 <input type='hidden' name='${prefix}billing_address_street' value='{$contact->primary_address_street}'><input type='hidden' name='${prefix}billing_address_city' value='{$contact->primary_address_city}'><input type='hidden' name='${prefix}billing_address_state' value='{$contact->primary_address_state}'><input type='hidden' name='${prefix}billing_address_postalcode' value='{$contact->primary_address_postalcode}'><input type='hidden' name='${prefix}billing_address_country' value='{$contact->primary_address_country}'>
347 <input type='hidden' name='${prefix}shipping_address_street' value='{$contact->alt_address_street}'><input type='hidden' name='${prefix}shipping_address_city' value='{$contact->alt_address_city}'><input type='hidden' name='${prefix}shipping_address_state' value='{$contact->alt_address_state}'><input type='hidden' name='${prefix}shipping_address_postalcode' value='{$contact->alt_address_postalcode}'><input type='hidden' name='${prefix}shipping_address_country' value='{$contact->alt_address_country}'>
348 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
349 <input type='hidden' name='${prefix}do_not_call' value='{$contact->do_not_call}'>
350 <input type='hidden' name='${prefix}email_opt_out' value='{$contact->email_opt_out}'>
351 <table width='100%' border="0" cellpadding="0" cellspacing="0">
353 <td width="20%" nowrap scope="row">$lbl_account_name <span class="required">$lbl_required_symbol</span></td>
354 <TD width="80%" nowrap scope="row">{$mod_strings['LBL_DESCRIPTION']}</TD>
357 <td nowrap ><input name='{$prefix}name' type="text" value="$contact->account_name"></td>
358 <TD rowspan="5" ><textarea name='{$prefix}description' rows='6' cols='50' >$default_desc</textarea></TD>
361 <td nowrap scope="row">$lbl_phone</td>
364 <td nowrap ><input name='{$prefix}phone_office' type="text" value="$contact->phone_work"></td>
367 <td nowrap scope="row">$lbl_website</td>
370 <td nowrap ><input name='{$prefix}website' type="text" value="http://"></td>
373 //carry forward custom lead fields common to accounts during Lead Conversion
374 $tempAccount = new Account();
375 if (method_exists($contact, 'convertCustomFieldsForm')) $contact->convertCustomFieldsForm($form, $tempAccount, $prefix);
382 $javascript = new javascript();
383 $javascript->setFormName($formname);
384 $javascript->setSugarBean(new Account());
385 $javascript->addRequiredFields($prefix);
386 $form .=$javascript->getScript();
387 $mod_strings = $temp_strings;
392 function handleSave($prefix,$redirect=true, $useRequired=false){
395 require_once('include/formbase.php');
397 $focus = new Account();
399 if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))){
402 $focus = populateFromPost($prefix, $focus);
404 if (isset($GLOBALS['check_notify'])) {
405 $check_notify = $GLOBALS['check_notify'];
408 $check_notify = FALSE;
411 if (empty($_POST['record']) && empty($_POST['dup_checked'])) {
412 $duplicateAccounts = $this->checkForDuplicates($prefix);
413 if(isset($duplicateAccounts)){
414 $location='module=Accounts&action=ShowDuplicates';
417 // Bug 25311 - Add special handling for when the form specifies many-to-many relationships
418 if(isset($_POST['relate_to']) && !empty($_POST['relate_to'])) {
419 $get .= '&Accountsrelate_to='.$_POST['relate_to'];
421 if(isset($_POST['relate_id']) && !empty($_POST['relate_id'])) {
422 $get .= '&Accountsrelate_id='.$_POST['relate_id'];
425 //add all of the post fields to redirect get string
426 foreach ($focus->column_fields as $field)
428 if (!empty($focus->$field) && !is_object($focus->$field))
430 $get .= "&Accounts$field=".urlencode($focus->$field);
434 foreach ($focus->additional_column_fields as $field)
436 if (!empty($focus->$field))
438 $get .= "&Accounts$field=".urlencode($focus->$field);
443 if($focus->hasCustomFields()) {
444 foreach($focus->field_defs as $name=>$field) {
445 if (!empty($field['source']) && $field['source'] == 'custom_fields')
447 $get .= "&Accounts$name=".urlencode($focus->$name);
454 $emailAddress = new SugarEmailAddress();
455 $get .= $emailAddress->getFormBaseURL($focus);
459 //create list of suspected duplicate account id's in redirect get string
461 foreach ($duplicateAccounts as $account)
463 $get .= "&duplicate[$i]=".$account['id'];
467 //add return_module, return_action, and return_id to redirect get string
468 $get .= '&return_module=';
469 if(!empty($_POST['return_module'])) $get .= $_POST['return_module'];
470 else $get .= 'Accounts';
471 $get .= '&return_action=';
472 if(!empty($_POST['return_action'])) $get .= $_POST['return_action'];
473 //else $get .= 'DetailView';
474 if(!empty($_POST['return_id'])) $get .= '&return_id='.$_POST['return_id'];
475 if(!empty($_POST['popup'])) $get .= '&popup='.$_POST['popup'];
476 if(!empty($_POST['create'])) $get .= '&create='.$_POST['create'];
478 $_SESSION['SHOW_DUPLICATES'] = $get;
479 //now redirect the post to modules/Accounts/ShowDuplicates.php
480 if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1')
483 $json = getJSONobj();
484 echo $json->encode(array('status' => 'dupe', 'get' => $location));
486 else if(!empty($_REQUEST['ajax_load']))
488 echo "<script>SUGAR.ajaxUI.loadContent('index.php?$location');</script>";
491 if(!empty($_POST['to_pdf']))
492 $location .= '&to_pdf='.$_POST['to_pdf'];
493 header("Location: index.php?$location");
498 if(!$focus->ACLAccess('Save')){
499 ACLController::displayNoAccess(true);
503 $focus->save($check_notify);
504 $return_id = $focus->id;
506 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
509 if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1') {
510 $json = getJSONobj();
511 echo $json->encode(array('status' => 'success',
513 $trackerManager = TrackerManager::getInstance();
514 $timeStamp = TimeDate::getInstance()->nowDb();
515 if($monitor = $trackerManager->getMonitor('tracker')){
516 $monitor->setValue('action', 'detailview');
517 $monitor->setValue('user_id', $GLOBALS['current_user']->id);
518 $monitor->setValue('module_name', 'Accounts');
519 $monitor->setValue('date_modified', $timeStamp);
520 $monitor->setValue('visible', 1);
522 if (!empty($this->bean->id)) {
523 $monitor->setValue('item_id', $return_id);
524 $monitor->setValue('item_summary', $focus->get_summary_text());
526 $trackerManager->saveMonitor($monitor, true, true);
531 if(isset($_POST['popup']) && $_POST['popup'] == 'true') {
533 if(!empty($_POST['return_module'])) $get .= $_POST['return_module'];
534 else $get .= 'Accounts';
536 if(!empty($_POST['return_action'])) $get .= $_POST['return_action'];
537 else $get .= 'Popup';
538 if(!empty($_POST['return_id'])) $get .= '&return_id='.$_POST['return_id'];
539 if(!empty($_POST['popup'])) $get .= '&popup='.$_POST['popup'];
540 if(!empty($_POST['create'])) $get .= '&create='.$_POST['create'];
541 if(!empty($_POST['to_pdf'])) $get .= '&to_pdf='.$_POST['to_pdf'];
542 $get .= '&name=' . $focus->name;
543 $get .= '&query=true';
544 header("Location: index.php?$get");
548 handleRedirect($return_id,'Accounts');