2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
4 /*********************************************************************************
5 * SugarCRM Community Edition is a customer relationship management program developed by
6 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Affero General Public License version 3 as published by the
10 * Free Software Foundation with the addition of the following permission added
11 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
20 * You should have received a copy of the GNU Affero General Public License along with
21 * this program; if not, see http://www.gnu.org/licenses or write to the Free
22 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
28 * The interactive user interfaces in modified source and object code versions
29 * of this program must display Appropriate Legal Notices, as required under
30 * Section 5 of the GNU Affero General Public License version 3.
32 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33 * these Appropriate Legal Notices must retain the display of the "Powered by
34 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35 * technical reasons, the Appropriate Legal Notices must display the words
36 * "Powered by SugarCRM".
37 ********************************************************************************/
39 /*********************************************************************************
41 * Description: Handles getting a list of fields to duplicate check and doing the duplicate checks
42 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
43 * All Rights Reserved.
44 ********************************************************************************/
46 class ImportDuplicateCheck
49 * Private reference to the bean we're dealing with
56 * @param object $focus bean
58 public function __construct(
62 $this->_focus = &$focus;
66 * Returns an array of indices for the current module
70 private function _getIndexVardefs()
72 $indexes = $this->_focus->getIndices();
74 if ( $this->_focus->getFieldDefinition('email1') )
76 'name' => 'special_idx_email1',
78 'fields' => array('email1')
80 if ( $this->_focus->getFieldDefinition('email2') )
82 'name' => 'special_idx_email2',
84 'fields' => array('email2')
91 * Returns an array with an element for each index
95 public function getDuplicateCheckIndexes()
97 $super_language_pack = sugarArrayMerge(
98 return_module_language($GLOBALS['current_language'], $this->_focus->module_dir),
99 $GLOBALS['app_strings']
102 $index_array = array();
103 foreach ($this->_getIndexVardefs() as $index){
104 if ($index['type'] == "index"){
105 $labelsArray = array();
106 foreach ($index['fields'] as $field){
107 if ($field == 'deleted') continue;
108 $fieldDef = $this->_focus->getFieldDefinition($field);
109 if ( isset($fieldDef['vname']) && isset($super_language_pack[$fieldDef['vname']]) )
110 $labelsArray[$fieldDef['name']] = $super_language_pack[$fieldDef['vname']];
112 $labelsArray[$fieldDef['name']] = $fieldDef['name'];
114 $index_array[$index['name']] = str_replace(":", "",implode(", ",$labelsArray));
122 * Checks to see if the given bean is a duplicate based off the given indexes
124 * @param array $indexlist
125 * @return bool true if this bean is a duplicate or false if it isn't
127 public function isADuplicateRecord(
131 // loop through var def indexes and compare with selected indexes
132 foreach ( $this->_getIndexVardefs() as $index ) {
133 // if we get an index not in the indexlist, loop
134 if ( !in_array($index['name'],$indexlist) )
137 // This handles the special case of duplicate email checking
138 if ( $index['name'] == 'special_idx_email1' || $index['name'] == 'special_idx_email2' ) {
139 $emailAddress = new SugarEmailAddress();
140 $email = $index['fields'][0];
141 if ( $emailAddress->getCountEmailAddressByBean(
142 $this->_focus->$email,
144 ($index['name'] == 'special_idx_email1')
148 // Adds a hook so you can define a method in the bean to handle dupe checking
149 elseif ( isset($index['dupeCheckFunction']) ) {
150 $functionName = substr_replace($index['dupeCheckFunction'],'',0,9);
151 if ( method_exists($this->_focus,$functionName) )
152 return $this->_focus->$functionName($index);
155 $index_fields = array('deleted' => '0');
156 foreach($index['fields'] as $field){
157 if ($field == 'deleted')
159 if (!in_array($field,$index_fields))
160 if (strlen($this->_focus->$field) > 0)
161 $index_fields[$field] = $this->_focus->$field;
164 // if there are no valid fields in the index field list, loop
165 if ( count($index_fields) <= 1 )
168 $newfocus = loadBean($this->_focus->module_dir);
169 $result = $newfocus->retrieve_by_string_fields($index_fields,true);
171 if ( !is_null($result) )