]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/UsersLastImport.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Import / UsersLastImport.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description: Bean class for the users_last_import table
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  ********************************************************************************/
44
45
46 require_once('modules/Import/Forms.php');
47
48 class UsersLastImport extends SugarBean
49 {
50         /**
51      * Fields in the table
52      */
53         public $id;
54         public $assigned_user_id;
55         public $import_module;
56         public $bean_type;
57         public $bean_id;
58         public $deleted;
59     
60     /**
61      * Set the default settings from Sugarbean
62      */
63     public $module_dir = 'Import';
64     public $table_name = "users_last_import";
65     public $object_name = "UsersLastImport";
66     public $column_fields = array(
67         "id",
68         "assigned_user_id",
69         "bean_type",
70         "bean_id",
71         "deleted"
72                 );
73     public $new_schema = true;
74     public $additional_column_fields = Array();
75
76     /**
77      * Constructor
78      */
79     public function __construct() 
80     {
81         parent::SugarBean();
82         }
83
84         /**
85      * Extends SugarBean::listviewACLHelper
86      *
87      * @return array
88      */
89     public function listviewACLHelper()
90     {
91                 $array_assign = parent::listviewACLHelper();
92                 $is_owner = false;
93                 if ( !ACLController::moduleSupportsACL('Accounts') 
94                 || ACLController::checkAccess('Accounts', 'view', $is_owner) ) {
95                         $array_assign['ACCOUNT'] = 'a';
96                 }
97         else {
98                         $array_assign['ACCOUNT'] = 'span';
99                 }
100                 return $array_assign;
101         }
102
103         /**
104      * Delete all the records for a particular user
105      *
106      * @param string $user_id user id of the user doing the import
107      */
108     public function mark_deleted_by_user_id(
109         $user_id
110         )
111     {
112         $query = "DELETE FROM $this->table_name 
113                     WHERE assigned_user_id = '$user_id'";
114         $this->db->query($query,true,"Error marking last imported records deleted: ");
115     }
116
117     /**
118      * Undo a single record
119      *
120      * @param string $id specific users_last_import id to undo
121      */
122     public function undoById(
123         $id
124         )
125     {
126         global $current_user;
127         
128         $query1 = "SELECT bean_id, bean_type 
129                         FROM users_last_import
130                         WHERE assigned_user_id = '$current_user->id'
131                             AND id = '$id'
132                             AND deleted=0";
133         
134         $result1 = $this->db->query($query1);
135         if ( !$result1 )
136             return false;
137
138         while ( $row1 = $this->db->fetchByAssoc($result1))
139             $this->_deleteRecord($row1['bean_id'],$row1['bean_type']);
140     
141         return true;
142     }
143     
144     /**
145      * Undo an import
146      *
147      * @param string $module  module being imported into
148      */
149     public function undo(
150         $module
151         )
152         {
153         global $current_user;
154         
155         $query1 = "SELECT bean_id, bean_type 
156                         FROM users_last_import
157                         WHERE assigned_user_id = '$current_user->id'
158                             AND import_module = '$module'
159                             AND deleted=0";
160         
161                 $result1 = $this->db->query($query1);
162         if ( !$result1 )
163             return false;
164
165         while ( $row1 = $this->db->fetchByAssoc($result1))
166             $this->_deleteRecord($row1['bean_id'],$row1['bean_type']);
167     
168         return true;
169         }
170     
171     /**
172      * Deletes a record in a bean
173      *
174      * @param $bean_id
175      * @param $module
176      */
177     protected function _deleteRecord(
178         $bean_id,
179         $module
180         )
181     {
182         static $focus;
183         
184         // load bean
185         if ( !( $focus instanceof $module) ) {
186             require_once($GLOBALS['beanFiles'][$module]);
187             $focus = new $module;
188         }
189         
190         $result = $this->db->query(
191             "DELETE FROM {$focus->table_name} 
192                 WHERE id = '{$bean_id}'"
193             );
194         if (!$result)
195             return false;
196         // Bug 26318: Remove all created e-mail addresses ( from jchi )
197         $result2 = $this->db->query(
198             "SELECT email_address_id
199                 FROM email_addr_bean_rel
200                 WHERE email_addr_bean_rel.bean_id='{$bean_id}'
201                     AND email_addr_bean_rel.bean_module='{$focus->module_dir}'");
202         $this->db->query(
203             "DELETE FROM email_addr_bean_rel
204                 WHERE email_addr_bean_rel.bean_id='{$bean_id}'
205                     AND email_addr_bean_rel.bean_module='{$focus->module_dir}'"
206             );
207         
208         while ( $row2 = $this->db->fetchByAssoc($result2)) {
209             if ( !$this->db->getOne(
210                     "SELECT email_address_id 
211                         FROM email_addr_bean_rel 
212                         WHERE email_address_id = '{$row2['email_address_id']}'") )
213                 $this->db->query(
214                     "DELETE FROM email_addresses 
215                         WHERE id = '{$row2['email_address_id']}'");
216         }
217         
218         if ($focus->hasCustomFields())
219             $this->db->query(
220                 "DELETE FROM {$focus->table_name}_cstm 
221                     WHERE id_c = '{$bean_id}'");
222     }
223     
224     /**
225      * Get a list of bean types created in the import
226      *
227      * @param string $module  module being imported into
228      */
229     public static function getBeansByImport(
230         $module
231         )
232         {
233         global $current_user;
234         
235         $query1 = "SELECT DISTINCT bean_type 
236                         FROM users_last_import
237                         WHERE assigned_user_id = '$current_user->id'
238                             AND import_module = '$module'
239                             AND deleted=0";
240
241                 $result1 = $GLOBALS['db']->query($query1);
242         if ( !$result1 )
243             return array($module);
244
245         $returnarray = array();
246         while ( $row1 = $GLOBALS['db']->fetchByAssoc($result1))
247             $returnarray[] = $row1['bean_type'];
248         
249         return $returnarray;
250     }
251
252 }
253 ?>