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-2013 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
40 * Description: Bean for import_map table
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 ********************************************************************************/
47 class ImportMap extends SugarBean
59 public $default_values;
63 public $date_modified;
64 public $assigned_user_id;
68 * Set the default settings from Sugarbean
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(
94 public function __construct()
100 * Returns an array with the field mappings
104 public function getMapping()
106 $mapping_arr = array();
107 if ( !empty($this->content) )
109 $pairs = explode("&",$this->content);
110 foreach ($pairs as $pair){
111 list($name,$value) = explode("=",$pair);
112 $mapping_arr[trim($name)] = $value;
120 * Sets $content with the mapping given
122 * @param string $mapping_arr
124 public function setMapping(
129 foreach ($mapping_arr as $key => $item) {
130 $output[] = "$key=$item";
132 $this->content = implode("&", $output);
136 * Returns an array with the default field values
140 public function getDefaultValues()
143 if ( !empty($this->default_values) )
145 $pairs = explode("&",$this->default_values);
146 foreach ($pairs as $pair){
147 list($name,$value) = explode("=",$pair);
148 $defa_arr[trim($name)] = $value;
156 * Sets $default_values with the default values given
158 * @param string $defa_arr
160 public function setDefaultValues(
165 foreach ($defa_arr as $key => $item) {
166 $output[] = "$key=$item";
168 $this->default_values = implode("&", $output);
172 * @see SugarBean::retrieve()
174 public function retrieve($id = -1, $encode=true,$deleted=true)
176 $returnVal = parent::retrieve($id,$encode,$deleted);
178 if ( !($returnVal instanceOf $this) ) {
182 if ( $this->source == 'tab' && $this->delimiter == '' ) {
183 $this->delimiter = "\t";
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
201 public function save(
211 $olddefault_values = $this->default_values;
212 $oldcontent = $this->content;
214 $this->retrieve_by_string_fields(
216 'assigned_user_id'=>$owner_id,
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 = ' ';
225 $this->assigned_user_id = $owner_id;
227 $this->source = $source;
228 $this->module = $module;
229 $this->delimiter = $delimiter;
230 $this->enclosure = $enclosure;
231 $this->has_header = $has_header;
233 $this->default_values = $olddefault_values;
234 $this->content = $oldcontent;
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;
247 * Checks to see if the user owns this mapping or is an admin first
248 * If true, then call parent function
252 public function mark_deleted(
256 global $current_user;
258 if ( !is_admin($current_user) ) {
259 $other_map = new ImportMap();
260 $other_map->retrieve_by_string_fields(array('id'=> $id), false);
262 if ( $other_map->assigned_user_id != $current_user->id )
266 return parent::mark_deleted($id);
270 * Mark an import map as published
272 * @param string $user_id
273 * @param bool $flag true if we are publishing or false if we are unpublishing
276 public function mark_published(
281 global $current_user;
283 if ( !is_admin($current_user) )
286 // check for problems
288 // if you are trying to publish your map
289 // but there's another published map
292 'name' =>$this->name,
293 'is_published' =>'yes'
297 // if you are trying to unpublish a map
298 // but you own an unpublished map by the same name
300 'name' => $this->name,
301 'assigned_user_id' => $user_id,
302 'is_published' => 'no'
305 $other_map = new ImportMap();
306 $other_map->retrieve_by_string_fields($query_arr, false);
308 // if we find this other map, quit
309 if ( isset($other_map->id) )
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}'";
318 $this->db->query($query,true,"Error marking import map published: ");
324 * Similar to retrieve_by_string_fields, but returns multiple objects instead of just one.
326 * @param array $fields_array
327 * @return array $obj_arr
329 public function retrieve_all_by_string_fields(
334 FROM {$this->table_name}
335 " . $this->get_where($fields_array);
337 $result = $this->db->query($query,true," Error: ");
340 while ($row = $this->db->fetchByAssoc($result,FALSE) ) {
341 $focus = new ImportMap();
343 foreach($this->column_fields as $field) {
344 if(isset($row[$field])) {
345 $focus->$field = $row[$field];
348 $focus->fill_in_additional_detail_fields();
356 * set and get field elements in request field to and from user preferences
358 * @param array $fields_array
359 * @return array $obj_arr
361 public function set_get_import_wizard_fields($ForceValsArr = '')
363 global $current_user;
365 //list of field values we track during import wizard
366 $import_step_fields = array(
368 // 'import_module', 'source', 'custom_enclosure', 'custom_enclosure_other', 'custom_delimiter', 'type',
370 // 'custom_delimiter', 'custom_enclosure', 'type', 'source', 'source_id', 'import_module', 'has_header',
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');
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);
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];
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;
397 //set preferences if any changes were made and return the new array
399 $preference_values_str = json_encode($preference_values);
400 $current_user->setPreference('field_values', $preference_values_str, 0, 'import');
402 if(empty($preference_values)){
406 return $preference_values;