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