]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/connectors/component.php
Release 6.2.0
[Github/sugarcrm.git] / include / connectors / component.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 class component{
41         protected $_has_testing_enabled = false;
42         protected $_source;
43
44         public function __construct() {}
45
46         public function init() {}
47
48         /**
49          * fillBean
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
53          *
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
58          */
59         public function fillBean($args=array(), $module=null, $bean=null) {
60             $result = 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);
67             } else {
68                throw new Exception("Invalid bean");
69             }
70             return $result;
71         }
72
73         /**
74          * fillBeans
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
78          *
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
83          */
84         public function fillBeans($args=array(), $module=null, $beans=array()) {
85                 $results = array();
86                 $args = $this->mapInput($args, $module);
87                 if(empty($args)) {
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']);
90                 }
91
92
93                 require_once('include/connectors/filters/FilterFactory.php');
94                 $filter = FilterFactory::getInstance(get_class($this->_source));
95                 $list = $filter->getList($args, $module);
96
97                 if(!empty($list)) {
98                         $resultSize = count($list);
99                         if(!empty($beans)) {
100                            if(count($beans) != $resultSize) {
101                                   throw new Exception($GLOBALS['app_strings']['ERR_CONNECTOR_FILL_BEANS_SIZE_MISMATCH']);
102                            }
103                         } else {
104
105                            for($x=0; $x < $resultSize; $x++) {
106                                    $beans[$x] = loadBean($module);
107                            }
108                         }
109
110                     $keys = array_keys($beans);
111                         $count = 0;
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);
120                                    $count++;
121                         }
122
123                         $field_defs = $this->getFieldDefs();
124                     $map = $this->getMapping();
125                         $hasOptions = !empty($map['options']) ? true : false;
126                         if($hasOptions) {
127                            $options = $map['options'];
128                            $optionFields = array();
129
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];
133                                        }
134                            }
135
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;
140                                    }
141                            } //foreach
142                         }
143                 }
144
145                 return $results;
146         }
147
148
149
150         /**
151          * Obtain a list of items
152          *
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.
155          */
156
157
158         /**
159          * Given a bean, persist it to a data source
160          *
161          * @param SugarBean $bean
162          */
163         public function save($bean){}
164
165
166         /**
167          * getConfig
168          * Returns the configuration Array as definied in the config.php file
169          *
170          * @return $config Array of the configuration mappings as defined in config.php
171          */
172         public function getConfig(){
173                 return $this->_source->getConfig;
174         }
175
176
177         public function getFieldDefs() {
178                 return $this->_source->getFieldDefs();
179         }
180
181         /**
182          * setConfig
183          * Used by the Factories to set the config on the corresponding object
184          *
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
190          */
191         public function setConfig($config){
192                 $this->_source->setConfig($config);
193         }
194
195         /**
196          * mapInput
197          */
198         public function mapInput($inputData, $module){
199                 $input_params = array();
200                 $map = $this->getMapping();
201                 if(empty($map['beans'][$module])) {
202                    return $input_params;
203                 }
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];
210                                 }
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 . '\']';
217                                         }
218                                         $eval_code .= "= \$val;";
219                                         eval($eval_code);
220                                 } else {
221                                         $input_params[$arg] = $val;
222                                 }
223                         } //if
224                 } //foreach
225                 return $input_params;
226         }
227
228         public function mapOutput($bean, $result){
229                 if(is_object($bean)) {
230                         $map = $this->getMapping();
231                         $mapping = $map['beans'][$bean->module_dir];
232
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']);
237                         }
238
239                         $mapped = array();
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;
244                                 }
245                         } else {
246                                 foreach($result as $key=>$value) {
247                                         if(isset($bean->field_defs[$key])) {
248                                            $bean->$key = $value;
249                                         }
250                                 }
251                         }
252
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);
256
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);
263                                         }
264                                 }
265                         }
266
267                         return $bean;
268                 }
269                 return $bean;
270         }
271
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'];
277                 }
278
279                 $value = SugarArray::staticGet($result, $out_field);
280
281                 if(is_array($def)){
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']);
288                                         }
289                                  }
290                                  $value = $function($bean, $out_field, $value);
291                          }
292                  }
293                  return $value;
294         }
295
296         public function saveConfig($persister=null) {
297                 $this->_source->saveConfig($persister);
298         }
299
300         public function loadConfig($persister=null) {
301                 $this->_source->loadConfig($persister);
302         }
303
304         public function setMapping($map=array()) {
305                 $this->_source->setMapping($map);
306         }
307
308         public function getMapping() {
309                 return $this->_source->getMapping();
310         }
311
312         public function getModuleMapping($module) {
313                 $map = $this->getMapping();
314                 return !empty($map['beans'][$module]) ? $map['beans'][$module] : array();
315         }
316
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];
326                         }else{
327                                 return $field;
328                         }
329                 }elseif(!empty($field_defs[$field])){
330                                 return $field_defs[$field];
331                 }else{
332                                 return $field;
333                 }
334         }
335
336         public function getSource(){
337                 return $this->_source;
338         }
339
340         public function setSource($source){
341                 $this->_source = $source;
342         }
343 }
344 ?>