/** * Establishes a connection to the salesforce server * * @param array $config configuration to be used for creating connection * @return bool true on success */ protected function _connect(array $config) { $this->config = $config; if (empty($this->config['my_wsdl'])) { throw new \ErrorException("A WSDL needs to be provided"); } else { $wsdl = CONFIG . DS . $this->config['my_wsdl']; } $mySforceConnection = new \SforceEnterpriseClient(); $mySoapClient = $mySforceConnection->createConnection($wsdl); $sflogin = (array) Cache::read('salesforce_login', 'salesforce'); if (!empty($sflogin['sessionId'])) { $mySforceConnection->setSessionHeader($sflogin['sessionId']); $mySforceConnection->setEndPoint($sflogin['serverUrl']); } else { try { $mylogin = $mySforceConnection->login($this->config['username'], $this->config['password']); $sflogin = array('sessionId' => $mylogin->sessionId, 'serverUrl' => $mylogin->serverUrl); Cache::write('salesforce_login', $sflogin, 'salesforce'); } catch (Exception $e) { $this->log("Error logging into salesforce - Salesforce down?"); $this->log("Username: "******"Password: " . $this->config['password']); } } $this->client = $mySforceConnection; $this->connected = true; return $this->connected; }
/** * Initialize method * * @param array $config The configuration for the Table. * @return void */ public function initialize(array $config) { parent::initialize($config); $this->table(false); $this->displayField('Id'); $this->primaryKey('Id'); if (!empty($config['connection']->config()['my_wsdl'])) { $wsdl = CONFIG . DS . $config['connection']->config()['my_wsdl']; } else { throw new \Exception("You need to provide a WSDL"); } $mySforceConnection = new \SforceEnterpriseClient(); $mySoapClient = $mySforceConnection->createConnection($wsdl); $sflogin = (array) Cache::read('salesforce_login', 'salesforce'); if (!empty($sflogin['sessionId'])) { $mySforceConnection->setSessionHeader($sflogin['sessionId']); $mySforceConnection->setEndPoint($sflogin['serverUrl']); } else { try { $mylogin = $mySforceConnection->login($config['connection']->config()['username'], $config['connection']->config()['password']); $sflogin = array('sessionId' => $mylogin->sessionId, 'serverUrl' => $mylogin->serverUrl); Cache::write('salesforce_login', $sflogin, 'salesforce'); } catch (Exception $e) { $this->log("Error logging into salesforce from Table - Salesforce down?"); } } if (!($sObject = Cache::read($this->name . '_sObject', 'salesforce'))) { $sObject = $mySforceConnection->describeSObject($this->name); Cache::write($this->name . '_sObject', $sObject, 'salesforce'); } foreach ($sObject->fields as $field) { if (substr($field->soapType, 0, 3) != "ens") { //we dont want type of ens if (substr($field->soapType, 4) == "int") { $type_name = "integer"; } elseif (substr($field->soapType, 4) == "boolean") { $type_name = "boolean"; } elseif (substr($field->soapType, 4) == "dateTime" || substr($field->soapType, 4) == "date") { $type_name = "datetime"; } else { $type_name = "string"; } if ($field->updateable) { $this->updatable_fields[$field->name] = ['type' => $type_name, 'length' => $field->length, 'null' => $field->nillable]; $this->selectable_fields[$field->name] = ['type' => $type_name, 'length' => $field->length, 'null' => $field->nillable]; } else { $this->selectable_fields[$field->name] = ['type' => $type_name, 'length' => $field->length, 'null' => $field->nillable]; } } } //Cache select fields right away as most likely need them immediately Cache::write($this->name . '_selectable_schema', $this->selectable_fields, 'salesforce'); if (!$this->schema(Cache::read($this->name . '_updatable_schema', 'salesforce'))) { $this->schema($this->updatable_fields); Cache::write($this->name . '_updatable_schema', $this->updatable_fields, 'salesforce'); } }
/** * Use WSDL for basic fields first of all * passes the salesforce credentals for login * @return boolean True on success, false on failure */ public function connect() { //Vendor loading seems to be picky using App::import require_once App::Path('Vendor')[0] . 'salesforce/soapclient/SforceEnterpriseClient.php'; if (empty($this->config['my_wsdl'])) { $wsdl = App::Path('Vendor')[0] . 'salesforce/soapclient/' . $this->config['standard_wsdl']; } else { $wsdl = APP . "Config" . DS . $this->config['my_wsdl']; } $mySforceConnection = new SforceEnterpriseClient(); //Dont forget to Change your API version in here! $mySoapClient = $mySforceConnection->createConnection($wsdl); $sflogin = (array) Cache::read('salesforce_login', 'short'); if (!empty($sflogin['sessionId'])) { $mySforceConnection->setSessionHeader($sflogin['sessionId']); $mySforceConnection->setEndPoint($sflogin['serverUrl']); } else { $mylogin = $mySforceConnection->login($this->config['username'], $this->config['password']); $sflogin = array('sessionId' => $mylogin->sessionId, 'serverUrl' => $mylogin->serverUrl); Cache::write('salesforce_login', $sflogin, 'short'); } $this->client = $mySforceConnection; $this->connected = true; return $this->connected; }