]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/connectors/sources/default/source.php
Release 6.3.0beta1
[Github/sugarcrm.git] / include / connectors / sources / default / source.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  * source is the parent class of any source object.
41  *
42  */
43 abstract class source{
44         /**
45          * The name of an wrapper to use if the class wants to provide an override
46          */
47         public $wrapperName;
48         protected $_config;
49         protected $_mapping;
50         protected $_field_defs;
51         protected $_enable_in_wizard = true;
52         protected $_enable_in_hover = false;
53         protected $_has_testing_enabled = false;
54         protected $_required_config_fields = array();
55         protected $_required_config_fields_for_button = array();        
56
57     /**
58      * The ExternalAPI Base that instantiated this connector.
59      * @var _eapm
60      */
61     protected $_eapm = null;
62
63         public function __construct(){
64                 $this->loadConfig();
65                 $this->loadMapping();
66                 $this->loadVardefs();
67         }
68         
69         public function init(){}
70         
71         //////// CALLED FROM component.php ///////
72         public function loadMapping() {
73                 $mapping = array();
74                 $dir = str_replace('_','/',get_class($this));
75                 if(file_exists("custom/modules/Connectors/connectors/sources/{$dir}/mapping.php")) {
76                         require("custom/modules/Connectors/connectors/sources/{$dir}/mapping.php");
77                 } else if(file_exists("modules/Connectors/connectors/sources/{$dir}/mapping.php")){
78                         require("modules/Connectors/connectors/sources/{$dir}/mapping.php");
79                 }
80             $this->_mapping = $mapping; 
81         }
82     
83     public function saveMappingHook($mapping) {
84         // Most classes don't care that the mapping has changed, but this is here if they do.
85         return;
86     }
87         
88         public function loadVardefs() {
89                 $class = get_class($this);
90                 $dir = str_replace('_','/',$class);
91                 if(file_exists("custom/modules/Connectors/connectors/sources/{$dir}/vardefs.php")) {
92                         require("custom/modules/Connectors/connectors/sources/{$dir}/vardefs.php");
93                 } else if(file_exists("modules/Connectors/connectors/sources/{$dir}/vardefs.php")){
94                         require("modules/Connectors/connectors/sources/{$dir}/vardefs.php");
95                 }
96                 
97                 $this->_field_defs = !empty($dictionary[$class]['fields']) ? $dictionary[$class]['fields'] : array();                   
98         }
99         
100         /**
101          * Given a parameter in a vardef field, return the list of fields that match the param and value
102          *
103          * @param unknown_type $param_name
104          * @param unknown_type $param_value
105          * @return unknown
106          */
107         public function getFieldsWithParams($param_name, $param_value) {
108                 if(empty($this->_field_defs)){
109                         $this->loadVardefs();                   
110                 }
111                 $fields_with_param = array();
112                 foreach($this->_field_defs as $key => $def){
113                         if(!empty($def[$param_name]) && ($def[$param_name] == $param_value)){
114                                 $fields_with_param[$key] = $def;
115                         }
116                 }
117                 return $fields_with_param;
118         }
119         
120         public function saveConfig() {
121                 $config_str = "<?php\n/***CONNECTOR SOURCE***/\n";
122
123         // Handle encryption
124         if(isset($this->_config['encrypt_properties'])&&is_array($this->_config['encrypt_properties'])&&isset($this->_config['properties'])){
125             require_once('include/utils/encryption_utils.php');
126             foreach($this->_config['encrypt_properties'] as $name) {
127                 if(isset($this->_config['properties'][$name])) {
128                     $this->_config['properties'][$name] = blowfishEncode(blowfishGetKey('encrypt_field'),$this->_config['properties'][$name]);
129                 }
130             }
131         }
132         
133
134                 foreach($this->_config as $key => $val) {
135                         if(!empty($val)){
136                                 $config_str .= override_value_to_string_recursive2('config', $key, $val, false);
137                         }
138                 }
139                 $dir = str_replace('_', '/', get_class($this));
140                                 
141             if(!file_exists("custom/modules/Connectors/connectors/sources/{$dir}")) {
142                mkdir_recursive("custom/modules/Connectors/connectors/sources/{$dir}");
143             }
144             $fp = sugar_fopen("custom/modules/Connectors/connectors/sources/{$dir}/config.php", 'w');
145                 fwrite($fp, $config_str);
146                 fclose($fp);
147         }
148         
149         public function loadConfig() {
150                 $config = array();
151                 $dir = str_replace('_','/',get_class($this));
152                 if(file_exists("modules/Connectors/connectors/sources/{$dir}/config.php")){
153                         require("modules/Connectors/connectors/sources/{$dir}/config.php");
154                 }
155                 if(file_exists("custom/modules/Connectors/connectors/sources/{$dir}/config.php")) {
156                         require("custom/modules/Connectors/connectors/sources/{$dir}/config.php");
157                 }
158                 $this->_config = $config;
159
160         // Handle decryption
161         if(isset($this->_config['encrypt_properties'])&&is_array($this->_config['encrypt_properties'])&&isset($this->_config['properties'])){
162             require_once('include/utils/encryption_utils.php');
163             foreach($this->_config['encrypt_properties'] as $name) {
164                 if(isset($this->_config['properties'][$name])) {
165                     $this->_config['properties'][$name] = blowfishDecode(blowfishGetKey('encrypt_field'),$this->_config['properties'][$name]);
166                 }
167             }
168         }
169         
170
171                 
172                 //If there are no required config fields specified, we will default them to all be required
173                 if(empty($this->_required_config_fields)) {
174                    foreach($this->_config['properties'] as $id=>$value) {
175                           $this->_required_config_fields[] = $id;
176                    }
177                 }
178         }
179
180     // Helper function for the settings panels
181     public function filterAllowedModules( $moduleList ) {
182         // Most modules can connect to everything, no further filtering necessary
183         return $moduleList;
184     }
185     
186         ////////////// GETTERS and SETTERS ////////////////////
187         public function getMapping(){
188                 return $this->_mapping;
189         }
190         
191         public function getOriginalMapping() {
192                 $mapping = array();
193                 $dir = str_replace('_','/',get_class($this));
194                 if(file_exists("modules/Connectors/connectors/sources/{$dir}/mapping.php")) {
195                         require("modules/Connectors/connectors/sources/{$dir}/mapping.php");
196                 } else if(file_exists("custom/modules/Connectors/connectors/sources/{$dir}/mapping.php")){
197                         require("custom/modules/Connectors/connectors/sources/{$dir}/mapping.php");
198                 }
199                 return $mapping;        
200         }       
201         
202         public function setMapping($mapping){
203                 $this->_mapping = $mapping;
204         }
205         
206         public function getFieldDefs(){
207                 return $this->_field_defs;
208         }
209         
210         public function getConfig(){
211                 return $this->_config;
212         }
213         
214         public function setConfig($config){
215                 $this->_config = $config;
216         }
217
218     public function setEAPM(ExternalAPIBase $eapm){
219         $this->_eapm = $eapm;
220     }
221
222     public function getEAPM(){
223         return $this->_eapm;
224     }
225         public function setProperties($properties=array()) {
226                 if(!empty($this->_config) && isset($this->_config['properties'])) {
227                    $this->_config['properties'] = $properties;
228                 }
229         }
230         
231         public function getProperties() {
232                 if(!empty($this->_config) && isset($this->_config['properties'])) {
233                    return $this->_config['properties'];
234                 }
235                 return array();
236         } 
237
238         public function getProperty($name){
239                 $properties = $this->getProperties();
240                 if(!empty($properties[$name])){
241                         return $properties[$name];
242                 }else{
243                         return '';
244                 }
245         }
246
247         /**
248          * hasTestingEnabled
249          * This method is used to indicate whether or not a data source has testing enabled so that
250          * the administration interface may call the test method on the data source instance
251          *
252          * @return enabled boolean value indicating whether or not testing is enabled
253          */
254         public function hasTestingEnabled() {
255                 return $this->_has_testing_enabled;
256         }
257         
258         /**
259          * test
260          * This method is called from the administration interface to run a test of the service
261          * It is up to subclasses to implement a test and set _has_testing_enabled to true so that
262          * a test button is rendered in the administration interface
263          * 
264          * @return result boolean result of the test function
265          */
266     public function test() {
267         return false;
268     } 
269         
270     
271     /**
272      * isEnabledInWizard 
273      * This method indicates whether or not the connector should be enabled in the wizard
274      * Connectors that do not support the getList/getItem methods via API calls should 
275      * set the protected class variable _enable_in_wizard to false.
276      * 
277      * @return $enabled boolean variable indicating whether or not the connector is enabled for the wizard
278      */
279     public function isEnabledInWizard() {
280         return $this->_enable_in_wizard;
281     }
282     
283     
284     /**
285      * isEnabledInHover
286      * This method indicates whether or not the connector should be enabled for the hover links
287      * Connectors that do not provide a formatter implementation should not
288      * set the protected class variable _enable_in_hover to true.
289      * 
290      * @return $enabled boolean variable indicating whether or not the connector is enabled for the hover links
291      * 
292      */
293     public function isEnabledInHover() {
294         return $this->_enable_in_hover;
295     }    
296     
297     
298     /**
299      * getRequiredConfigFields
300      * This method returns an Array of the configuration keys that are required for the Connector.
301      * Subclasses should set the class variable _required_config_fields to 
302      * return an Array of keys as specified in the Connector's config.php that are required.
303      * 
304      * @return $fields Array of Connector config fields that are required
305      */
306     public function getRequiredConfigFields() {
307         return $this->_required_config_fields;
308     }
309     
310     
311     /**
312      * isRequiredConfigFieldsSet
313      * This method checks the configuration parameters against the required config fields
314      * to see if they are set
315      * 
316      * @return $set boolean value indicating whether or not the required config fields are set
317      */
318     public function isRequiredConfigFieldsSet() {
319         //Check if required fields are set
320                 foreach($this->_required_config_fields as $field) {
321                 if(empty($this->_config['properties'][$field])) {
322                    return false;
323                 }
324                 }
325         return true;            
326     }
327     
328     
329     /**
330      * getRequiredConfigFieldsForButton
331      * This method returns an Array of the configuration keys that are required before the
332      * "Get Data" button will include the Connector.  We use it as a subset of the 
333      * $this->_required_config_fields Array.
334      * 
335      * @return $fields Array of Connector config fields that are required to be set for the "Get Data" button to appear
336      */
337     public function getRequiredConfigFieldsForButton() {
338         return $this->_required_config_fields_for_button;
339     }
340
341
342     /**
343      * isRequiredConfigFieldsForButtonSet
344      * This method checks the configuration parameters against the required config fields
345      * for the "Get Button" to see if they are set
346      * 
347      * @return $set boolean value indicating whether or not the required config fields are set
348      */
349     public function isRequiredConfigFieldsForButtonSet() {
350         //Check if required fields for button are set
351                 foreach($this->_required_config_fields_for_button as $field) {
352                 if(empty($this->_config['properties'][$field])) {
353                    return false;
354                 }
355                 }
356         return true;            
357     }    
358     
359     
360     /**
361      * Allow data sources to log information
362      *
363      * @param string $log_data
364      */
365     protected function log($log_data){
366         $name = get_class($this);
367         $property_name = $this->getProperty('name');
368         if(!empty($property_name)){
369                 $name = $property_name;
370         }
371         $GLOBALS['log']->info($name. ': '.$log_data);
372     }
373     
374         /**
375          * getItem
376          * Returns an array containing a key/value pair(s) of a connector record. To be overridden by the implementation
377          * source.
378          * 
379          * @param $args Array of arguments to search/filter by
380          * @param $module String optional value of the module that the connector framework is attempting to map to
381          * @return Array of key/value pair(s) of connector record; empty Array if no results are found
382          */
383         public abstract function getItem($args=array(), $module=null);
384
385         
386         /**
387          * getList
388          * Returns a nested array containing a key/value pair(s) of a connector record. To be overridden by the 
389          * implementation source.
390          * 
391          * @param $args Array of arguments to search/filter by
392          * @param $module String optional value of the module that the connector framework is attempting to map to
393          * @return Array of key/value pair(s) of connector record; empty Array if no results are found
394          */     
395         public abstract function getList($args=array(), $module=null);
396         
397         /**
398          * Default destructor
399          *
400          */
401         public function __destruct(){}
402 }
403 ?>