]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/UsersLastImport.php
Release 6.5.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-2012 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($user_id)
110     {
111         $query = "DELETE FROM $this->table_name WHERE assigned_user_id = '$user_id'";
112         $this->db->query($query,true,"Error marking last imported records deleted: ");
113     }
114
115     /**
116      * Undo a single record
117      *
118      * @param string $id specific users_last_import id to undo
119      */
120     public function undoById($id)
121     {
122         global $current_user;
123
124         $query1 = "SELECT bean_id, bean_type FROM users_last_import WHERE assigned_user_id = '$current_user->id'
125                    AND id = '$id' AND deleted=0";
126
127         $result1 = $this->db->query($query1);
128         if ( !$result1 )
129             return false;
130
131         while ( $row1 = $this->db->fetchByAssoc($result1))
132             $this->_deleteRecord($row1['bean_id'],$row1['bean_type']);
133
134         return true;
135     }
136
137     /**
138      * Undo an import
139      *
140      * @param string $module  module being imported into
141      */
142     public function undo($module)
143     {
144         global $current_user;
145
146         $query1 = "SELECT bean_id, bean_type FROM users_last_import WHERE assigned_user_id = '$current_user->id'
147                    AND import_module = '$module' AND deleted=0";
148
149         $result1 = $this->db->query($query1);
150         if ( !$result1 )
151             return false;
152
153         while ( $row1 = $this->db->fetchByAssoc($result1))
154             $this->_deleteRecord($row1['bean_id'],$row1['bean_type']);
155
156         return true;
157     }
158
159     /**
160      * Deletes a record in a bean
161      *
162      * @param $bean_id
163      * @param $module
164      */
165     protected function _deleteRecord($bean_id,$module)
166     {
167         static $focus;
168
169         // load bean
170         if ( !( $focus instanceof $module) ) {
171             require_once($GLOBALS['beanFiles'][$module]);
172             $focus = new $module;
173         }
174
175         $result = $this->db->query(
176             "DELETE FROM {$focus->table_name}
177                 WHERE id = '{$bean_id}'"
178             );
179         if (!$result)
180             return false;
181         // Bug 26318: Remove all created e-mail addresses ( from jchi )
182         $result2 = $this->db->query(
183             "SELECT email_address_id
184                 FROM email_addr_bean_rel
185                 WHERE email_addr_bean_rel.bean_id='{$bean_id}'
186                     AND email_addr_bean_rel.bean_module='{$focus->module_dir}'");
187         $this->db->query(
188             "DELETE FROM email_addr_bean_rel
189                 WHERE email_addr_bean_rel.bean_id='{$bean_id}'
190                     AND email_addr_bean_rel.bean_module='{$focus->module_dir}'"
191             );
192
193         while ( $row2 = $this->db->fetchByAssoc($result2)) {
194             if ( !$this->db->getOne(
195                     "SELECT email_address_id
196                         FROM email_addr_bean_rel
197                         WHERE email_address_id = '{$row2['email_address_id']}'") )
198                 $this->db->query(
199                     "DELETE FROM email_addresses
200                         WHERE id = '{$row2['email_address_id']}'");
201         }
202
203         if ($focus->hasCustomFields())
204             $this->db->query(
205                 "DELETE FROM {$focus->table_name}_cstm
206                     WHERE id_c = '{$bean_id}'");
207     }
208
209     /**
210      * Get a list of bean types created in the import
211      *
212      * @param string $module  module being imported into
213      */
214     public static function getBeansByImport($module)
215     {
216         global $current_user;
217
218         $query1 = "SELECT DISTINCT bean_type FROM users_last_import WHERE assigned_user_id = '$current_user->id'
219                    AND import_module = '$module' AND deleted=0";
220
221         $result1 = $GLOBALS['db']->query($query1);
222         if ( !$result1 )
223             return array($module);
224
225         $returnarray = array();
226         while ( $row1 = $GLOBALS['db']->fetchByAssoc($result1))
227             $returnarray[] = $row1['bean_type'];
228
229         return $returnarray;
230     }
231
232 }
233 ?>