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