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.
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 public $column_fields = array(
93 public function __construct()
99 * Returns an array with the field mappings
103 public function getMapping()
105 $mapping_arr = array();
106 if ( !empty($this->content) )
108 $pairs = explode("&",$this->content);
109 foreach ($pairs as $pair){
110 list($name,$value) = explode("=",$pair);
111 $mapping_arr[trim($name)] = $value;
119 * Sets $content with the mapping given
121 * @param string $mapping_arr
123 public function setMapping(
128 foreach ($mapping_arr as $key => $item) {
129 $output[] = "$key=$item";
131 $this->content = implode("&", $output);
135 * Returns an array with the default field values
139 public function getDefaultValues()
142 if ( !empty($this->default_values) )
144 $pairs = explode("&",$this->default_values);
145 foreach ($pairs as $pair){
146 list($name,$value) = explode("=",$pair);
147 $defa_arr[trim($name)] = $value;
155 * Sets $default_values with the default values given
157 * @param string $defa_arr
159 public function setDefaultValues(
164 foreach ($defa_arr as $key => $item) {
165 $output[] = "$key=$item";
167 $this->default_values = implode("&", $output);
171 * @see SugarBean::retrieve()
173 public function retrieve($id = -1, $encode=true,$deleted=true)
175 $returnVal = parent::retrieve($id,$encode,$deleted);
177 if ( !($returnVal instanceOf $this) ) {
181 if ( $this->source == 'tab' && $this->delimiter == '' ) {
182 $this->delimiter = "\t";
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
200 public function save(
210 $olddefault_values = $this->default_values;
211 $oldcontent = $this->content;
213 $this->retrieve_by_string_fields(
215 'assigned_user_id'=>$owner_id,
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 = ' ';
224 $this->assigned_user_id = $owner_id;
226 $this->source = $source;
227 $this->module = $module;
228 $this->delimiter = $delimiter;
229 $this->enclosure = $enclosure;
230 $this->has_header = $has_header;
232 $this->default_values = $olddefault_values;
233 $this->content = $oldcontent;
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;
246 * Checks to see if the user owns this mapping or is an admin first
247 * If true, then call parent function
251 public function mark_deleted(
255 global $current_user;
257 if ( !is_admin($current_user) ) {
258 $other_map = new ImportMap();
259 $other_map->retrieve_by_string_fields(array('id'=> $id), false);
261 if ( $other_map->assigned_user_id != $current_user->id )
265 return parent::mark_deleted($id);
269 * Mark an import map as published
271 * @param string $user_id
272 * @param bool $flag true if we are publishing or false if we are unpublishing
275 public function mark_published(
280 global $current_user;
282 if ( !is_admin($current_user) )
285 // check for problems
287 // if you are trying to publish your map
288 // but there's another published map
291 'name' =>$this->name,
292 'is_published' =>'yes'
296 // if you are trying to unpublish a map
297 // but you own an unpublished map by the same name
299 'name' => $this->name,
300 'assigned_user_id' => $user_id,
301 'is_published' => 'no'
304 $other_map = new ImportMap();
305 $other_map->retrieve_by_string_fields($query_arr, false);
307 // if we find this other map, quit
308 if ( isset($other_map->id) )
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}'";
317 $this->db->query($query,true,"Error marking import map published: ");
323 * Similar to retrieve_by_string_fields, but returns multiple objects instead of just one.
325 * @param array $fields_array
326 * @return array $obj_arr
328 public function retrieve_all_by_string_fields(
333 FROM {$this->table_name}
334 " . $this->get_where($fields_array);
336 $result = $this->db->query($query,true," Error: ");
339 while ($row = $this->db->fetchByAssoc($result,-1,FALSE) ) {
340 $focus = new ImportMap();
342 foreach($this->column_fields as $field) {
343 if(isset($row[$field])) {
344 $focus->$field = $row[$field];
347 $focus->fill_in_additional_detail_fields();