PHP code example of fullscreeninteractive / silverstripe-xero
1. Go to this page and download the library: Download fullscreeninteractive/silverstripe-xero library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fullscreeninteractive / silverstripe-xero example snippets
/** @var \XeroPHP\Application **/
$app = XeroFactory::singleton()->getApplication();
use Psr\Log\LoggerInterface;
use SilverStripe\ORM\DataExtension;
use FullscreenInteractive\SilverStripeXero\XeroFactory;
use SilverStripe\Core\Injector\Injector;
class XeroMemberExtension extends DataExtension
{
private static $db = [
'XeroContactID' => 'Varchar'
];
public function updateCMSFields(FieldList $fields)
{
$fields->makeFieldReadonly('XeroContactID');
}
public function getXeroContact()
{
try {
$xero = XeroFactory::singleton()->getApplication();
} catch (Throwable $e) {
$xero = null;
}
if ($xero) {
try {
$contact = null;
if ($id = $this->owner->XeroContactID) {
$contact = $xero->loadByGUID('Accounting\\Contact', $id);
}
if (!$contact) {
$existing = $xero->load('Accounting\\Contact')
->where('EmailAddress!=null AND EmailAddress.Contains("' . trim($this->owner->Email) . '")')
->execute();
if (count($existing) > 1) {
$contact = $existing->offsetGet(0);
}
}
if (!$contact) {
// create the record
$contact = new \XeroPHP\Models\Accounting\Contact();
$contact->setName($this->owner->Name);
$contact->setFirstName($this->owner->FirstName);
$contact->setLastName($this->owner->Surname);
$contact->setEmailAddress($this->owner->Email);
try {
$xero->save($contact);
} catch (Exception $e) {
if (strpos($e->getMessage(), 'Already assigned to') !== false) {
$contact->setName($this->owner->Name . ' ' . date('Y-m-d'));
try {
$xero->save($contact);
} catch (Exception $e) {
Injector::inst()->get(LoggerInterface::class)->warning($e);
}
}
}
}
return $contact;
} catch (Exception $e) {
Injector::inst()->get(LoggerInterface::class)->error($e);
}
}
}
}