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-2013 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
54 * holds current field when a duplicate has been found
56 public $_dupedFields =array();
61 * @param object $focus bean
63 public function __construct($focus)
65 $this->_focus = $focus;
69 * Returns an array of indices for the current module
73 private function _getIndexVardefs()
75 $indexes = $this->_focus->getIndices();
77 //grab any custom indexes if they exist
78 if($this->_focus->hasCustomFields()){
79 $custmIndexes = $this->_focus->db->helper->get_indices($this->_focus->table_name.'_cstm');
80 $indexes = array_merge($custmIndexes,$indexes);
83 if ( $this->_focus->getFieldDefinition('email1') )
85 'name' => 'special_idx_email1',
87 'fields' => array('email1')
89 if ( $this->_focus->getFieldDefinition('email2') )
91 'name' => 'special_idx_email2',
93 'fields' => array('email2')
100 * Returns an array with an element for each index
104 public function getDuplicateCheckIndexes()
106 $super_language_pack = sugarLangArrayMerge(
107 return_module_language($GLOBALS['current_language'], $this->_focus->module_dir),
108 $GLOBALS['app_strings']
111 $index_array = array();
112 foreach ($this->_getIndexVardefs() as $index){
113 if ($index['type'] == "index"){
114 $labelsArray = array();
115 foreach ($index['fields'] as $field){
116 if ($field == 'deleted') continue;
117 $fieldDef = $this->_focus->getFieldDefinition($field);
118 if ( isset($fieldDef['vname']) && isset($super_language_pack[$fieldDef['vname']]) )
119 $labelsArray[$fieldDef['name']] = $super_language_pack[$fieldDef['vname']];
121 $labelsArray[$fieldDef['name']] = $fieldDef['name'];
123 $index_array[$index['name']] = str_replace(":", "",implode(", ",$labelsArray));
131 * Checks to see if the given bean is a duplicate based off the given fields
133 * @param array $indexlist
134 * @return bool true if this bean is a duplicate or false if it isn't
136 public function isADuplicateRecordByFields($fieldList)
138 foreach($fieldList as $field)
140 if ( $field == 'email1' || $field == 'email2' )
142 $emailAddress = new SugarEmailAddress();
144 if ( $emailAddress->getCountEmailAddressByBean($this->_focus->$email,$this->_focus,($field == 'email1')) > 0 )
149 $index_fields = array('deleted' => '0');
150 if( is_array($field) )
152 foreach($field as $tmpField)
154 if ($tmpField == 'deleted')
156 if (strlen($this->_focus->$tmpField) > 0)
157 $index_fields[$tmpField] = $this->_focus->$tmpField;
160 elseif($field != 'deleted' && strlen($this->_focus->$field) > 0)
161 $index_fields[$field] = $this->_focus->$field;
163 if ( count($index_fields) <= 1 )
166 $newfocus = loadBean($this->_focus->module_dir);
167 $result = $newfocus->retrieve_by_string_fields($index_fields,true);
169 if ( !is_null($result) )
178 * Checks to see if the given bean is a duplicate based off the given indexes
180 * @param array $indexlist
181 * @return bool true if this bean is a duplicate or false if it isn't
183 public function isADuplicateRecord( $indexlist )
185 // Bug #51264 : Importing updates to rows prevented by duplicates check
186 if ( !empty($this->_focus) && ($this->_focus instanceof SugarBean) && !empty($this->_focus->id) )
188 $_focus = clone $this->_focus;
190 $_focus->retrieve($this->_focus->id);
191 if ( !empty($_focus->id) )
198 //lets strip the indexes of the name field in the value and leave only the index name
199 $origIndexList = $indexlist;
202 $customIndexlist=array();
203 foreach($origIndexList as $iv){
204 if(empty($iv)) continue;
205 $field_index_array = explode('::',$iv);
206 if($field_index_array[0] == 'customfield'){
207 //this is a custom field, so place in custom array
208 $customIndexlist[] = $field_index_array[1];
211 //this is not a custom field, so place in index list
212 $indexlist[] = $field_index_array[0];
213 if(isset($field_index_array[1])) {
214 $fieldlist[] = $field_index_array[1];
219 //if full_name is set, then manually search on the first and last name fields before iterating through rest of fields
220 //this is a special handling of the name fields on people objects, the rest of the fields are checked individually
221 if(in_array('full_name',$indexlist)){
222 $newfocus = loadBean($this->_focus->module_dir);
223 $result = $newfocus->retrieve_by_string_fields(array('deleted' =>'0', 'first_name'=>$this->_focus->first_name, 'last_name'=>$this->_focus->last_name),true);
225 if ( !is_null($result) ){
226 //set dupe field to full_name and name fields
227 $this->_dupedFields[] = 'full_name';
228 $this->_dupedFields[] = 'first_name';
229 $this->_dupedFields[] = 'last_name';
234 // loop through var def indexes and compare with selected indexes
235 foreach ($this->_getIndexVardefs() as $index){
236 // if we get an index not in the indexlist, loop
237 if ( !in_array($index['name'],$indexlist) )
240 // This handles the special case of duplicate email checking
241 if ( $index['name'] == 'special_idx_email1' || $index['name'] == 'special_idx_email2' ) {
242 $emailAddress = new SugarEmailAddress();
243 $email = $index['fields'][0];
244 if ( $emailAddress->getCountEmailAddressByBean(
245 $this->_focus->$email,
247 ($index['name'] == 'special_idx_email1')
248 ) > 0 ){ foreach($index['fields'] as $field){
249 if($field !='deleted')
250 $this->_dupedFields[] = $field;
254 // Adds a hook so you can define a method in the bean to handle dupe checking
255 elseif ( isset($index['dupeCheckFunction']) ) {
256 $functionName = substr_replace($index['dupeCheckFunction'],'',0,9);
257 if ( method_exists($this->_focus,$functionName) && $this->_focus->$functionName($index) === true)
258 return $this->_focus->$functionName($index);
261 $index_fields = array('deleted' => '0');
262 //search only for the field we have selected
263 foreach($index['fields'] as $field){
264 if ($field == 'deleted' || !in_array($field,$fieldlist))
266 if (!in_array($field,$index_fields))
267 if (isset($this->_focus->$field) && strlen($this->_focus->$field) > 0)
268 $index_fields[$field] = $this->_focus->$field;
271 // if there are no valid fields in the index field list, loop
272 if ( count($index_fields) <= 1 )
275 $newfocus = loadBean($this->_focus->module_dir);
276 $result = $newfocus->retrieve_by_string_fields($index_fields,true);
278 if ( !is_null($result) ){
279 //remove deleted as a duped field
280 unset($index_fields['deleted']);
282 //create string based on array of dupe fields
283 $this->_dupedFields = array_merge(array_keys($index_fields),$this->_dupedFields);
288 //return true if any dupes were found
289 if(!empty($this->_dupedFields)){
297 public function getDuplicateCheckIndexedFiles()
299 require_once('include/export_utils.php');
300 $import_fields = $this->_focus->get_importable_fields();
301 $importable_keys = array_keys($import_fields);//
303 $index_array = array();
304 $fields_used = array();
305 $mstr_exclude_array = array('all'=>array('team_set_id','id','deleted'),'contacts'=>array('email2'), array('leads'=>'reports_to_id'), array('prospects'=>'tracker_key'));
307 //create exclude array from subset of applicable mstr_exclude_array elements
308 $exclude_array = isset($mstr_exclude_array[strtolower($this->_focus->module_dir)])?array_merge($mstr_exclude_array[strtolower($this->_focus->module_dir)], $mstr_exclude_array['all']) : $mstr_exclude_array['all'];
312 //process all fields belonging to indexes
313 foreach ($this->_getIndexVardefs() as $index){
314 if ($index['type'] == "index"){
316 foreach ($index['fields'] as $field){
319 //skip this field if it is the deleted field, not in the importable keys array, or a field in the exclude array
320 if (!in_array($field, $importable_keys) || in_array($field, $exclude_array)) continue;
321 $fieldDef = $this->_focus->getFieldDefinition($field);
323 //skip if this field is already defined (from another index)
324 if (in_array($fieldDef['name'],$fields_used)) continue;
326 //get the proper export label
327 $fieldName = translateForExport($fieldDef['name'],$this->_focus);
330 $index_array[$index['name'].'::'.$fieldDef['name']] = $fieldName;
331 $fields_used[] = $fieldDef['name'];
337 //special handling for beans with first_name and last_name
338 if(in_array('first_name', $fields_used) && in_array('last_name', $fields_used)){
339 //since both full name and last name fields have been mapped, add full name index
340 $index_array['full_name::full_name'] = translateForExport('full_name',$this->_focus);
341 $fields_used[] = 'full_name';