<?php

class SalesManago{
	var $smEndpoint;
	var $smIdClient;
	var $smApiSecret;
	var $smMicrositeKey;
	var $smEmail;
	
	function __construct(){
		$this->smEndpoint = $GLOBALS['config']['salesmanago_endpoint'];
		$this->smIdClient = $GLOBALS['config']['salesmanago_id_client'];
		$this->smApiSecret = $GLOBALS['config']['salesmanago_api_secret'];
		$this->smMicrositeKey = $GLOBALS['config']['salesmanago_microsite_key'];
		$this->smEmail = $GLOBALS['config']['salesmanago_email'];
	}
	
	function checkSettings(){
		return ( isset( $smEndpoint ) && isset( $smIdClient ) && isset( $smApiSecret ) && isset( $smMicrositeKey ) ) ? true : false;
	}
	
	function saveLog( $aData, $aReturn ){
		file_put_contents( 'logs/salesmanago_log.txt', var_export( array( $aData, $aReturn ), true ), FILE_APPEND );
	}
	
	function sendRequest( $sUrl, $aData ){
		$aData = json_encode( $aData );
		
		$ch = curl_init( $sUrl );
		curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
		curl_setopt( $ch, CURLOPT_POSTFIELDS, $aData );
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
		curl_setopt( $ch, CURLOPT_HTTPHEADER,
			array(
				'Content-Type: application/json',
				'Content-Length: ' . strlen( $aData )
			)
		);
		$aReturn = curl_exec( $ch );
		if( $aReturn['success'] == false && DEBUG_FIRELESS ) $this->saveLog( $aData, $aReturn );
		$aReturn = json_decode( $aReturn );
		
		
		return $aReturn;
	}
	
	function upsertContact( $aForm ){
		$oDB = DataBase::getInstance( );
		$aForm['sCountry2'] = $GLOBALS['oOrder']->throwCountry( $aForm['iCountry'], 'sCountry2' );
		$sUrl = 'http://' . $this->smEndpoint .'/api/contact/upsert';
		$aData = array( 
			'clientId' => $this->smIdClient,
			'apiKey' => $this->smMicrositeKey,
			'requestTime' => time(),
			'sha' => sha1( $this->smMicrositeKey . $this->smIdClient . $this->smApiSecret ),
			'contact' => array(
				'company' => $aForm['sCompany'],
				'email' => ( isset( $_SESSION['sEmail'] ) ) ? $_SESSION['sEmail'] : $aForm['sEmail'],
				'name' => $aForm['sFirstName'] . $aForm['sLastName'],
				'phone' => $aForm['sPhone'],
				'address' => array(
					'streetAddress' => $aForm['sStreet'],
					'zipCode' => $aForm['sZipCode'],
					'city' => $aForm['sCity'],
					'country' => $aForm['sCountry2']
				)
			),
			'newEmail' => $aForm['sEmail'],
			'owner' => $this->smEmail,
			'tags' => array( 'Formularz_edycji_konta' ),
			'lang' => 'PL',
			'useApiDoubleOptIn' => true,
			'forceOptIn' => true,
			'forceOptOut' => false,
			//'forcePhoneOptIn' => ( $aForm['iForumNotifyEmail'] == 1 ) ? true : false,
			//'forcePhoneOptOut' => false
		);
		
		return $this->sendRequest( $sUrl, $aData );
	}
	
