component::getFieldValue()   C
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56
Metric Value
cc 7
eloc 15
nc 10
nop 3
dl 0
loc 23
ccs 0
cts 21
cp 0
crap 56
rs 6.7272
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-2013 SugarCRM Inc.
6
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU Affero General Public License version 3 as published by the
12
 * Free Software Foundation with the addition of the following permission added
13
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
14
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
15
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16
 *
17
 * This program is distributed in the hope that it will be useful, but WITHOUT
18
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
20
 * details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License along with
23
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
 * 02110-1301 USA.
26
 *
27
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
28
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
29
 *
30
 * The interactive user interfaces in modified source and object code versions
31
 * of this program must display Appropriate Legal Notices, as required under
32
 * Section 5 of the GNU Affero General Public License version 3.
33
 *
34
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
35
 * these Appropriate Legal Notices must retain the display of the "Powered by
36
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
37
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
38
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
39
 ********************************************************************************/
40
41
/**
42
 * Connector component
43
 * @api
44
 */
45
class component{
46
	protected $_has_testing_enabled = false;
47
	protected $_source;
48
49
	public function __construct() {}
50
51
	public function init() {}
52
53
	/**
54
	 * fillBean
55
	 * This function wraps the call to getItem, but takes an additional SugarBean argument
56
	 * and loads the SugarBean's fields with the results as defined in the connector
57
	 * loadBean configuration mapping
58
	 *
59
	 * @param $args Array of arguments to pass into getItem
60
	 * @param $module String value of the module to map bean to
61
	 * @param $bean SugarBean instance to load values into
62
	 * @throws Exception Thrown if results could not be loaded into bean
63
	 */
64
	public function fillBean($args=array(), $module=null, $bean=null) {
65
	    $result = null;
66
		if(is_object($bean)) {
67
		   $args = $this->mapInput($args, $module);
68
	       $item = $this->_source->getItem($args, $module);
69
	       $result = $this->mapOutput($bean, $item);
70
	    } else if(!empty($module) && ($bean = loadBean($module))) {
0 ignored issues
show
Deprecated Code introduced by
The function loadBean() has been deprecated with message: use SugarModule::loadBean() instead

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
71
	       return $this->fillBean($args, $module, $bean);
72
	    } else {
73
	       throw new Exception("Invalid bean");
74
	    }
75
	    return $result;
76
	}
77
78
	/**
79
	 * fillBeans
80
	 * This function wraps the call to getList, but takes an additional Array argument
81
	 * and loads the SugarBean's fields with the results as defined in the connector
82
	 * loadBean configuration mapping
83
	 *
84
	 * @param $args Array of arguments to pass into getItem
85
	 * @param $module String value of the module to map bean to
86
	 * @param $bean Array to load SugarBean intances into
87
	 * @throws Exception Thrown if errors are found
88
	 */
89
	public function fillBeans($args=array(), $module=null, $beans=array()) {
90
		$results = array();
91
		$args = $this->mapInput($args, $module);
92
		if(empty($args)) {
93
		   $GLOBALS['log']->fatal($GLOBALS['app_strings']['ERR_MISSING_MAPPING_ENTRY_FORM_MODULE']);
94
		   throw new Exception($GLOBALS['app_strings']['ERR_MISSING_MAPPING_ENTRY_FORM_MODULE']);
95
		}
96
97
98
		require_once('include/connectors/filters/FilterFactory.php');
99
		$filter = FilterFactory::getInstance(get_class($this->_source));
100
		$list = $filter->getList($args, $module);
101
102
		if(!empty($list)) {
103
			$resultSize = count($list);
104
			if(!empty($beans)) {
105
			   if(count($beans) != $resultSize) {
106
			   	  throw new Exception($GLOBALS['app_strings']['ERR_CONNECTOR_FILL_BEANS_SIZE_MISMATCH']);
107
			   }
108
			} else {
109
110
			   for($x=0; $x < $resultSize; $x++) {
111
			   	   $beans[$x] = loadBean($module);
0 ignored issues
show
Deprecated Code introduced by
The function loadBean() has been deprecated with message: use SugarModule::loadBean() instead

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
112
			   }
113
			}
114
115
		    $keys = array_keys($beans);
116
			$count = 0;
117
			foreach($list as $entry) {
118
				   //Change the result keys to lower case.  This has important ramifications.
119
				   //This was done because the listviewdefs.php files may not know the proper casing
120
				   //of the fields to display.  We change the keys to lowercase so that the values
121
				   //may be mapped to the beans without having to rely on the proper string casing
122
				   //in the listviewdefs.php files.
123
				   $entry = array_change_key_case($entry, CASE_LOWER);
124
			   	   $results[] = $this->mapOutput($beans[$keys[$count]], $entry);
125
			   	   $count++;
126
			}
127
128
			$field_defs = $this->getFieldDefs();
129
		    $map = $this->getMapping();
130
			$hasOptions = !empty($map['options']) ? true : false;
131
			if($hasOptions) {
132
			   $options = $map['options'];
133
			   $optionFields = array();
134
135
			   foreach($field_defs as $name=>$field) {
136
			   	       if(!empty($field['options']) && !empty($map['options'][$field['options']]) && !empty($map['beans'][$module][$name])) {
137
			   	       	  $optionFields[$name] = $map['beans'][$module][$name];
138
			   	       }
139
			   }
140
141
			   foreach($results as $key=>$bean) {
142
			   	   foreach($optionFields as $sourceField=>$sugarField) {
143
			   	   	       $options_map = $options[$field_defs[$sourceField]['options']];
144
			   	   	       $results[$key]->$sugarField =  !empty($options_map[$results[$key]->$sugarField]) ? $options_map[$results[$key]->$sugarField] : $results[$key]->$sugarField;
145
			   	   }
146
			   } //foreach
147
			}
148
		}
149
150
		return $results;
151
	}
152
153
154
155
	/**
156
	 * Obtain a list of items
157
	 *
158
	 * @param string $module ideally this method should return a list of beans of type $module.
159
	 * @param Mixed $args this represents the 'query' on the data source.
160
	 */
161
162
163
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
164
 	 * Given a bean, persist it to a data source
165
 	 *
166
 	 * @param SugarBean $bean
167
 	 */
168
 	public function save($bean){}
169
170
171
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
172
 	 * getConfig
173
 	 * Returns the configuration Array as definied in the config.php file
174
 	 *
175
 	 * @return $config Array of the configuration mappings as defined in config.php
0 ignored issues
show
Documentation introduced by
The doc-type $config could not be parsed: Unknown type name "$config" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
176
 	 */
177
	public function getConfig(){
178
 		return $this->_source->getConfig;
179
 	}
180
181
182
 	public function getFieldDefs() {
183
 		return $this->_source->getFieldDefs();
184
 	}
185
186
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
187
 	 * setConfig
188
 	 * Used by the Factories to set the config on the corresponding object
189
 	 *
190
 	 * @param array $config this file will be specified in config file corresponding to the wrapper or data source we are
191
 	 * using. The name would be something like hoovers.php if we are using the hoovers data source or hoovers wrapper
192
 	 * and it would exist in the same directory as the connector and/or wrapper.
193
 	 * Note that the confing specified at the connector level takes precendence over the config specified at the wrapper level.
194
 	 * This logic is performed in ConnectorFactory.php
195
 	 */
196
	public function setConfig($config){
197
 		$this->_source->setConfig($config);
198
 	}
199
200
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
201
 	 * mapInput
202
 	 */
203
 	public function mapInput($inputData, $module){
204
 		$input_params = array();
205
 		$map = $this->getMapping();
206
 		if(empty($map['beans'][$module])) {
207
 		   return $input_params;
208
 		}
209
 		$mapping = array_flip($map['beans'][$module]);
210
 		$field_defs = $this->getFieldDefs();
211
 		foreach($inputData as $arg=>$val){
212
 			if(!empty($mapping[$arg]) || !empty($field_defs[$arg])) {
213
 				if(!empty($mapping[$arg])){
214
 					$arg = $mapping[$arg];
215
 				}
216
				if(!empty($field_defs[$arg]['input'])){
217
					$in_field = $field_defs[$arg]['input'];
218
					$temp = explode('.', $in_field);
219
					$eval_code = "\$input_params";
220
					foreach($temp as $arr_key) {
221
						$eval_code .= '[\'' . $arr_key . '\']';
222
					}
223
					$eval_code .= "= \$val;";
224
					eval($eval_code);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
225
				} else {
226
					$input_params[$arg] = $val;
227
				}
228
 			} //if
229
		} //foreach
230
		return $input_params;
231
 	}
232
233
 	public function mapOutput($bean, $result){
234
 		if(is_object($bean)) {
235
 			$map = $this->getMapping();
236
 			$mapping = $map['beans'][$bean->module_dir];
237
238
 		    //Check for situation where nothing was mapped or the only field mapped was id
239
 			if(empty($mapping) || (count($mapping) == 1 && isset($mapping['id']))) {
240
 			   $GLOBALS['log']->error($GLOBALS['mod_strings']['ERROR_NO_DISPLAYABLE_MAPPED_FIELDS']);
241
 			   throw new Exception($GLOBALS['mod_strings']['ERROR_NO_DISPLAYABLE_MAPPED_FIELDS']);
242
 			}
243
244
 			$mapped = array();
245
 			if(!empty($mapping)) {
246
		 		foreach($mapping as $source_field => $sugar_field){
247
		 			$bean->$sugar_field = $this->getFieldValue($bean, $result, $source_field);
248
		 			$mapped[$source_field] = $bean->$sugar_field;
249
		 		}
250
 			} else {
251
 				foreach($result as $key=>$value) {
252
 					if(isset($bean->field_defs[$key])) {
253
 					   $bean->$key = $value;
254
 					}
255
 				}
256
 			}
257
258
 			//set the data_source_id field which contain the unique id for the source
259
 			$source_field = 'id';
260
 			$bean->data_source_id = $this->getFieldValue($bean, $result, $source_field);
261
262
 			//now let's check for any fields that have not been mapped which may be required
263
 			$required_fields = $this->_source->getFieldsWithParams('required', true);
264
 			if(!empty($required_fields)){
265
 				foreach($required_fields as $key => $def){
266
 					if(empty($mapped[$key])){
267
 						$bean->$key = $this->getFieldValue($bean, $result, $key);
268
 					}
269
 				}
270
 			}
271
272
	 		return $bean;
273
 		}
274
 		return $bean;
275
 	}
276
277
 	private function getFieldValue($bean, $result, $source_field){
278
 		$def = $this->getModuleFieldDef($bean->module_dir, $source_field);
279
 		$out_field = $source_field;
280
		if(!empty($def['output'])){
281
	 		$out_field = $def['output'];
282
	 	}
283
284
 		$value = SugarArray::staticGet($result, $out_field);
285
286
		if(is_array($def)){
287
			if(!empty($def['function'])){
288
				$function = $def['function'];
289
				if(is_array($function) && isset($function['name'])){
290
					$function = $def['function']['name'];
291
					if(!empty($def['function']['include'])){
292
						require_once($def['function']['include']);
293
					}
294
				 }
295
				 $value = $function($bean, $out_field, $value);
296
			 }
297
		 }
298
		 return $value;
299
 	}
300
301
 	public function saveConfig($persister=null) {
302
 		$this->_source->saveConfig($persister);
303
 	}
304
305
 	public function loadConfig($persister=null) {
306
		$this->_source->loadConfig($persister);
307
 	}
308
309
 	public function setMapping($map=array()) {
310
 		$this->_source->setMapping($map);
311
 	}
312
313
 	public function getMapping() {
314
 		return $this->_source->getMapping();
315
 	}
316
317
	public function getModuleMapping($module) {
318
 		$map = $this->getMapping();
319
		return !empty($map['beans'][$module]) ? $map['beans'][$module] : array();
320
 	}
321
322
 	public function getModuleFieldDef($module, $field){
323
 		$map = $this->getMapping();
324
 		$field_defs = $this->getFieldDefs();
325
 		if(!empty($map['beans'][$module][$field])){
326
 			$source_field = $field;
327
 			if(!empty($field_defs[$source_field])){
328
 				return $field_defs[$source_field];
329
 			}elseif(!empty($field_defs[$field])){
330
 				return $field_defs[$field];
331
 			}else{
332
 				return $field;
333
 			}
334
 		}elseif(!empty($field_defs[$field])){
335
 				return $field_defs[$field];
336
 		}else{
337
 				return $field;
338
 		}
339
 	}
340
341
 	public function getSource(){
342
 		return $this->_source;
343
 	}
344
345
 	public function setSource($source){
346
 		$this->_source = $source;
347
 	}
348
}
349
?>
350