]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/maps/ImportMap.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Import / maps / ImportMap.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 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     var $disable_custom_fields = true;
75     public $column_fields = array(
76         "id",
77         "name",
78         "module",
79         "source",
80         "enclosure",
81         "delimiter",
82         "content",
83         "has_header",
84         "deleted",
85         "date_entered",
86         "date_modified",
87         "assigned_user_id",
88         "is_published",
89         );
90
91     /**
92      * Constructor
93      */
94     public function __construct()
95     {
96         parent::SugarBean();
97     }
98
99     /**
100      * Returns an array with the field mappings
101      *
102      * @return array
103      */
104     public function getMapping()
105     {
106         $mapping_arr = array();
107         if ( !empty($this->content) )
108         {
109             $pairs = explode("&",$this->content);
110             foreach ($pairs as $pair){
111                 list($name,$value) = explode("=",$pair);
112                 $mapping_arr[trim($name)] = $value;
113             }
114         }
115
116         return $mapping_arr;
117     }
118
119     /**
120      * Sets $content with the mapping given
121      *
122      * @param string $mapping_arr
123      */
124     public function setMapping(
125         $mapping_arr
126         )
127     {
128         $output = array ();
129         foreach ($mapping_arr as $key => $item) {
130             $output[] = "$key=$item";
131         }
132         $this->content = implode("&", $output);
133     }
134
135     /**
136      * Returns an array with the default field values
137      *
138      * @return array
139      */
140     public function getDefaultValues()
141     {
142         $defa_arr = array();
143         if ( !empty($this->default_values) )
144         {
145             $pairs = explode("&",$this->default_values);
146             foreach ($pairs as $pair){
147                 list($name,$value) = explode("=",$pair);
148                 $defa_arr[trim($name)] = $value;
149             }
150         }
151
152         return $defa_arr;
153     }
154
155     /**
156      * Sets $default_values with the default values given
157      *
158      * @param string $defa_arr
159      */
160     public function setDefaultValues(
161         $defa_arr
162         )
163     {
164         $output = array ();
165         foreach ($defa_arr as $key => $item) {
166             $output[] = "$key=$item";
167         }
168         $this->default_values = implode("&", $output);
169     }
170
171     /**
172      * @see SugarBean::retrieve()
173      */
174     public function retrieve($id = -1, $encode=true,$deleted=true)
175         {
176             $returnVal = parent::retrieve($id,$encode,$deleted);
177
178             if ( !($returnVal instanceOf $this) ) {
179                 return $returnVal;
180             }
181
182             if ( $this->source == 'tab' && $this->delimiter == '' ) {
183                 $this->delimiter = "\t";
184             }
185
186             return $this;
187         }
188
189     /**
190      * Save
191      *
192      * @param  string $owner_id
193      * @param  string $name
194      * @param  string $module
195      * @param  string $source
196      * @param  string $has_header
197      * @param  string $delimiter
198      * @param  string $enclosure
199      * @return bool
200      */
201     public function save(
202         $owner_id,
203         $name,
204         $module,
205         $source,
206         $has_header,
207         $delimiter,
208         $enclosure
209         )
210     {
211         $olddefault_values = $this->default_values;
212         $oldcontent = $this->content;
213
214         $this->retrieve_by_string_fields(
215             array(
216                 'assigned_user_id'=>$owner_id,
217                 'name'=>$name),
218             false
219             );
220
221         // Bug 23354 - Make sure enclosure gets saved as an empty string if
222         // it is an empty string, instead of as a null
223         if ( strlen($enclosure) <= 0 ) $enclosure = ' ';
224
225         $this->assigned_user_id = $owner_id;
226         $this->name             = $name;
227         $this->source           = $source;
228         $this->module           = $module;
229         $this->delimiter        = $delimiter;
230         $this->enclosure        = $enclosure;
231         $this->has_header       = $has_header;
232         $this->deleted          = 0;
233         $this->default_values   = $olddefault_values;
234         $this->content          = $oldcontent;
235         parent::save();
236
237         // Bug 29365 - The enclosure character isn't saved correctly if it's a tab using MssqlManager, so resave it
238         if ( $enclosure == '\\t' && $this->db instanceOf MssqlManager ) {
239             $this->enclosure = $enclosure;
240             parent::save();
241         }
242
243         return 1;
244     }
245
246     /**
247      * Checks to see if the user owns this mapping or is an admin first
248      * If true, then call parent function
249      *
250      * @param $id
251      */
252     public function mark_deleted(
253         $id
254         )
255     {
256         global $current_user;
257
258         if ( !is_admin($current_user) ) {
259             $other_map = new ImportMap();
260             $other_map->retrieve_by_string_fields(array('id'=> $id), false);
261
262             if ( $other_map->assigned_user_id != $current_user->id )
263                 return false;
264         }
265
266         return parent::mark_deleted($id);
267     }
268
269     /**
270      * Mark an import map as published
271      *
272      * @param  string $user_id
273      * @param  bool   $flag     true if we are publishing or false if we are unpublishing
274      * @return bool
275      */
276     public function mark_published(
277         $user_id,
278         $flag
279         )
280     {
281         global $current_user;
282
283         if ( !is_admin($current_user) )
284             return false;
285
286         // check for problems
287         if ($flag) {
288             // if you are trying to publish your map
289             // but there's another published map
290             // by the same name
291             $query_arr = array(
292                 'name'         =>$this->name,
293                 'is_published' =>'yes'
294                 );
295         }
296         else {
297             // if you are trying to unpublish a map
298             // but you own an unpublished map by the same name
299             $query_arr = array(
300                 'name'             => $this->name,
301                 'assigned_user_id' => $user_id,
302                 'is_published'     => 'no'
303                 );
304         }
305         $other_map = new ImportMap();
306         $other_map->retrieve_by_string_fields($query_arr, false);
307
308         // if we find this other map, quit
309         if ( isset($other_map->id) )
310             return false;
311
312         // otherwise update the is_published flag
313         $query = "UPDATE $this->table_name
314                     SET is_published = '". ($flag?'yes':'no') . "',
315                         assigned_user_id = '$user_id'
316                     WHERE id = '{$this->id}'";
317
318         $this->db->query($query,true,"Error marking import map published: ");
319
320         return true;
321     }
322
323     /**
324      * Similar to retrieve_by_string_fields, but returns multiple objects instead of just one.
325      *
326      * @param  array $fields_array
327      * @return array $obj_arr
328      */
329     public function retrieve_all_by_string_fields(
330         $fields_array
331         )
332     {
333         $query = "SELECT *
334                     FROM {$this->table_name}
335                     " . $this->get_where($fields_array);
336
337         $result = $this->db->query($query,true," Error: ");
338         $obj_arr = array();
339
340         while ($row = $this->db->fetchByAssoc($result,FALSE) ) {
341             $focus = new ImportMap();
342
343             foreach($this->column_fields as $field) {
344                 if(isset($row[$field])) {
345                     $focus->$field = $row[$field];
346                 }
347             }
348             $focus->fill_in_additional_detail_fields();
349             $obj_arr[]=$focus;
350         }
351
352         return $obj_arr;
353     }
354
355     /**
356      * set and get field elements in request field to and from user preferences
357      *
358      * @param  array $fields_array
359      * @return array $obj_arr
360      */
361     public function set_get_import_wizard_fields($ForceValsArr = '')
362     {
363         global $current_user;
364         $set = false;
365         //list of field values we track during import wizard
366         $import_step_fields = array(
367         //step1
368           //  'import_module', 'source', 'custom_enclosure', 'custom_enclosure_other', 'custom_delimiter', 'type',
369         //step2
370            // 'custom_delimiter', 'custom_enclosure', 'type', 'source', 'source_id', 'import_module', 'has_header',
371          //step3
372             'display_tabs_def','custom_delimiter', 'custom_enclosure', 'import_type', 'source', 'source_id', 'import_module', 'has_header', 'importlocale_charset',
373             'importlocale_dateformat', 'importlocale_timeformat', 'importlocale_timezone', 'importlocale_currency',
374             'importlocale_default_currency_significant_digits', 'importlocale_num_grp_sep', 'importlocale_dec_sep',
375         '   importlocale_default_locale_name_format');
376
377         //retrieve user preferences and populate preference array
378         $preference_values_str = $current_user->getPreference('field_values', 'import');
379         $preference_values = json_decode($preference_values_str,true);
380
381         foreach ($import_step_fields as $val){
382             //overwrite preference array with new values from request if the value is different or new
383             if((isset($_REQUEST[$val]) && !isset($preference_values[$val])) || (isset($_REQUEST[$val]) && $preference_values[$val] != $_REQUEST[$val])){
384                 $preference_values[$val] = $_REQUEST[$val];
385                 $set = true;
386             }
387         }
388
389         //force the values to passed in array if array is set
390         if(!empty($ForceValsArr) && is_array($ForceValsArr)){
391             foreach ($ForceValsArr as $forceKey=>$forceVal){
392                 $preference_values[$forceKey] = $forceVal;
393                 $set = true;
394             }
395         }
396
397         //set preferences if any changes were made and return the new array
398         if($set){
399             $preference_values_str =  json_encode($preference_values);
400             $current_user->setPreference('field_values', $preference_values_str, 0, 'import');
401         }
402         if(empty($preference_values)){
403             return array();
404         }
405
406         return $preference_values;
407     }
408
409 }
410
411
412 ?>