get paid to paste

Simple Class to Connect Salesforce Data from...

/**
  Class to handle connection to salesforce
  You should provide WSDL files from salesforce (see at list of define), 
     and rename it with prefix sand_ (for sandbox) or prod_ (for production)

  Also will need files on require_once, could be found in Salesforce too
*/
require_once("soapclient/SforcePartnerClient.php");
require_once("soapclient/BulkApiClient.php");


define('SOAP_PATH', "soapclient");
define('WSDL_XML_PARTNER', "partner.wsdl.xml");
define('WSDL_XML_ENTERPRISE', "enterprise.wsdl.xml");
define('WSDL_XML_METADATA', "metadata.wsdl.xml");


class SFConnectionClass
{
  public $connection;
  public $server = 'sand'; // option (sand or prod)
  public $username = '';
  public $password = '';
  public $token = '';
  public $sfSessionID = '';
  public $sfLocation = '';


  /*
    constructor
    // if parameter empty, use default
  */
  public function SFConnectionClass($server = 'sand', $username = '', $password = '', $token = '')
  {
    if ($server != "") $this->server = $server;
    if ($username != "") $this->username = $username;
    if ($password != "") $this->password = $password;
    if ($token != "") $this->token = $token;
    
    $this->connection = $this->getConnectionSF();
  }


  /*
    get database connection to salesforce
  */
  public function getConnectionSF()
  {
    try {


      $username = $this->username;
      $password = $this->password;
      $token    = $this->token;


      $SforceConnection = new SforcePartnerClient();
      //$wsdl_xml = ($isEnterprise) ? WSDL_XML_ENTERPRISE : WSDL_XML_PARTNER;
      $wsdl_xml = SOAP_PATH."/".$this->server."_".WSDL_XML_PARTNER;
      $SforceConnection->createConnection($wsdl_xml);
      $SforceConnection->login($username, $password.$token);
      
      $this->connection  = $SforceConnection;
      $this->sfSessionID = $SforceConnection->getSessionID();
      $this->sfLocation  = $SforceConnection->getLocation();
      
      return $SforceConnection; // soap salesforce
    }
    catch (Exception $e) {
      print_r($e);
      die();
    }
    return null;
  }
  
  /*
    function to get query result in csv format
  */
  public function fetchQueryCSV($strSQL)
  {
    $queryResult = $this->getQueryResultSF($strSQL);
    $strRes = "";
    if (isset($queryResult->records) && count($queryResult->records) > 0)
    {
      // generate header
      $arrHeader = array();
      $strHeader = "";
      foreach($queryResult->records[0]->fields AS $strKey => $objV)
      {
        $arrHeader[$strKey] = $strKey;
        $strHeader .= ($strHeader == "") ? $strKey : ",".$strKey;
      }
      if (isset($queryResult->records[0]->sobjects))
      {
        foreach($queryResult->records[0]->sobjects[0]->fields AS $strKey => $objV)
        {
          $strKey = $queryResult->records[0]->sobjects[0]->type .".".$strKey;
          $arrHeader[$strKey] = $strKey;
          $strHeader .= ($strHeader == "") ? $strKey : ",".$strKey;
        }
      }
      
      // generate detail
      foreach ($queryResult->records AS $i => $obj)
      {
        $arrTmp = array();
        foreach ($obj->fields AS $strKey => $objV)
        {
          $arrTmp[$strKey] = (string)$objV;
        }
        if (isset($obj->sobjects))
        {
          foreach($obj->sobjects AS $j => $objSO)
          { 
            foreach($objSO->fields AS $strKey => $objV)
              $arrTmp[$objSO->type.".".$strKey] = (string)$objV;
          }
        }
        $strDetail = "";
        $x = 0;
        foreach ($arrHeader AS $key)
        {
          $strVal = (isset($arrTmp[$key])) ? $arrTmp[$key] : "";
          if (strstr($strVal, ",")) $strVal = '"'.$strVal.'"';
          $strDetail .= ($x == 0) ? $strVal : ",".$strVal;
          $x++;
        }
        if ($strRes != "") $strRes .= chr(13).chr(10);
        $strRes .= $strDetail;
      }
      $strRes = $strHeader .chr(13).chr(10).$strRes;
    }
    return $strRes;
  }


