Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Polcode_Crm_Model_Proxy_Sugar extends Polcode_Crm_Model_Proxy implements Polcode_Crm_Model_Proxy_Interface {
- protected $_code = 'sugar';
- private $instanceUrl;
- private $username;
- private $password;
- private $authUrl;
- private $oauthToken;
- public function __construct() {
- $this->getConfiguration();
- $this->getOauthToken();
- parent::__construct();
- }
- private function getConfiguration() {
- $this->instanceUrl = $this->getConfigData('api');
- $this->username = $this->getConfigData('login');
- $this->password = $this->getConfigData('password');
- }
- private function getOauthToken() {
- $this->authUrl = $this->instanceUrl . "/oauth2/token";
- $oauthTokenArguments = array(
- "grant_type" => "password",
- "client_id" => "sugar",
- "client_secret" => "",
- "username" => $this->username,
- "password" => $this->password,
- "platform" => "custom"
- );
- $authRequest = curl_init($this->authUrl);
- curl_setopt($authRequest, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
- curl_setopt($authRequest, CURLOPT_HEADER, false);
- curl_setopt($authRequest, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($authRequest, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($authRequest, CURLOPT_FOLLOWLOCATION, 0);
- curl_setopt($authRequest, CURLOPT_HTTPHEADER, array(
- "Content-Type: application/json"
- ));
- //convert arguments to json
- $jsonArguments = json_encode($oauthTokenArguments);
- curl_setopt($authRequest, CURLOPT_POSTFIELDS, $jsonArguments);
- //execute request
- $oauthTokenResponse = curl_exec($authRequest);
- //decode oauth2 response to get token
- $oauthTokenResponseObj = json_decode($oauthTokenResponse);
- $this->oauthToken = $oauthTokenResponseObj->access_token;
- }
- private function curlSetopt($request) {
- curl_setopt($request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
- curl_setopt($request, CURLOPT_HEADER, false);
- curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($request, CURLOPT_FOLLOWLOCATION, 0);
- curl_setopt($request, CURLOPT_HTTPHEADER, array(
- "Content-Type: application/json",
- "oauth-token: {$this->oauthToken}"
- ));
- }
- public function getCustomers() {
- /* rekordy z API tłumaczona na Varien Object */
- /* mapowanie danych z crm na Varien_Data_Collection zawierajacy Varien_Object */
- $fetchUrl = $this->instanceUrl . "/Accounts?max_num=2";
- $fetchRequest = curl_init($fetchUrl);
- $this->curlSetopt($fetchRequest);
- //execute request
- $fetchResponse = curl_exec($fetchRequest);
- $fetchResponseObj = json_decode($fetchResponse);
- $customers = new Varien_Data_Collection;
- foreach ($fetchResponseObj->records as $item) {
- /* @var $customer Varien_Object */
- $customer = new Varien_Object;
- $customer->setId($item->id);
- $personal = explode(' ', $item->name);
- $customer->setFirstname($personal[0]);
- $customer->setLastname($personal[1]);
- $customer->setEmail($item->email1);
- $customer->setPassword(md5($personal[0]));
- $customers->addItem($customer);
- }
- /* return Varien Data Collection */
- return $customers;
- }
- public function getProducts() {
- $fetchUrl = $this->instanceUrl . "/Products?max_num=2";
- $fetchRequest = curl_init($fetchUrl);
- $this->curlSetopt($fetchRequest);
- //execute request
- $fetchResponse = curl_exec($fetchRequest);
- $fetchResponseObj = json_decode($fetchResponse);
- $products = new Varien_Data_Collection;
- foreach ($fetchResponseObj->records as $item) {
- $product = new Varien_Object;
- $product->setSku($item->id)
- ->setName($item->name)
- ->setDescription($item->description)
- ->setShortDescription('eeee')
- ->setSku($item->id)
- ->setWeight($item->weight)
- ->setStatus(1)
- ->setPrice($item->cost_price)
- ->setVisibility(4)
- ->setTaxClassId(4)
- ->setStockData(array(
- 'use_config_manage_stock' => 0,
- 'manage_stock' => 1,
- 'min_sale_qty' => 1,
- 'max_sale_qty' => 10000,
- 'is_in_stock' => 1,
- 'qty' => $item->quantity
- ));
- $products->addItem($product);
- }
- return $products;
- }
- public function saveCustomer($customer) {
- $response = $this->checkAccountExist($customer->getEmail());
- // print_r($data->records);
- // print_r(!empty($data->records));
- if (empty($response->records)) {
- $url = $this->instanceUrl . "/Accounts";
- $record = array(
- 'name' => $customer->getFirstname() . ' ' . $customer->getLastname(),
- 'email1' => $customer->getEmail(),
- 'first_name' => $customer->getFirstname(),
- 'last_name' => $customer->getLastname(),
- );
- $curl_request = curl_init($url);
- $this->curlSetopt($curl_request);
- //convert arguments to json
- $json_arguments = json_encode($record);
- curl_setopt($curl_request, CURLOPT_POSTFIELDS, $json_arguments);
- //execute request
- $curl_response = curl_exec($curl_request);
- //decode json
- $createdRecord = json_decode($curl_response);
- //display the created record
- curl_close($curl_request);
- return $createdRecord;
- }
- }
- public function saveProduct($product) {
- $response = $this->checkProductExist($product->getSku());
- // print_r($data->records);
- if (empty($response->recodrs)) {
- $url = $this->instanceUrl . "/Products";
- $record = array(
- 'name' => $product->getName(),
- 'cost_price' => $product->getPrice(),
- 'weight' => $product->getWeight(),
- );
- $curl_request = curl_init($url);
- $this->curlSetopt($curl_request);
- //convert arguments to json
- $json_arguments = json_encode($record);
- curl_setopt($curl_request, CURLOPT_POSTFIELDS, $json_arguments);
- //execute request
- $curl_response = curl_exec($curl_request);
- //decode json
- $createdRecord = json_decode($curl_response);
- //display the created record
- curl_close($curl_request);
- return $createdRecord;
- }
- }
- public function checkAccountExist($param) {
- $fetchUrl = $this->instanceUrl . "/Accounts/";
- $data = array(
- 'filter' => array(
- '$equals' => array(
- 'email1' => $param
- )
- )
- );
- //Add data to the URL
- $fetchUrl = $fetchUrl . "?" . http_build_query($data);
- $fetchRequest = curl_init($fetchUrl);
- $this->curlSetopt($fetchRequest);
- //execute request
- $fetchResponse = curl_exec($fetchRequest);
- $fetchResponseObj = json_decode($fetchResponse);
- return $fetchResponseObj;
- }
- public function checkProductExist($param) {
- $fetchUrl = $this->instanceUrl . "/Products/";
- /* TODO porownanie id z sugar z sku produktu */
- $data = array(
- 'filter' => array(
- '$equals' => array(
- 'id' => $param
- )
- )
- );
- //Add data to the URL
- $fetchUrl = $fetchUrl . "?" . http_build_query($data);
- $fetchRequest = curl_init($fetchUrl);
- $this->curlSetopt($fetchRequest);
- //execute request
- $fetchResponse = curl_exec($fetchRequest);
- $fetchResponseObj = json_decode($fetchResponse);
- return $fetchResponseObj;
- }
- }
Add Comment
Please, Sign In to add comment