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.
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.
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
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
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.
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.
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 ********************************************************************************/
40 * source is the parent class of any source object.
43 abstract class source{
45 * The name of an wrapper to use if the class wants to provide an override
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();
57 public function __construct(){
63 public function init(){}
65 //////// CALLED FROM component.php ///////
66 public function loadMapping() {
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");
74 $this->_mapping = $mapping;
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");
86 $this->_field_defs = !empty($dictionary[$class]['fields']) ? $dictionary[$class]['fields'] : array();
90 * Given a parameter in a vardef field, return the list of fields that match the param and value
92 * @param unknown_type $param_name
93 * @param unknown_type $param_value
96 public function getFieldsWithParams($param_name, $param_value) {
97 if(empty($this->_field_defs)){
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;
106 return $fields_with_param;
109 public function saveConfig() {
110 $config_str = "<?php\n/***CONNECTOR SOURCE***/\n";
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]);
123 foreach($this->_config as $key => $val) {
125 $config_str .= override_value_to_string_recursive2('config', $key, $val, false);
128 $dir = str_replace('_', '/', get_class($this));
130 if(!file_exists("custom/modules/Connectors/connectors/sources/{$dir}")) {
131 mkdir_recursive("custom/modules/Connectors/connectors/sources/{$dir}");
133 $fp = sugar_fopen("custom/modules/Connectors/connectors/sources/{$dir}/config.php", 'w');
134 fwrite($fp, $config_str);
138 public function loadConfig() {
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");
144 if(file_exists("custom/modules/Connectors/connectors/sources/{$dir}/config.php")) {
145 require("custom/modules/Connectors/connectors/sources/{$dir}/config.php");
147 $this->_config = $config;
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]);
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;
169 ////////////// GETTERS and SETTERS ////////////////////
170 public function getMapping(){
171 return $this->_mapping;
174 public function getOriginalMapping() {
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");
185 public function setMapping($mapping){
186 $this->_mapping = $mapping;
189 public function getFieldDefs(){
190 return $this->_field_defs;
193 public function getConfig(){
194 return $this->_config;
197 public function setConfig($config){
198 $this->_config = $config;
201 public function setProperties($properties=array()) {
202 if(!empty($this->_config) && isset($this->_config['properties'])) {
203 $this->_config['properties'] = $properties;
207 public function getProperties() {
208 if(!empty($this->_config) && isset($this->_config['properties'])) {
209 return $this->_config['properties'];
214 public function getProperty($name){
215 $properties = $this->getProperties();
216 if(!empty($properties[$name])){
217 return $properties[$name];
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
228 * @return enabled boolean value indicating whether or not testing is enabled
230 public function hasTestingEnabled() {
231 return $this->_has_testing_enabled;
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
240 * @return result boolean result of the test function
242 public function test() {
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.
253 * @return $enabled boolean variable indicating whether or not the connector is enabled for the wizard
255 public function isEnabledInWizard() {
256 return $this->_enable_in_wizard;
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.
266 * @return $enabled boolean variable indicating whether or not the connector is enabled for the hover links
269 public function isEnabledInHover() {
270 return $this->_enable_in_hover;
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.
280 * @return $fields Array of Connector config fields that are required
282 public function getRequiredConfigFields() {
283 return $this->_required_config_fields;
288 * isRequiredConfigFieldsSet
289 * This method checks the configuration parameters against the required config fields
290 * to see if they are set
292 * @return $set boolean value indicating whether or not the required config fields are set
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])) {
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.
311 * @return $fields Array of Connector config fields that are required to be set for the "Get Data" button to appear
313 public function getRequiredConfigFieldsForButton() {
314 return $this->_required_config_fields_for_button;
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
323 * @return $set boolean value indicating whether or not the required config fields are set
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])) {
337 * Allow data sources to log information
339 * @param string $log_data
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;
347 $GLOBALS['log']->info($name. ': '.$log_data);
352 * Returns an array containing a key/value pair(s) of a connector record. To be overridden by the implementation
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
359 public abstract function getItem($args=array(), $module=null);
364 * Returns a nested array containing a key/value pair(s) of a connector record. To be overridden by the
365 * implementation source.
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
371 public abstract function getList($args=array(), $module=null);
377 public function __destruct(){}