  /*
    function to get query result in array of fields=>value
  */
  public function fetchQueryArray($strSQL)
  {
    $queryResult = $this->getQueryResultSF($strSQL);
    $arrRes = array();
    if (isset($queryResult->records))
    {
      foreach ($queryResult->records AS $i => $obj)
      {
        if (isset($obj->Id)) $arrRes[$i]['ID'] = $obj->Id;
        if (isset($obj->Id)) $arrRes[$i]['OBJECT_NAME'] = $obj->type;
        if (isset($obj->fields))
        {
          foreach($obj->fields AS $strKey => $objV)
            $arrRes[$i][$strKey] = (string)$objV;
        }
        if (isset($obj->sobjects))
        {
          foreach($obj->sobjects AS $j => $objSO)
          { 
            $arrRes[$i]['REFERENCES'] = array();
            foreach($objSO->fields AS $strKey => $objV)
              $arrRes[$i]['REFERENCES'][$objSO->type][$strKey] = (string)$objV;
          }
        }
      }
    }
    return $arrRes;
  }


  /*
    function to get query result - SOAP
  */
  public function getQueryResultSF($strSQL)
  {
    $response = $this->connection->query($strSQL);
    $queryResult = new QueryResult($response);


    return $queryResult;
  }
  
  /* function to execute delete data based on result of the query
  */
  public function deleteResultData($strSQL)
  {
    $arrData = $this->fetchQueryArray($strSQL);
    $strCsv = "";
    $strObjectName = "";
    foreach ($arrData AS $i => $records)
    {
      if ($records['OBJECT_NAME'] != "") $strObjectName = $records['OBJECT_NAME'];
      if ($records['ID'] != "") 
      {
        if ($strCsv != "") $strCsv .= chr(13).chr(10);
        $strCsv .= $records['ID'];
      }
    }
    
    if ($strCsv == "" || $strObjectName == "") return "ERROR: No Data!";
    
    $strCsv = "Id".chr(13).chr(10).$strCsv;
    return $this->processBulkAPI("delete", $strObjectName, $strCsv);
  }
  
  /*
    function to execute bulkAPI processing data
    input   : process type (insert/update/delete), object name, csv
    output  : result
  */
  public function processBulkAPI($type, $strObject, $strCsv = '')
  {
    if ($strCsv == "") return false;
    try {
      // BulkAPI client
      $myBulkApiConnection = new BulkApiClient($this->sfLocation, $this->sfSessionID);
      $myBulkApiConnection->setLoggingEnabled(true);
      $myBulkApiConnection->setCompressionEnabled(true);


      // Hand oops Bulk Jobs :P
      $job = new JobInfo();
      $job->setObject($strObject);
      $job->setOpertion($type);
      $job->setContentType("CSV");
      $job->setConcurrencyMode("Parallel");
      $job = $myBulkApiConnection->createJob($job);


      // Result
      $result = $myBulkApiConnection->createBatch($job, $strCsv);
      $tmp = $myBulkApiConnection->updateJobState($job->getId(), "Closed");
      $resultBatch = $myBulkApiConnection->getBatchResults($job->getId(), $result->getId());
      
      return $resultBatch;
    }
    catch (Exception $e)
    {
      return $e->getMessage();
    }
  }
  
  // function to parse csv string to array
  public function parseCsv($strCsv)
  {
    $arrResult = array();
    $arrH = array();
    $arr = explode("\n", $strCsv);
    $i = 0;
    foreach($arr AS $j => $str)
    {
      $arrD = explode(",", $str);
      if ($i == 0)
      {
        $arrH = $arrD;
      }
      else
      {
        $arrTmp = array();
        foreach($arrD AS $k=>$tmp)
        {
          $strKey = str_replace("\"", "", $arrH[$k]);
          $arrTmp[$strKey] = str_replace("\"", "", $tmp);
        }
        $arrResult[] = $arrTmp;
      }
      $i++;
    }
    return $arrResult;
  }


}
?>

Pasted: Sep 27, 2011, 4:50:55 am
Views: 4