]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/ImportDuplicateCheck.php
Release 6.5.8
[Github/sugarcrm.git] / modules / Import / ImportDuplicateCheck.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
4 /*********************************************************************************
5  * SugarCRM Community Edition is a customer relationship management program developed by
6  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7  * 
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.
14  * 
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
18  * details.
19  * 
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
23  * 02110-1301 USA.
24  * 
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.
27  * 
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.
31  * 
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  ********************************************************************************/
38
39 /*********************************************************************************
40
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  ********************************************************************************/
45
46 class ImportDuplicateCheck
47 {
48     /**
49      * Private reference to the bean we're dealing with
50      */
51     private $_focus;
52
53     /*
54      * holds current field when a duplicate has been found
55      */
56     public $_dupedFields =array();
57
58     /**
59      * Constructor
60      *
61      * @param object $focus bean
62      */
63     public function __construct($focus)
64     {
65         $this->_focus = $focus;
66     }
67
68     /**
69      * Returns an array of indices for the current module
70      *
71      * @return array
72      */
73     private function _getIndexVardefs()
74     {
75         $indexes = $this->_focus->getIndices();
76
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);
81         }
82
83         if ( $this->_focus->getFieldDefinition('email1') )
84             $indexes[] = array(
85                 'name' => 'special_idx_email1',
86                 'type' => 'index',
87                 'fields' => array('email1')
88                 );
89         if ( $this->_focus->getFieldDefinition('email2') )
90             $indexes[] = array(
91                 'name' => 'special_idx_email2',
92                 'type' => 'index',
93                 'fields' => array('email2')
94                 );
95
96         return $indexes;
97     }
98
99     /**
100      * Returns an array with an element for each index
101      *
102      * @return array
103      */
104     public function getDuplicateCheckIndexes()
105     {
106         $super_language_pack = sugarLangArrayMerge(
107             return_module_language($GLOBALS['current_language'], $this->_focus->module_dir),
108             $GLOBALS['app_strings']
109             );
110
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']];
120                     else
121                         $labelsArray[$fieldDef['name']] = $fieldDef['name'];
122                 }
123                 $index_array[$index['name']] = str_replace(":", "",implode(", ",$labelsArray));
124             }
125         }
126
127         return $index_array;
128     }
129
130     /**
131      * Checks to see if the given bean is a duplicate based off the given fields
132      *
133      * @param  array $indexlist
134      * @return bool true if this bean is a duplicate or false if it isn't
135      */
136     public function isADuplicateRecordByFields($fieldList)
137     {
138         foreach($fieldList as $field)
139         {
140             if ( $field == 'email1' || $field == 'email2' )
141             {
142                 $emailAddress = new SugarEmailAddress();
143                 $email = $field;
144                 if ( $emailAddress->getCountEmailAddressByBean($this->_focus->$email,$this->_focus,($field == 'email1')) > 0 )
145                     return true;
146             }
147             else
148             {
149                 $index_fields = array('deleted' => '0');
150                 if( is_array($field) )
151                 {
152                     foreach($field as $tmpField)
153                     {
154                         if ($tmpField == 'deleted')
155                             continue;
156                         if (strlen($this->_focus->$tmpField) > 0)
157                             $index_fields[$tmpField] = $this->_focus->$tmpField;
158                     }
159                 }
160                 elseif($field != 'deleted' && strlen($this->_focus->$field) > 0)
161                     $index_fields[$field] = $this->_focus->$field;
162
163                 if ( count($index_fields) <= 1 )
164                     continue;
165
166                 $newfocus = loadBean($this->_focus->module_dir);
167                 $result = $newfocus->retrieve_by_string_fields($index_fields,true);
168
169                 if ( !is_null($result) )
170                     return true;
171             }
172         }
173
174         return false;
175     }
176
177     /**
178      * Checks to see if the given bean is a duplicate based off the given indexes
179      *
180      * @param  array $indexlist
181      * @return bool true if this bean is a duplicate or false if it isn't
182      */
183     public function isADuplicateRecord( $indexlist )
184     {
185         // Bug #51264 : Importing updates to rows prevented by duplicates check
186         if ( !empty($this->_focus) && ($this->_focus instanceof SugarBean) && !empty($this->_focus->id) )
187         {
188             $_focus = clone $this->_focus;
189             $_focus->id = null;
190             $_focus->retrieve($this->_focus->id);
191             if ( !empty($_focus->id) )
192             {
193                 return false;
194             }
195             unset($_focus);
196         }
197
198         //lets strip the indexes of the name field in the value and leave only the index name
199         $origIndexList = $indexlist;
200         $indexlist=array();
201         $fieldlist=array();
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];
209
210             }else{
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];
215                 }
216             }
217         }
218
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);
224
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';
230
231             }
232         }
233
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) )
238                 continue;
239
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,
246                         $this->_focus,
247                         ($index['name'] == 'special_idx_email1')
248                         ) > 0 ){ foreach($index['fields'] as $field){
249                         if($field !='deleted')
250                             $this->_dupedFields[] = $field;
251                     }
252                 }
253             }
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) )
258                     return $this->_focus->$functionName($index);
259             }
260             else {
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))
265                         continue;
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;
269                 }
270
271                 // if there are no valid fields in the index field list, loop
272                 if ( count($index_fields) <= 1 )
273                     continue;
274
275                 $newfocus = loadBean($this->_focus->module_dir);
276                 $result = $newfocus->retrieve_by_string_fields($index_fields,true);
277
278                 if ( !is_null($result) ){
279                     //remove deleted as a duped field
280                     unset($index_fields['deleted']);
281
282                     //create string based on array of dupe fields
283                     $this->_dupedFields = array_merge(array_keys($index_fields),$this->_dupedFields);
284                 }
285             }
286         }
287
288         //return true if any dupes were found
289         if(!empty($this->_dupedFields)){
290             return true;
291         }
292
293         return false;
294     }
295
296
297     public function getDuplicateCheckIndexedFiles()
298     {
299         require_once('include/export_utils.php');
300         $import_fields = $this->_focus->get_importable_fields();
301         $importable_keys = array_keys($import_fields);//
302
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'));
306
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'];
309
310
311
312         //process all fields belonging to indexes
313         foreach ($this->_getIndexVardefs() as $index){
314             if ($index['type'] == "index"){
315
316                 foreach ($index['fields'] as $field){
317                     $fieldName='';
318
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);
322
323                     //skip if this field is already defined (from another index)
324                     if (in_array($fieldDef['name'],$fields_used)) continue;
325
326                     //get the proper export label
327                     $fieldName = translateForExport($fieldDef['name'],$this->_focus);
328
329
330                     $index_array[$index['name'].'::'.$fieldDef['name']] = $fieldName;
331                     $fields_used[] = $fieldDef['name'];
332                 }
333
334             }
335         }
336
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';
342         }
343
344         asort($index_array);
345         return $index_array;
346     }
347 }
348
349 ?>