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