Skip to content

Commit

Permalink
Merge branch 'master' of [email protected]:novaksolutions/infusionsoft-p…
Browse files Browse the repository at this point in the history
…hp-sdk.git
  • Loading branch information
joeynovak committed Apr 9, 2014
2 parents 6879403 + 2b502b2 commit 4641eb9
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 58 deletions.
130 changes: 83 additions & 47 deletions Infusionsoft/AffiliateDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,43 @@


class Infusionsoft_AffiliateDataService extends Infusionsoft_Service {
static $first_run = false;
static $done = false;
static $first_order_date = null;
static $earliest_month_to_query = null;
static $affiliates_cache = array();
static $orderByField = 'DateEarned';
static $firstOrderDate = null;
static $affiliatesCache = null;
static $orderByField = null;
static $pageToMonthOMaps = array();
//'DateEarned' is the only field for which ordering is supported.
//If month is 0, all commissions for the current month will be returned
public static function queryWithOrderBy($object, $queryData, $orderByField, $ascending = true, $limit = 1000, $month = 0, $returnFields = false, Infusionsoft_App $app = null){
//Set a hard date for earliest_month_to_query so that we don't get into infinite loops trying to find commissions in the 1700's (this happens when you sync an empty application)
if (empty(self::$earliest_month_to_query)){
self::$earliest_month_to_query = '2010-01-01';
public static function queryWithOrderBy($object, $queryData, $orderByField = null, $ascending = true, $limit = 1000, $page = 0, $returnFields = false, Infusionsoft_App $app = null){
if(self::$firstOrderDate === null) self::setFirstOrderDate();
if(self::$firstOrderDate === false) return array();
if(self::$affiliatesCache === null) self::populateAffiliateCache();
if(self::$affiliatesCache === false) return array();

$month = self::getMonthForPage($object, $page);
$records = self::queryByMonthWithOrderBy($object, $queryData, $orderByField, $ascending, $limit, $month, $returnFields, $app);
while($records !== false && count($records) == 0){
$month++;
$records = self::queryByMonthWithOrderBy($object, $queryData, $orderByField, $ascending, $limit, $month, $returnFields, $app);
}
self::setPageForMonth($object, $month, $page);
return $records;
}

public static function queryByMonthWithOrderBy($object, $queryData, $orderByField, $ascending = true, $limit = 1000, $month = 0, $returnFields = false, Infusionsoft_App $app = null){
//Calculate beginning of this month
$startDate = date('Y-m-01 00:00:00', strtotime(" - $month months"));
$startDate = date(Infusionsoft_Service::apiDateFormat, strtotime($startDate));

$endDate = date('Y-m-d 23:59:59', strtotime($startDate . ' + 1 month - 1 day'));
$endDate = date(Infusionsoft_Service::apiDateFormat, strtotime($endDate));

if (self::$first_run === true && !empty(self::$first_order_date) && strtotime($startDate) < strtotime(self::$first_order_date)){
self::$done = true;
}
if (!empty(self::$earliest_month_to_query)){
if (strtotime($startDate) < strtotime(self::$earliest_month_to_query)){
self::$done = true;
}
}
//get an array of all affiliates
$affiliates = self::$affiliates_cache;
if (empty($affiliates)){
$page = 0;
do {
$affiliatesPage = Infusionsoft_DataService::query(
new Infusionsoft_Affiliate(),
array('Id' => '%'),
1000,
$page,
array('Id'),
$app
);
$page += 1;
$affiliates = array_merge($affiliates, $affiliatesPage);
} while (sizeof($affiliatesPage) >= 1000);
}

if (self::$first_run === true && self::$first_order_date === null){
$firstOrder = Infusionsoft_DataService::queryWithOrderBy(new Infusionsoft_Job(), array('Id' => '%'), 'Id', true, 1);
if (!empty($firstOrder)){
self::$first_order_date = $firstOrder[0]->DateCreated;
} else {
self::$first_order_date = false;
}
if (strtotime($startDate) < strtotime(self::$firstOrderDate) || strtotime($startDate) < strtotime("2000-01-01")){
return false;
}

//Now get all of the commissions from each one
//Now get all of the commissions or clawbacks from each one
$objects = array();
foreach ($affiliates as $affiliate) {
foreach (self::$affiliatesCache as $affiliate) {
if (get_class($object) == 'Infusionsoft_Commission'){
$objects = array_merge(
$objects,
Expand All @@ -81,12 +59,70 @@ public static function queryWithOrderBy($object, $queryData, $orderByField, $asc
}
}

self::$orderByField = $orderByField;
usort($objects, array('Infusionsoft_AffiliateDataService', 'sortCommissions'));
if($orderByField != null){
self::$orderByField = $orderByField;
usort($objects, array('Infusionsoft_AffiliateDataService', 'sortCommissions'));
}

return $objects;
}

public static function sortCommissions($a, $b){
return $a->{self::$orderByField} < $b->{self::$orderByField};
}

private static function getMonthForPage($object, $page){
$className = get_class($object);
if(!isset(self::$pageToMonthOMaps[$className])){
self::$pageToMonthOMaps[$className] = array();
if($page == 0){
self::$pageToMonthOMaps[$className][$page] = 0;
} else {
throw new Exception("The Affiliate Data Service MUST have pages accessed in order.");
}
}


if(!isset(self::$pageToMonthOMaps[$className][$page])){
end(self::$pageToMonthOMaps[$className]);
return self::$pageToMonthOMaps[$className][key(self::$pageToMonthOMaps[$className])] + 1;
} else{
return self::$pageToMonthOMaps[$className][$page];
}
}

private static function setPageForMonth($object, $month, $page){
$className = get_class($object);
if(!isset(self::$pageToMonthOMaps[$className])){
self::$pageToMonthOMaps[$className] = array();
}
self::$pageToMonthOMaps[$className][$page][$month];
}

private static function setFirstOrderDate(){
$firstOrder = Infusionsoft_DataService::queryWithOrderBy(new Infusionsoft_Job(), array('Id' => '%'), 'DateCreated', true, 1);
if (!empty($firstOrder)){
$firstOrder = array_shift($firstOrder);
self::$firstOrderDate = $firstOrder->DateCreated;
} else {
self::$firstOrderDate = false;
}
}

private static function populateAffiliateCache(){
$page = 0;
$pageSize = 1000;
self::$affiliatesCache = array();
do {
$affiliatesPage = Infusionsoft_DataService::query(
new Infusionsoft_Affiliate(),
array('Id' => '%'),
$pageSize,
$page,
array('Id')
);
$page += 1;
self::$affiliatesCache = array_merge(self::$affiliatesCache, $affiliatesPage);
} while (sizeof($affiliatesPage) == $pageSize);
}
}
1 change: 1 addition & 0 deletions Infusionsoft/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct($hostname, $apiKey, $port = 443){
$this->client = new xmlrpc_client('/api/xmlrpc', $this->getHostname(), $this->port);
$this->client->setSSLVerifyPeer(true);
$this->client->setCaCertificate(dirname(__FILE__) . '/infusionsoft.pem');
$this->client->request_charset_encoding = "UTF-8";
}

public function logger(Infusionsoft_Logger $object){
Expand Down
45 changes: 45 additions & 0 deletions Infusionsoft/EmailSent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Created by PhpStorm.
* User: joey
* Date: 3/31/14
* Time: 8:04 AM
*/

class Infusionsoft_EmailSent extends Infusionsoft_Generated_Base {
protected static $tableFields = array(
"Id",
"ContactId",
"FirstName",
"LastName",
"Email",
"BatchId",
"Sent",
"BounceType",
"Bounced",
"Opened",
"Clicked",
"LinkClickedId",
"LinkClicked",
"Opted",
"OptType",
"OptNotes"
);

public function __construct($idString = null, $app = null){
$this->table = 'EmailSent';
}

public function getFields(){
return self::$tableFields;
}

public function save() {
throw new Infusionsoft_Exception("EmailSent cannot be saved since they are loaded from a saved search, and not accessible via the Data Service");
}

public function loadFromArray($data){
parent::loadFromArray($data, true);
$this->Id = $data['EmailSentId'];
}
}
9 changes: 1 addition & 8 deletions Infusionsoft/InvoiceServiceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,6 @@ public static function getPluginStatus($fullyQualifiedClassName, Infusionsoft_Ap
return parent::send($app, "InvoiceService.getPluginStatus", $params);
}

public static function getAllShippingOptions(Infusionsoft_App $app = null){
$params = array(
);

return parent::send($app, "InvoiceService.getAllShippingOptions", $params);
}

public static function getPayments($invoiceId, Infusionsoft_App $app = null){
$params = array(
(int) $invoiceId
Expand Down Expand Up @@ -277,4 +270,4 @@ public static function deleteInvoice($invoiceId, Infusionsoft_App $app = null){
return parent::send($app, "InvoiceService.deleteInvoice", $params);
}

}
}
39 changes: 39 additions & 0 deletions Infusionsoft/SavedSearchDataService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: prescott
* Date: 5/13/13
* Time: 11:22 AM
* To change this template use File | Settings | File Templates.
*/


class Infusionsoft_SavedSearchDataService extends Infusionsoft_Service {
public static function queryWithOrderBy($object, $queryData, $orderByField = null, $ascending = true, $limit = 1000, $page = 0, $returnFields = false, Infusionsoft_App $app = null){
$results = array();

$Settings = ClassRegistry::init("Settings.Setting");
$savedSearchId = $Settings->getValue(get_class($object) . '.SavedSearchId');
if($savedSearchId == ''){
throw new Exception("Saved Search Id For Object: " . get_class($object) . ' not set in settings, please set the setting: ' . get_class($object) . '.SavedSearchId' . ' to the saved search id');
}

$userId = $Settings->getValue('SavedSearchUserId');
if($userId == ''){
throw new Exception("Saved Search UserId not set, please set the setting: " . 'SavedSearchUserId' . ' to a valid Infusionsoft UserId.');
}

$rows = Infusionsoft_SearchService::getSavedSearchResultsAllFields($savedSearchId, $userId, $page);
$className = get_class($object);
foreach($rows as $row){
/**
* @var Infusionsoft_Generated_Base $dataObject
*/
$dataObject = new $className();
$dataObject->loadFromArray($row, true);
$results[] = $dataObject;
}

return $results;
}
}
4 changes: 4 additions & 0 deletions Infusionsoft/ShippingService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
class Infusionsoft_ShippingService extends Infusionsoft_ShippingServiceBase{

}
90 changes: 90 additions & 0 deletions Infusionsoft/ShippingServiceBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
class Infusionsoft_ShippingServiceBase extends Infusionsoft_Service{

public static function getAllShippingOptions(Infusionsoft_App $app = null){
$params = array(
);

return parent::send($app, "ShippingService.getAllShippingOptions", $params);
}

public static function getAllConfiguredShippingOptions(Infusionsoft_App $app = null){
$params = array(
);

return parent::send($app, "ShippingService.getAllShippingOptions", $params);
}

public static function getFlatRateShippingOption($optionId, Infusionsoft_App $app = null){
$params = array(
(int) $optionId
);

return parent::send($app, "ShippingService.getFlatRateShippingOption", $params);
}

public static function getOrderTotalShippingOption($optionId, Infusionsoft_App $app = null){
$params = array(
(int) $optionId
);

return parent::send($app, "ShippingService.getOrderTotalShippingOption", $params);
}

public static function getOrderTotalShippingRanges($optionId, Infusionsoft_App $app = null){
$params = array(
(int) $optionId
);

return parent::send($app, "ShippingService.getOrderTotalShippingRanges", $params);
}

public static function getProductBasedShippingOption($optionId, Infusionsoft_App $app = null){
$params = array(
(int) $optionId
);

return parent::send($app, "ShippingService.getProductBasedShippingOption", $params);
}

public static function getProductShippingPricesForProductShippingOption($optionId, Infusionsoft_App $app = null){
$params = array(
(int) $optionId
);

return parent::send($app, "ShippingService.getProductShippingPricesForProductShippingOption", $params);
}

public static function getOrderQuantityShippingOption($optionId, Infusionsoft_App $app = null){
$params = array(
(int) $optionId
);

return parent::send($app, "ShippingService.getOrderQuantityShippingOption", $params);
}

public static function getWeightBasedShippingOption($optionId, Infusionsoft_App $app = null){
$params = array(
(int) $optionId
);

return parent::send($app, "ShippingService.getWeightBasedShippingOption", $params);
}

public static function getWeightBasedShippingRanges($optionId, Infusionsoft_App $app = null){
$params = array(
(int) $optionId
);

return parent::send($app, "ShippingService.getWeightBasedShippingRanges", $params);
}

public static function getUpsShippingOption($optionId, Infusionsoft_App $app = null){
$params = array(
(int) $optionId
);

return parent::send($app, "ShippingService.getUpsShippingOption", $params);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
include('testUtils.php');

if(isset($_REQUEST['go'])){
$out = Infusionsoft_InvoiceService::getAllShippingOptions();
$out = Infusionsoft_ShippingService::getAllShippingOptions();
var_dump($out);
}
}
Loading

0 comments on commit 4641eb9

Please sign in to comment.