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.
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 ********************************************************************************/
41 protected $_has_testing_enabled = false;
44 public function __construct() {}
46 public function init() {}
50 * This function wraps the call to getItem, but takes an additional SugarBean argument
51 * and loads the SugarBean's fields with the results as defined in the connector
52 * loadBean configuration mapping
54 * @param $args Array of arguments to pass into getItem
55 * @param $module String value of the module to map bean to
56 * @param $bean SugarBean instance to load values into
57 * @throws Exception Thrown if results could not be loaded into bean
59 public function fillBean($args=array(), $module=null, $bean=null) {
61 if(is_object($bean)) {
62 $args = $this->mapInput($args, $module);
63 $item = $this->_source->getItem($args, $module);
64 $result = $this->mapOutput($bean, $item);
65 } else if(!empty($module) && ($bean = loadBean($module))) {
66 return $this->fillBean($args, $module, $bean);
68 throw new Exception("Invalid bean");
75 * This function wraps the call to getList, but takes an additional Array argument
76 * and loads the SugarBean's fields with the results as defined in the connector
77 * loadBean configuration mapping
79 * @param $args Array of arguments to pass into getItem
80 * @param $module String value of the module to map bean to
81 * @param $bean Array to load SugarBean intances into
82 * @throws Exception Thrown if errors are found
84 public function fillBeans($args=array(), $module=null, $beans=array()) {
86 $args = $this->mapInput($args, $module);
88 $GLOBALS['log']->fatal($GLOBALS['app_strings']['ERR_MISSING_MAPPING_ENTRY_FORM_MODULE']);
89 throw new Exception($GLOBALS['app_strings']['ERR_MISSING_MAPPING_ENTRY_FORM_MODULE']);
93 require_once('include/connectors/filters/FilterFactory.php');
94 $filter = FilterFactory::getInstance(get_class($this->_source));
95 $list = $filter->getList($args, $module);
98 $resultSize = count($list);
100 if(count($beans) != $resultSize) {
101 throw new Exception($GLOBALS['app_strings']['ERR_CONNECTOR_FILL_BEANS_SIZE_MISMATCH']);
105 for($x=0; $x < $resultSize; $x++) {
106 $beans[$x] = loadBean($module);
110 $keys = array_keys($beans);
112 foreach($list as $entry) {
113 //Change the result keys to lower case. This has important ramifications.
114 //This was done because the listviewdefs.php files may not know the proper casing
115 //of the fields to display. We change the keys to lowercase so that the values
116 //may be mapped to the beans without having to rely on the proper string casing
117 //in the listviewdefs.php files.
118 $entry = array_change_key_case($entry, CASE_LOWER);
119 $results[] = $this->mapOutput($beans[$keys[$count]], $entry);
123 $field_defs = $this->getFieldDefs();
124 $map = $this->getMapping();
125 $hasOptions = !empty($map['options']) ? true : false;
127 $options = $map['options'];
128 $optionFields = array();
130 foreach($field_defs as $name=>$field) {
131 if(!empty($field['options']) && !empty($map['options'][$field['options']]) && !empty($map['beans'][$module][$name])) {
132 $optionFields[$name] = $map['beans'][$module][$name];
136 foreach($results as $key=>$bean) {
137 foreach($optionFields as $sourceField=>$sugarField) {
138 $options_map = $options[$field_defs[$sourceField]['options']];
139 $results[$key]->$sugarField = !empty($options_map[$results[$key]->$sugarField]) ? $options_map[$results[$key]->$sugarField] : $results[$key]->$sugarField;
151 * Obtain a list of items
153 * @param string $module ideally this method should return a list of beans of type $module.
154 * @param Mixed $args this represents the 'query' on the data source.
159 * Given a bean, persist it to a data source
161 * @param SugarBean $bean
163 public function save($bean){}
168 * Returns the configuration Array as definied in the config.php file
170 * @return $config Array of the configuration mappings as defined in config.php
172 public function getConfig(){
173 return $this->_source->getConfig;
177 public function getFieldDefs() {
178 return $this->_source->getFieldDefs();
183 * Used by the Factories to set the config on the corresponding object
185 * @param array $config this file will be specified in config file corresponding to the wrapper or data source we are
186 * using. The name would be something like hoovers.php if we are using the hoovers data source or hoovers wrapper
187 * and it would exist in the same directory as the connector and/or wrapper.
188 * Note that the confing specified at the connector level takes precendence over the config specified at the wrapper level.
189 * This logic is performed in ConnectorFactory.php
191 public function setConfig($config){
192 $this->_source->setConfig($config);
198 public function mapInput($inputData, $module){
199 $input_params = array();
200 $map = $this->getMapping();
201 if(empty($map['beans'][$module])) {
202 return $input_params;
204 $mapping = array_flip($map['beans'][$module]);
205 $field_defs = $this->getFieldDefs();
206 foreach($inputData as $arg=>$val){
207 if(!empty($mapping[$arg]) || !empty($field_defs[$arg])) {
208 if(!empty($mapping[$arg])){
209 $arg = $mapping[$arg];
211 if(!empty($field_defs[$arg]['input'])){
212 $in_field = $field_defs[$arg]['input'];
213 $temp = explode('.', $in_field);
214 $eval_code = "\$input_params";
215 foreach($temp as $arr_key) {
216 $eval_code .= '[\'' . $arr_key . '\']';
218 $eval_code .= "= \$val;";
221 $input_params[$arg] = $val;
225 return $input_params;
228 public function mapOutput($bean, $result){
229 if(is_object($bean)) {
230 $map = $this->getMapping();
231 $mapping = $map['beans'][$bean->module_dir];
233 //Check for situation where nothing was mapped or the only field mapped was id
234 if(empty($mapping) || (count($mapping) == 1 && isset($mapping['id']))) {
235 $GLOBALS['log']->error($GLOBALS['mod_strings']['ERROR_NO_DISPLAYABLE_MAPPED_FIELDS']);
236 throw new Exception($GLOBALS['mod_strings']['ERROR_NO_DISPLAYABLE_MAPPED_FIELDS']);
240 if(!empty($mapping)) {
241 foreach($mapping as $source_field => $sugar_field){
242 $bean->$sugar_field = $this->getFieldValue($bean, $result, $source_field);
243 $mapped[$source_field] = $bean->$sugar_field;
246 foreach($result as $key=>$value) {
247 if(isset($bean->field_defs[$key])) {
248 $bean->$key = $value;
253 //set the data_source_id field which contain the unique id for the source
254 $source_field = 'id';
255 $bean->data_source_id = $this->getFieldValue($bean, $result, $source_field);
257 //now let's check for any fields that have not been mapped which may be required
258 $required_fields = $this->_source->getFieldsWithParams('required', true);
259 if(!empty($required_fields)){
260 foreach($required_fields as $key => $def){
261 if(empty($mapped[$key])){
262 $bean->$key = $this->getFieldValue($bean, $result, $key);
272 private function getFieldValue($bean, $result, $source_field){
273 $def = $this->getModuleFieldDef($bean->module_dir, $source_field);
274 $out_field = $source_field;
275 if(!empty($def['output'])){
276 $out_field = $def['output'];
279 $value = SugarArray::staticGet($result, $out_field);
282 if(!empty($def['function'])){
283 $function = $def['function'];
284 if(is_array($function) && isset($function['name'])){
285 $function = $def['function']['name'];
286 if(!empty($def['function']['include'])){
287 require_once($def['function']['include']);
290 $value = $function($bean, $out_field, $value);
296 public function saveConfig($persister=null) {
297 $this->_source->saveConfig($persister);
300 public function loadConfig($persister=null) {
301 $this->_source->loadConfig($persister);
304 public function setMapping($map=array()) {
305 $this->_source->setMapping($map);
308 public function getMapping() {
309 return $this->_source->getMapping();
312 public function getModuleMapping($module) {
313 $map = $this->getMapping();
314 return !empty($map['beans'][$module]) ? $map['beans'][$module] : array();
317 public function getModuleFieldDef($module, $field){
318 $map = $this->getMapping();
319 $field_defs = $this->getFieldDefs();
320 if(!empty($map['beans'][$module][$field])){
321 $source_field = $field;
322 if(!empty($field_defs[$source_field])){
323 return $field_defs[$source_field];
324 }elseif(!empty($field_defs[$field])){
325 return $field_defs[$field];
329 }elseif(!empty($field_defs[$field])){
330 return $field_defs[$field];
336 public function getSource(){
337 return $this->_source;
340 public function setSource($source){
341 $this->_source = $source;