Guest User

Untitled

a guest
Jun 30th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.45 KB | None | 0 0
  1. <?php
  2.  
  3. class Polcode_Crm_Model_Proxy_Sugar extends Polcode_Crm_Model_Proxy implements Polcode_Crm_Model_Proxy_Interface {
  4.  
  5.     protected $_code = 'sugar';
  6.     private $instanceUrl;
  7.     private $username;
  8.     private $password;
  9.     private $authUrl;
  10.     private $oauthToken;
  11.  
  12.     public function __construct() {
  13.         $this->getConfiguration();
  14.         $this->getOauthToken();
  15.  
  16.         parent::__construct();
  17.     }
  18.  
  19.     private function getConfiguration() {
  20.         $this->instanceUrl = $this->getConfigData('api');
  21.         $this->username = $this->getConfigData('login');
  22.         $this->password = $this->getConfigData('password');
  23.     }
  24.  
  25.     private function getOauthToken() {
  26.         $this->authUrl = $this->instanceUrl . "/oauth2/token";
  27.  
  28.         $oauthTokenArguments = array(
  29.             "grant_type" => "password",
  30.             "client_id" => "sugar",
  31.             "client_secret" => "",
  32.             "username" => $this->username,
  33.             "password" => $this->password,
  34.             "platform" => "custom"
  35.         );
  36.  
  37.         $authRequest = curl_init($this->authUrl);
  38.         curl_setopt($authRequest, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  39.         curl_setopt($authRequest, CURLOPT_HEADER, false);
  40.         curl_setopt($authRequest, CURLOPT_SSL_VERIFYPEER, 0);
  41.         curl_setopt($authRequest, CURLOPT_RETURNTRANSFER, 1);
  42.         curl_setopt($authRequest, CURLOPT_FOLLOWLOCATION, 0);
  43.         curl_setopt($authRequest, CURLOPT_HTTPHEADER, array(
  44.             "Content-Type: application/json"
  45.         ));
  46.  
  47.         //convert arguments to json
  48.         $jsonArguments = json_encode($oauthTokenArguments);
  49.         curl_setopt($authRequest, CURLOPT_POSTFIELDS, $jsonArguments);
  50.  
  51.         //execute request
  52.         $oauthTokenResponse = curl_exec($authRequest);
  53.  
  54.         //decode oauth2 response to get token
  55.         $oauthTokenResponseObj = json_decode($oauthTokenResponse);
  56.         $this->oauthToken = $oauthTokenResponseObj->access_token;
  57.     }
  58.  
  59.     private function curlSetopt($request) {
  60.         curl_setopt($request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  61.         curl_setopt($request, CURLOPT_HEADER, false);
  62.         curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
  63.         curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
  64.         curl_setopt($request, CURLOPT_FOLLOWLOCATION, 0);
  65.         curl_setopt($request, CURLOPT_HTTPHEADER, array(
  66.             "Content-Type: application/json",
  67.             "oauth-token: {$this->oauthToken}"
  68.         ));
  69.     }
  70.  
  71.     public function getCustomers() {
  72.         /* rekordy z API tłumaczona na Varien Object */
  73.         /* mapowanie danych z crm na Varien_Data_Collection zawierajacy Varien_Object */
  74.  
  75.         $fetchUrl = $this->instanceUrl . "/Accounts?max_num=2";
  76.  
  77.         $fetchRequest = curl_init($fetchUrl);
  78.         $this->curlSetopt($fetchRequest);
  79.  
  80.         //execute request
  81.         $fetchResponse = curl_exec($fetchRequest);
  82.         $fetchResponseObj = json_decode($fetchResponse);
  83.  
  84.         $customers = new Varien_Data_Collection;
  85.  
  86.         foreach ($fetchResponseObj->records as $item) {
  87.             /* @var $customer Varien_Object */
  88.             $customer = new Varien_Object;
  89.  
  90.             $customer->setId($item->id);
  91.             $personal = explode(' ', $item->name);
  92.             $customer->setFirstname($personal[0]);
  93.             $customer->setLastname($personal[1]);
  94.             $customer->setEmail($item->email1);
  95.             $customer->setPassword(md5($personal[0]));
  96.  
  97.             $customers->addItem($customer);
  98.         }
  99.  
  100.         /* return Varien Data Collection */
  101.         return $customers;
  102.     }
  103.  
  104.     public function getProducts() {
  105.         $fetchUrl = $this->instanceUrl . "/Products?max_num=2";
  106.  
  107.         $fetchRequest = curl_init($fetchUrl);
  108.         $this->curlSetopt($fetchRequest);
  109.  
  110.         //execute request
  111.         $fetchResponse = curl_exec($fetchRequest);
  112.         $fetchResponseObj = json_decode($fetchResponse);
  113.  
  114.         $products = new Varien_Data_Collection;
  115.  
  116.         foreach ($fetchResponseObj->records as $item) {
  117.             $product = new Varien_Object;
  118.  
  119.             $product->setSku($item->id)
  120.                     ->setName($item->name)
  121.                     ->setDescription($item->description)
  122.                     ->setShortDescription('eeee')
  123.                     ->setSku($item->id)
  124.                     ->setWeight($item->weight)
  125.                     ->setStatus(1)
  126.                     ->setPrice($item->cost_price)
  127.                     ->setVisibility(4)
  128.                     ->setTaxClassId(4)
  129.                     ->setStockData(array(
  130.                         'use_config_manage_stock' => 0,
  131.                         'manage_stock' => 1,
  132.                         'min_sale_qty' => 1,
  133.                         'max_sale_qty' => 10000,
  134.                         'is_in_stock' => 1,
  135.                         'qty' => $item->quantity
  136.             ));
  137.  
  138.             $products->addItem($product);
  139.         }
  140.  
  141.         return $products;
  142.     }
  143.  
  144.     public function saveCustomer($customer) {
  145.         $response = $this->checkAccountExist($customer->getEmail());
  146. //        print_r($data->records);
  147. //        print_r(!empty($data->records));
  148.  
  149.         if (empty($response->records)) {
  150.             $url = $this->instanceUrl . "/Accounts";
  151.  
  152.             $record = array(
  153.                 'name' => $customer->getFirstname() . ' ' . $customer->getLastname(),
  154.                 'email1' => $customer->getEmail(),
  155.                 'first_name' => $customer->getFirstname(),
  156.                 'last_name' => $customer->getLastname(),
  157.             );
  158.  
  159.             $curl_request = curl_init($url);
  160.             $this->curlSetopt($curl_request);
  161.  
  162.             //convert arguments to json
  163.             $json_arguments = json_encode($record);
  164.             curl_setopt($curl_request, CURLOPT_POSTFIELDS, $json_arguments);
  165.             //execute request
  166.             $curl_response = curl_exec($curl_request);
  167.             //decode json
  168.             $createdRecord = json_decode($curl_response);
  169.  
  170.             //display the created record
  171.             curl_close($curl_request);
  172.  
  173.             return $createdRecord;
  174.         }
  175.     }
  176.  
  177.     public function saveProduct($product) {
  178.         $response = $this->checkProductExist($product->getSku());
  179. //        print_r($data->records);
  180.  
  181.         if (empty($response->recodrs)) {
  182.             $url = $this->instanceUrl . "/Products";
  183.  
  184.             $record = array(
  185.                 'name' => $product->getName(),
  186.                 'cost_price' => $product->getPrice(),
  187.                 'weight' => $product->getWeight(),
  188.             );
  189.  
  190.             $curl_request = curl_init($url);
  191.             $this->curlSetopt($curl_request);
  192.  
  193.             //convert arguments to json
  194.             $json_arguments = json_encode($record);
  195.  
  196.             curl_setopt($curl_request, CURLOPT_POSTFIELDS, $json_arguments);
  197.             //execute request
  198.             $curl_response = curl_exec($curl_request);
  199.  
  200.             //decode json
  201.             $createdRecord = json_decode($curl_response);
  202.  
  203.             //display the created record
  204.             curl_close($curl_request);
  205.  
  206.             return $createdRecord;
  207.         }
  208.     }
  209.  
  210.     public function checkAccountExist($param) {
  211.         $fetchUrl = $this->instanceUrl . "/Accounts/";
  212.  
  213.         $data = array(
  214.             'filter' => array(
  215.                 '$equals' => array(
  216.                     'email1' => $param
  217.                 )
  218.             )
  219.         );
  220.  
  221.         //Add data to the URL
  222.         $fetchUrl = $fetchUrl . "?" . http_build_query($data);
  223.  
  224.         $fetchRequest = curl_init($fetchUrl);
  225.         $this->curlSetopt($fetchRequest);
  226.  
  227.         //execute request
  228.         $fetchResponse = curl_exec($fetchRequest);
  229.         $fetchResponseObj = json_decode($fetchResponse);
  230.  
  231.         return $fetchResponseObj;
  232.     }
  233.  
  234.     public function checkProductExist($param) {
  235.         $fetchUrl = $this->instanceUrl . "/Products/";
  236.         /* TODO porownanie id z sugar z sku produktu */
  237.         $data = array(
  238.             'filter' => array(
  239.                 '$equals' => array(
  240.                     'id' => $param
  241.                 )
  242.             )
  243.         );
  244.  
  245.         //Add data to the URL
  246.         $fetchUrl = $fetchUrl . "?" . http_build_query($data);
  247.  
  248.         $fetchRequest = curl_init($fetchUrl);
  249.         $this->curlSetopt($fetchRequest);
  250.  
  251.         //execute request
  252.         $fetchResponse = curl_exec($fetchRequest);
  253.         $fetchResponseObj = json_decode($fetchResponse);
  254.  
  255.         return $fetchResponseObj;
  256.     }
  257.  
  258. }
Add Comment
Please, Sign In to add comment