]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/UsersLastImport.php
Release 6.2.0
[Github/sugarcrm.git] / modules / Import / UsersLastImport.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition 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     var $disable_custom_fields = true;
67     public $column_fields = array(
68         "id",
69         "assigned_user_id",
70         "bean_type",
71         "bean_id",
72         "deleted"
73         );
74     public $new_schema = true;
75     public $additional_column_fields = Array();
76
77     /**
78      * Constructor
79      */
80     public function __construct()
81     {
82         parent::SugarBean();
83     }
84
85     /**
86      * Extends SugarBean::listviewACLHelper
87      *
88      * @return array
89      */
90     public function listviewACLHelper()
91     {
92         $array_assign = parent::listviewACLHelper();
93         $is_owner = false;
94         if ( !ACLController::moduleSupportsACL('Accounts')
95                 || ACLController::checkAccess('Accounts', 'view', $is_owner) ) {
96             $array_assign['ACCOUNT'] = 'a';
97         }
98         else {
99             $array_assign['ACCOUNT'] = 'span';
100         }
101         return $array_assign;
102     }
103
104     /**
105      * Delete all the records for a particular user
106      *
107      * @param string $user_id user id of the user doing the import
108      */
109     public function mark_deleted_by_user_id(
110         $user_id
111         )
112     {
113         $query = "DELETE FROM $this->table_name
114                     WHERE assigned_user_id = '$user_id'";
115         $this->db->query($query,true,"Error marking last imported records deleted: ");
116     }
117
118     /**
119      * Undo a single record
120      *
121      * @param string $id specific users_last_import id to undo
122      */
123     public function undoById(
124         $id
125         )
126     {
127         global $current_user;
128
129         $query1 = "SELECT bean_id, bean_type
130                         FROM users_last_import
131                         WHERE assigned_user_id = '$current_user->id'
132                             AND id = '$id'
133                             AND deleted=0";
134
135         $result1 = $this->db->query($query1);
136         if ( !$result1 )
137             return false;
138
139         while ( $row1 = $this->db->fetchByAssoc($result1))
140             $this->_deleteRecord($row1['bean_id'],$row1['bean_type']);
141
142         return true;
143     }
144
145     /**
146      * Undo an import
147      *
148      * @param string $module  module being imported into
149      */
150     public function undo(
151         $module
152         )
153     {
154         global $current_user;
155
156         $query1 = "SELECT bean_id, bean_type
157                         FROM users_last_import
158                         WHERE assigned_user_id = '$current_user->id'
159                             AND import_module = '$module'
160                             AND deleted=0";
161
162         $result1 = $this->db->query($query1);
163         if ( !$result1 )
164             return false;
165
166         while ( $row1 = $this->db->fetchByAssoc($result1))
167             $this->_deleteRecord($row1['bean_id'],$row1['bean_type']);
168
169         return true;
170     }
171
172     /**
173      * Deletes a record in a bean
174      *
175      * @param $bean_id
176      * @param $module
177      */
178     protected function _deleteRecord(
179         $bean_id,
180         $module
181         )
182     {
183         static $focus;
184
185         // load bean
186         if ( !( $focus instanceof $module) ) {
187             require_once($GLOBALS['beanFiles'][$module]);
188             $focus = new $module;
189         }
190
191         $result = $this->db->query(
192             "DELETE FROM {$focus->table_name}
193                 WHERE id = '{$bean_id}'"
194             );
195         if (!$result)
196             return false;
197         // Bug 26318: Remove all created e-mail addresses ( from jchi )
198         $result2 = $this->db->query(
199             "SELECT email_address_id
200                 FROM email_addr_bean_rel
201                 WHERE email_addr_bean_rel.bean_id='{$bean_id}'
202                     AND email_addr_bean_rel.bean_module='{$focus->module_dir}'");
203         $this->db->query(
204             "DELETE FROM email_addr_bean_rel
205                 WHERE email_addr_bean_rel.bean_id='{$bean_id}'
206                     AND email_addr_bean_rel.bean_module='{$focus->module_dir}'"
207             );
208
209         while ( $row2 = $this->db->fetchByAssoc($result2)) {
210             if ( !$this->db->getOne(
211                     "SELECT email_address_id
212                         FROM email_addr_bean_rel
213                         WHERE email_address_id = '{$row2['email_address_id']}'") )
214                 $this->db->query(
215                     "DELETE FROM email_addresses
216                         WHERE id = '{$row2['email_address_id']}'");
217         }
218
219         if ($focus->hasCustomFields())
220             $this->db->query(
221                 "DELETE FROM {$focus->table_name}_cstm
222                     WHERE id_c = '{$bean_id}'");
223     }
224
225     /**
226      * Get a list of bean types created in the import
227      *
228      * @param string $module  module being imported into
229      */
230     public static function getBeansByImport(
231         $module
232         )
233     {
234         global $current_user;
235
236         $query1 = "SELECT DISTINCT bean_type
237                         FROM users_last_import
238                         WHERE assigned_user_id = '$current_user->id'
239                             AND import_module = '$module'
240                             AND deleted=0";
241
242         $result1 = $GLOBALS['db']->query($query1);
243         if ( !$result1 )
244             return array($module);
245
246         $returnarray = array();
247         while ( $row1 = $GLOBALS['db']->fetchByAssoc($result1))
248             $returnarray[] = $row1['bean_type'];
249
250         return $returnarray;
251     }
252
253 }
254 ?>