	function addEventContact( $sAction, $aProducts, $aInfo = null ){
		/**********
			sAction - rodzaj akcji użytkownika ( typ zdarzenie, dopuszczalne wartości: PURCHASE, CART, VISIT, PHONE_CALL, OTHER )
			aProducts - informacje o zamówionych produktach dla akcji PURCHASE; informacje o produkcie dla akcji CART oraz VISIT;
			aInfo - informacje o zamówieniu dla akcji PURCHASE; informacje o koszyku dla akcji CART;
		**********/
		$sUrl = 'http://' . $this->smEndpoint .'/api/contact/addContactExtEvent';
		
		switch( $sAction ){
			case 'PURCHASE': // zamówienie
				if( count( $aProducts ) > 0 ){
					foreach( $aProducts as $aProduct ){
						$aProductsName[] = $aProduct['sName'];
					}
					$aInfo['sProductsList'] = implode( ", ", $aProductsName );
				}	
				$aData = array( 
					'clientId' => $this->smIdClient,
					'apiKey' => $this->smMicrositeKey,
					'requestTime' => time('Y-m-d'),
					'sha' => sha1( $this->smMicrositeKey . $this->smIdClient . $this->smApiSecret ),
					'owner' => $this->smEmail,
					'email' => $aInfo['sEmail'],
					'contactEvent' => array(
						'date' => date('Y-m-d'),
						'description' => $aInfo['sDescriptionEvent'],
						'products' => $aInfo['sProductsList'],
						'value' => $aInfo['sSummary'],
						'location' => null,
						'contactExtEventType' => $sAction,
						'detail1' => 'Numer zamówienia ' . $aInfo['iOrder'],
						'detail2' => 'Sposób płatności ' . $aInfo['aPayment']['sName'],
						'detail3' => null
					)
				);
				break;
			case 'CART': // koszyk
				if( count( $aProducts ) > 0 ){
					foreach( $aProducts as $aProduct ){
						$aProductsName[] = $aProduct['sName'];
					}
					$aInfo['sProductsList'] = implode( ", ", $aProductsName );
				}	
				$aData = array( 
					'clientId' => $this->smIdClient,
					'apiKey' => $this->smMicrositeKey,
					'requestTime' => time('Y-m-d'),
					'sha' => sha1( $this->smMicrositeKey . $this->smIdClient . $this->smApiSecret ),
					'owner' => $this->smEmail,
					'email' => $aInfo['sUserEmail'],
					'contactEvent' => array(
						'date' => date('Y-m-d'),
						'description' => $aInfo['sDescriptionEvent'],
						'products' => $aInfo['sProductsList'],
						'value' => $aInfo['sSummary'],
						'location' => null,
						'contactExtEventType' => $sAction
					)
				);
				break;
			case 'VISIT': // oglądanie produktu
				$aData = array( 
					'clientId' => $this->smIdClient,
					'apiKey' => $this->smMicrositeKey,
					'requestTime' => time('Y-m-d'),
					'sha' => sha1( $this->smMicrositeKey . $this->smIdClient . $this->smApiSecret ),
					'owner' => $this->smEmail,
					'email' => $aInfo['sUserEmail'],
					'contactEvent' => array(
						'date' => date('Y-m-d'),
						'description' => 'Oglądanie produktu',
						'products' => $aProducts['sName'] . " " . $aProducts['iProduct'], 
						'value' => $aProducts['fPrice'],
						'contactExtEventType' => $sAction
					)
				);
				break;
			case 'OTHER': // as storage
				if( count( $aProducts ) > 0 ){
					foreach( $aProducts as $aProduct ){
						$aProductsName[] = $aProduct['sName'];
					}
					$aInfo['sProductsList'] = implode( ", ", $aProductsName );
				}
				$aData = array( 
					'clientId' => $this->smIdClient,
					'apiKey' => $this->smMicrositeKey,
					'requestTime' => time('Y-m-d'),
					'sha' => sha1( $this->smMicrositeKey . $this->smIdClient . $this->smApiSecret ),
					'owner' => $this->smEmail,
					'email' => $aInfo['sUserEmail'],
					'contactEvent' => array(
						'date' => date('Y-m-d'),
						'description' => 'Schowek produktów',
						'products' => $aInfo['sProductsList'], 
						'value' => $aProducts['fPrice'],
						'contactExtEventType' => $sAction
					)
				);
				break;
			default:
				break;
		}
		
		return $this->sendRequest( $sUrl, $aData );
	}
}

?>