]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/ImportMap.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Import / ImportMap.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 for import_map table
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  ********************************************************************************/
44
45
46
47 class ImportMap extends SugarBean
48 {
49         /**
50      * Fields in the table
51      */
52     public $id;
53     public $name;
54     public $module;
55     public $source;
56     public $delimiter;
57     public $enclosure;
58     public $content;
59     public $default_values;
60     public $has_header;
61     public $deleted;
62     public $date_entered;
63     public $date_modified;
64     public $assigned_user_id;
65     public $is_published;
66
67         /**
68      * Set the default settings from Sugarbean
69      */
70     public $table_name  = "import_maps";
71     public $object_name = "ImportMap";
72     public $module_dir  = 'Import';
73     public $new_schema  = true;
74     public $column_fields = array(
75         "id",
76         "name",
77         "module",
78         "source",
79         "enclosure",
80         "delimiter",
81         "content",
82         "has_header",
83         "deleted",
84         "date_entered",
85         "date_modified",
86         "assigned_user_id",
87         "is_published",
88                 );
89
90     /**
91      * Constructor
92      */
93         public function __construct()
94         {
95         parent::SugarBean();
96         }
97
98     /**
99      * Returns an array with the field mappings
100      *
101      * @return array
102      */
103     public function getMapping()
104     {
105         $mapping_arr = array();
106         if ( !empty($this->content) )
107         {
108             $pairs = explode("&",$this->content);
109             foreach ($pairs as $pair){
110                 list($name,$value) = explode("=",$pair);
111                 $mapping_arr[trim($name)] = $value;
112             }
113         }
114         
115         return $mapping_arr;
116     }
117     
118     /**
119      * Sets $content with the mapping given
120      *
121      * @param string $mapping_arr
122      */
123     public function setMapping(
124         $mapping_arr
125         )
126     {
127         $output = array ();
128         foreach ($mapping_arr as $key => $item) {
129             $output[] = "$key=$item";
130         }
131         $this->content = implode("&", $output);
132     }
133     
134     /**
135      * Returns an array with the default field values
136      *
137      * @return array
138      */
139     public function getDefaultValues()
140     {
141         $defa_arr = array();
142         if ( !empty($this->default_values) )
143         {
144             $pairs = explode("&",$this->default_values);
145             foreach ($pairs as $pair){
146                 list($name,$value) = explode("=",$pair);
147                 $defa_arr[trim($name)] = $value;
148             }
149         }
150         
151         return $defa_arr;
152     }
153     
154     /**
155      * Sets $default_values with the default values given
156      *
157      * @param string $defa_arr
158      */
159     public function setDefaultValues(
160         $defa_arr
161         )
162     {
163         $output = array ();
164         foreach ($defa_arr as $key => $item) {
165             $output[] = "$key=$item";
166         }
167         $this->default_values = implode("&", $output);
168     }
169     
170     /**
171      * @see SugarBean::retrieve()
172      */
173     public function retrieve($id = -1, $encode=true,$deleted=true)
174         {
175             $returnVal = parent::retrieve($id,$encode,$deleted);
176             
177             if ( !($returnVal instanceOf $this) ) {
178                 return $returnVal;
179             }
180             
181             if ( $this->source == 'tab' && $this->delimiter == '' ) {
182                 $this->delimiter = "\t";
183             }
184             
185             return $this;
186         }
187
188     /**
189      * Save
190      *
191      * @param  string $owner_id
192      * @param  string $name
193      * @param  string $module
194      * @param  string $source
195      * @param  string $has_header
196      * @param  string $delimiter
197      * @param  string $enclosure
198      * @return bool
199      */
200     public function save( 
201         $owner_id, 
202         $name, 
203         $module, 
204         $source, 
205         $has_header,
206         $delimiter,
207         $enclosure
208         )
209     {
210         $olddefault_values = $this->default_values;
211         $oldcontent = $this->content;
212         
213                 $this->retrieve_by_string_fields(
214             array(
215                 'assigned_user_id'=>$owner_id,
216                 'name'=>$name), 
217             false
218             );
219         
220         // Bug 23354 - Make sure enclosure gets saved as an empty string if
221         // it is an empty string, instead of as a null
222         if ( strlen($enclosure) <= 0 ) $enclosure = ' ';
223         
224         $this->assigned_user_id = $owner_id;
225         $this->name             = $name;
226         $this->source           = $source;
227         $this->module           = $module;
228         $this->delimiter        = $delimiter;
229         $this->enclosure        = $enclosure;
230         $this->has_header       = $has_header;
231         $this->deleted          = 0;
232         $this->default_values   = $olddefault_values;
233         $this->content          = $oldcontent;
234         parent::save();
235         
236         // Bug 29365 - The enclosure character isn't saved correctly if it's a tab using MssqlManager, so resave it
237         if ( $enclosure == '\\t' && $this->db instanceOf MssqlManager ) {
238             $this->enclosure = $enclosure;
239             parent::save();
240         }
241         
242         return 1;
243     }
244
245     /**
246      * Checks to see if the user owns this mapping or is an admin first
247      * If true, then call parent function
248      *
249      * @param $id
250      */
251     public function mark_deleted(
252         $id
253         )
254     {
255         global $current_user;
256         
257         if ( !is_admin($current_user) ) {
258             $other_map = new ImportMap();
259             $other_map->retrieve_by_string_fields(array('id'=> $id), false);
260             
261             if ( $other_map->assigned_user_id != $current_user->id )
262                 return false;
263         }
264         
265         return parent::mark_deleted($id);
266     }
267     
268     /**
269      * Mark an import map as published
270      *
271      * @param  string $user_id
272      * @param  bool   $flag     true if we are publishing or false if we are unpublishing
273      * @return bool
274      */
275     public function mark_published(
276         $user_id,
277         $flag
278         )
279     {
280         global $current_user;
281         
282         if ( !is_admin($current_user) )
283             return false;
284         
285         // check for problems
286         if ($flag) {
287             // if you are trying to publish your map
288             // but there's another published map
289             // by the same name
290             $query_arr = array(
291                 'name'         =>$this->name,
292                 'is_published' =>'yes'
293                 );
294         }
295         else {
296             // if you are trying to unpublish a map
297             // but you own an unpublished map by the same name
298             $query_arr = array(
299                 'name'             => $this->name,
300                 'assigned_user_id' => $user_id,
301                 'is_published'     => 'no'
302                 );
303         }
304         $other_map = new ImportMap();
305         $other_map->retrieve_by_string_fields($query_arr, false);
306
307         // if we find this other map, quit
308         if ( isset($other_map->id) )
309             return false;
310
311         // otherwise update the is_published flag
312         $query = "UPDATE $this->table_name 
313                     SET is_published = '". ($flag?'yes':'no') . "', 
314                         assigned_user_id = '$user_id' 
315                     WHERE id = '{$this->id}'";
316
317         $this->db->query($query,true,"Error marking import map published: ");
318         
319         return true;
320     }
321
322     /**
323      * Similar to retrieve_by_string_fields, but returns multiple objects instead of just one.
324      *
325      * @param  array $fields_array
326      * @return array $obj_arr
327      */
328     public function retrieve_all_by_string_fields(
329         $fields_array
330         )
331         {
332         $query = "SELECT * 
333                     FROM {$this->table_name} 
334                     " . $this->get_where($fields_array);
335                 
336         $result = $this->db->query($query,true," Error: ");
337         $obj_arr = array();
338
339         while ($row = $this->db->fetchByAssoc($result,-1,FALSE) ) {
340                         $focus = new ImportMap();
341
342                         foreach($this->column_fields as $field) {
343                                 if(isset($row[$field])) {
344                                         $focus->$field = $row[$field];
345                                 }
346                         }
347                         $focus->fill_in_additional_detail_fields();
348                         $obj_arr[]=$focus;
349                 }
350         
351                 return $obj_arr;
352         }
353
354 }
355
356
357 ?>