]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/ImportDuplicateCheck.php
Release 6.4.0
[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-2011 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 = sugarArrayMerge(
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
186         //lets strip the indexes of the name field in the value and leave only the index name
187         $origIndexList = $indexlist;
188         $indexlist=array();
189         $fieldlist=array();
190         $customIndexlist=array();
191         foreach($origIndexList as $iv){
192             if(empty($iv)) continue;
193             $field_index_array = explode('::',$iv);
194             if($field_index_array[0] == 'customfield'){
195                 //this is a custom field, so place in custom array
196                 $customIndexlist[] = $field_index_array[1];
197
198             }else{
199                 //this is not a custom field, so place in index list
200                 $indexlist[] = $field_index_array[0];
201                 if(isset($field_index_array[1])) {
202                     $fieldlist[] = $field_index_array[1];
203                 }
204             }
205         }
206
207         //if full_name is set, then manually search on the first and last name fields before iterating through rest of fields
208         //this is a special handling of the name fields on people objects, the rest of the fields are checked individually
209         if(in_array('full_name',$indexlist)){
210             $newfocus = loadBean($this->_focus->module_dir);
211             $result = $newfocus->retrieve_by_string_fields(array('deleted' =>'0', 'first_name'=>$this->_focus->first_name, 'last_name'=>$this->_focus->last_name),true);
212
213             if ( !is_null($result) ){
214                 //set dupe field to full_name and name fields
215                 $this->_dupedFields[] = 'full_name';
216                 $this->_dupedFields[] = 'first_name';
217                 $this->_dupedFields[] = 'last_name';
218
219             }
220         }
221
222         // loop through var def indexes and compare with selected indexes
223         foreach ($this->_getIndexVardefs() as $index){
224             // if we get an index not in the indexlist, loop
225             if ( !in_array($index['name'],$indexlist) )
226                 continue;
227
228             // This handles the special case of duplicate email checking
229             if ( $index['name'] == 'special_idx_email1' || $index['name'] == 'special_idx_email2' ) {
230                 $emailAddress = new SugarEmailAddress();
231                 $email = $index['fields'][0];
232                 if ( $emailAddress->getCountEmailAddressByBean(
233                         $this->_focus->$email,
234                         $this->_focus,
235                         ($index['name'] == 'special_idx_email1')
236                         ) > 0 ){ foreach($index['fields'] as $field){
237                         if($field !='deleted')
238                             $this->_dupedFields[] = $field;
239                     }
240                 }
241             }
242             // Adds a hook so you can define a method in the bean to handle dupe checking
243             elseif ( isset($index['dupeCheckFunction']) ) {
244                 $functionName = substr_replace($index['dupeCheckFunction'],'',0,9);
245                 if ( method_exists($this->_focus,$functionName) )
246                     return $this->_focus->$functionName($index);
247             }
248             else {
249                 $index_fields = array('deleted' => '0');
250                 //search only for the field we have selected
251                 foreach($index['fields'] as $field){
252                     if ($field == 'deleted' ||  !in_array($field,$fieldlist))
253                         continue;
254                     if (!in_array($field,$index_fields))
255                         if (isset($this->_focus->$field) && strlen($this->_focus->$field) > 0)
256                             $index_fields[$field] = $this->_focus->$field;
257                 }
258
259                 // if there are no valid fields in the index field list, loop
260                 if ( count($index_fields) <= 1 )
261                     continue;
262
263                 $newfocus = loadBean($this->_focus->module_dir);
264                 $result = $newfocus->retrieve_by_string_fields($index_fields,true);
265
266                 if ( !is_null($result) ){
267                     //remove deleted as a duped field
268                     unset($index_fields['deleted']);
269
270                     //create string based on array of dupe fields
271                     $this->_dupedFields = array_merge(array_keys($index_fields),$this->_dupedFields);
272                 }
273             }
274         }
275
276         //return true if any dupes were found
277         if(!empty($this->_dupedFields)){
278             return true;
279         }
280
281         return false;
282     }
283
284
285     public function getDuplicateCheckIndexedFiles()
286     {
287         require_once('include/export_utils.php');
288         $import_fields = $this->_focus->get_importable_fields();
289         $importable_keys = array_keys($import_fields);//
290
291         $index_array = array();
292         $fields_used = array();
293         $mstr_exclude_array = array('all'=>array('team_set_id','id','deleted'),'contacts'=>array('email2'), array('leads'=>'reports_to_id'), array('prospects'=>'tracker_key'));
294
295         //create exclude array from subset of applicable mstr_exclude_array elements
296         $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'];
297
298
299
300         //process all fields belonging to indexes
301         foreach ($this->_getIndexVardefs() as $index){
302             if ($index['type'] == "index"){
303
304                 foreach ($index['fields'] as $field){
305                     $fieldName='';
306
307                     //skip this field if it is the deleted field, not in the importable keys array, or a field in the exclude array
308                     if (!in_array($field, $importable_keys) || in_array($field, $exclude_array)) continue;
309                     $fieldDef = $this->_focus->getFieldDefinition($field);
310
311                     //skip if this field is already defined (from another index)
312                     if (in_array($fieldDef['name'],$fields_used)) continue;
313
314                     //get the proper export label
315                     $fieldName = translateForExport($fieldDef['name'],$this->_focus);
316
317
318                     $index_array[$index['name'].'::'.$fieldDef['name']] = $fieldName;
319                     $fields_used[] = $fieldDef['name'];
320                 }
321
322             }
323         }
324
325         //special handling for beans with first_name and last_name
326         if(in_array('first_name', $fields_used) && in_array('last_name', $fields_used)){
327             //since both full name and last name fields have been mapped, add full name index
328             $index_array['full_name::full_name'] = translateForExport('full_name',$this->_focus);
329             $fields_used[] = 'full_name';
330         }
331
332         asort($index_array);
333         return $index_array;
334     }
335 }
336
337 ?>