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 ********************************************************************************/
38 require_once("include/EditView/EditView2.php");
39 require_once("include/upload_file.php");
41 class ViewConvertLead extends SugarView
43 protected $fileName = "modules/Leads/metadata/convertdefs.php";
45 public function __construct(
47 $view_object_map = array()
50 parent::SugarView($bean, $view_object_map);
51 $this->medataDataFile = $this->fileName;
52 if (file_exists("custom/$this->fileName"))
54 $this->medataDataFile = "custom/$this->fileName";
58 public function preDisplay()
60 if (!$this->bean->ACLAccess('edit')) {
61 ACLController::displayNoAccess();
67 * @see SugarView::display()
69 public function display()
71 if (!empty($_REQUEST['handle']) && $_REQUEST['handle'] == 'save')
73 return $this->handleSave();
78 // get the EditView defs to check if opportunity_name exists, for a check below for populating data
79 $opportunityNameInLayout = false;
80 $editviewFile = 'modules/Leads/metadata/editviewdefs.php';
81 $this->medataDataFile = $editviewFile;
82 if (file_exists("custom/{$editviewFile}"))
84 $this->medataDataFile = "custom/{$editviewFile}";
86 include($this->medataDataFile);
87 foreach($viewdefs['Leads']['EditView']['panels'] as $panel_index => $section){
88 foreach($section as $row_array){
89 foreach($row_array as $cell){
90 if(isset($cell['name']) && $cell['name'] == 'opportunity_name'){
91 $opportunityNameInLayout = true;
97 $this->medataDataFile = $this->fileName;
98 if (file_exists("custom/$this->fileName"))
100 $this->medataDataFile = "custom/$this->fileName";
104 $this->checkForDuplicates($this->focus);
105 $smarty = new Sugar_Smarty();
106 $ev = new EditView();
108 $ev->view = "ConvertLead";
109 echo $this->getModuleTitle();
111 require_once("include/QuickSearchDefaults.php");
112 $qsd = new QuickSearchDefaults();
113 $qsd->setFormName("ConvertLead");
115 $this->contact = new Contact();
116 $smarty->assign("contact_def", $this->contact->field_defs);
117 $smarty->assign("form_name", "ConvertLead");
118 $smarty->assign("form_id", "ConvertLead");
119 $smarty->assign("module", "Leads");
120 $smarty->assign("view", "convertlead");
121 $smarty->assign("bean", $this->focus);
122 $smarty->assign("record_id", $this->focus->id);
123 $smarty->display("modules/Leads/tpls/ConvertLeadHeader.tpl");
125 echo "<div class='edit view' style='width:auto;'>";
127 foreach($this->defs as $module => $vdef)
129 if(!isset($beanList[$module]))
133 $bean = $beanList[$module];
134 $focus = new $bean();
135 $focus->fill_in_additional_detail_fields();
136 foreach($focus->field_defs as $field => $def)
138 if(isset($vdef[$ev->view]['copyData']) && $vdef[$ev->view]['copyData'])
140 if ($module == "Accounts" && $field == 'name')
142 $focus->name = $this->focus->account_name;
144 else if ($module == "Opportunities" && $field == 'amount')
146 $focus->amount = unformat_number($this->focus->opportunity_amount);
148 else if ($module == "Opportunities" && $field == 'name') {
149 if ($opportunityNameInLayout && !empty($this->focus->opportunity_name)){
150 $focus->name = $this->focus->opportunity_name;
153 else if ($field == "id")
155 //If it is not a contact, don't copy the ID from the lead
156 if ($module == "Contacts") {
157 $focus->$field = $this->focus->$field;
159 } else if (is_a($focus, "Company") && $field == 'phone_office')
161 //Special case where company and person have the same field with a different name
162 $focus->phone_office = $this->focus->phone_work;
164 else if (strpos($field, "billing_address") !== false && $focus->field_defs[$field]["type"] == "varchar") /* Bug 42219 fix */
166 $tmp_field = str_replace("billing_", "primary_", $field);
167 $focus->field_defs[$field]["type"] = "text";
168 if (isset($this->focus->$tmp_field)) {
169 $focus->$field = $this->focus->$tmp_field;
173 else if (strpos($field, "shipping_address") !== false && $focus->field_defs[$field]["type"] == "varchar") /* Bug 42219 fix */
175 $tmp_field = str_replace("shipping_", "primary_", $field);
176 if (isset($this->focus->$tmp_field)) {
177 $focus->$field = $this->focus->$tmp_field;
179 $focus->field_defs[$field]["type"] = "text";
181 else if (isset($this->focus->$field))
183 $focus->$field = $this->focus->$field;
188 //Copy over email data
189 $ev->setup($module, $focus, $this->medataDataFile, "modules/Leads/tpls/ConvertLead.tpl", false);
191 echo($ev->display(false));
192 echo($this->getValidationJS($module, $focus, $vdef[$ev->view]));
195 echo ($qsd->getQSScriptsJSONAlreadyDefined());
196 $smarty->display("modules/Leads/tpls/ConvertLeadFooter.tpl");
199 protected function getRecord()
201 $this->focus = new Lead();
202 if (isset($_REQUEST['record']))
204 $this->focus->retrieve($_REQUEST['record']);
208 protected function loadDefs()
211 include($this->medataDataFile);
212 $this->defs = $viewdefs;
216 * Returns the javascript to enable/disable validation of each module's sub-form
217 * //TODO: This should probably be on the smarty template
218 * @param $module String the target module name.
219 * @param $focus SugarBean instance of the target module.
220 * @param $focus EditView def for the module.
221 * @return String, javascript to echo to page.
223 protected function getValidationJS(
229 $validateSelect = isset($viewdef['required']) && $viewdef['required'] && !empty($viewdef['select']);
231 <script type='text/javascript'>
232 if (!SUGAR.convert) SUGAR.convert = {requiredFields: {}};
233 SUGAR.convert.toggle$module = function(){
235 inputsWithErrors = [];
236 if(!SUGAR.convert.{$module}Enabled)
238 for(var i in SUGAR.convert.requiredFields.$module)
240 addToValidate('ConvertLead', '$module' + i, 'varchar', true, SUGAR.convert.requiredFields.{$module}[i]);
243 if ($validateSelect) {
245 removeFromValidate('ConvertLead', '{$viewdef['select']}');";
249 SUGAR.convert.{$module}Enabled = true;
251 for(var i in SUGAR.convert.requiredFields.$module)
253 removeFromValidate('ConvertLead', '$module' + i);
255 if ($validateSelect) {
257 addToValidate('ConvertLead', '{$viewdef['select']}', 'varchar', true, '"
258 . translate($this->contact->field_defs[$viewdef['select']]['vname']) . "');";
261 SUGAR.convert.{$module}Enabled = false;
263 YAHOO.util.Dom.get('convert_create_{$module}').value = SUGAR.convert.{$module}Enabled;
266 if (isset($viewdef['required']) && $viewdef['required'])
268 if (!empty($viewdef['select']) && (empty($viewdef['default_action']) || $viewdef['default_action'] != 'create'))
271 SUGAR.convert.{$module}Enabled = true;";
274 YAHOO.util.Event.onDOMReady(SUGAR.convert.toggle$module);";
275 } else if (isset($viewdef['default_action']) && $viewdef['default_action'] == "create")
277 $jsOut .= "\n SUGAR.convert.{$module}Enabled = true;";
280 YAHOO.util.Event.addListener('new$module', 'click', SUGAR.convert.toggle$module);
281 SUGAR.convert.requiredFields.$module = {};";
282 foreach($focus->field_defs as $field => $def)
284 if (isset($def['required']) && $def['required'])
287 SUGAR.convert.requiredFields.$module.$field = '". translate($def['vname'], $module) . "';\n";
298 * Saves a new Contact as well as any related items passed in.
302 protected function handleSave()
304 require_once('modules/Campaigns/utils.php');
305 require_once("include/formbase.php");
307 if (!empty($_REQUEST['record']))
310 $lead->retrieve($_REQUEST['record']);
316 $selectedBeans = array();
318 //Make sure the contact object is availible for relationships.
319 $beans['Contacts'] = new Contact();
320 $beans['Contacts']->id = create_guid();
321 $beans['Contacts']->new_with_id = true;
323 // Bug 39287 - Check for Duplicates on selected modules before save
324 if (!empty($_REQUEST['selectedContact']))
326 $beans['Contacts']->retrieve($_REQUEST['selectedContact']);
327 if (!empty($beans['Contacts']->id))
329 $beans['Contacts']->new_with_id = false;
330 unset($_REQUEST["convert_create_Contacts"]);
331 unset($_POST["convert_create_Contacts"]);
334 elseif (!empty($_REQUEST["convert_create_Contacts"]) && $_REQUEST["convert_create_Contacts"] != "false" && !isset($_POST['ContinueContact']))
336 require_once('modules/Contacts/ContactFormBase.php');
337 $contactForm = new ContactFormBase();
338 $duplicateContacts = $contactForm->checkForDuplicates('Contacts');
339 if (isset($duplicateContacts))
341 echo $contactForm->buildTableForm($duplicateContacts, 'Contacts');
345 if (!empty($_REQUEST['selectedAccount']))
347 $_REQUEST['account_id'] = $_REQUEST['selectedAccount'];
348 unset($_REQUEST["convert_create_Accounts"]);
349 unset($_POST["convert_create_Accounts"]);
351 elseif (!empty($_REQUEST["convert_create_Accounts"]) && $_REQUEST["convert_create_Accounts"] != "false" && empty($_POST['ContinueAccount']))
353 require_once('modules/Accounts/AccountFormBase.php');
354 $accountForm = new AccountFormBase();
355 $duplicateAccounts = $accountForm->checkForDuplicates('Accounts');
356 if (isset($duplicateAccounts))
358 echo $accountForm->buildTableForm($duplicateAccounts);
363 foreach ($this->defs as $module => $vdef)
365 //Create a new record if "create" was selected
366 if (!empty($_REQUEST["convert_create_$module"]) && $_REQUEST["convert_create_$module"] != "false")
368 //Save the new record
369 $bean = $beanList[$module];
370 if (empty($beans[$module]))
371 $beans[$module] = new $bean();
373 $this->populateNewBean($module, $beans[$module], $beans['Contacts'], $lead);
376 //If an existing bean was selected, relate it to the contact
377 else if (!empty($vdef['ConvertLead']['select']))
379 //Save the new record
380 $select = $vdef['ConvertLead']['select'];
381 $fieldDef = $beans['Contacts']->field_defs[$select];
382 if (!empty($fieldDef['id_name']) && !empty($_REQUEST[$fieldDef['id_name']]))
384 $beans['Contacts']->$fieldDef['id_name'] = $_REQUEST[$fieldDef['id_name']];
385 $selects[$module] = $_REQUEST[$fieldDef['id_name']];
386 if (!empty($_REQUEST[$select]))
388 $beans['Contacts']->$select = $_REQUEST[$select];
390 // Bug 39268 - Add the existing beans to a list of beans we'll potentially add the lead's activities to
391 $bean = loadBean($module);
392 $bean->retrieve($_REQUEST[$fieldDef['id_name']]);
393 $selectedBeans[$module] = $bean;
398 $this->handleActivities($lead, $beans);
399 // Bug 39268 - Add the lead's activities to the selected beans
400 $this->handleActivities($lead, $selectedBeans);
402 //link selected account to lead if it exists
403 if (!empty($selectedBeans['Accounts']))
405 $lead->account_id = $selectedBeans['Accounts']->id;
408 //Handle non-contacts relationships
409 foreach ($beans as $bean)
413 if (empty($bean->assigned_user_id))
415 $bean->assigned_user_id = $lead->assigned_user_id;
417 $leadsRel = $this->findRelationship($bean, $lead);
418 if (!empty($leadsRel))
420 $bean->load_relationship($leadsRel);
421 $relObject = $bean->$leadsRel->getRelationshipObject();
422 if ($relObject->relationship_type == "one-to-many" && $bean->$leadsRel->_get_bean_position())
424 $id_field = $relObject->rhs_key;
425 $lead->$id_field = $bean->id;
429 $bean->$leadsRel->add($lead->id);
433 //Special case code for opportunities->Accounts
434 if ($bean->object_name == "Opportunity" && empty($bean->account_id))
436 if (isset($beans['Accounts']))
438 $bean->account_id = $beans['Accounts']->id;
439 $bean->account_name = $beans['Accounts']->name;
441 else if (!empty($selects['Accounts']))
443 $bean->account_id = $selects['Accounts'];
447 //create meetings-users relationship
448 if ($bean->object_name == "Meeting")
450 $bean = $this->setMeetingsUsersRelationship($bean);
452 $this->copyAddressFields($bean, $beans['Contacts']);
455 //if campaign id exists then there should be an entry in campaign_log table for the newly created contact: bug 44522
456 if (isset($lead->campaign_id) && $lead->campaign_id != null && $bean->object_name == "Contact")
458 campaign_log_lead_or_contact_entry($lead->campaign_id, $lead, $beans['Contacts'], 'contact');
462 { //Mark the original Lead converted
463 $lead->status = "Converted";
464 $lead->converted = '1';
465 $lead->in_workflow = true;
469 $this->displaySaveResults($beans);
472 public function setMeetingsUsersRelationship($bean)
474 global $current_user;
475 $meetingsRel = $this->findRelationshipByName($bean, $this->defs['Meetings']['ConvertLead']['relationship']);
476 if (!empty($meetingsRel))
478 $bean->load_relationship($meetingsRel);
479 $bean->$meetingsRel->add($current_user->id);
487 protected function displaySaveResults(
493 foreach($beans as $bean)
495 $beanName = $bean->object_name;
496 if ( $beanName == 'Contact' && !$bean->new_with_id ) {
497 echo "<li>" . translate("LBL_EXISTING_CONTACT") . " -
498 <a href='index.php?module={$bean->module_dir}&action=DetailView&record={$bean->id}'>
499 {$bean->get_summary_text()}
503 global $app_list_strings;
504 if(!empty($app_list_strings['moduleListSingular'][$bean->module_dir])) {
505 $module_name = $app_list_strings['moduleListSingular'][$bean->module_dir];
507 $module_name = translate('LBL_MODULE_NAME', $bean->module_dir);
509 if(empty($module_name)) {
510 $module_name = translate($beanName);
512 echo "<li>" . translate("LBL_CREATED_NEW") . ' ' . $module_name . " -
513 <a href='index.php?module={$bean->module_dir}&action=DetailView&record={$bean->id}'>
514 {$bean->get_summary_text()}
522 protected function handleActivities(
527 global $app_list_strings;
528 $parent_types = $app_list_strings['record_type_display'];
530 $activities = $this->getActivitiesFromLead($lead);
532 foreach($beans as $module => $bean)
534 if (isset($parent_types[$module]))
536 foreach($activities as $activity)
538 $this->copyActivityAndRelateToBean($activity, $bean);
545 * Gets the list of activities related to the lead
546 * @param Lead $lead Lead to get activities from
547 * @return Array of Activity SugarBeans .
549 protected function getActivitiesFromLead(
555 global $beanList, $db;
557 $activitesList = array("Calls", "Tasks", "Meetings", "Emails", "Notes");
558 $activities = array();
560 foreach($activitesList as $module)
562 $beanName = $beanList[$module];
563 $activity = new $beanName();
564 $query = "SELECT id FROM {$activity->table_name} WHERE parent_id = '{$lead->id}' AND parent_type = 'Leads'";
565 $result = $db->query($query,true);
566 while($row = $db->fetchByAssoc($result))
568 $activity = new $beanName();
569 $activity->retrieve($row['id']);
570 $activity->fixUpFormatting();
571 $activities[] = $activity;
578 protected function copyActivityAndRelateToBean(
585 $newActivity = clone $activity;
586 $newActivity->id = create_guid();
587 $newActivity->new_with_id = true;
589 //Special case to prevent duplicated tasks from appearing under Contacts multiple times
590 if ($newActivity->module_dir == "Tasks" && $bean->module_dir != "Contacts")
592 $newActivity->contact_id = $activity->contact_name = "";
595 if ($rel = $this->findRelationship($newActivity, $bean))
597 $newActivity->load_relationship ($rel) ;
598 $relObj = $newActivity->$rel->getRelationshipObject();
599 if ( $relObj->relationship_type=='one-to-one' || $relObj->relationship_type == 'one-to-many' )
601 $key = $relObj->rhs_key;
602 $newActivity->$key = $bean->id;
604 $newActivity->$rel->add($bean->id);
605 $newActivity->parent_id = $bean->id;
606 $newActivity->parent_type = $bean->module_dir;
607 $newActivity->update_date_modified = false; //bug 41747
608 $newActivity->save();
609 if ($newActivity->module_dir == "Notes" && $newActivity->filename) {
610 UploadFile::duplicate_file($activity->id, $newActivity->id, $newActivity->filename);
616 * Populates the passed in Bean fron the contact and the $_REQUEST
617 * @param String $module Module of new bean
618 * @param SugarBean $bean SugarBean to be populated.
619 * @param Contact $contact Contact to relate the bean to.
621 protected function populateNewBean(
628 populateFromPost($module, $bean, true);
630 //Copy data from the contact to new bean
631 foreach($bean->field_defs as $field => $def)
633 if(!isset($_REQUEST[$module . $field]) && isset($lead->$field) && $field != 'id')
635 $bean->$field = $lead->$field;
636 if($field == 'date_entered') $bean->$field = gmdate($GLOBALS['timedate']->get_db_date_time_format()); //bug 41030
639 //Try to link to the new contact
641 if (!empty($vdef['ConvertLead']['select']))
643 $select = $vdef['ConvertLead']['select'];
644 $fieldDef = $contact->field_defs[$select];
645 if (!empty($fieldDef['id_name']))
647 $bean->id = create_guid();
648 $bean->new_with_id = true;
649 $contact->$fieldDef['id_name'] = $bean->id ;
650 if ($fieldDef['id_name'] != $select) {
651 $rname = isset($fieldDef['rname']) ? $fieldDef['rname'] : "";
652 if (!empty($rname) && isset($bean->$rname))
653 $contact->$select = $bean->$rname;
655 $contact->$select = $bean->name;
658 } else if ($module != "Contacts"){
659 $contactRel = $this->findRelationship($contact, $bean);
660 if (!empty($contactRel))
662 $bean->id = create_guid();
663 $bean->new_with_id = true;
664 $contact->load_relationship ($contactRel) ;
665 $relObject = $contact->$contactRel->getRelationshipObject();
666 if ($relObject->relationship_type == "one-to-many" && $contact->$contactRel->_get_bean_position())
668 $id_field = $relObject->rhs_key;
669 $bean->$id_field = $contact->id;
671 $contact->$contactRel->add($bean->id);
673 //Set the parent of activites to the new Contact
674 if (isset($bean->field_defs['parent_id']) && isset($bean->field_defs['parent_type']))
676 $bean->parent_id = $contact->id;
677 $bean->parent_type = "Contacts";
683 protected function copyAddressFields($bean, $contact)
685 //Copy over address info from the contact to any beans with address not set
686 foreach($bean->field_defs as $field => $def)
688 if(!isset($_REQUEST[$bean->module_dir . $field]) && strpos($field, "_address_") !== false)
691 if (strpos($field, "alt_") !== false || strpos($field, "shipping_") !== false)
695 if(strpos($field, "_address_street_2") !== false)
696 $type = "_address_street_2";
697 else if(strpos($field, "_address_street_3") !== false)
698 $type = "_address_street_3";
699 else if(strpos($field, "_address_street_4") !== false)
701 else if(strpos($field, "_address_street") !== false)
702 $type = "_address_street";
703 else if(strpos($field, "_address_city") !== false)
704 $type = "_address_city";
705 else if(strpos($field, "_address_state") !== false)
706 $type = "_address_state";
707 else if(strpos($field, "_address_postalcode") !== false)
708 $type = "_address_postalcode";
709 else if(strpos($field, "_address_country") !== false)
710 $type = "_address_country";
713 if (isset($contact->$var))
714 $bean->$field = $contact->$var;
720 protected function findRelationship(
726 require_once("modules/TableDictionary.php");
727 foreach ($from->field_defs as $field=>$def)
729 if (isset($def['type']) && $def['type'] == "link" && isset($def['relationship']))
731 $rel_name = $def['relationship'];
733 if (isset($dictionary[$from->object_name]['relationships']) && isset($dictionary[$from->object_name]['relationships'][$rel_name]))
735 $rel_def = $dictionary[$from->object_name]['relationships'][$rel_name];
737 else if (isset($dictionary[$to->object_name]['relationships']) && isset($dictionary[$to->object_name]['relationships'][$rel_name]))
739 $rel_def = $dictionary[$to->object_name]['relationships'][$rel_name];
741 else if (isset($dictionary[$rel_name]) && isset($dictionary[$rel_name]['relationships'])
742 && isset($dictionary[$rel_name]['relationships'][$rel_name]))
744 $rel_def = $dictionary[$rel_name]['relationships'][$rel_name];
746 if (!empty($rel_def)) {
747 if ($rel_def['lhs_module'] == $from->module_dir && $rel_def['rhs_module'] == $to->module_dir )
751 else if ($rel_def['rhs_module'] == $from->module_dir && $rel_def['lhs_module'] == $to->module_dir )
761 protected function findRelationshipByName($from, $rel_name)
764 require_once("modules/TableDictionary.php");
765 foreach ($from->field_defs as $field => $def)
767 if (isset($def['relationship']) && $def['relationship'] == $rel_name)
775 * @see SugarView::_getModuleTitleParams()
777 protected function _getModuleTitleParams($browserTitle = false)
780 $params = parent::_getModuleTitleParams($browserTitle);
781 $params[] = "<a href='index.php?module=Leads&action=DetailView&record={$this->bean->id}'>{$this->bean->name}</a>";
782 $params[] = $mod_strings['LBL_CONVERTLEAD'];
787 protected function checkForDuplicates(
791 if ($lead->status == "Converted")
793 echo ("<span class='error'>" . translate('LBL_CONVERTLEAD_WARNING'));
795 $q = "SELECT id, first_name, last_name FROM contacts WHERE first_name LIKE '{$lead->first_name}' AND last_name LIKE '{$lead->last_name}'";
796 $result = $lead->db->query($q);
797 while($row = $lead->db->fetchByAssoc($result)) {
798 $contact = new Contact();
799 $contact->retrieve($row['id']);
800 $dupes[$row['id']] = $contact->name;
804 foreach($dupes as $id => $name)
806 echo (translate('LBL_CONVERTLEAD_WARNING_INTO_RECORD') . "<a href='index.php?module=Contacts&action=DetailView&record=$id'>$name</a>");