]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/ImportDuplicateCheck.php
Release 6.2.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      * Constructor
55      *
56      * @param object $focus bean 
57      */
58     public function __construct(
59         &$focus
60         )
61     {
62         $this->_focus = &$focus;
63     }
64     
65     /**
66      * Returns an array of indices for the current module
67      *
68      * @return array
69      */
70     private function _getIndexVardefs()
71     {
72         $indexes = $this->_focus->getIndices();
73         
74         if ( $this->_focus->getFieldDefinition('email1') )
75             $indexes[] = array(
76                 'name' => 'special_idx_email1',
77                 'type' => 'index',
78                 'fields' => array('email1')
79                 );
80         if ( $this->_focus->getFieldDefinition('email2') )
81             $indexes[] = array(
82                 'name' => 'special_idx_email2',
83                 'type' => 'index',
84                 'fields' => array('email2')
85                 );
86         
87         return $indexes;
88     }
89     
90     /**
91      * Returns an array with an element for each index
92      *
93      * @return array
94      */
95     public function getDuplicateCheckIndexes()
96     {
97         $super_language_pack = sugarArrayMerge(
98             return_module_language($GLOBALS['current_language'], $this->_focus->module_dir), 
99             $GLOBALS['app_strings']
100             );
101         
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']];
111                     else
112                         $labelsArray[$fieldDef['name']] = $fieldDef['name'];
113                 }
114                 $index_array[$index['name']] = str_replace(":", "",implode(", ",$labelsArray));
115             }
116         }
117         
118         return $index_array;
119     }
120     
121     /**
122      * Checks to see if the given bean is a duplicate based off the given indexes
123      *
124      * @param  array $indexlist
125      * @return bool true if this bean is a duplicate or false if it isn't
126      */
127     public function isADuplicateRecord(
128         $indexlist
129         )
130     {
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) )
135                 continue;
136             
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,
143                         $this->_focus,
144                         ($index['name'] == 'special_idx_email1')
145                         ) > 0 )
146                     return true;
147             }
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);
153             }
154             else {
155                 $index_fields = array('deleted' => '0');
156                 foreach($index['fields'] as $field){
157                     if ($field == 'deleted') 
158                         continue;
159                     if (!in_array($field,$index_fields))
160                         if (strlen($this->_focus->$field) > 0)
161                             $index_fields[$field] = $this->_focus->$field;
162                 }
163                 
164                 // if there are no valid fields in the index field list, loop
165                 if ( count($index_fields) <= 1 )
166                     continue;
167                 
168                 $newfocus = loadBean($this->_focus->module_dir);
169                 $result = $newfocus->retrieve_by_string_fields($index_fields,true);
170                 
171                 if ( !is_null($result) )
172                     return true;
173             }
174         }
175         return false;
176     }
177 }
178  
179 ?>