]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Leads/views/view.convertlead.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Leads / views / view.convertlead.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 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 require_once("include/EditView/EditView2.php");
39
40 class ViewConvertLead extends SugarView
41 {
42     protected $fileName = "modules/Leads/metadata/convertdefs.php";
43     
44     public function __construct(
45         $bean = null, 
46         $view_object_map = array()
47         )
48     {
49         parent::SugarView($bean, $view_object_map);
50         $this->medataDataFile = $this->fileName;
51         if (file_exists("custom/$this->fileName"))
52         {
53             $this->medataDataFile = "custom/$this->fileName";
54         }
55     }
56         
57     /**
58          * @see SugarView::display()
59          */
60         public function display()
61     {
62         if (!empty($_REQUEST['handle']) && $_REQUEST['handle'] == 'save')
63         {
64                 return $this->handleSave();
65         }
66         
67         global $beanList;
68         
69         // get the EditView defs to check if opportunity_name exists, for a check below for populating data
70         $opportunityNameInLayout = false;
71         $editviewFile = 'modules/Leads/metadata/editviewdefs.php';
72         $this->medataDataFile = $editviewFile;
73         if (file_exists("custom/{$editviewFile}"))
74         {
75             $this->medataDataFile = "custom/{$editviewFile}";
76         }
77         include($this->medataDataFile);
78         foreach($viewdefs['Leads']['EditView']['panels'] as $panel_index => $section){
79             foreach($section as $row_array){
80                 foreach($row_array as $cell){
81                         if(isset($cell['name']) && $cell['name'] == 'opportunity_name'){
82                             $opportunityNameInLayout = true;
83                         }
84                 }
85             }
86         }
87         
88         $this->medataDataFile = $this->fileName;
89         if (file_exists("custom/$this->fileName"))
90         {
91             $this->medataDataFile = "custom/$this->fileName";
92         }
93         $this->loadDefs();
94         $this->getRecord();
95         $this->checkForDuplicates($this->focus);
96         $smarty = new Sugar_Smarty();
97         $ev = new EditView();
98         $ev->ss = $smarty;
99         $ev->view = "ConvertLead";
100         echo $this->getModuleTitle();
101         
102         require_once("include/QuickSearchDefaults.php");
103         $qsd = new QuickSearchDefaults();
104         $qsd->setFormName("ConvertLead");
105         
106         $this->contact = new Contact();
107         $smarty->assign("contact_def", $this->contact->field_defs);
108         $smarty->assign("form_name", "ConvertLead");
109         $smarty->assign("form_id", "ConvertLead");
110         $smarty->assign("module", "Leads");
111         $smarty->assign("view", "convertlead");
112         $smarty->assign("bean", $this->focus);
113                 $smarty->assign("record_id", $this->focus->id);
114         $smarty->display("modules/Leads/tpls/ConvertLeadHeader.tpl");
115         
116         echo "<div class='edit view' style='width:auto;'>";
117         
118         foreach($this->defs as $module => $vdef)
119         {
120             $bean = $beanList[$module];
121             $focus = new $bean();
122             $focus->fill_in_additional_detail_fields();
123             foreach($focus->field_defs as $field => $def)
124             {
125                 if(isset($vdef[$ev->view]['copyData']) && $vdef[$ev->view]['copyData'])
126                 {
127                         if ($module == "Accounts" && $field == 'name')
128                         {
129                             $focus->name = $this->focus->account_name;
130                         } 
131                         else if ($module == "Opportunities" && $field == 'amount')
132                         {
133                             $focus->amount = unformat_number($this->focus->opportunity_amount);
134                         } 
135                         else if ($module == "Opportunities" && $field == 'name') {
136                                 if ($opportunityNameInLayout && !empty($this->focus->opportunity_name)){
137                            $focus->name = $this->focus->opportunity_name;
138                                 }
139                         }
140                         else if ($field == "id")
141                     {
142                                                 //If it is not a contact, don't copy the ID from the lead
143                         if ($module == "Contacts") {
144                            $focus->$field = $this->focus->$field;
145                         }
146                     } else if (is_a($focus, "Company") && $field == 'phone_office')
147                         {
148                                 //Special case where company and person have the same field with a different name
149                                 $focus->phone_office = $this->focus->phone_work;
150                         }
151                     else if (isset($this->focus->$field))
152                     {
153                         $focus->$field = $this->focus->$field;
154                     }
155                 }
156             }
157             
158             //Copy over email data
159             $ev->setup($module, $focus, $this->medataDataFile, "modules/Leads/tpls/ConvertLead.tpl", false);
160             $ev->process();
161             echo($ev->display(false));
162             echo($this->getValidationJS($module, $focus, $vdef[$ev->view]));
163         }
164         echo "</div>";
165         echo ($qsd->getQSScriptsJSONAlreadyDefined());
166         $smarty->display("modules/Leads/tpls/ConvertLeadFooter.tpl");
167     }
168     
169     protected function getRecord() 
170     {
171         $this->focus = new Lead();
172         if (isset($_REQUEST['record']))
173         {
174                 $this->focus->retrieve($_REQUEST['record']); 
175         }
176     }
177     
178     protected function loadDefs()
179     {
180         $viewdefs = array();
181         include($this->medataDataFile);
182         $this->defs = $viewdefs;
183     }
184     
185     /**
186      * Returns the javascript to enable/disable validation of each module's sub-form
187      * //TODO: This should probably be on the smarty template
188      * @param $module String the target module name.
189      * @param $focus SugarBean instance of the target module.
190      * @param $focus EditView def for the module.
191      * @return String, javascript to echo to page.
192      */
193     protected function getValidationJS(
194         $module, 
195         $focus, 
196         $viewdef
197         )
198     {
199         $validateSelect = isset($viewdef['required']) && $viewdef['required'] && !empty($viewdef['select']);
200         $jsOut  = "
201         <script type='text/javascript'>
202             if (!SUGAR.convert)  SUGAR.convert = {requiredFields: {}};
203             SUGAR.convert.toggle$module = function(){
204                 clear_all_errors();
205                 inputsWithErrors = [];
206                 if(!SUGAR.convert.{$module}Enabled)
207                 {
208                     for(var i in SUGAR.convert.requiredFields.$module)
209                     {
210                         addToValidate('ConvertLead', '$module' + i, 'varchar', true, SUGAR.convert.requiredFields.{$module}[i]);
211                     }
212                     ";
213         if ($validateSelect) {
214                 $jsOut  .= "
215                     removeFromValidate('ConvertLead', '{$viewdef['select']}');";
216         }
217         
218         $jsOut .= "
219                     SUGAR.convert.{$module}Enabled = true;
220                 } else {
221                     for(var i in SUGAR.convert.requiredFields.$module)
222                     {
223                         removeFromValidate('ConvertLead', '$module' + i);
224                     }";
225         if ($validateSelect) {
226             $jsOut  .= "
227                 addToValidate('ConvertLead', '{$viewdef['select']}', 'varchar', true, '" 
228             . translate($this->contact->field_defs[$viewdef['select']]['vname']) . "');";
229         }
230         $jsOut .= "
231                     SUGAR.convert.{$module}Enabled = false;
232                 }
233                 YAHOO.util.Dom.get('convert_create_{$module}').value = SUGAR.convert.{$module}Enabled;
234             };\n";
235         
236         if (isset($viewdef['required']) && $viewdef['required'])
237         {
238             if (!empty($viewdef['select']) && (empty($viewdef['default_action']) || $viewdef['default_action'] != 'create'))
239             {
240                 $jsOut .= "
241             SUGAR.convert.{$module}Enabled = true;";
242             }
243             $jsOut .= "
244             YAHOO.util.Event.onDOMReady(SUGAR.convert.toggle$module);";
245         } else if (isset($viewdef['default_action'])  && $viewdef['default_action'] == "create")
246         {
247              $jsOut .= "\n            SUGAR.convert.{$module}Enabled = true;";
248         }
249         $jsOut .= "
250             YAHOO.util.Event.addListener('new$module', 'click', SUGAR.convert.toggle$module);
251             SUGAR.convert.requiredFields.$module = {};";
252         foreach($focus->field_defs as $field => $def)
253         {
254             if (isset($def['required']) && $def['required'])
255             {
256                 $jsOut .= "
257             SUGAR.convert.requiredFields.$module.$field = '". translate($def['vname'], $module) . "';\n";
258             }
259         }
260         
261         $jsOut .= "
262         </script>";
263         
264         return $jsOut;
265     }
266     
267     /**
268      * Saves a new Contact as well as any related items passed in.
269      * 
270      * @return null
271      */
272     protected function handleSave()
273     {   
274         require_once("include/formbase.php");
275         $lead = false;
276                 if (!empty($_REQUEST['record']))
277                 {
278                         $lead = new Lead();
279                         $lead->retrieve($_REQUEST['record']);
280                 }
281                         
282         global $beanList;
283         $this->loadDefs();
284         $beans = array();
285         $selectedBeans = array();
286         $selects = array();
287         //Make sure the contact object is availible for relationships.
288         $beans['Contacts'] = new Contact();
289         $beans['Contacts']->id = create_guid();
290         $beans['Contacts']->new_with_id = true;
291         
292         // Bug 39287 - Check for Duplicates on selected modules before save
293         if ( !empty($_REQUEST['selectedContact']) ) {
294             $beans['Contacts']->retrieve($_REQUEST['selectedContact']);
295             if ( !empty($beans['Contacts']->id) ) {
296                 $beans['Contacts']->new_with_id = false;
297                 unset($_REQUEST["convert_create_Contacts"]);
298                 unset($_POST["convert_create_Contacts"]);
299             }
300         }
301         elseif (!empty($_REQUEST["convert_create_Contacts"]) && $_REQUEST["convert_create_Contacts"] != "false" && !isset($_POST['ContinueContact'])) {
302             require_once('modules/Contacts/ContactFormBase.php');
303             $contactForm = new ContactFormBase();
304             $duplicateContacts = $contactForm->checkForDuplicates('Contacts');
305             if(isset($duplicateContacts)){
306                 echo $contactForm->buildTableForm($duplicateContacts,  'Contacts');
307                 return;
308             }
309         }
310         if ( !empty($_REQUEST['selectedAccount']) ) {
311             $_REQUEST['account_id'] = $_REQUEST['selectedAccount'];
312                 unset($_REQUEST["convert_create_Accounts"]);
313                 unset($_POST["convert_create_Accounts"]);
314         }
315         elseif (!empty($_REQUEST["convert_create_Accounts"]) && $_REQUEST["convert_create_Accounts"] != "false" && empty($_POST['ContinueAccount'])){
316             require_once('modules/Accounts/AccountFormBase.php');
317             $accountForm = new AccountFormBase();
318             $duplicateAccounts = $accountForm->checkForDuplicates('Accounts');
319             if(isset($duplicateAccounts)){
320                 echo $accountForm->buildTableForm($duplicateAccounts);
321                 return;
322             }
323         }
324         
325         foreach($this->defs as $module => $vdef)
326         {
327             //Create a new record if "create" was selected
328                 if (!empty($_REQUEST["convert_create_$module"]) && $_REQUEST["convert_create_$module"] != "false")
329             {
330                 //Save the new record
331                 $bean = $beanList[$module];
332                     if (empty($beans[$module]))
333                         $beans[$module] = new $bean();
334                         
335                 $this->populateNewBean($module, $beans[$module], $beans['Contacts'], $lead);
336                 
337             } 
338             //If an existing bean was selected, relate it to the contact
339             else if (!empty($vdef['ConvertLead']['select'])) {
340                 //Save the new record
341                 $select = $vdef['ConvertLead']['select'];
342                 $fieldDef = $beans['Contacts']->field_defs[$select];
343                 if (!empty($fieldDef['id_name']) && !empty($_REQUEST[$fieldDef['id_name']]))
344                 {
345                         $beans['Contacts']->$fieldDef['id_name'] = $_REQUEST[$fieldDef['id_name']];
346                         $selects[$module] = $_REQUEST[$fieldDef['id_name']];
347                         if (!empty($_REQUEST[$select]))
348                         {
349                             $beans['Contacts']->$select = $_REQUEST[$select];
350                         }
351                         // Bug 39268 - Add the existing beans to a list of beans we'll potentially add the lead's activities to
352                         $bean = loadBean($module);
353                     $bean->retrieve($_REQUEST[$fieldDef['id_name']]);
354                     $selectedBeans[$module] = $bean;
355                 }
356             }
357         }
358                 
359                 $this->handleActivities($lead, $beans);
360                 // Bug 39268 - Add the lead's activities to the selected beans
361                 $this->handleActivities($lead, $selectedBeans);
362                 
363         //Handle non-contacts relationships
364             foreach($beans as $bean)
365         {
366                 if (!empty($lead))
367                         {
368                         if (empty($bean->assigned_user_id))
369                                 {
370                                         $bean->assigned_user_id = $lead->assigned_user_id;
371                                 }
372                                 $leadsRel = $this->findRelationship($bean, $lead);
373                                 if (!empty($leadsRel))
374                                 {
375                                         
376                                         $bean->load_relationship ($leadsRel) ;
377                                         $relObject = $bean->$leadsRel->getRelationshipObject();
378                                         if ($relObject->relationship_type == "one-to-many" && $bean->$leadsRel->_get_bean_position())
379                                         {
380                                                 $id_field = $relObject->rhs_key;
381                                                 $lead->$id_field = $bean->id;
382                                         } else {
383                                                 $bean->$leadsRel->add($lead->id);
384                                         }
385                                 }
386                         }
387                         //Special case code for opportunities->Accounts
388                         if ($bean->object_name == "Opportunity" && empty($bean->account_id))
389                         {
390                                 if( isset($beans['Accounts'])) {
391                                         $bean->account_id = $beans['Accounts']->id;
392                                         $bean->account_name = $beans['Accounts']->name;
393                                 }
394                                 else if (!empty($selects['Accounts'])){
395                                          $bean->account_id = $selects['Accounts'];
396                                 }
397                         }
398                         
399                 $this->copyAddressFields($bean, $beans['Contacts']);
400                 
401                         $bean->save();
402         }
403         if (!empty($lead))
404                 {       //Mark the original Lead converted
405                   $lead->status = "Converted";
406                   $lead->converted = '1';
407                   $lead->in_workflow = true;
408                   $lead->save();
409                 }
410                 
411         $this->displaySaveResults($beans);
412     }
413     
414     protected function displaySaveResults(
415         $beans
416         )
417     {
418         global $beanList;
419         echo "<div><ul>";
420         foreach($beans as $bean)
421         {
422             $beanName = $bean->object_name;
423             if ( $beanName == 'Contact' && !$bean->new_with_id ) {
424                 echo "<li>" . translate("LBL_EXISTING_CONTACT") . " - 
425                     <a href='index.php?module={$bean->module_dir}&action=DetailView&record={$bean->id}'> 
426                        {$bean->get_summary_text()} 
427                     </a></li>";
428             }
429             else {
430                 echo "<li>" . translate("LBL_CREATED_NEW") . translate($beanName) . " - 
431                     <a href='index.php?module={$bean->module_dir}&action=DetailView&record={$bean->id}'> 
432                        {$bean->get_summary_text()} 
433                     </a></li>";
434             }
435         }
436         
437         echo "</ul></div>";
438     }
439     
440     protected function handleActivities(
441         $lead, 
442         $beans
443         )
444     {
445         global $app_list_strings;
446         $parent_types = $app_list_strings['record_type_display'];
447         
448         $activities = $this->getActivitiesFromLead($lead);
449         
450         foreach($beans as $module => $bean)
451         {
452                 if (isset($parent_types[$module]))
453                 {
454                 foreach($activities as $activity)
455                         {
456                                 $this->copyActivityAndRelateToBean($activity, $bean);
457                         }
458                 }
459         }
460     }
461         
462     /**
463      * Gets the list of activities related to the lead
464      * @param Lead $lead Lead to get activities from
465      * @return Array of Activity SugarBeans .
466      */
467         protected function getActivitiesFromLead(
468             $lead
469             )
470         {
471                 if (!$lead) return;
472                 
473                 global $beanList, $db;
474                 
475                 $activitesList = array("Calls", "Tasks", "Meetings", "Emails", "Notes");
476                 $activities = array();
477                 
478                 foreach($activitesList as $module)
479                 {
480                         $beanName = $beanList[$module];
481                         $activity = new $beanName();
482                         $query = "SELECT id FROM {$activity->table_name} WHERE parent_id = '{$lead->id}' AND parent_type = 'Leads'";
483                         $result = $db->query($query,true);
484             while($row = $db->fetchByAssoc($result))
485             {
486                 $activity = new $beanName();
487                                 $activity->retrieve($row['id']);
488                                 $activity->fixUpFormatting();
489                                 $activities[] = $activity;
490             }
491                 }
492                 
493                 return $activities;
494         }
495         
496         protected function copyActivityAndRelateToBean(
497             $activity, 
498             $bean
499             )
500         {
501                 global $beanList;
502                 
503                 $newActivity = clone $activity;
504                 $newActivity->id = create_guid();
505                 $newActivity->new_with_id = true;
506                 
507                 //Special case to prevent duplicated tasks from appearing under Contacts multiple times
508         if ($newActivity->module_dir == "Tasks" && $bean->module_dir != "Contacts")
509         {
510                 $newActivity->contact_id = $activity->contact_name = "";
511         }
512         
513                 if ($rel = $this->findRelationship($newActivity, $bean))
514         {
515             $newActivity->load_relationship ($rel) ;
516             $relObj = $newActivity->$rel->getRelationshipObject();
517             if ( $relObj->relationship_type=='one-to-one' || $relObj->relationship_type == 'one-to-many' )
518             {
519                 $key = $relObj->rhs_key;
520                 $newActivity->$key = $bean->id;
521             }
522             $newActivity->$rel->add($bean->id);
523             $newActivity->parent_id = $bean->id;
524                 $newActivity->parent_type = $bean->module_dir;
525                 $newActivity->save();
526          }
527         }
528     
529     /**
530      * Populates the passed in Bean fron the contact and the $_REQUEST
531      * @param String $module Module of new bean
532      * @param SugarBean $bean SugarBean to be populated.
533      * @param Contact $contact Contact to relate the bean to.
534      */
535         protected function populateNewBean(
536             $module, 
537             $bean, 
538             $contact,
539             $lead
540             )
541         {
542                 populateFromPost($module, $bean, true);
543                 
544                 //Copy data from the contact to new bean
545                 foreach($bean->field_defs as $field => $def)
546                 {
547                         if(!isset($_REQUEST[$module . $field]) && isset($lead->$field) && $field != 'id')
548                         {
549                                 $bean->$field = $lead->$field;
550                                 if($field == 'date_entered') $bean->$field = gmdate($GLOBALS['timedate']->get_db_date_time_format()); //bug 41030
551                         }
552                 }
553                 //Try to link to the new contact
554                 $contactRel = "";
555                 if (!empty($vdef['ConvertLead']['select']))
556                 {
557                         $select = $vdef['ConvertLead']['select'];
558                         $fieldDef = $contact->field_defs[$select];
559                         if (!empty($fieldDef['id_name']))
560                         {
561                                 $bean->id = create_guid();
562                                 $bean->new_with_id = true;
563                                 $contact->$fieldDef['id_name'] = $bean->id ;
564                                 if ($fieldDef['id_name'] != $select) {
565                                         $rname = isset($fieldDef['rname']) ? $fieldDef['rname'] : "";
566                                         if (!empty($rname) && isset($bean->$rname))
567                                                 $contact->$select = $bean->$rname;
568                                         else 
569                                                 $contact->$select = $bean->name;
570                                 }
571                         }
572                 } else if ($module != "Contacts"){
573                         $contactRel = $this->findRelationship($contact, $bean);
574                         if (!empty($contactRel))
575                         {
576                                 $bean->id = create_guid();
577                                 $bean->new_with_id = true;
578                                 $contact->load_relationship ($contactRel) ;
579                                 $relObject = $contact->$contactRel->getRelationshipObject();
580                                 if ($relObject->relationship_type == "one-to-many" && $contact->$contactRel->_get_bean_position())
581                                 {
582                                         $id_field = $relObject->rhs_key;
583                                         $bean->$id_field = $contact->id;
584                                 } else {
585                                         $contact->$contactRel->add($bean->id);
586                                 }
587                                 //Set the parent of activites to the new Contact
588                                 if (isset($bean->field_defs['parent_id']) && isset($bean->field_defs['parent_type']))
589                                 {
590                                         $bean->parent_id = $contact->id;
591                                         $bean->parent_type = "Contacts";
592                                 }
593                         }
594                 }
595         }
596         
597         protected function copyAddressFields($bean, $contact)
598         {
599         //Copy over address info from the contact to any beans with address not set
600                 foreach($bean->field_defs as $field => $def)
601                         {
602                                 if(!isset($_REQUEST[$bean->module_dir . $field]) && strpos($field, "_address_") !== false)
603                                 {
604                                         $set = "primary";
605                                         if (strpos($field, "alt_") !== false || strpos($field, "shipping_") !== false)
606                                                 $set = "alt";
607                                         $type = "";
608                                                 
609                                         if(strpos($field, "_address_street_2") !== false)
610                                                 $type = "_address_street_2";
611                                         else if(strpos($field, "_address_street_3") !== false)
612                                                 $type = "_address_street_3";
613                                         else if(strpos($field, "_address_street_4") !== false)
614                                                 $type = "";
615                                         else if(strpos($field, "_address_street") !== false)
616                                                 $type = "_address_street";
617                                         else if(strpos($field, "_address_city") !== false)
618                                                 $type = "_address_city";
619                                         else if(strpos($field, "_address_state") !== false)
620                                                 $type = "_address_state";
621                                         else if(strpos($field, "_address_postalcode") !== false)
622                                                 $type = "_address_postalcode";
623                                         else if(strpos($field, "_address_country") !== false)
624                                                 $type = "_address_country";
625                                                 
626                                                 $var = $set.$type;
627                                         if (isset($contact->$var))
628                                                 $bean->$field = $contact->$var;
629                                 }
630                         }
631         }
632     
633
634     protected function findRelationship(
635         $from, 
636         $to
637         ) 
638     {
639         global $dictionary;
640         require_once("modules/TableDictionary.php");
641         foreach ($from->field_defs as $field=>$def)
642         {
643             if (isset($def['type']) && $def['type'] == "link" && isset($def['relationship'])) {
644                 $rel_name = $def['relationship'];
645                 $rel_def = "";
646                 if (isset($dictionary[$from->object_name]['relationships']) && isset($dictionary[$from->object_name]['relationships'][$rel_name]))
647                 {
648                     $rel_def = $dictionary[$from->object_name]['relationships'][$rel_name];
649                 }
650                 else if (isset($dictionary[$to->object_name]['relationships']) && isset($dictionary[$to->object_name]['relationships'][$rel_name]))
651                 {
652                     $rel_def = $dictionary[$to->object_name]['relationships'][$rel_name];
653                 }
654                 else if (isset($dictionary[$rel_name]) && isset($dictionary[$rel_name]['relationships']) 
655                         && isset($dictionary[$rel_name]['relationships'][$rel_name])) 
656                 {
657                         $rel_def = $dictionary[$rel_name]['relationships'][$rel_name];
658                 }
659                 if (!empty($rel_def)) {
660                     if ($rel_def['lhs_module'] == $from->module_dir && $rel_def['rhs_module'] == $to->module_dir )
661                     {
662                         return $field;
663                     } 
664                     else if ($rel_def['rhs_module'] == $from->module_dir && $rel_def['lhs_module'] == $to->module_dir )
665                     {
666                         return $field;
667                     }
668                 }
669             }
670         }
671         return false;
672     }
673     
674         /**
675          * @see SugarView::_getModuleTitleParams()
676          */
677         protected function _getModuleTitleParams()
678         {
679             global $mod_strings;
680             $params = parent::_getModuleTitleParams();
681             $params[] = "<a href='index.php?module=Leads&action=DetailView&record={$this->bean->id}'>{$this->bean->name}</a>";
682             $params[] = $mod_strings['LBL_CONVERTLEAD'];
683         return $params;
684     }
685     
686     
687     protected function checkForDuplicates(
688         $lead
689         ) 
690     {
691         if ($lead->status == "Converted")
692         {
693                 echo ("<span class='error'>" . translate('LBL_CONVERTLEAD_WARNING'));
694                 $dupes = array();
695                 $q = "SELECT id, first_name, last_name FROM contacts WHERE first_name LIKE '{$lead->first_name}' AND last_name LIKE '{$lead->last_name}'";
696                 $result = $lead->db->query($q);
697                 while($row = $lead->db->fetchByAssoc($result)) {
698                         $contact = new Contact();
699                         $contact->retrieve($row['id']);
700                         $dupes[$row['id']] = $contact->name;
701                 }
702                 if (!empty($dupes))
703                 {
704                         foreach($dupes as $id => $name)
705                         {
706                                 echo (translate('LBL_CONVERTLEAD_WARNING_INTO_RECORD') . "<a href='index.php?module=Contacts&action=DetailView&record=$id'>$name</a>");
707                                 break;
708                         }
709                 }
710                 echo "</span>";
711         }
712         return false;
713     } 
714 }